cuikcbirrt.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 
80 int main(int argc,char **arg)
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  Trrt rrt;
92 
93  boolean connected;
94  double pl;
95  unsigned int ns;
96  double **path;
97 
98  double planningTime;
99 
100  unsigned int it,nRepetitions;
101  Taverages averages;
102 
103  unsigned int ri;
104  time_t t;
105 
106  TRRTStatistics *rst;
107 
108  #if (EXPLORATION_RRT)
109  Error("cuikcbirrt can not be used in exploration mode (use ccrrt)");
110  #endif
111 
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 cuikcbirrt 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("cuikcbirrt 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  nvs=ReadTwoSamples(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&world),&s1,&s2);
148 
149  /* Random seed initialization */
150  t=time(NULL); /* Get the time at which input files have been read */
151  ri=(unsigned int)t;
152  randomSet(ri);
153  fprintf(stderr,"Random seed : %u\n",ri);
154 
155  /* Start the process to connect the two samples */
156  InitAverages(nRepetitions,FALSE,TRUE,NO_UINT,&averages);
157 
158  for(it=0;it<nRepetitions;it++)
159  {
160  /* Init a bi-directional RRT (bi-directional despite the value of BI_RRT parameter) */
161  InitRRT(&parameters,FALSE,FALSE,s1,TWO_TREES,FALSE,s2,nvs,0,&world,&rrt);
162  fprintf(stderr,"**************************************************\n");
163 
164  /* Try to connect the two trees */
165  connected=cBiRRT(&parameters,s2,
166  &planningTime,
167  &pl,&ns,&path,
168  rst,&rrt);
169 
170  /* Save the results (only if one shot execution) */
171  if (nRepetitions==1)
172  {
173  Tfilename frrt;
174 
175  if (connected)
176  SaveSamples(arg[1],FALSE,nvs,ns,path);
177 
178  CreateFileName(NULL,arg[1],NULL,RRT_EXT,&frrt);
179  fprintf(stderr,"Writing RRT to : %s\n",GetFileFullName(&frrt));
180  SaveRRT(&frrt,&rrt);
181  DeleteFileName(&frrt);
182  }
183 
184  /* Summarize and release allocated objects for this repetition*/
185  if (connected)
186  {
187  NewSuccesfulExperiment(planningTime,RRTMemSize(&rrt),pl,0,
188  NO_UINT,
189  (double)GetRRTNumNodes(&rrt),
190  NULL,NULL,
191  &averages);
192  DeleteSamples(ns,path);
193  }
194  else
195  fprintf(stderr," Execution failed (%f sec)\n",planningTime);
196 
197  DeleteRRT(&rrt);
198 
199  fprintf(stderr,"Execution compleated %u/%u\n",it+1,nRepetitions);
200  }
201 
202  /* Print statistics about the execution (only if many iterations) */
203  if (nRepetitions>1)
204  {
205  PrintAveragesHeader(stderr,argc,arg,&averages);
206 
207  fprintf(stderr,"%% **************************************************\n");
208  fprintf(stderr," Random seed : %u\n",ri);
209  PrintRRTDefines(stderr);
210  PrintParameters(stderr,&parameters);
211 
212  #if (GET_RRT_STATISTICS)
213  PrintRRTStatistics(NULL,rst);
214  DeleteRRTStatistics(rst);
215  free(rst);
216  #endif
217 
218  PrintAverages(stderr,&averages);
219 
220  fprintf(stderr,"%% **************************************************\n");
221  }
222 
223  /* Release memory */
224  DeleteAverages(&averages);
225 
226  free(s1);
227  free(s2);
228 
229  DeleteParameters(&parameters);
230 
231  CS_WD_DELETE(&world);
232 
233  DeleteFileName(&fparam);
234  }
235  else
236  {
237  fprintf(stderr," Wrong number of parameters.\n");
238  fprintf(stderr," Use:\n");
239  fprintf(stderr," cuikcbirrt <problem filename>.%s [num Repetitions]\n",CS_WD_EXT);
240  fprintf(stderr," where <problem filename> the equations/world description\n");
241  fprintf(stderr," <num Repetitions> experiment repetitions to gather statistics\n");
242  fprintf(stderr," This is optional.\n");
243  fprintf(stderr," (the '.%s' extension is not required)\n",CS_WD_EXT);
244  }
245  return(EXIT_SUCCESS);
246 }
247