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  unsigned int i,n,k,m;
89  double *sol;
90  boolean state;
91  boolean *systemVars;
92 
93  Tbox b_sol;
94  unsigned int n_sol;
95 
96  unsigned int ri;
97 
98  /*Init parameters*/
99  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
100  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
101  fprintf(stderr,"Reading parameter file : %s\n",GetFileFullName(&fparam));
102 
103  if (GetParameter(CT_MAX_NEWTON_ITERATIONS,&parameters)<1)
104  ChangeParameter(CT_MAX_NEWTON_ITERATIONS,100,&parameters);
105 
106  /* Read the problem file */
107  CreateFileName(NULL,arg[1],NULL,CUIK_EXT,&fcuik);
108  InitCuikSystemFromFile(&parameters,GetFileFullName(&fcuik),&cuiksystem);
109  fprintf(stderr,"Reading cuik file : %s\n",GetFileFullName(&fcuik));
110 
111  ri=(unsigned int)time(NULL);
112  //ri=1405333052;
113  randomSet(ri);
114  fprintf(stderr,"Random seed : %u\n",ri);
115 
116  n=GetCSSystemVars(&systemVars,&cuiksystem);
117  NEW(sol,n,double);
118 
119  if (argc>2)
120  {
121  unsigned int nErrors;
122 
123  m=atoi(arg[2]);
124  n_sol=1;
125 
126  nErrors=0;
127  for(k=0;k<m;k++)
128  {
129  state=CuikNewton(&parameters,sol,&b_sol,&cuiksystem);
130 
131  printf("Sample: %u\n",k+1);
132  if (state)
133  {
134  printf(" Solution %u\n",n_sol);
135  n_sol++;
136  printf(" Solution Point: ");
137  for(i=0;i<n;i++)
138  {
139  if (systemVars[i])
140  printf("%.16g ",sol[i]);
141  }
142  printf("\n Solution Box : ");
143 
144  PrintBoxSubset(stdout,systemVars,NULL,&b_sol);
145  printf(" Solution error: %g\n",ErrorInSolution(&b_sol,&cuiksystem));
146  printf(" Solution error: %g\n",ErrorInCSEquations(sol,&cuiksystem));
147  }
148  else
149  {
150  printf(" Error in Newton\n");
151  nErrors++;
152  }
153 
154  DeleteBox(&b_sol);
155  }
156  printf("Newton error ratio: %.2f\n",(double)nErrors/(double)m);
157  }
158  else
159  {
160  /* iterate over all boxes */
161  Tfilename fsol,fnewton;
162  FILE *fin;
163  FILE *fout;
164  Tbox box,b_sol;
165  unsigned int out;
166  int token;
167  unsigned int nb;
168  unsigned int rep;
169 
170  CreateFileName(NULL,arg[1],NULL,SOL_EXT,&fsol);
171  rep=(unsigned int)(GetParameter(CT_REPRESENTATION,&parameters));
172  if (rep==REP_JOINTS)
173  CreateFileName(NULL,arg[1],"_newton",JOINTS_EXT,&fnewton);
174  else
175  CreateFileName(NULL,arg[1],"_newton",LINKS_EXT,&fnewton);
176 
177  fin=fopen(GetFileFullName(&fsol),"r");
178  if (!fin)
179  Error("Could not open the input file with the solutions");
180  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsol));
181 
182  fout=fopen(GetFileFullName(&fnewton),"w");
183  fprintf(stderr,"Writing samples to : %s\n",GetFileFullName(&fnewton));
184 
185  nb=0;
186  do {
187  token=ReadBox(fin,&box);
188  if (token!=EOF)
189  {
190  out=CuikNewtonInBox(&parameters,&box,sol,&b_sol,&cuiksystem);
191  nb++;
192 
193  switch(out)
194  {
195  case DIVERGED:
196  fprintf(fout,"*DV \n");
197  break;
199  /* Use the center of the box */
200  fprintf(fout,"*CO(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
201  break;
202  case CONVERGED_IN_GLOBAL:
203  fprintf(fout,"*CG(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
204  break;
205  case CONVERGED_IN_BOX:
206  fprintf(fout,"*CV(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
207  break;
208  default:
209  Error("Unknown output from CuikNewtonInBox");
210  }
211 
212  for(i=0;i<n;i++)
213  {
214  if (systemVars[i])
215  fprintf(fout,"%.12g ",sol[i]);
216  }
217  fprintf(fout,"\n");
218 
219  DeleteBox(&box);
220  DeleteBox(&b_sol);
221  }
222  } while (token!=EOF);
223 
224  fclose(fin);
225  fclose(fout);
226  DeleteFileName(&fsol);
227  DeleteFileName(&fnewton);
228  }
229 
230  free(systemVars);
231  free(sol);
232 
233  DeleteCuikSystem(&cuiksystem);
234  DeleteParameters(&parameters);
235 
236  DeleteFileName(&fcuik);
237  DeleteFileName(&fparam);
238  }
239  else
240  {
241  fprintf(stderr," Wrong number of parameters.\n");
242  fprintf(stderr," Use:\n");
243  fprintf(stderr," cuiknewton <problem filename>.cuik <n>\n");
244  fprintf(stderr," cuiknewton <problem filename>.cuik \n");
245  fprintf(stderr," where\n");
246  fprintf(stderr," <n> is the number of Newtons to execute\n");
247  fprintf(stderr," <problem filename> contains the kinematic equations\n");
248  fprintf(stderr," (the '.cuik' extension is not required)\n");
249  fprintf(stderr," In the first case, we attempt to generate <n> random samples.\n");
250  fprintf(stderr," In the second case, we generte one sample for each one of\n");
251  fprintf(stderr," the solution boxes in the <problem filename>.sol file.\n");
252  }
253 
254  return(EXIT_SUCCESS);
255 }
#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
int main(int argc, char **arg)
Main body of the cuiknewton application.
Definition: cuiknewton.c:78
void DeleteCuikSystem(TCuikSystem *cs)
Destructor.
Definition: cuiksystem.c:5450
unsigned int GetCSSystemVars(boolean **sv, TCuikSystem *cs)
Identifies the system variables.
Definition: cuiksystem.c:2659
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
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
boolean CuikNewton(Tparameters *p, double *sol, Tbox *b_sol, TCuikSystem *cs)
Applies Newton-Rhapson to a set of equations.
Definition: cuiksystem.c:3947
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
unsigned int CuikNewtonInBox(Tparameters *p, Tbox *bIn, double *sol, Tbox *b_sol, TCuikSystem *cs)
Applies Newton-Rhapson to a set of equations.
Definition: cuiksystem.c:3714
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
#define CT_MAX_NEWTON_ITERATIONS
Maximum number of iterations in the Newton-Raphson function.
Definition: parameters.h:311
double GetParameter(unsigned int n, Tparameters *p)
Gets the value for a particular parameter.
Definition: parameters.c:93
Definition of basic randomization functions.
void ChangeParameter(unsigned int n, double v, Tparameters *p)
Sets the value for a particular parameter.
Definition: parameters.c:164
#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
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