cuiksmoothpath.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 "atlas.h"
9 #include "samples.h"
10 #include "random.h"
11 
12 #include <stdlib.h>
13 
79 int main(int argc, char **arg)
80 {
81  TAtlasBase world; /* The set of mechanism and obstacles. */
82  Tparameters parameters; /* Parameters used in the Cuik process. */
83 
84  Tfilename fparam;
85  Tfilename fpath;
86 
87  char *suffix;
88  boolean defaultPathName;
89 
90  /* input path */
91  unsigned int nvs,ns;
92  double **path;
93 
94  /* smoothed path */
95  unsigned int sns;
96  double **spath;
97  unsigned int mode;
98  boolean parallel;
99  unsigned int numIterations;
100 
101  unsigned int ri;
102  time_t t;
103 
104  if (argc>=2)
105  {
106  /* if only one parameter given, assume default values
107  even for the path name */
108  defaultPathName=(argc==2);
109 
110  /*Init parameters*/
111  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
112  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
113  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
114 
115  /*Read the world from file*/
116  CS_WD_INIT(&parameters,arg[1],&world);
117 
118  t=time(NULL); /* Get the time at which input files have been read */
119  ri=(unsigned int)t;
120  //ri=1405498615;
121  randomSet(ri);
122  fprintf(stderr,"Random seed : %u\n",ri);
123 
124  mode=SMOOTH_SHORTCUT;
125  if (defaultPathName)
126  suffix="_path_s";
127  else
128  suffix="_s";
129  if (argc>=4)
130  {
131  /* Select the smoothing mode */
132  switch(arg[3][0])
133  {
134  case 'G':
135  case 'g':
136  mode=SMOOTH_GRADIENT;
137  if (defaultPathName)
138  suffix="_path_g";
139  else
140  suffix="_g";
141  break;
142  case 'R':
143  case 'r':
144  mode=SMOOTH_RANDOM;
145  if (defaultPathName)
146  suffix="_path_r";
147  else
148  suffix="_r";
149  break;
150  case 'S':
151  case 's':
152  mode=SMOOTH_SHORTCUT;
153  if (defaultPathName)
154  suffix="_path_s";
155  else
156  suffix="_s";
157  break;
158  case 'E':
159  case 'e':
160  mode=SMOOTH_EFFORT;
161  if (defaultPathName)
162  suffix="_path_e";
163  else
164  suffix="_e";
165  break;
166  case 'D':
167  case 'd':
168  mode=SMOOTH_DISPERSION;
169  if (defaultPathName)
170  suffix="_path_d";
171  else
172  suffix="_d";
173  break;
174  default:
175  Error("Undefined smooth method");
176  }
177  }
178 
179  if (argc>=5)
180  numIterations=atoi(arg[4]);
181  else
182  numIterations=(mode==SMOOTH_RANDOM? 2 : 1000);
183 
184  if (argc>=6)
185  parallel=atoi(arg[5]);
186  else
187  parallel=(((mode==SMOOTH_GRADIENT)||(mode==SMOOTH_EFFORT)||(mode==SMOOTH_DISPERSION))? TRUE : FALSE);
188 
189  if (defaultPathName)
190  CreateFileName(NULL,arg[1],"_path",SOL_EXT,&fpath);
191  else
192  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fpath);
193 
194  if (LoadSamples(&fpath,&nvs,&ns,&path))
195  {
196  SmoothSamples(&parameters,parallel,mode,numIterations,ns,path,&sns,&spath,&world);
197 
198  if (defaultPathName)
199  SaveSamples(arg[1],suffix,nvs,sns,spath);
200  else
201  SaveSamples(arg[2],suffix,nvs,sns,spath);
202 
203  DeleteSamples(ns,path);
204  DeleteSamples(sns,spath);
205  }
206  else
207  Error("Could not read the input path file");
208 
209  DeleteParameters(&parameters);
210  CS_WD_DELETE(&world);
211 
212  DeleteFileName(&fparam);
213  DeleteFileName(&fpath);
214  }
215  else
216  {
217  fprintf(stderr," Wrong number of parameters.\n");
218  fprintf(stderr," Use:\n");
219  fprintf(stderr," cuiksmoothpath <problem name> <path name> [<mode> <iterations> <parallel>]\n");
220  fprintf(stderr," where <problem name> is the file describing the problem.\n");
221  fprintf(stderr," <path name> is the file with the path to smooth.\n");
222  fprintf(stderr," <mode> [optional] is the algorithm to use: RANDOM, GRADIENT, SHORTCUT or EFFORT.\n");
223  fprintf(stderr," The defatul is SHORTCUT (faster but might return suboptimal paths).\n");
224  fprintf(stderr," <iterations> [optional] is the maximum number of iterations.\n");
225  fprintf(stderr," The default is 2 for RANDOM smoothing and 1000 for GRADIENT smoothing.\n");
226  fprintf(stderr," For the RANDOM method this number is scaled by the number of steps in the path.\n");
227  fprintf(stderr," The GRADIENT method might stop earlier if the gradient becomes 0.\n");
228  fprintf(stderr," <parallel> [optional] 1 if the smooth has to be exectued in parallel and 0 for serial execution.\n");
229  fprintf(stderr," The default is 0 for RANDOM and SHORTCUT and 1 for GRADIENT.\n");
230  fprintf(stderr,"\n");
231  fprintf(stderr," When called as:\n");
232  fprintf(stderr," cuiksmoothpath <problem name>\n");
233  fprintf(stderr," The default parameters and the default path name are assumed.\n");
234 
235  }
236  return(EXIT_SUCCESS);
237 }
238