cuikatlasrrt.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 "chart.h"
12 #include "samples.h"
13 #include "averages.h"
14 
15 #include <stdlib.h>
16 #include <string.h>
17 #include <time.h>
18 
79 int main(int argc, char **arg)
80 {
81 
82  TAtlasBase world; /* The set of mechanism and obstacles. */
83  Tparameters parameters; /* Parameters used in the Cuik process. */
84 
85  Tfilename fparam;
86 
87  double *s1,*s2; /* Origin/goal of the RRT. */
88 
89  unsigned int nvs;
90 
91  Tatlasrrt atlasrrt;
92 
93  boolean connected;
94  double pl;
95  unsigned int ns;
96  double **path;
97  double **actions;
98  double *times;
99  boolean birrt;
100  boolean parallel;
101 
102  double planningTime;
103 
104  unsigned int it,nRepetitions;
105  Taverages averages;
106 
107  unsigned int ri; /* random seed*/
108  time_t t; /* Used to timestamp the results */
109 
110  unsigned int db,da;
111 
112  TAtlasRRTStatistics *arst;
113 
114  if (argc>1)
115  {
116  if (argc>2)
117  {
118  nRepetitions=atoi(arg[2]);
119  if (nRepetitions==0)
120  Error("Second parameter for cuikatlasrrt is wrong");
121  }
122  else
123  nRepetitions=1;
124 
125  if ((nRepetitions>1)&&(GET_ATLASRRT_STATISTICS))
126  Warning("To get accurate execution time statistics, set GET_ATLASRRT_STATISTICS to 0");
127 
128  if ((nRepetitions>1)&&(ATLASRRT_VERBOSE))
129  Warning("To get accurate execution time statistics, set ATLASRRT_VERBOSE to 0");
130 
131  if ((GET_ATLASRRT_STATISTICS)&&(nRepetitions>1))
132  {
133  NEW(arst,1,TAtlasRRTStatistics);
135  }
136  else
137  arst=NULL;
138 
139  /*Init parameters*/
140  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
141  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
142  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
143 
144  db=(unsigned int)GetParameter(CT_DETECT_BIFURCATIONS,&parameters);
145  if (db>0)
146  Error("cuikatlasrrt does not deal with bifurcations (yet)");
147 
148  if (EXPLORATION_RRT)
149  birrt=FALSE;
150  else
151  birrt=(GetParameter(CT_BI_RRT,&parameters)>0.5);
152 
153  #if (_DEBUG>1)
154  parallel=FALSE;
155  #else
156  parallel=(GetParameter(CT_DYNAMICS,&parameters)>0.5);
157  #endif
158 
159  /*Read the world/cuik from file*/
160  CS_WD_INIT(&parameters,arg[1],&world);
161 
162  /* Read samples */
163  if (EXPLORATION_RRT)
164  {
165  nvs=ReadOneSample(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1);
166  s2=NULL;
167  }
168  else
169  nvs=ReadTwoSamples(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1,&s2);
170 
171  /* Random seed initialization */
172  t=time(NULL); /* Get the time at which input files have been read */
173  ri=(unsigned int)t;
174  //ri=1585753154;
175  randomSet(ri);
176  fprintf(stderr,"Random seed : %u\n",ri);
177 
178  if (nRepetitions==1)
179  {
180  fprintf(stderr,"%% **************************************************\n");
181  fprintf(stderr," Random seed : %u\n",ri);
182  PrintAtlasRRTDefines(stderr);
183  PrintParameters(stderr,&parameters);
184  }
185 
186  /* Start the process to connect the two samples */
187  InitAverages(nRepetitions,TRUE,TRUE,NO_UINT,&averages);
188 
189  for(it=0;it<nRepetitions;it++)
190  {
191  /* Initialize the atlas */
192  InitAtlasRRT(&parameters,parallel,s1,(birrt?TWO_TREES:ONE_TREE),FALSE/*graph*/,s2,&world,&atlasrrt);
193  fprintf(stderr,"************************************************\n");
194 
195  /* Define the path using the atlas */
196  connected=AtlasRRT(&parameters,s2,&planningTime,&pl,&ns,&path,&da,&actions,&times,arst,&atlasrrt);
197 
198  /* Save the results (only if one shot execution) */
199  if (nRepetitions==1)
200  {
201  if (connected)
202  SaveTrajectory(arg[1],FALSE,nvs,ns,path,da,actions,times);
203 
204  SaveAtlasRRT(&parameters,arg[1],&atlasrrt);
205  }
206 
207  /* Summarize and release allocated objects for this repetition*/
208 
209  if (EXPLORATION_RRT)
210  fprintf(stderr," Exploration concluded in %f sec\n",planningTime);
211  else
212  {
213  if (connected)
214  {
215  double pt; /* path time */
216 
217  if (times==NULL)
218  pt=0.0;
219  else
220  pt=AddVectorPositiveElements(ns,times); /* avoid the -1 in the trajectory connection */
221 
222  fprintf(stderr," Execution succeeded\n");
223 
224  NewSuccesfulExperiment(planningTime,AtlasRRTMemSize(&atlasrrt),pl,pt,
225  (double)GetAtlasRRTNumCharts(&atlasrrt),
226  (double)GetAtlasRRTNumNodes(&atlasrrt),
227  NULL,NULL,
228  &averages);
229  DeleteTrajectory(ns,path,actions,times);
230  }
231  else
232  fprintf(stderr," Execution failed (%f sec)\n",planningTime);
233  }
234 
235  DeleteAtlasRRT(&atlasrrt);
236 
237  fprintf(stderr,"Execution compleated %u/%u\n",it+1,nRepetitions);
238  }
239 
240  /* Print statistics about the execution (only if many iterations) */
241 
242  if (nRepetitions>1)
243  {
244  fprintf(stderr,"%% **************************************************\n");
245  fprintf(stderr,"Random seed : %u\n",ri);
246  PrintAtlasRRTDefines(stderr);
247  PrintParameters(stderr,&parameters);
248  fprintf(stderr,"%% **************************************************\n");
249 
250  PrintAveragesHeader(stderr,argc,arg,&averages);
251 
252  #if (GET_ATLASRRT_STATISTICS)
253  PrintAtlasRRTStatistics(NULL,arst);
255  free(arst);
256  #endif
257 
258  PrintAverages(stderr,&averages);
259 
260  fprintf(stderr,"%% **************************************************\n");
261  }
262 
263  /* Release memory */
264  DeleteAverages(&averages);
265 
266  /* Release memory */
267  free(s1);
268  free(s2);
269 
270  DeleteParameters(&parameters);
271 
272  CS_WD_DELETE(&world);
273 
274  DeleteFileName(&fparam);
275  }
276  else
277  {
278  fprintf(stderr," Wrong number of parameters.\n");
279  fprintf(stderr," Use:\n");
280  fprintf(stderr," cuikatlasrrt <problem filename>.%s [num Repetitions]\n",CS_WD_EXT);
281  fprintf(stderr," where <problem filename> the equations/world description\n");
282  fprintf(stderr," <num Repetitions> experiment repetitions to gather statistics\n");
283  fprintf(stderr," This is optional.\n");
284  fprintf(stderr," (the '.%s' extension is not required)\n",CS_WD_EXT);
285  }
286  return(EXIT_SUCCESS);
287 }