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 #include "basic_algebra.h"
80 
81 int main(int argc, char **arg)
82 {
83  TAtlasBase world; /* The set of mechanism and obstacles. */
84  Tparameters parameters; /* Parameters used in the Cuik process. */
85 
86  Tfilename fparam;
87  Tfilename fpath;
88 
89  char *suffix;
90  boolean defaultPathName;
91 
92  /* input path */
93  unsigned int nvs,ns;
94  double **path;
95 
96  /* smoothed path */
97  unsigned int sns;
98  double **spath;
99  unsigned int mode;
100  boolean parallel;
101  unsigned int numIterations;
102 
103  unsigned int ri;
104  time_t t;
105 
106  if (argc>=2)
107  {
108  /* if only one parameter given, assume default values
109  even for the path name */
110  defaultPathName=(argc==2);
111 
112  /*Init parameters*/
113  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
114  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
115  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
116 
117  /*Read the world from file*/
118  CS_WD_INIT(&parameters,arg[1],&world);
119 
120  t=time(NULL); /* Get the time at which input files have been read */
121  ri=(unsigned int)t;
122  //ri=1405498615;
123  randomSet(ri);
124  fprintf(stderr,"Random seed : %u\n",ri);
125 
126  mode=SMOOTH_SHORTCUT;
127  if (defaultPathName)
128  suffix="_path_s";
129  else
130  suffix="_s";
131  if (argc>=4)
132  {
133  /* Select the smoothing mode */
134  switch(arg[3][0])
135  {
136  case 'G':
137  case 'g':
138  mode=SMOOTH_GRADIENT;
139  if (defaultPathName)
140  suffix="_path_g";
141  else
142  suffix="_g";
143  break;
144  case 'R':
145  case 'r':
146  mode=SMOOTH_RANDOM;
147  if (defaultPathName)
148  suffix="_path_r";
149  else
150  suffix="_r";
151  break;
152  case 'S':
153  case 's':
154  mode=SMOOTH_SHORTCUT;
155  if (defaultPathName)
156  suffix="_path_s";
157  else
158  suffix="_s";
159  break;
160  case 'E':
161  case 'e':
162  mode=SMOOTH_EFFORT;
163  if (defaultPathName)
164  suffix="_path_e";
165  else
166  suffix="_e";
167  break;
168  case 'D':
169  case 'd':
170  mode=SMOOTH_DISPERSION;
171  if (defaultPathName)
172  suffix="_path_d";
173  else
174  suffix="_d";
175  break;
176  default:
177  Error("Undefined smooth method");
178  }
179  }
180 
181  if (argc>=5)
182  numIterations=atoi(arg[4]);
183  else
184  numIterations=(mode==SMOOTH_RANDOM? 2 : 1000);
185 
186  if (argc>=6)
187  parallel=atoi(arg[5]);
188  else
189  parallel=(((mode==SMOOTH_GRADIENT)||(mode==SMOOTH_EFFORT)||(mode==SMOOTH_DISPERSION))? TRUE : FALSE);
190 
191  if (defaultPathName)
192  CreateFileName(NULL,arg[1],"_path",SOL_EXT,&fpath);
193  else
194  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fpath);
195 
196  if (LoadSamples(&fpath,&nvs,&ns,&path))
197  {
198  SmoothSamples(&parameters,parallel,mode,numIterations,ns,path,&sns,&spath,&world);
199 
200  if (defaultPathName)
201  SaveSamples(arg[1],suffix,nvs,sns,spath);
202  else
203  SaveSamples(arg[2],suffix,nvs,sns,spath);
204 
205  DeleteSamples(ns,path);
206  DeleteSamples(sns,spath);
207  }
208  else
209  Error("Could not read the input path file");
210 
211  DeleteParameters(&parameters);
212  CS_WD_DELETE(&world);
213 
214  DeleteFileName(&fparam);
215  DeleteFileName(&fpath);
216  }
217  else
218  {
219  fprintf(stderr," Wrong number of parameters.\n");
220  fprintf(stderr," Use:\n");
221  fprintf(stderr," cuiksmoothpath <problem name> <path name> [<mode> <iterations> <parallel>]\n");
222  fprintf(stderr," where <problem name> is the file describing the problem.\n");
223  fprintf(stderr," <path name> is the file with the path to smooth.\n");
224  fprintf(stderr," <mode> [optional] is the algorithm to use: RANDOM, GRADIENT, SHORTCUT or EFFORT.\n");
225  fprintf(stderr," The defatul is SHORTCUT (faster but might return suboptimal paths).\n");
226  fprintf(stderr," <iterations> [optional] is the maximum number of iterations.\n");
227  fprintf(stderr," The default is 2 for RANDOM smoothing and 1000 for GRADIENT smoothing.\n");
228  fprintf(stderr," For the RANDOM method this number is scaled by the number of steps in the path.\n");
229  fprintf(stderr," The GRADIENT method might stop earlier if the gradient becomes 0.\n");
230  fprintf(stderr," <parallel> [optional] 1 if the smooth has to be exectued in parallel and 0 for serial execution.\n");
231  fprintf(stderr," The default is 0 for RANDOM and SHORTCUT and 1 for GRADIENT.\n");
232  fprintf(stderr,"\n");
233  fprintf(stderr," When called as:\n");
234  fprintf(stderr," cuiksmoothpath <problem name>\n");
235  fprintf(stderr," The default parameters and the default path name are assumed.\n");
236 
237  }
238  return(EXIT_SUCCESS);
239 }
240 
#define SMOOTH_DISPERSION
One of the possible smoothing methods.
Definition: samples.h:90
#define FALSE
FALSE.
Definition: boolean.h:30
Data structure to hold the information about the name of a file.
Definition: filename.h:271
#define SMOOTH_GRADIENT
One of the possible smoothing methods.
Definition: samples.h:57
Definition of the Tfilename type and the associated functions.
#define TRUE
TRUE.
Definition: boolean.h:21
void Error(const char *s)
General error function.
Definition: error.c:80
#define PARAM_EXT
File extension for parameter files.
Definition: filename.h:132
#define CS_WD_DELETE(wcs)
Destructor of the equation structure.
Definition: wcs.h:592
int main(int argc, char **arg)
Main body of the cuiksmoothpath application.
void SaveSamples(char *fname, char *suffix, unsigned int nvs, unsigned int ns, double **path)
Saves a set of samples to a file.
Definition: samples.c:2879
Definition of the Tworld type and the associated functions.
Error and warning functions.
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
void SmoothSamples(Tparameters *pr, boolean parallel, int mode, unsigned int maxIterations, unsigned int ns, double **path, unsigned int *sns, double ***spath, TAtlasBase *w)
Path smoothing.
Definition: samples.c:2541
boolean LoadSamples(Tfilename *fname, unsigned int *nvs, unsigned int *ns, double ***path)
Reads a set of samples from file.
Definition: samples.c:2913
Definitions of constants and macros used in several parts of the cuik library.
#define SMOOTH_EFFORT
One of the possible smoothing methods.
Definition: samples.h:79
#define SMOOTH_SHORTCUT
One of the possible smoothing methods.
Definition: samples.h:67
A table of parameters.
void CreateFileName(char *path, char *name, char *suffix, char *ext, Tfilename *fn)
Constructor.
Definition: filename.c:22
void InitParametersFromFile(char *file, Tparameters *p)
Constructor from a file.
Definition: parameters.c:51
Type defining the equations on which the atlas is defined.
Definition: wcs.h:30
Definition of an atlas on a manifold.
char * GetFileFullName(Tfilename *fn)
Gets the file full name (paht+name+extension).
Definition: filename.c:151
#define SOL_EXT
File extension for solution files.
Definition: filename.h:138
#define SMOOTH_RANDOM
One of the possible smoothing methods.
Definition: samples.h:47
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:294
#define CS_WD_INIT(pr, name, wcs)
Initializes the equations from a file.
Definition: wcs.h:89
Auxiliary functions to deal with sets of samples.
Definition of basic randomization functions.
void DeleteSamples(unsigned int ns, double **path)
Deletes the space used by a set of samples.
Definition: samples.c:3159
Definition of the Tparameters type and the associated functions.
void randomSet(unsigned int seed)
Sets the random seed.
Definition: random.c:25