cuikatlasrrtstar.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 "atlasrrt.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 #include <math.h>
18 
84 int main(int argc, char **arg)
85 {
86  TAtlasBase world; /* The set of mechanism and obstacles. */
87  Tparameters parameters; /* Parameters used in the Cuik process. */
88 
89  Tfilename fparam;
90 
91  double *s1,*s2; /* Origin/goal of the AtlasRRT. */
92 
93  unsigned int nvs;
94 
95  Tatlasrrt atlasrrt;
96 
97  boolean connected;
98  double planningTime;
99  double pl;
100  unsigned int ns;
101  double **path;
102  boolean birrt;
103  boolean rrtgraph;
104 
105  unsigned int it,nRepetitions;
106  Taverages averages;
107 
108  unsigned int ri;
109  time_t t;
110 
111  /* Statistics about the time take up to each iteration and
112  the path lengh at that point */
113  unsigned int maxIt,execIt;
114  double *times,*costs;
115 
116  TAtlasRRTStatistics *arst;
117 
118  if (argc>1)
119  {
120  if (argc>2)
121  {
122  nRepetitions=atoi(arg[2]);
123  if (nRepetitions==0)
124  Error("Second parameter for cuikatlasrrtstar is wrong");
125  }
126  else
127  nRepetitions=1;
128 
129  if ((nRepetitions>1)&&(RRT_VERBOSE))
130  Warning("To get accurate execution time statistics, set GET_ATLASRRT_STATISTICS to 0");
131 
132  if ((GET_ATLASRRT_STATISTICS)&&(nRepetitions>1))
133  {
134  NEW(arst,1,TAtlasRRTStatistics);
136  }
137  else
138  arst=NULL;
139 
140  /*Init parameters*/
141  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
142  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
143  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
144 
145  if (GetParameter(CT_DYNAMICS,&parameters)>0)
146  Error("cuikatlasrrtstar do not work for problems with dynamics");
147 
148  birrt=(GetParameter(CT_BI_RRT,&parameters)>0.5);
149  rrtgraph=(GetParameter(CT_RRT_GRAPH,&parameters)>0.5);
150 
151  /*Read the world/cuik from file*/
152  CS_WD_INIT(&parameters,arg[1],&world);
153 
154  /* Read samples */
155  nvs=ReadTwoSamples(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1,&s2);
156 
157  /* Random seed initialization */
158  t=time(NULL); /* Get the time at which input files have been read */
159  ri=(unsigned int)t;
160  //ri=1341338413; // with gamma=0 the c8 give a long path
161  //ri=1341346253; // seed used to generate the growing RRTs for c8
162  ri=1416402361;
163  randomSet(ri);
164  fprintf(stderr,"Random seed : %u\n",ri);
165 
166  /* Maximum of iteraitons (set to NO_UINT to limit execution only in time) */
167  maxIt=(unsigned int)GetParameter(CT_MAX_PLANNING_ITERATIONS,&parameters);
168 
169  /* Start the process to connect the two samples */
170  InitAverages(nRepetitions,TRUE,TRUE,maxIt,&averages);
171 
172  /* Buffer to store statistics about the path length */
173  if (maxIt!=NO_UINT)
174  {
175  NEW(times,maxIt,double);
176  NEW(costs,maxIt,double);
177  }
178 
179  for(it=0;it<nRepetitions;it++)
180  {
181  /* Init an AtlasRRT (bi-directional or with graph structure depending on parameters) */
182  InitAtlasRRT(&parameters,TRUE/*parallel*/,s1,(birrt?TWO_TREES_WITH_SWAP:ONE_TREE),rrtgraph,s2,&world,&atlasrrt);
183  fprintf(stderr,"************************************************\n");
184 
185  /* Try to connect the goal with a tree from the start with
186  an optimal path */
187  connected=AtlasRRTstar(&parameters,s2,
188  &execIt,
189  times,costs,
190  &planningTime,&pl,&ns,&path,arst,&atlasrrt);
191 
192  /* Save the results (only if one shot execution) */
193  if (nRepetitions==1)
194  {
195  if (connected)
196  SaveSamples(arg[1],FALSE,nvs,ns,path);
197 
198  SaveAtlasRRT(&parameters,arg[1],&atlasrrt);
199  }
200 
201  /* Summarize and release allocated objects for this repetition*/
202  if (connected)
203  {
204  NewSuccesfulExperiment(planningTime,AtlasRRTMemSize(&atlasrrt),pl,0,
205  (double)GetAtlasRRTNumCharts(&atlasrrt),
206  (double)GetAtlasRRTNumNodes(&atlasrrt),
207  (execIt==maxIt?times:NULL),
208  (execIt==maxIt?costs:NULL),
209  &averages);
210 
211  DeleteSamples(ns,path);
212  }
213  else
214  fprintf(stderr," Execution failed\n");
215 
216  DeleteAtlasRRT(&atlasrrt);
217 
218  fprintf(stderr,"Execution compleated %u/%u\n",it+1,nRepetitions);
219  }
220 
221  /* Print data about the path length at each iteration. 0 means no path
222  to goal found yet */
223 
224  /* Print statistics about the execution (only if many iterations) */
225 
226  if (nRepetitions>1)
227  {
228  PrintAveragesHeader(stderr,argc,arg,&averages);
229 
230  fprintf(stderr,"%% **************************************************\n");
231  fprintf(stderr," Random seed : %u\n",ri);
232  PrintAtlasRRTDefines(stderr);
233  PrintParameters(stderr,&parameters);
234 
235  #if (GET_ATLASRRT_STATISTICS)
236  PrintAtlasRRTStatistics(NULL,arst);
238  free(arst);
239  #endif
240 
241  PrintAverages(stderr,&averages);
242 
243  fprintf(stderr,"%% **************************************************\n");
244  }
245 
246  /* Release memory */
247  if (maxIt!=NO_UINT)
248  {
249  free(times);
250  free(costs);
251  }
252 
253  DeleteAverages(&averages);
254 
255  free(s1);
256  free(s2);
257 
258  DeleteParameters(&parameters);
259 
260  CS_WD_DELETE(&world);
261 
262  DeleteFileName(&fparam);
263  }
264  else
265  {
266  fprintf(stderr," Wrong number of parameters.\n");
267  fprintf(stderr," Use:\n");
268  fprintf(stderr," cuikatlasrrtstar <problem filename>.%s [num Repetitions]\n",CS_WD_EXT);
269  fprintf(stderr," where <problem filename> the equations/world description\n");
270  fprintf(stderr," <num Repetitions> experiment repetitions to gather statistics\n");
271  fprintf(stderr," This is optional.\n");
272  fprintf(stderr," (the '.%s' extension is not required)\n",CS_WD_EXT);
273  }
274  return(EXIT_SUCCESS);
275 }
276