| 1 |
tim |
987 |
#ifndef _CONSTRAINT_H_
|
| 2 |
|
|
#define _CONSTRAINT_H_
|
| 3 |
|
|
|
| 4 |
|
|
#include <vector>
|
| 5 |
|
|
|
| 6 |
|
|
#include "NLModel.hpp"
|
| 7 |
|
|
|
| 8 |
|
|
#define ERROR_CONSTRAINT 10
|
| 9 |
|
|
|
| 10 |
|
|
using namespace std;
|
| 11 |
|
|
|
| 12 |
tim |
995 |
typedef enum ConsType {simpleBound = 1, linearEqu = 2, linearInequ = 4,
|
| 13 |
tim |
987 |
nonlinearEqu = 8, nonlinearInequ =16};
|
| 14 |
|
|
|
| 15 |
tim |
995 |
typedef enum BoundType{upper, lower, equ};
|
| 16 |
tim |
987 |
|
| 17 |
tim |
995 |
/**
|
| 18 |
|
|
* Abstract class of constraint for nonlinear optimization problem
|
| 19 |
|
|
*/
|
| 20 |
tim |
987 |
class ConstraintBase{
|
| 21 |
|
|
public:
|
| 22 |
|
|
ConstraintBase();
|
| 23 |
|
|
ConstraintBase(int dim);
|
| 24 |
|
|
|
| 25 |
|
|
virtual void setDim(int dim);
|
| 26 |
|
|
bool isDimSet();
|
| 27 |
|
|
|
| 28 |
|
|
int getConsType() { return consType};
|
| 29 |
|
|
virtual double calcResidual(vector<double>& x) = 0;
|
| 30 |
|
|
virtual vector<double> calcConsGrad(vector<double>& x) = 0;
|
| 31 |
|
|
virtual SymMatrix calcConsHessian(vector<double>& x) = 0;
|
| 32 |
|
|
|
| 33 |
|
|
protected:
|
| 34 |
|
|
|
| 35 |
|
|
bool init_ndim;
|
| 36 |
|
|
int ndim;
|
| 37 |
|
|
|
| 38 |
|
|
double bound;
|
| 39 |
|
|
BoundType boundType;
|
| 40 |
|
|
ConsType consType;
|
| 41 |
|
|
|
| 42 |
|
|
};
|
| 43 |
|
|
|
| 44 |
tim |
995 |
/**
|
| 45 |
|
|
* Simple Bound Constraint for nonlinear optimization problem
|
| 46 |
|
|
* boundType is used to identify whether it is upper bound or lower bound
|
| 47 |
|
|
*/
|
| 48 |
tim |
987 |
class SimpleBoundCons : public ConstraintBase{
|
| 49 |
|
|
public:
|
| 50 |
|
|
|
| 51 |
|
|
SimpleBoundCons(int theIndex, double b, bool flag);
|
| 52 |
|
|
SimpleBoundCons(int dim, int theIndex, double b, bool flag);
|
| 53 |
|
|
|
| 54 |
|
|
virtual double calcResidual(vector<double>& x);
|
| 55 |
|
|
virtual vector<double> calcConsGrad(vector<double>& x);
|
| 56 |
|
|
virtual SymMatrix calcConsHessian(vector<double>& x);
|
| 57 |
|
|
|
| 58 |
|
|
protected:
|
| 59 |
|
|
|
| 60 |
|
|
int index;
|
| 61 |
|
|
};
|
| 62 |
|
|
|
| 63 |
tim |
995 |
/**
|
| 64 |
|
|
* Linear Constraint for nonlinear optimization problem
|
| 65 |
|
|
* boundType is used to identify whether it is linear equation constraint or linear inequality
|
| 66 |
|
|
* constraint. If it is inear inequality constraint
|
| 67 |
|
|
*/
|
| 68 |
|
|
|
| 69 |
tim |
987 |
class LinearCons : public ConstraintBase{
|
| 70 |
|
|
|
| 71 |
|
|
public:
|
| 72 |
|
|
|
| 73 |
|
|
LinearCons(vector<int>& theIndex, vector<double>& , double b, BoundType bType);
|
| 74 |
|
|
LinearCons(int dim, vector<int>& theIndex, vector<double>& , double b, BoundType bType);
|
| 75 |
|
|
virtual double calcResidual(vector<double>& x);
|
| 76 |
|
|
virtual vector<double> calcConsGrad(vector<double>& x);
|
| 77 |
|
|
virtual SymMatrix calcConsHessian(vector<double>& x);
|
| 78 |
|
|
|
| 79 |
|
|
protected:
|
| 80 |
|
|
|
| 81 |
|
|
vector<int> index;
|
| 82 |
|
|
vector<double> coeff;
|
| 83 |
|
|
};
|
| 84 |
|
|
|
| 85 |
tim |
995 |
/**
|
| 86 |
|
|
* Linear Constraint for nonlinear optimization problem
|
| 87 |
|
|
* boundType is used to identify whether it is linear equality constraint or linear inequality
|
| 88 |
|
|
* constraint
|
| 89 |
|
|
*/
|
| 90 |
|
|
|
| 91 |
|
|
|
| 92 |
tim |
987 |
class NonlinearCons : public ConstraintBase{
|
| 93 |
|
|
|
| 94 |
|
|
public:
|
| 95 |
|
|
NonLinearCons(vector<int>& theIndex, NLModel* theModel , double b, BoundType bType);
|
| 96 |
|
|
NonLinearCons(int dim, vector<int>& theIndex, NLModel* theModel , double b, BoundType bType);
|
| 97 |
|
|
|
| 98 |
|
|
void setDim(int dim);
|
| 99 |
|
|
|
| 100 |
|
|
virtual double calcResidual(vector<double>& x);
|
| 101 |
|
|
virtual vector<double> calcConsGrad(vector<double>& x);
|
| 102 |
|
|
virtual SymMatrix calcConsHessian(vector<double>& x);
|
| 103 |
|
|
|
| 104 |
|
|
protected:
|
| 105 |
|
|
|
| 106 |
|
|
vector<int> index;
|
| 107 |
|
|
NLModel* model;
|
| 108 |
|
|
|
| 109 |
|
|
};
|
| 110 |
|
|
|
| 111 |
|
|
class ConstraintList{
|
| 112 |
|
|
public:
|
| 113 |
|
|
ConstraintList();
|
| 114 |
|
|
~ConstraintList();
|
| 115 |
|
|
|
| 116 |
|
|
addConstraint(ConstraintBase* cons);
|
| 117 |
|
|
int getNumOfCons() {return constraints.size();}
|
| 118 |
|
|
int getConsType() {return consType;}
|
| 119 |
|
|
vector<ConstraintBase*> getConstraints() {return constraints;}
|
| 120 |
|
|
|
| 121 |
|
|
protected:
|
| 122 |
|
|
|
| 123 |
|
|
vector<ConstraintBase*> constraints;
|
| 124 |
|
|
int consType;
|
| 125 |
|
|
};
|
| 126 |
|
|
|
| 127 |
|
|
#endif
|