| 1 | 
#include "LinearCons.hpp"
 | 
| 2 | 
 | 
| 3 | 
LinearCons::LinearCons(vector<int>&  theIndex, vector<double>&  theCoeff, double b, BoundType bType)
 | 
| 4 | 
              :ConstraintBase(){
 | 
| 5 | 
              
 | 
| 6 | 
  bound = b;
 | 
| 7 | 
  boundType = bType;
 | 
| 8 | 
  if(bType == btEqu){
 | 
| 9 | 
    consType = linearEqu;
 | 
| 10 | 
  }
 | 
| 11 | 
  else{
 | 
| 12 | 
    consType = linearInequ;
 | 
| 13 | 
  }
 | 
| 14 | 
 | 
| 15 | 
  index = theIndex;
 | 
| 16 | 
  coeff = theCoeff;
 | 
| 17 | 
}
 | 
| 18 | 
 | 
| 19 | 
LinearCons::LinearCons(int dim, vector<int>&  theIndex, vector<double>&  theCoeff, double b, BoundType bType)
 | 
| 20 | 
              :ConstraintBase(dim) {
 | 
| 21 | 
 | 
| 22 | 
  if (dim != theCoeff.size()){
 | 
| 23 | 
    cout << "LinearCons Error: the dimension of index and coeff does not match" << endl;
 | 
| 24 | 
    exit(ERROR_CONSTRAINT);
 | 
| 25 | 
  }
 | 
| 26 | 
  
 | 
| 27 | 
  bound = b;
 | 
| 28 | 
  boundType = bType;
 | 
| 29 | 
  if(bType == btEqu){
 | 
| 30 | 
    consType = linearEqu;
 | 
| 31 | 
  }
 | 
| 32 | 
  else{
 | 
| 33 | 
    consType = linearInequ;
 | 
| 34 | 
  }
 | 
| 35 | 
 | 
| 36 | 
  index = theIndex;
 | 
| 37 | 
  coeff = theCoeff;
 | 
| 38 | 
}
 | 
| 39 | 
 | 
| 40 | 
double LinearCons::calcResidual(vector<double>& x){
 | 
| 41 | 
  double residue;
 | 
| 42 | 
  double valueOfLinearCons;
 | 
| 43 | 
 | 
| 44 | 
  valueOfLinearCons = 0;
 | 
| 45 | 
  
 | 
| 46 | 
  for (int i = 0; i < coeff.size(); i++)
 | 
| 47 | 
    valueOfLinearCons += coeff[i] * x[index[i]];
 | 
| 48 | 
  
 | 
| 49 | 
  residue = valueOfLinearCons - bound;
 | 
| 50 | 
 | 
| 51 | 
  if(boundType == btLower)
 | 
| 52 | 
    residue = -residue;
 | 
| 53 | 
  
 | 
| 54 | 
  return residue;  
 | 
| 55 | 
}
 | 
| 56 | 
 | 
| 57 | 
vector<double> LinearCons::calcConsGrad(vector<double>& x){
 | 
| 58 | 
  vector<double> consGrad(ndim, 0);
 | 
| 59 | 
  double sign;
 | 
| 60 | 
 | 
| 61 | 
  if(boundType == btLower)
 | 
| 62 | 
    sign = -1.0;
 | 
| 63 | 
  else
 | 
| 64 | 
    sign = 1.0;
 | 
| 65 | 
  
 | 
| 66 | 
  for(int i = 0; i < coeff.size(); i++)      
 | 
| 67 | 
    consGrad[index[i]] = coeff[i] * sign;
 | 
| 68 | 
  
 | 
| 69 | 
  return consGrad;
 | 
| 70 | 
}
 | 
| 71 | 
 | 
| 72 | 
SymMatrix LinearCons::calcConsHessian(vector<double>& x){
 | 
| 73 | 
    SymMatrix H(ndim);
 | 
| 74 | 
    H = 0;
 | 
| 75 | 
    return H;  
 | 
| 76 | 
}
 | 
| 77 | 
 |