cuikminimize.c
Go to the documentation of this file.
1 #include "world.h"
2 #include "atlas.h"
3 #include "filename.h"
4 #include "samples.h"
5 #include "error.h"
6 #include "parameters.h"
7 
8 #include <stdlib.h>
9 #include <string.h>
10 #include <time.h>
11 
63 int main(int argc, char **arg)
64 {
65  if (argc>1)
66  {
67  Tworld world; /* The tensegrity description. */
68  Tatlas atlas; /* The atlas defined during minimization. */
69  Tparameters parameters; /* Parameters used in the Cuik process. */
70 
71  Tfilename fparam;
72  Tfilename fatlas;
73 
74  double *s1; /* Starting point for the minimization. */
75 
76  unsigned int maxIterations;
77 
78  TAtlasBase ab;
79 
80  /*Init parameters*/
81  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
82  fprintf(stderr,"Reading parameters from : %s\n",GetFileFullName(&fparam));
83  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
84 
85  if (GetParameter(CT_DYNAMICS,&parameters)>0)
86  Error("cuikminimize do not work (yet) for problems with dynamics");
87 
88  /*Init the world (tensegrity only)*/
89  if (!InitTensegrityFromFile(&parameters,arg[1],&world))
90  Error("Can not open the tensegrity file");
91  CS_WD_FROM_WORLD(&world,&ab);
92 
93  /* Read the start sample */
94  ReadOneSample(&parameters,arg[1],CS_WD_GET_NUM_SYSTEM_VARS(&ab),&s1);
95 
96  if (argc>2)
97  {
98  maxIterations=atoi(arg[2]);
99  if (maxIterations==0)
100  Error("Wrong maximum number of iterations");
101  }
102  else
103  maxIterations=1000;
104  fprintf(stderr,"Number of iterations : %u\n",maxIterations);
105 
106  /* Minimize */
107  MinimizeOnAtlas(&parameters,arg[1],s1,&ab,maxIterations,
110  (void *)(&world),&atlas);
111 
112 
113  /* Save the results (the tracked paths are already saved in MinimizeOnAtlas) */
114  CreateFileName(NULL,arg[1],NULL,ATLAS_EXT,&fatlas);
115  fprintf(stderr,"Writing atlas to : %s\n",GetFileFullName(&fatlas));
116  SaveAtlas(&parameters,&fatlas,&atlas);
117 
118  /* Release memory */
119  free(s1);
120  DeleteAtlas(&atlas);
121  DeleteParameters(&parameters);
122  DeleteWorld(&world);
123 
124  DeleteFileName(&fparam);
125  DeleteFileName(&fatlas);
126  }
127  else
128  {
129  fprintf(stderr," Wrong number of parameters.\n");
130  fprintf(stderr," Use:\n");
131  fprintf(stderr," cuikminimize <problem filename>.tens [max iterations]\n");
132  fprintf(stderr," where <problem filename>.tens contains the tensegrity description\n");
133  fprintf(stderr," [max iterations] is the optional maximum number of iterations\n");
134  fprintf(stderr," The default is 1000\n");
135  fprintf(stderr," The extension for the problem file (ex. tens) is not required.\n");
136  }
137  return(EXIT_SUCCESS);
138 }
139