cuikcctrrt.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 
85 int main(int argc, char **arg)
86 {
87  TAtlasBase world; /* The set of mechanism and obstacles. */
88  Tparameters parameters; /* Parameters used in the Cuik process. */
89 
90  Tfilename fparam;
91 
92  double *s1,*s2; /* Origin/goal of the RRT. */
93 
94  unsigned int nvs;
95 
96  Trrt rrt;
97 
98  boolean connected;
99  double pl, pc;
100  unsigned int ns;
101  double **path;
102 
103  double planningTime;
104 
105  unsigned int it,nRepetitions;
106  Taverages averages;
107 
108  unsigned int ri;
109  time_t t;
110 
111  TRRTStatistics *rst;
112 
113  if (argc>1)
114  {
115  if (argc>2)
116  {
117  nRepetitions=atoi(arg[2]);
118  if (nRepetitions==0)
119  Error("Second parameter for cuikcctrrt is wrong");
120  }
121  else
122  nRepetitions=1;
123 
124  if ((nRepetitions>1)&&((RRT_VERBOSE)||(GET_RRT_STATISTICS)))
125  Warning("To get accurate execution time statistics, set RRT_VERBOSE and GET_RRT_STATISTICS to 0");
126 
127  if ((GET_RRT_STATISTICS)&&(nRepetitions>1))
128  {
129  NEW(rst,1,TRRTStatistics);
130  InitRRTStatistics(rst);
131  }
132  else
133  rst=NULL;
134 
135  /*Init parameters*/
136  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
137  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
138  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
139 
140  if (GetParameter(CT_DYNAMICS,&parameters)>0)
141  Error("cuikcctrrt do not work for problems with dynamics");
142 
143  /*Read the world from file*/
144  CS_WD_INIT(&parameters,arg[1],&world);
145 
146  /* Read samples */
147  #if (EXPLORATION_RRT)
148  nvs=ReadOneSample(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1);
149  NEW(s2,nvs,double);
150  memcpy(s2,s1,sizeof(double)*nvs);
151  #else
152  nvs=ReadTwoSamples(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1,&s2);
153  #endif
154 
155  /* Random seed initialization */
156  t=time(NULL); /* Get the time at which input files have been read */
157  ri=(unsigned int)t;
158  randomSet(ri);
159  fprintf(stderr,"Random seed : %u\n",ri);
160 
161  /* Start the process to connect the two samples */
162  InitAverages(nRepetitions,FALSE,TRUE,NO_UINT,&averages);
163 
164  for(it=0;it<nRepetitions;it++)
165  {
166  /* Init a one-directional RRT */
167  InitRRT(&parameters,FALSE,FALSE,s1,ONE_TREE,FALSE,s2,nvs,0,&world,&rrt);
168  fprintf(stderr,"************************************************\n");
169 
170  /* Try to connect the goal with a tree from the start */
171  connected=ccTRRT(&parameters,s2,
172  &planningTime,
173  &pl,&pc,&ns,&path,CS_WD_COST_FN(&world),
174  CS_WD_COST_PTR(&world),rst,&rrt);
175 
176  /* Save the results (only if one shot execution) */
177  if (nRepetitions==1)
178  {
179  Tfilename frrt;
180 
181  #if (!EXPLORATION_RRT)
182  if (connected)
183  SaveSamples(arg[1],FALSE,nvs,ns,path);
184  #endif
185 
186  CreateFileName(NULL,arg[1],NULL,RRT_EXT,&frrt);
187  fprintf(stderr,"Writing RRT to : %s\n",GetFileFullName(&frrt));
188  SaveRRT(&frrt,&rrt);
189  DeleteFileName(&frrt);
190  }
191 
192  /* Summarize and release allocated objects for this repetition*/
193  if ((EXPLORATION_RRT)||(connected))
194  {
195  NewSuccesfulExperiment(planningTime,RRTMemSize(&rrt),pl,pc,
196  NO_UINT,
197  (double)GetRRTNumNodes(&rrt),
198  NULL,NULL,
199  &averages);
200  DeleteSamples(ns,path);
201  }
202  else
203  fprintf(stderr," Execution failed (%f sec)\n",planningTime);
204 
205  DeleteRRT(&rrt);
206 
207  fprintf(stderr,"Execution compleated %u/%u\n",it+1,nRepetitions);
208  }
209 
210  /* Print statistics about the execution (only if many iterations) */
211  if (nRepetitions>1)
212  {
213  PrintAveragesHeader(stderr,argc,arg,&averages);
214 
215  fprintf(stderr,"%% **************************************************\n");
216  fprintf(stderr," Random seed : %u\n",ri);
217  PrintRRTDefines(stderr);
218  PrintParameters(stderr,&parameters);
219 
220  #if (GET_RRT_STATISTICS)
221  PrintRRTStatistics(NULL,rst);
222  DeleteRRTStatistics(rst);
223  free(rst);
224  #endif
225 
226  PrintAverages(stderr,&averages);
227 
228  fprintf(stderr,"%% **************************************************\n");
229  }
230 
231  /* Release memory */
232  DeleteAverages(&averages);
233 
234  free(s1);
235  free(s2);
236 
237  DeleteParameters(&parameters);
238 
239  CS_WD_DELETE(&world);
240 
241  DeleteFileName(&fparam);
242  }
243  else
244  {
245  fprintf(stderr," Wrong number of parameters.\n");
246  fprintf(stderr," Use:\n");
247  fprintf(stderr," cuikcctrrt <problem filename>.%s [num Repetitions]\n",CS_WD_EXT);
248  fprintf(stderr," where <problem filename> the equations/world description\n");
249  fprintf(stderr," <num Repetitions> experiment repetitions to gather statistics\n");
250  fprintf(stderr," This is optional.\n");
251  fprintf(stderr," (the '.%s' extension is not required)\n",CS_WD_EXT);
252  }
253  return(EXIT_SUCCESS);
254 }
255