FLOPC++
MP_constraint.cpp
Go to the documentation of this file.
00001 // ******************** FlopCpp **********************************************
00002 // File: MP_constraint.cpp
00003 // $Id$
00004 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
00005 // Copyright (C) 2003 Tim Helge Hultberg
00006 // All Rights Reserved.
00007 //****************************************************************************
00008 
00009 #include <iostream>
00010 #include <sstream>
00011 using std::cout;
00012 using std::endl;
00013 
00014 #include <OsiSolverInterface.hpp>
00015 #include "MP_constraint.hpp"
00016 #include "MP_expression.hpp"
00017 #include "MP_model.hpp"
00018 #include "MP_constant.hpp"
00019 #include "MP_data.hpp"
00020 
00021 using namespace flopc;
00022 
00023 
00024 void MP_constraint::operator=(const Constraint &v) {
00025   left = v->left;
00026   right = v->right;
00027   sense = v->sense;
00028 }
00029 
00030 int MP_constraint::row_number() const {
00031   int i1 = S1.check(I1->evaluate());
00032   int i2 = S2.check(I2->evaluate());
00033   int i3 = S3.check(I3->evaluate());
00034   int i4 = S4.check(I4->evaluate());
00035   int i5 = S5.check(I5->evaluate());
00036     
00037   if (i1==outOfBound || i2==outOfBound || i3==outOfBound ||
00038       i4==outOfBound || i5==outOfBound) {
00039     return outOfBound;
00040   } else {
00041     return offset + f(I1->evaluate(),I2->evaluate(),I3->evaluate(),
00042                       I4->evaluate(),I5->evaluate()); 
00043   }
00044 }
00045 
00046 double MP_constraint::price(int i1, int i2, int i3, int i4, int i5) const {
00047   return  M->Solver->getRowPrice()[offset + f(i1,i2,i3,i4,i5)];
00048 }
00049  
00050 MP_constraint::MP_constraint(
00051   const MP_set_base &s1, 
00052   const MP_set_base &s2, 
00053   const MP_set_base &s3,
00054   const MP_set_base &s4, 
00055   const MP_set_base &s5) :
00056   RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
00057   M(MP_model::current_model),
00058   offset(-1),
00059   S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
00060   I1(0),I2(0),I3(0),I4(0),I5(0)
00061 {
00062   MP_model::current_model->add(*this);
00063 }
00064 
00065 void MP_constraint::coefficients(vector<MP::Coef>& cfs) {
00066   MP::GenerateFunctor f(this, cfs);
00067 // f.setConstraint(this);
00068   
00069   vector<Constant> v;
00070   if (left.isDefined() && right.isDefined()) {
00071     left->generate((S1(I1)*S2(I2)*S3(I3)*S4(I4)*S5(I5)).such_that(B),v,f,1.0);
00072     right->generate((S1(I1)*S2(I2)*S3(I3)*S4(I4)*S5(I5)).such_that(B),v,f,-1.0);
00073   } else {
00074     cout<<"FlopCpp Warning: Constraint declared but not defined."<<endl;
00075   }
00076 }
00077 
00078 void MP_constraint::insertVariables(set<MP_variable*>& v) {
00079   if (left.operator->()!=0) {
00080     left->insertVariables(v);
00081   }
00082   if (right.operator->()!=0) {
00083     right->insertVariables(v);
00084   }
00085 }
00086 
00087 void MP_constraint::display(string s) const {
00088   cout<<s<<endl;
00089   if (offset >=0) {
00090     for (int i=offset; i<offset+size(); i++) {
00091       cout<<i<<"  "<<M->Solver->getRowLower()[i]<<"  "<<M->Solver->getRowActivity()[i]<<"  "<<M->Solver->getRowUpper()[i]<<"  "<<M->Solver->getRowPrice()[i]<<endl;
00092     }
00093   } else {
00094     cout<<"No solution available!"<<endl;
00095   }
00096 }