cuiknewton.c
Go to the documentation of this file.
1 #include "box.h"
2 #include "random.h"
3 #include "defines.h"
4 #include "filename.h"
5 #include "cuiksystem.h"
6 #include "link.h"
7 
8 #include <stdlib.h>
9 
78 int main(int argc, char **arg)
79 {
80  if (argc>1)
81  {
82  Tparameters parameters;
83  TCuikSystem cuiksystem;
84 
85  Tfilename fcuik;
86  Tfilename fparam;
87 
88  boolean dynamics;
89 
90  unsigned int i,n,k,m,ns;
91  double *sol;
92  boolean state;
93  boolean *systemVars;
94  boolean *systemVarsDynamics;
95  boolean *posVars;
96 
97  Tbox b_sol,b_dynamics;
98  unsigned int n_sol;
99 
100  unsigned int ri;
101 
102  /*Init parameters*/
103  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
104  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
105  fprintf(stderr,"Reading parameter file : %s\n",GetFileFullName(&fparam));
106 
107  if (GetParameter(CT_DYNAMICS,&parameters)>0)
108  {
109  /* In problems with dynamics we considere only position constraints
110  and set the velocity to 0 */
111  dynamics=TRUE;
112  /* Des-activate the automatic generation of velocity equations */
113  ChangeParameter(CT_DYNAMICS,0,&parameters);
114  }
115  else
116  dynamics=FALSE;
117 
118  if (GetParameter(CT_MAX_NEWTON_ITERATIONS,&parameters)<1)
119  ChangeParameter(CT_MAX_NEWTON_ITERATIONS,100,&parameters);
120 
121  /* Read the problem file */
122  CreateFileName(NULL,arg[1],NULL,CUIK_EXT,&fcuik);
123  InitCuikSystemFromFile(&parameters,GetFileFullName(&fcuik),&cuiksystem);
124  fprintf(stderr,"Reading cuik file : %s\n",GetFileFullName(&fcuik));
125 
126  /* Even if the automatic generation of velocity equations is deactivated,
127  the input file may already include such equations. Ifso the user wants to
128  execte Newton over the state manifold -> deactivate the zero
129  velocity generation */
130  if (CuikHasVelocity(&parameters,&cuiksystem))
131  dynamics=FALSE;
132 
133  ri=(unsigned int)time(NULL);
134  //ri=1405333052;
135  randomSet(ri);
136  fprintf(stderr,"Random seed : %u\n",ri);
137 
138  n=GetCSSystemVars(&systemVars,&cuiksystem);
139  ns=GetCSNumSystemVariables(&cuiksystem);
140  NEW(sol,n,double);
141  if (dynamics)
142  {
143  InitBox(2*n,NULL,&b_dynamics); /* initialized as zeros */
144  NEW(systemVarsDynamics,2*n,boolean);
145  NEW(posVars,2*n,boolean);
146  for(i=0;i<n;i++)
147  {
148  posVars[i]=TRUE;
149  posVars[n+i]=FALSE;
150  systemVarsDynamics[i]=systemVars[i];
151  systemVarsDynamics[n+i]=systemVars[i];
152  }
153  }
154 
155  if (argc>2)
156  {
157  unsigned int nErrors;
158 
159  m=atoi(arg[2]);
160  n_sol=1;
161 
162  nErrors=0;
163  for(k=0;k<m;k++)
164  {
165  state=CuikNewton(&parameters,sol,&b_sol,&cuiksystem);
166 
167  printf("Sample: %u\n",k+1);
168  if (state)
169  {
170  printf(" Solution %u\n",n_sol);
171  n_sol++;
172  printf(" Solution Point: ");
173  for(i=0;i<n;i++)
174  {
175  if (systemVars[i])
176  printf("%.16g ",sol[i]);
177  }
178  if (dynamics)
179  {
180  /* Zero velocity. This always fulfill the velocity equations */
181  for(i=0;i<ns;i++)
182  printf("0 ");
183  }
184  printf("\n Solution Box : ");
185 
186  if (dynamics)
187  {
188  /* b_dynamics is a box with b_sol at the first positon and zeros at the end */
189  SetBoxSubset(posVars,&b_sol,&b_dynamics);
190  /* Print the system variables for b_dynamics. fullSystemVariables is a
191  duplicate of systemVariables */
192  PrintBoxSubset(stdout,systemVarsDynamics,NULL,&b_dynamics);
193  }
194  else
195  PrintBoxSubset(stdout,systemVars,NULL,&b_sol);
196 
197  printf(" Solution error: %g\n",ErrorInSolution(&b_sol,&cuiksystem));
198  printf(" Solution error: %g\n",ErrorInCSEquations(&parameters,sol,&cuiksystem));
199  }
200  else
201  {
202  printf(" Error in Newton\n");
203  nErrors++;
204  }
205 
206  DeleteBox(&b_sol);
207  }
208  printf("Newton error ratio: %.2f\n",(double)nErrors/(double)m);
209  }
210  else
211  {
212  /* iterate over all boxes */
213  Tfilename fsol,fnewton;
214  FILE *fin;
215  FILE *fout;
216  Tbox box,b_sol;
217  unsigned int out;
218  int token;
219  unsigned int nb;
220  unsigned int rep;
221 
222  CreateFileName(NULL,arg[1],NULL,SOL_EXT,&fsol);
223  rep=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
224  if (rep==REP_JOINTS)
225  CreateFileName(NULL,arg[1],"_newton",JOINTS_EXT,&fnewton);
226  else
227  CreateFileName(NULL,arg[1],"_newton",LINKS_EXT,&fnewton);
228 
229  fin=fopen(GetFileFullName(&fsol),"r");
230  if (!fin)
231  Error("Could not open the input file with the solutions");
232  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsol));
233 
234  fout=fopen(GetFileFullName(&fnewton),"w");
235  fprintf(stderr,"Writing samples to : %s\n",GetFileFullName(&fnewton));
236 
237  nb=0;
238  do {
239  token=ReadBox(fin,&box);
240  if (token!=EOF)
241  {
242  out=CuikNewtonInBox(&parameters,&box,sol,&b_sol,&cuiksystem);
243  nb++;
244 
245  switch(out)
246  {
247  case DIVERGED:
248  fprintf(fout,"*DV \n");
249  break;
251  /* Use the center of the box */
252  fprintf(fout,"*CO(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
253  break;
254  case CONVERGED_IN_GLOBAL:
255  fprintf(fout,"*CG(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
256  break;
257  case CONVERGED_IN_BOX:
258  fprintf(fout,"*CV(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
259  break;
260  default:
261  Error("Unknown output from CuikNewtonInBox");
262  }
263 
264  for(i=0;i<n;i++)
265  {
266  if (systemVars[i])
267  fprintf(fout,"%.16g ",sol[i]);
268  }
269  if (dynamics)
270  {
271  /* Zero velocity. This always fulfill the velocity equations */
272  for(i=0;i<ns;i++)
273  printf("0 ");
274  }
275  fprintf(fout,"\n");
276 
277  DeleteBox(&box);
278  DeleteBox(&b_sol);
279  }
280  } while (token!=EOF);
281 
282  fclose(fin);
283  fclose(fout);
284  DeleteFileName(&fsol);
285  DeleteFileName(&fnewton);
286  }
287 
288  if (dynamics)
289  {
290  DeleteBox(&b_dynamics);
291  free(systemVarsDynamics);
292  free(posVars);
293  }
294  free(systemVars);
295  free(sol);
296 
297  DeleteCuikSystem(&cuiksystem);
298  DeleteParameters(&parameters);
299 
300  DeleteFileName(&fcuik);
301  DeleteFileName(&fparam);
302  }
303  else
304  {
305  fprintf(stderr," Wrong number of parameters.\n");
306  fprintf(stderr," Use:\n");
307  fprintf(stderr," cuiknewton <problem filename>.cuik <n>\n");
308  fprintf(stderr," cuiknewton <problem filename>.cuik \n");
309  fprintf(stderr," where\n");
310  fprintf(stderr," <n> is the number of Newtons to execute\n");
311  fprintf(stderr," <problem filename> contains the kinematic equations\n");
312  fprintf(stderr," (the '.cuik' extension is not required)\n");
313  fprintf(stderr," In the first case, we attempt to generate <n> random samples.\n");
314  fprintf(stderr," In the second case, we generte one sample for each one of\n");
315  fprintf(stderr," the solution boxes in the <problem filename>.sol file.\n");
316  }
317 
318  return(EXIT_SUCCESS);
319 }