cuikgradient.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 
95 int main(int argc, char **arg)
96 {
97  if (argc>1)
98  {
99  Tparameters parameters;
100  TCuikSystem cuiksystem;
101 
102  Tfilename fcuik;
103  Tfilename fparam;
104 
105  unsigned int i,n,k,m;
106  double *sol;
107  boolean state;
108  boolean *systemVars;
109 
110  Tbox b_sol;
111  unsigned int n_sol;
112 
113  unsigned int ri;
114 
115  /*Init parameters*/
116  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
117  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
118  fprintf(stderr,"Reading parameter file : %s\n",GetFileFullName(&fparam));
119 
120  /* Read the problem file */
121  CreateFileName(NULL,arg[1],NULL,CUIK_EXT,&fcuik);
122  InitCuikSystemFromFile(&parameters,GetFileFullName(&fcuik),&cuiksystem);
123  fprintf(stderr,"Reading cuik file : %s\n",GetFileFullName(&fcuik));
124 
125  ri=(unsigned int)time(NULL);
126  //ri=1405333052;
127  randomSet(ri);
128  fprintf(stderr,"Random seed : %u\n",ri);
129 
130  n=GetCSSystemVars(&systemVars,&cuiksystem);
131  NEW(sol,n,double);
132 
133  if (argc>2)
134  {
135  unsigned int nErrors;
136 
137  m=atoi(arg[2]);
138  n_sol=1;
139 
140  nErrors=0;
141  for(k=0;k<m;k++)
142  {
143  state=CuikGradient(&parameters,sol,&b_sol,&cuiksystem);
144 
145  printf("Sample: %u\n",k+1);
146  if (state)
147  {
148  printf(" Solution %u\n",n_sol);
149  n_sol++;
150  printf(" Solution Point: ");
151  for(i=0;i<n;i++)
152  {
153  if (systemVars[i])
154  printf("%.16g ",sol[i]);
155  }
156  printf("\n Solution Box : ");
157 
158  PrintBoxSubset(stdout,systemVars,NULL,&b_sol);
159  printf(" Solution error: %g\n",ErrorInSolution(&b_sol,&cuiksystem));
160  printf(" Solution error: %g\n",ErrorInCSEquations(sol,&cuiksystem));
161  }
162  else
163  {
164  printf(" Error in gradient\n");
165  nErrors++;
166  }
167 
168  DeleteBox(&b_sol);
169  }
170  printf("Gradient error ratio: %.2f\n",(double)nErrors/(double)m);
171  }
172  else
173  {
174  /* iterate over all boxes */
175  Tfilename fsol,fgradient;
176  FILE *fin;
177  FILE *fout;
178  Tbox box,b_sol;
179  unsigned int out;
180  int token;
181  unsigned int nb;
182  unsigned int rep;
183 
184  CreateFileName(NULL,arg[1],NULL,SOL_EXT,&fsol);
185  rep=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
186  if (rep==REP_JOINTS)
187  CreateFileName(NULL,arg[1],"_gradient",JOINTS_EXT,&fgradient);
188  else
189  CreateFileName(NULL,arg[1],"_gradient",LINKS_EXT,&fgradient);
190 
191  fin=fopen(GetFileFullName(&fsol),"r");
192  if (!fin)
193  Error("Could not open the input file with the solutions");
194  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsol));
195 
196  fout=fopen(GetFileFullName(&fgradient),"w");
197  fprintf(stderr,"Writing samples to : %s\n",GetFileFullName(&fgradient));
198 
199  nb=0;
200  do {
201  token=ReadBox(fin,&box);
202  if (token!=EOF)
203  {
204  out=CuikGradientInBox(&parameters,&box,sol,&b_sol,&cuiksystem);
205  nb++;
206 
207  switch(out)
208  {
209  case DIVERGED:
210  fprintf(fout,"*DV \n");
211  break;
213  /* Use the center of the box */
214  fprintf(fout,"*CO(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
215  break;
216  case CONVERGED_IN_GLOBAL:
217  fprintf(fout,"*CG(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
218  break;
219  case CONVERGED_IN_BOX:
220  fprintf(fout,"*CV(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
221  break;
222  default:
223  Error("Unknown output from CuikGradientInBox");
224  }
225 
226  for(i=0;i<n;i++)
227  {
228  if (systemVars[i])
229  fprintf(fout,"%.12g ",sol[i]);
230  }
231  fprintf(fout,"\n");
232 
233  DeleteBox(&box);
234  DeleteBox(&b_sol);
235  }
236  } while (token!=EOF);
237 
238  fclose(fin);
239  fclose(fout);
240  DeleteFileName(&fsol);
241  DeleteFileName(&fgradient);
242  }
243 
244  free(systemVars);
245  free(sol);
246 
247  DeleteCuikSystem(&cuiksystem);
248  DeleteParameters(&parameters);
249 
250  DeleteFileName(&fcuik);
251  DeleteFileName(&fparam);
252  }
253  else
254  {
255  fprintf(stderr," Wrong number of parameters.\n");
256  fprintf(stderr," Use:\n");
257  fprintf(stderr," cuikgradient <problem filename>.cuik <n>\n");
258  fprintf(stderr," cuikgradient <problem filename>.cuik \n");
259  fprintf(stderr," where\n");
260  fprintf(stderr," <n> is the number of gradient descents to execute\n");
261  fprintf(stderr," <problem filename> contains the kinematic equations\n");
262  fprintf(stderr," (the '.cuik' extension is not required)\n");
263  fprintf(stderr," In the first case, we attempt to generate <n> random samples.\n");
264  fprintf(stderr," In the second case, we generte one sample for each one of\n");
265  fprintf(stderr," the solution boxes in the <problem filename>.sol file.\n");
266  }
267 
268  return(EXIT_SUCCESS);
269 }
#define REP_JOINTS
One of the possible values of the REPRESENTATION parameter.
Definition: parameters.h:60
#define CONVERGED_OUTSIDE_GLOBAL
One of the possible outputs of the Newton iteration.
Definition: cuiksystem.h:150
#define NEW(_var, _n, _type)
Allocates memory space.
Definition: defines.h:385
Data structure to hold the information about the name of a file.
Definition: filename.h:271
void DeleteCuikSystem(TCuikSystem *cs)
Destructor.
Definition: cuiksystem.c:5450
unsigned int GetCSSystemVars(boolean **sv, TCuikSystem *cs)
Identifies the system variables.
Definition: cuiksystem.c:2659
boolean CuikGradient(Tparameters *p, double *sol, Tbox *b_sol, TCuikSystem *cs)
Applies gradient descent to a set of equations.
Definition: cuiksystem.c:4171
Definition of the Tfilename type and the associated functions.
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 CUIK_EXT
File extension for equation files.
Definition: filename.h:71
#define CONVERGED_IN_BOX
One of the possible outputs of the Newton iteration.
Definition: cuiksystem.h:124
void DeleteFileName(Tfilename *fn)
Destructor.
Definition: filename.c:205
int ReadBox(FILE *f, Tbox *b)
Reads a box from a file.
Definition: box.c:1196
double ErrorInCSEquations(double *p, TCuikSystem *cs)
Evalates the norm of the error in a point.
Definition: cuiksystem.c:5120
int main(int argc, char **arg)
Main body of the cuikgradient application.
Definition: cuikgradient.c:95
Definition of the Tbox type and the associated functions.
Definitions of constants and macros used in several parts of the cuik library.
void InitCuikSystemFromFile(Tparameters *p, char *filename, TCuikSystem *cs)
Constructor from a file.
#define CONVERGED_IN_GLOBAL
One of the possible outputs of the Newton iteration.
Definition: cuiksystem.h:138
A table of parameters.
Definition of the TCuikSystem type and the associated functions.
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
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 CT_REPRESENTATION
Representation.
Definition: parameters.h:215
A box.
Definition: box.h:83
#define JOINTS_EXT
File extension for files with samples represented by the joint values.
Definition: filename.h:188
A cuiksystem, i.e., a set of variables and equations defining a position analysis problem...
Definition: cuiksystem.h:181
void DeleteParameters(Tparameters *p)
Destructor.
Definition: parameters.c:294
void DeleteBox(void *b)
Destructor.
Definition: box.c:1283
void PrintBoxSubset(FILE *f, boolean *used, char **varNames, Tbox *b)
Prints a (sub-)box.
Definition: box.c:1162
double GetParameter(unsigned int n, Tparameters *p)
Gets the value for a particular parameter.
Definition: parameters.c:93
Definition of basic randomization functions.
#define DIVERGED
One of the possible outputs of the Newton iteration.
Definition: cuiksystem.h:111
#define LINKS_EXT
File extension for files with samples represented by the link poses.
Definition: filename.h:181
unsigned int CuikGradientInBox(Tparameters *p, Tbox *bIn, double *sol, Tbox *b_sol, TCuikSystem *cs)
Applies gradient descent to a set of equations.
Definition: cuiksystem.c:3961
double ErrorInSolution(Tbox *b, TCuikSystem *cs)
Maximum error for a box.
Definition: cuiksystem.c:5180
void randomSet(unsigned int seed)
Sets the random seed.
Definition: random.c:25