Xmipp  v3.23.11-Nereus
Public Member Functions | Public Attributes | Protected Attributes | Friends | List of all members
ObjectiveFunction Class Referenceabstract

#include <ObjectiveFunction.h>

Inheritance diagram for ObjectiveFunction:
Inheritance graph
[legend]
Collaboration diagram for ObjectiveFunction:
Collaboration graph
[legend]

Public Member Functions

virtual double evalNLConstraint (int j, Vector v, int *nerror)=0
 
virtual Vector evalGradNLConstraint (int j, Vector v, int *nerror)
 
virtual void evalGradNLConstraint (int j, Vector v, Vector result, int *nerror)=0
 
 ObjectiveFunction ()
 
virtual ~ObjectiveFunction ()
 
virtual double eval (Vector v, int *nerror)=0
 
int dim ()
 
void initData ()
 
virtual void saveValue (Vector tmp, double valueOF, int nerror)
 
virtual void printStats (char cc=1)
 
void saveStats (char *filename, Vector vG, Matrix mH, Vector vLambda)
 
virtual void finalize (Vector vG, Matrix mH, Vector vLambda)
 
void setName (char *s)
 
void setSaveFile (char *b=NULL)
 
void updateCounter (double df, Vector vX, int nerror=0)
 
char isFeasible (Vector vx, double *d=NULL)
 
void initBounds ()
 
void endInit ()
 
void initTolLC (Vector vX)
 
void initTolNLC (Vector c, double delta)
 
virtual int getNFE ()
 
virtual int getNFE2 ()
 

Public Attributes

char name [9]
 
char startPointIsGiven
 
Vector xStart
 
Vector xBest
 
Vector xOptimal
 
double valueOptimal
 
double valueBest
 
double noiseAbsolute
 
double noiseRelative
 
double objectiveConst
 
Matrix data
 
int t
 
int nNLConstraints
 
int isConstrained
 
Vector bl
 
Vector bu
 
Matrix A
 
Vector b
 
double tolRelFeasibilityForNLC
 
double tolNLC
 
double tolRelFeasibilityForLC
 
double tolLC
 

Protected Attributes

int nfe
 
int nfe2
 

Friends

class CorrectScaleOF
 

Detailed Description

Definition at line 36 of file ObjectiveFunction.h.

Constructor & Destructor Documentation

◆ ObjectiveFunction()

ObjectiveFunction::ObjectiveFunction ( )
inline

Definition at line 77 of file ObjectiveFunction.h.

80  tolRelFeasibilityForLC(1e-6), tolLC(1e-8),
81  saveFileName(NULL), dfold(INF), maxNormLC(0.0), maxNormNLC(0.0), nfe(0), nfe2(0) { };
#define INF

◆ ~ObjectiveFunction()

virtual ObjectiveFunction::~ObjectiveFunction ( )
inlinevirtual

Definition at line 82 of file ObjectiveFunction.h.

82 { if (saveFileName) free(saveFileName); };
free((char *) ob)

Member Function Documentation

◆ dim()

int ObjectiveFunction::dim ( )

Definition at line 208 of file ObjectiveFunction.cpp.

209 {
210  int n=xStart.sz();
211  if (n>0) return n;
212  return data.nColumn()-2;
213 }
int nColumn()
Definition: Matrix.h:84
unsigned sz()
Definition: Vector.h:79
int * n

◆ endInit()

void ObjectiveFunction::endInit ( )

Definition at line 88 of file ObjectiveFunction.cpp.

90 {
91  int i,mdim=dim();
92  double *bbl=bl, *bbu=bu;
93 
94  isConstrained=0;
95  for (i=0; i<mdim; i++)
96  {
97  if (bbl[i]>-INF) {isConstrained=1; maxNormLC=mmax(maxNormLC, condorAbs(bbl[i])); }
98  if (bbu[i]< INF) {isConstrained=1; maxNormLC=mmax(maxNormLC, condorAbs(bbu[i])); }
99  }
100  if (b.sz()) { isConstrained=1; maxNormLC=mmax(maxNormLC,b.LnftyNorm()); }
101  tolLC=(1.0+maxNormLC)*tolRelFeasibilityForLC*(mdim*2+A.nLine());
102 
104 }
int * mmax
double LnftyNorm()
Definition: Vector.cpp:264
#define i
unsigned sz()
Definition: Vector.h:79
int nLine()
Definition: Matrix.h:83
double condorAbs(const double t1)
Definition: tools.h:47
#define INF
Definition: svm.cpp:43

◆ eval()

virtual double ObjectiveFunction::eval ( Vector  v,
int *  nerror 
)
pure virtual

◆ evalGradNLConstraint() [1/2]

Vector ObjectiveFunction::evalGradNLConstraint ( int  j,
Vector  v,
int *  nerror 
)
virtual

Reimplemented in UnconstrainedObjectiveFunction.

Definition at line 328 of file ObjectiveFunction.cpp.

329 {
330  Vector R(dim());
331  evalGradNLConstraint(j, v, R, nerror);
332  return R;
333 }
Definition: Vector.h:37
#define j
virtual Vector evalGradNLConstraint(int j, Vector v, int *nerror)

◆ evalGradNLConstraint() [2/2]

virtual void ObjectiveFunction::evalGradNLConstraint ( int  j,
Vector  v,
Vector  result,
int *  nerror 
)
pure virtual

◆ evalNLConstraint()

virtual double ObjectiveFunction::evalNLConstraint ( int  j,
Vector  v,
int *  nerror 
)
pure virtual

◆ finalize()

virtual void ObjectiveFunction::finalize ( Vector  vG,
Matrix  mH,
Vector  vLambda 
)
inlinevirtual

Reimplemented in CorrectScaleOF.

Definition at line 89 of file ObjectiveFunction.h.

89 {};

◆ getNFE()

virtual int ObjectiveFunction::getNFE ( )
inlinevirtual

Reimplemented in CorrectScaleOF.

Definition at line 98 of file ObjectiveFunction.h.

98 { return nfe; }

◆ getNFE2()

virtual int ObjectiveFunction::getNFE2 ( )
inlinevirtual

Reimplemented in CorrectScaleOF.

Definition at line 99 of file ObjectiveFunction.h.

99 { return nfe2; }

◆ initBounds()

void ObjectiveFunction::initBounds ( )

Definition at line 315 of file ObjectiveFunction.cpp.

316 {
317  int dim=this->dim();
318  bl.setSize(dim);
319  bu.setSize(dim);
320  double *dbl=bl,*dbu=bu;
321  while (dim--)
322  {
323  *(dbl++)=-INF;
324  *(dbu++)=INF;
325  }
326 }
void setSize(int _n)
Definition: Vector.cpp:112
#define INF
Definition: svm.cpp:43

◆ initData()

void ObjectiveFunction::initData ( )

Definition at line 259 of file ObjectiveFunction.cpp.

260 {
261  if (data.nLine()==0)
262  {
263  initTolLC(xStart);
264  addClosestFeasiblePointInData(xStart);
265  return;
266  }
267 
268  if (startPointIsGiven)
269  {
270  int i=data.lineIndex(xStart);
271  if (i!=-1)
272  {
273  data.swapLines(0,i);
274  return;
275  }
276  initTolLC(xStart);
277  addClosestFeasiblePointInData(xStart);
278  return;
279  }
280 
281  // find THE best point in the datas.
282  int mdim=dim();
283  Vector r(mdim);
284  data.getLine(0,r,mdim);
285  initTolLC(r);
286 
287  int k=-1,j,i=data.nLine();
288  double v,best=INF,best2=INF;
289  while (i--)
290  {
291  if (((double**)data)[i][mdim+1]) continue;
292  v=((double**)data)[i][mdim];
293  if (v<best2) { j=i; best2=v; }
294  if (!isConstrained) continue;
295  data.getLine(i,r,mdim);
296  if (isFeasible(r)&&(v<best)) { k=i; best=v; }
297  }
298 
299  if (!isConstrained)
300  {
301  data.swapLines(0,j);
302  return;
303  }
304 
305  if (k!=-1)
306  {
307  data.swapLines(0,k);
308  return;
309  }
310 
311  data.getLine(j,r,mdim);
312  addClosestFeasiblePointInData(r);
313 }
Definition: Vector.h:37
void swapLines(int i, int j)
Definition: Matrix.cpp:1155
void initTolLC(Vector vX)
#define i
ql0001_ & k(htemp+1),(cvec+1),(atemp+1),(bj+1),(bl+1),(bu+1),(x+1),(clamda+1), &iout, infoqp, &zero,(w+1), &lenw,(iw+1), &leniw, &glob_grd.epsmac
int nLine()
Definition: Matrix.h:83
int lineIndex(Vector r, int nn=0)
Definition: Matrix.cpp:1168
char isFeasible(Vector vx, double *d=NULL)
#define j
Vector getLine(int i, int n=0, int startCol=0)
Definition: Matrix.cpp:1094
#define INF
Definition: svm.cpp:43

◆ initTolLC()

void ObjectiveFunction::initTolLC ( Vector  vX)

Definition at line 106 of file ObjectiveFunction.cpp.

107 {
108  if (!isConstrained) return;
109  int i;
110  double *ofb=b;
111 
112  for (i=0; i<A.nLine(); i++)
113  maxNormLC=mmax(maxNormLC, condorAbs(ofb[i]-A.scalarProduct(i,vX)));
114 
115  tolLC=(1.0+maxNormLC)*tolRelFeasibilityForLC*(dim()*2+A.nLine());
116 }
int * mmax
double scalarProduct(int nl, Vector v)
Definition: Matrix.cpp:695
#define i
int nLine()
Definition: Matrix.h:83
double condorAbs(const double t1)
Definition: tools.h:47

◆ initTolNLC()

void ObjectiveFunction::initTolNLC ( Vector  c,
double  delta 
)

Definition at line 118 of file ObjectiveFunction.cpp.

119 {
120  int i;
121 
122  for (i=0; i<nNLConstraints; i++) maxNormNLC=mmax(maxNormNLC,condorAbs(c[i]));
123  if (delta<INF) maxNormNLC=mmax(maxNormNLC,delta*delta);
124 
125  tolNLC=(1.0+maxNormNLC)*tolRelFeasibilityForNLC*nNLConstraints;
126 }
int * mmax
#define i
double condorAbs(const double t1)
Definition: tools.h:47
#define INF
Definition: svm.cpp:43
double * delta

◆ isFeasible()

char ObjectiveFunction::isFeasible ( Vector  vx,
double *  d = NULL 
)

Definition at line 57 of file ObjectiveFunction.cpp.

58 {
59  if (vx==Vector::emptyVector) return 1;
60  if (!isConstrained) return 1;
61  int i, dim=vx.sz(), nerror;
62  char feasible=1;
63  double *bbl=bl, *bbu=bu, *x=vx, t;
64 
65  if (d) *d=0.0;
66  initTolLC(vx);
67 
68  for (i=0; i<dim; i++)
69  if ((t=bbl[i]-x[i])>tolLC)
70  { if (d) *d=mmax(*d,t); else return 0; feasible=0; }
71 
72  for (i=0; i<dim; i++)
73  if ((t=x[i]-bbu[i])>tolLC)
74  { if (d) *d=mmax(*d,t); else return 0; feasible=0; }
75 
76  for (i=0; i<A.nLine(); i++)
77  if ((t=b[i]-A.scalarProduct(i,vx))>tolLC)
78  { if (d) *d=mmax(*d,t); else return 0; feasible=0; }
79 
80  for (i=0; i<nNLConstraints; i++)
81  if ((t=-evalNLConstraint(i,vx,&nerror))>tolNLC )
82  { if (d) *d=mmax(*d,t); else return 0; feasible=0; }
83 
84 // printf("");
85  return feasible;
86 }
int * mmax
double scalarProduct(int nl, Vector v)
Definition: Matrix.cpp:695
void initTolLC(Vector vX)
doublereal * x
#define i
virtual double evalNLConstraint(int j, Vector v, int *nerror)=0
doublereal * d
unsigned sz()
Definition: Vector.h:79
int nLine()
Definition: Matrix.h:83
static Vector emptyVector
Definition: Vector.h:119

◆ printStats()

void ObjectiveFunction::printStats ( char  cc = 1)
virtual

Definition at line 166 of file ObjectiveFunction.cpp.

167 {
168  printf("\n\nProblem Name: %s\n",name);
169  printf("Dimension of the search space: %i\n",dim());
170  printf("best (lowest) value found: %e\n", valueBest+objectiveConst);
171  printf("Number of function Evaluation: %i (%i)\n",nfe,nfe2);
172  if (xOptimal.sz())
173  {
174  printf("Lnfty distance to the optimum: %e\n", xBest.LnftyDistance(xOptimal));
175  // printf("Euclidian distance to the optimum: %e\n", xBest.euclidianDistance(xOptimal));
176  }
177  int idim=xBest.sz(),j=0;
178  if (idim<20)
179  {
180  double *dd=xBest;
181  printf("Solution Vector is : \n[%e",dd[0]);
182  for (j=1; j<idim; j++) printf(", %e",dd[j]);
183  printf("]\n"); j=0;
184  }
185  if ((cc==0)||(!isConstrained)) return;
186  double *dbl=bl,*dbu=bu;
187  while (idim--)
188  {
189  if (*(dbl++)>-INF) j++;
190  if (*(dbu++)< INF) j++;
191  }
192  printf("number of box constraints:%i\n"
193  "number of linear constraints:%i\n"
194  "number of non-linear constraints:%i\n",j,A.nLine(),nNLConstraints);
195 
196 }
double LnftyDistance(Vector v)
Definition: Vector.cpp:256
unsigned sz()
Definition: Vector.h:79
int nLine()
Definition: Matrix.h:83
#define j
#define INF
Definition: svm.cpp:43

◆ saveStats()

void ObjectiveFunction::saveStats ( char *  filename,
Vector  vG,
Matrix  mH,
Vector  vLambda 
)

Definition at line 37 of file ObjectiveFunction.cpp.

38 {
39  FILE *ff=fopen(resultsFile,"w");
40  fprintf(ff,";dimension of search-space, total NFE, NFE before best point found, value OF at solution\n"
41  "%i\t%i\t(%i)\t%e\n"
42  ";Solution vector\n", dim(), nfe, nfe2, valueBest);
43  xBest.save(ff,2);
44  fprintf(ff,";Hessian matrix at the solution\n");
45  mH.save(ff,3);
46  fprintf(ff,";Gradient vector at the solution (should be zero if no active constraints)\n");
47  vG.save(ff,2);
48  if (isConstrained)
49  {
50  fprintf(ff,";Lagrangian Vector at the solution (lower,upper,linear,non-linear)\n");
51  vLambda.save(ff,2);
52  }
53  fprintf(ff,"\n");
54  fclose(ff);
55 }
void save(char *filename, char ascii)
Definition: Vector.cpp:441
void save(char *filename, char ascii)
Definition: Matrix.cpp:282
fprintf(glob_prnt.io, "\)

◆ saveValue()

void ObjectiveFunction::saveValue ( Vector  tmp,
double  valueOF,
int  nerror 
)
virtual

Reimplemented in CorrectScaleOF.

Definition at line 198 of file ObjectiveFunction.cpp.

199 {
200  int nl=data.nLine(), mdim=tmp.sz();
201  data.setNColumn(mdim+2);
202  data.append(tmp);
203  ((double**)data)[nl][mdim]=valueOF;
204  ((double**)data)[nl][mdim+1]=nerror;
205  if (saveFileName) data.updateSave(saveFileName);
206 }
void append(Vector tmp)
Definition: Matrix.cpp:1227
unsigned sz()
Definition: Vector.h:79
int nLine()
Definition: Matrix.h:83
void setNColumn(int _nColumn)
Definition: Matrix.cpp:199
void updateSave(char *saveFileName)
Definition: Matrix.cpp:349

◆ setName()

void ObjectiveFunction::setName ( char *  s)

Definition at line 154 of file ObjectiveFunction.cpp.

155 {
156  char *p=s+strlen(s)-1;
157  while ((*p!='.')&&(p!=s)) p--;
158  if (p==s) { strncpy(name,s, 8); name[8]=0; return; }
159  *p='\0';
160  while ((*p!='\\')&&(*p!='/')&&(p!=s)) p--;
161  if (p==s) { strncpy(name,s, 8); name[8]=0; return; }
162  p++;
163  strncpy(name,p, 8); name[8]=0;
164 }

◆ setSaveFile()

void ObjectiveFunction::setSaveFile ( char *  b = NULL)

Definition at line 142 of file ObjectiveFunction.cpp.

143 {
144  char buffer[300];
145  if (saveFileName) free(saveFileName);
146  if (s==NULL)
147  {
148  strcpy(buffer,name); strcat(buffer,".dat"); s=buffer;
149  }
150  saveFileName=(char*)malloc(strlen(s)+1);
151  strcpy(saveFileName,s);
152 }
HBITMAP buffer
Definition: svm-toy.cpp:37
free((char *) ob)

◆ updateCounter()

void ObjectiveFunction::updateCounter ( double  df,
Vector  vX,
int  nerror = 0 
)

Definition at line 128 of file ObjectiveFunction.cpp.

129 {
130  nfe++;
131  if ((dfold==INF)&&(nerror==0)) { dfref=(1+condorAbs(df))*1e-8; dfold=df; nfe2=nfe; return; }
132  if (dfold-df<dfref) return;
133  if (!isFeasible(vX)) return;
134 
135  if (nerror==0)
136  {
137  nfe2=nfe;
138  dfold=df;
139  }
140 }
char isFeasible(Vector vx, double *d=NULL)
double condorAbs(const double t1)
Definition: tools.h:47
#define INF
Definition: svm.cpp:43

Friends And Related Function Documentation

◆ CorrectScaleOF

friend class CorrectScaleOF
friend

Definition at line 39 of file ObjectiveFunction.h.

Member Data Documentation

◆ A

Matrix ObjectiveFunction::A

Definition at line 65 of file ObjectiveFunction.h.

◆ b

Vector ObjectiveFunction::b

Definition at line 66 of file ObjectiveFunction.h.

◆ bl

Vector ObjectiveFunction::bl

Definition at line 62 of file ObjectiveFunction.h.

◆ bu

Vector ObjectiveFunction::bu

Definition at line 62 of file ObjectiveFunction.h.

◆ data

Matrix ObjectiveFunction::data

Definition at line 53 of file ObjectiveFunction.h.

◆ isConstrained

int ObjectiveFunction::isConstrained

Definition at line 54 of file ObjectiveFunction.h.

◆ name

char ObjectiveFunction::name[9]

Definition at line 41 of file ObjectiveFunction.h.

◆ nfe

int ObjectiveFunction::nfe
protected

Definition at line 106 of file ObjectiveFunction.h.

◆ nfe2

int ObjectiveFunction::nfe2
protected

Definition at line 106 of file ObjectiveFunction.h.

◆ nNLConstraints

int ObjectiveFunction::nNLConstraints

Definition at line 54 of file ObjectiveFunction.h.

◆ noiseAbsolute

double ObjectiveFunction::noiseAbsolute

Definition at line 46 of file ObjectiveFunction.h.

◆ noiseRelative

double ObjectiveFunction::noiseRelative

Definition at line 46 of file ObjectiveFunction.h.

◆ objectiveConst

double ObjectiveFunction::objectiveConst

Definition at line 46 of file ObjectiveFunction.h.

◆ startPointIsGiven

char ObjectiveFunction::startPointIsGiven

Definition at line 41 of file ObjectiveFunction.h.

◆ t

int ObjectiveFunction::t

Definition at line 54 of file ObjectiveFunction.h.

◆ tolLC

double ObjectiveFunction::tolLC

Definition at line 75 of file ObjectiveFunction.h.

◆ tolNLC

double ObjectiveFunction::tolNLC

Definition at line 74 of file ObjectiveFunction.h.

◆ tolRelFeasibilityForLC

double ObjectiveFunction::tolRelFeasibilityForLC

Definition at line 75 of file ObjectiveFunction.h.

◆ tolRelFeasibilityForNLC

double ObjectiveFunction::tolRelFeasibilityForNLC

Definition at line 74 of file ObjectiveFunction.h.

◆ valueBest

double ObjectiveFunction::valueBest

Definition at line 46 of file ObjectiveFunction.h.

◆ valueOptimal

double ObjectiveFunction::valueOptimal

Definition at line 46 of file ObjectiveFunction.h.

◆ xBest

Vector ObjectiveFunction::xBest

Definition at line 42 of file ObjectiveFunction.h.

◆ xOptimal

Vector ObjectiveFunction::xOptimal

Definition at line 42 of file ObjectiveFunction.h.

◆ xStart

Vector ObjectiveFunction::xStart

Definition at line 42 of file ObjectiveFunction.h.


The documentation for this class was generated from the following files: