cuikcost.c
Go to the documentation of this file.
1 #include "filename.h"
2 #include "box_list.h"
3 #include "error.h"
4 #include "defines.h"
5 #include "parameters.h"
6 #include "world.h"
7 #include "chart.h"
8 
9 
10 #include <stdio.h>
11 #include <math.h>
12 
79 int main(int argc, char **arg)
80 {
81  Tparameters parameters;
82  TAtlasBase world;
83  Tfilename fparam,fsols,fcost;
84  Tbox *box;
85  FILE *fout;
86  unsigned int n,k,nBoxes;
87  double *point;
88  Tlist boxList;
89  Titerator i;
90  double *cost,minCost,maxCost,c;
91 
92  if (argc>2)
93  {
94  /*Init parameters*/
95  CreateFileName(NULL,arg[1],NULL,PARAM_EXT,&fparam);
96  fprintf(stderr,"Reading parameters : %s\n",GetFileFullName(&fparam));
97  InitParametersFromFile(GetFileFullName(&fparam),&parameters);
98 
99  /*Read the world from file*/
100  CS_WD_INIT(&parameters,arg[1],&world);
101 
102  CreateFileName(NULL,arg[2],NULL,SOL_EXT,&fsols);
103  fprintf(stderr,"Reading solution file : %s\n",GetFileFullName(&fsols));
104  ReadListOfBoxes(GetFileFullName(&fsols),&boxList);
105  nBoxes=ListSize(&boxList);
106  if (nBoxes==0)
107  Error("Empty solution file in cuikcost");
108 
109  /* Compute the energy for each conformation */
110  InitIterator(&i,&boxList);
111  First(&i);
112  k=0;
113  NEW(cost,nBoxes,double);
114  while(!EndOfList(&i))
115  {
116  box=(Tbox *)GetCurrent(&i);
117  if (k==0)
118  {
119  n=GetBoxNIntervals(box);
120  NEW(point,n,double);
121  }
122  GetBoxCenter(NULL,point,box);
123  cost[k]=CS_WD_COST(&parameters,FALSE,point,&world);
124  if (k==0)
125  {
126  minCost=cost[k];
127  maxCost=cost[k];
128  }
129  else
130  {
131  if (cost[k]>maxCost)
132  maxCost=cost[k];
133  else
134  {
135  if (cost[k]<minCost)
136  minCost=cost[k];
137  }
138  }
139  k++;
140  Advance(&i);
141  }
142  fprintf(stderr,"Extreme costs : %g %g\n",minCost,maxCost);
143 
144  /* Normalize and write */
145  CreateFileName(NULL,arg[2],NULL,COST_EXT,&fcost);
146  fprintf(stderr,"Writing cost file : %s\n",GetFileFullName(&fcost));
147 
148  fout=fopen(GetFileFullName(&fcost),"w");
149  if (!fout)
150  Error("Output file for costs can not be opened");
151 
152  for(k=0;k<nBoxes;k++)
153  {
154  c=(cost[k]-minCost)/(maxCost-minCost);
155  if (c<1e-4) c=1e-4;
156  fprintf(fout,"%g\n",c);
157  }
158 
159  fclose(fout);
160 
161  DeleteFileName(&fsols);
162  DeleteFileName(&fcost);
163  DeleteFileName(&fparam);
164 
165  free(point);
166  free(cost);
167  DeleteListOfBoxes(&boxList);
168  DeleteParameters(&parameters);
169  CS_WD_DELETE(&world);
170  }
171  else
172  {
173  fprintf(stderr,"Use:\n");
174  fprintf(stderr," cuikcost <problem_name> <sol_name> \n");
175  fprintf(stderr," <problem_name> the input problem.\n");
176  fprintf(stderr," <sol_name> Configurations to evaluate.\n");
177  fprintf(stderr," The ouput file is <sol_name>.cost\n");
178  }
179 
180  return(EXIT_SUCCESS);
181 }