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 
82 int main(int argc, char **arg)
83 {
84  if (argc>1)
85  {
86  Tparameters parameters;
87  TCuikSystem cuiksystem;
88 
89  Tfilename fcuik;
90  Tfilename fparam;
91 
92  unsigned int i,n,k,m;
93  double *sol;
94  boolean state;
95  boolean *systemVars;
96 
97  Tbox b_sol;
98  unsigned int n_sol;
99 
100  randomReset();
101  randomSet(1);
102 
103  /*Init parameters*/
104  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
105  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
106  fprintf(stderr,"Reading parameter file : %s\n",GetFileFullName(&fparam));
107 
108  if (GetParameter(CT_MAX_NEWTON_ITERATIONS,&parameters)<1)
109  ChangeParameter(CT_MAX_NEWTON_ITERATIONS,100,&parameters);
110 
111  /* Read the problem file */
112  CreateFileName(NULL,arg[1],NULL,CUIK_EXT,&fcuik);
113  InitCuikSystemFromFile(&parameters,GetFileFullName(&fcuik),&cuiksystem);
114  fprintf(stderr,"Reading cuik file : %s\n",GetFileFullName(&fcuik));
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  /* For the first solution box we store additional information */
191  out=CuikNewtonInBox(&parameters,&box,sol,&b_sol,&cuiksystem);
192  nb++;
193 
194  switch(out)
195  {
196  case DIVERGED:
197  /* Use the center of the box */
198  fprintf(fout,"*DV \n");
199  break;
201  /* Use the center of the box */
202  fprintf(fout,"*CO(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
203  break;
204  case CONVERGED_IN_GLOBAL:
205  fprintf(fout,"*CG(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
206  break;
207  case CONVERGED_IN_BOX:
208  fprintf(fout,"*CV(%g) \n ",ErrorInSolution(&b_sol,&cuiksystem));
209  break;
210  default:
211  Error("Unknown output from CuikNewtonInBox");
212  }
213 
214  for(i=0;i<n;i++)
215  {
216  if (systemVars[i])
217  fprintf(fout,"%.12g ",sol[i]);
218  }
219  fprintf(fout,"\n");
220 
221  DeleteBox(&box);
222  DeleteBox(&b_sol);
223  }
224  } while (token!=EOF);
225 
226  fclose(fin);
227  fclose(fout);
228  DeleteFileName(&fsol);
229  DeleteFileName(&fnewton);
230  }
231 
232  free(systemVars);
233  free(sol);
234 
235  DeleteCuikSystem(&cuiksystem);
236  DeleteParameters(&parameters);
237 
238  DeleteFileName(&fcuik);
239  DeleteFileName(&fparam);
240  }
241  else
242  {
243  fprintf(stderr," Wrong number of parameters.\n");
244  fprintf(stderr," Use:\n");
245  fprintf(stderr," cuiknewton <problem filename>.cuik <n>\n");
246  fprintf(stderr," cuiknewton <problem filename>.cuik \n");
247  fprintf(stderr," where\n");
248  fprintf(stderr," <n> is the number of Newtons to execute\n");
249  fprintf(stderr," <problem filename> contains the kinematic equations\n");
250  fprintf(stderr," (the '.cuik' extension is not required)\n");
251  fprintf(stderr," In the first case, we attempt to generate <n> random samples.\n");
252  fprintf(stderr," In the second case, we generte one sample for each one of\n");
253  fprintf(stderr," the solution boxes in the <problem filename>.sol file.\n");
254  }
255 
256  return(EXIT_SUCCESS);
257 }
#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:248
int main(int argc, char **arg)
Main body of the cuiknewton application.
Definition: cuiknewton.c:82
void randomReset()
Resets the random seed.
Definition: random.c:18
void DeleteCuikSystem(TCuikSystem *cs)
Destructor.
Definition: cuiksystem.c:5113
unsigned int GetCSSystemVars(boolean **sv, TCuikSystem *cs)
Identifies the system variables.
Definition: cuiksystem.c:2614
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:131
#define CUIK_EXT
File extension for equation files.
Definition: filename.h:70
#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:1172
double ErrorInCSEquations(double *p, TCuikSystem *cs)
Evalates the norm of the error in a point.
Definition: cuiksystem.c:4826
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:3905
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:3667
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:137
#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:187
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:295
void DeleteBox(void *b)
Destructor.
Definition: box.c:1259
void PrintBoxSubset(FILE *f, boolean *used, char **varNames, Tbox *b)
Prints a (sub-)box.
Definition: box.c:1138
#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:180
double ErrorInSolution(Tbox *b, TCuikSystem *cs)
Maximum error for a box.
Definition: cuiksystem.c:4885
void randomSet(unsigned int seed)
Sets the random seed.
Definition: random.c:25