Xmipp  v3.23.11-Nereus
ObjectiveFunction.h
Go to the documentation of this file.
1 /*
2 
3 CONDOR 1.06 - COnstrained, Non-linear, Direct, parallel Optimization
4  using trust Region method for high-computing load,
5  noisy functions
6 Copyright (C) 2004 Frank Vanden Berghen
7 
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation version 2
11 of the License.
12 
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17 
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
21 
22 If you want to include this tools in any commercial product,
23 you can contact the author at fvandenb@iridia.ulb.ac.be
24 
25 */
26 
27 #ifndef OBJECTIVEFUNCTION_INCLUDE
28 #define OBJECTIVEFUNCTION_INCLUDE
29 
30 #include <stdio.h>
31 #include "Vector.h"
32 #include "Matrix.h"
33 
34 #define INF 1.7E+308
35 
37 {
38  public:
39  friend class CorrectScaleOF;
40 
43  // xOptimal is the theoretical exact solution of the optimization problem
44  // if this value is set, it is used by printstat. It's an optional value to set.
45  // xBest is the solution given by the optimization algorithm.
47  // valueOptimal is the value of the obj.funct. at the theoretical exact solution of the optimization problem.
48  // valueBest is the value of the obj.funct. at the solution given by the optimization algorithm.
49  // objectiveConst is used inside method "printStats" to give a correction to the evaluation of the obj.funct.
50  // noiseAbsolute, noiseRelative are the absolute and relative noise on the evaluation of the obj.funct.
51  // These are optional values to set. If not given, the optimizer will assumea value
52  // of zero.
55  // nfe: total number of function evalution
56  // nfe2: number of function evalution needed to reach xBest without knowing it's the optimal solution
57  // t: type of the OF (used inside "getObjectiveFunction")
58  // nNLConstraints: number of non-linear constraints
59 
60  // CONSTRAINTS:
61  // for lower/upper bounds (box constraints)
63 
64  // for linear constraints Ax>=b
67 
68  // for non-linear constraints
69  virtual double evalNLConstraint(int j, Vector v, int *nerror)=0;
70  virtual Vector evalGradNLConstraint(int j, Vector v, int *nerror);
71  virtual void evalGradNLConstraint(int j, Vector v, Vector result, int *nerror)=0;
72 
73  // tolerances for constraints (relative and absolute)
76 
77  ObjectiveFunction() : startPointIsGiven(0), valueOptimal(INF), valueBest(INF), noiseAbsolute(0.0),
78  noiseRelative(0.0), objectiveConst(0.0), nNLConstraints(0),
79  isConstrained(1), tolRelFeasibilityForNLC(1e-9), tolNLC(1e-6),
80  tolRelFeasibilityForLC(1e-6), tolLC(1e-8),
81  saveFileName(NULL), dfold(INF), maxNormLC(0.0), maxNormNLC(0.0), nfe(0), nfe2(0) { };
82  virtual ~ObjectiveFunction(){ if (saveFileName) free(saveFileName); };
83  virtual double eval(Vector v, int *nerror)=0;
84  int dim();
85  void initData();
86  virtual void saveValue(Vector tmp,double valueOF, int nerror);
87  virtual void printStats(char cc=1);
88  void saveStats(char *filename, Vector vG, Matrix mH, Vector vLambda);
89  virtual void finalize(Vector vG, Matrix mH, Vector vLambda){};
90  void setName(char *s);
91  void setSaveFile(char *b=NULL);
92  void updateCounter(double df, Vector vX, int nerror=0);
93  char isFeasible(Vector vx, double *d=NULL);
94  void initBounds();
95  void endInit();
96  void initTolLC(Vector vX);
97  void initTolNLC(Vector c, double delta);
98  virtual int getNFE() { return nfe; }
99  virtual int getNFE2() { return nfe2; }
100 
101 private:
102  char *saveFileName;
103  double dfold, dfref, maxNormLC, maxNormNLC;
104  void addClosestFeasiblePointInData(Vector vX);
105 protected:
106  int nfe,nfe2;
107 };
108 
110 {
111  public:
114 
115  virtual double evalNLConstraint(int j, Vector v, int *nerror=NULL){ return 0; };
116  virtual Vector evalGradNLConstraint(int j, Vector v, int *nerror=NULL){ return Vector::emptyVector; };
117  virtual void evalGradNLConstraint(int j, Vector v, Vector result, int *nerror=NULL) { result=Vector::emptyVector; };
118 };
119 
120 
122 {
123  public:
125  CorrectScaleOF(int _t, ObjectiveFunction *_of, Vector _rescaling);
126  CorrectScaleOF(int _t, ObjectiveFunction *_of);
128  void saveValue(Vector X,double valueOF,int nerror);
129  double eval(Vector v, int *nerror=NULL);
130  virtual double evalNLConstraint(int j, Vector v, int *nerror=NULL);
131  virtual void evalGradNLConstraint(int j, Vector v, Vector result, int *nerror=NULL);
132  virtual void finalize(Vector vG, Matrix mH, Vector vLambda);
133  virtual int getNFE() { return of->nfe; }
134  virtual int getNFE2() { return of->nfe2; }
135  private:
136  void init();
137  ObjectiveFunction *of;
138  Vector xTemp;
139 };
140 
141 #endif
142 
virtual int getNFE()
virtual void saveValue(Vector tmp, double valueOF, int nerror)
virtual void finalize(Vector vG, Matrix mH, Vector vLambda)
void initTolNLC(Vector c, double delta)
doublereal * c
virtual int getNFE2()
Definition: Vector.h:37
friend class CorrectScaleOF
void initTolLC(Vector vX)
virtual double evalNLConstraint(int j, Vector v, int *nerror)=0
virtual Vector evalGradNLConstraint(int j, Vector v, int *nerror=NULL)
doublereal * d
virtual double eval(Vector v, int *nerror)=0
virtual void evalGradNLConstraint(int j, Vector v, Vector result, int *nerror=NULL)
void setSaveFile(char *b=NULL)
char isFeasible(Vector vx, double *d=NULL)
virtual int getNFE()
virtual double evalNLConstraint(int j, Vector v, int *nerror=NULL)
free((char *) ob)
virtual ~ObjectiveFunction()
Definition: Matrix.h:38
virtual int getNFE2()
#define j
void saveStats(char *filename, Vector vG, Matrix mH, Vector vLambda)
#define INF
static Vector emptyVector
Definition: Vector.h:119
virtual Vector evalGradNLConstraint(int j, Vector v, int *nerror)
virtual void printStats(char cc=1)
double * delta
void updateCounter(double df, Vector vX, int nerror=0)