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