cuikkinoest.c
Go to the documentation of this file.
1 
2 #include "world.h"
3 #include "parameters.h"
4 
5 #include "defines.h"
6 #include "error.h"
7 #include "filename.h"
8 #include "rrt.h"
9 #include "random.h"
10 #include "geom.h"
11 #include "samples.h"
12 #include "averages.h"
13 
14 #include <stdlib.h>
15 #include <string.h>
16 #include <time.h>
17 
87 int main(int argc, char **arg)
88 {
89  TAtlasBase world; /* The set of mechanism and obstacles. */
90  Tparameters parameters; /* Parameters used in the Cuik process. */
91 
92  Tfilename fparam;
93 
94  double *s1,*s2; /* Origin/goal of the EST. */
95 
96  unsigned int nvs;
97 
98  Trrt rrt;
99 
100  boolean connected;
101  double pl;
102  unsigned int ns;
103  double **path;
104  double **actions;
105  double *times;
106  unsigned int da,dof;
107 
108  double planningTime;
109 
110  unsigned int dynamics;
111  boolean birrt;
112 
113  unsigned int it,nRepetitions;
114  Taverages averages;
115 
116  unsigned int ri;
117  time_t t;
118  Tintegrator *intFunction;
119 
120  TRRTStatistics *rst;
121 
122  if (argc>1)
123  {
124  if (argc>2)
125  {
126  nRepetitions=atoi(arg[2]);
127  if (nRepetitions==0)
128  Error("Second parameter for cuikkinoest is wrong");
129  }
130  else
131  nRepetitions=1;
132 
133  if ((nRepetitions>1)&&((RRT_VERBOSE)||(GET_RRT_STATISTICS)))
134  Warning("To get accurate execution time statistics, set RRT_VERBOSE and GET_RRT_STATISTICS to 0");
135 
136  if ((GET_RRT_STATISTICS)&&(nRepetitions>1))
137  {
138  NEW(rst,1,TRRTStatistics);
139  InitRRTStatistics(rst);
140  }
141  else
142  rst=NULL;
143 
144  /*Init parameters*/
145  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
146  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
147  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
148 
149  dynamics=(unsigned int)GetParameter(CT_DYNAMICS,&parameters);
150 
151  switch(dynamics)
152  {
153  case 0:
154  Error("cuikkinorrt operates in problems with dynamics");
155  break;
156  case 1:
157  Error("cuikkinorrt does not operate with local coordinates (only Euler or RK4). Use: cuikatlasrrt");
158  break;
159  case 2:
160  intFunction=NextDynamicStateEuler;
161  break;
162  case 3:
163  intFunction=NextDynamicStateRK4;
164  break;
165  default:
166  Error("Wrong value for parameter DYNAMICS (must be 0..3)");
167  }
168 
169  birrt=(boolean)GetParameter(CT_BI_RRT,&parameters);
170  if (birrt)
171  Error("bi-EST is not implemented yet");
172 
173  dof=(unsigned int)GetParameter(CT_N_DOF,&parameters);
174 
175  /*Read the world from file*/
176  CS_WD_INIT(&parameters,arg[1],&world);
177 
178  /* Read samples */
179  nvs=ReadTwoSamples(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1,&s2);
180 
181  /* Random seed initialization */
182  t=time(NULL); /* Get the time at which input files have been read */
183  ri=(unsigned int)t;
184  randomSet(ri);
185  fprintf(stderr,"Random seed : %u\n",ri);
186 
187  /* Start the process to connect the two samples */
188  InitAverages(nRepetitions,FALSE,TRUE,NO_UINT,&averages);
189 
190  for(it=0;it<nRepetitions;it++)
191  {
192  /* Init an EST */
193  InitRRT(&parameters,FALSE,FALSE,s1,(birrt?TWO_TREES:ONE_TREE),FALSE,s2,nvs,dof/2,&world,&rrt);
194  fprintf(stderr,"**************************************************\n");
195 
196  /* Try to connect the goal with a tree from the start */
197  if (birrt)
198  Error("bi-EST is not implemented yet");
199  else
200  connected=kinoEST(&parameters,intFunction,s2,
201  &planningTime,
202  &pl,&ns,&path,
203  &da,&actions,&times,
204  rst,&world,&rrt);
205 
206  /* Save the results (only if one shot execution) */
207  if (nRepetitions==1)
208  {
209  Tfilename frrt;
210 
211  if (connected)
212  SaveTrajectory(arg[1],FALSE,nvs,ns,path,da,actions,times);
213 
214  CreateFileName(NULL,arg[1],NULL,RRT_EXT,&frrt);
215  fprintf(stderr,"Writing RRT to : %s\n",GetFileFullName(&frrt));
216  SaveRRT(&frrt,&rrt);
217  DeleteFileName(&frrt);
218  }
219 
220  /* Summarize and release allocated objects for this repetition*/
221  if (connected)
222  {
223  NewSuccesfulExperiment(planningTime,RRTMemSize(&rrt),pl,0,
224  NO_UINT,
225  (double)GetRRTNumNodes(&rrt),
226  NULL,NULL,
227  &averages);
228  DeleteSamples(ns,path);
229  }
230  else
231  fprintf(stderr," Execution failed (%f sec)\n",planningTime);
232 
233  DeleteRRT(&rrt);
234 
235  fprintf(stderr,"Execution compleated %u/%u\n",it+1,nRepetitions);
236  }
237 
238  /* Print statistics about the execution (only if many iterations) */
239  if (nRepetitions>1)
240  {
241  PrintAveragesHeader(stderr,argc,arg,&averages);
242 
243  fprintf(stderr,"%% **************************************************\n");
244  fprintf(stderr," Random seed : %u\n",ri);
245  PrintRRTDefines(stderr);
246  PrintParameters(stderr,&parameters);
247 
248  #if (GET_RRT_STATISTICS)
249  PrintRRTStatistics(NULL,rst);
250  DeleteRRTStatistics(rst);
251  free(rst);
252  #endif
253 
254  PrintAverages(stderr,&averages);
255 
256  fprintf(stderr,"%% **************************************************\n");
257  }
258 
259  /* Release memory */
260  DeleteAverages(&averages);
261 
262  free(s1);
263  free(s2);
264 
265  DeleteParameters(&parameters);
266 
267  CS_WD_DELETE(&world);
268 
269  DeleteFileName(&fparam);
270  }
271  else
272  {
273  fprintf(stderr," Wrong number of parameters.\n");
274  fprintf(stderr," Use:\n");
275  fprintf(stderr," cuikkinoest <problem filename>.%s [num Repetitions]\n",CS_WD_EXT);
276  fprintf(stderr," where <problem filename> the equations/world description\n");
277  fprintf(stderr," <num Repetitions> experiment repetitions to gather statistics\n");
278  fprintf(stderr," This is optional.\n");
279  fprintf(stderr," (the '.%s' extension is not required)\n",CS_WD_EXT);
280  }
281  return(EXIT_SUCCESS);
282 }