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