1 // ******************** FlopCpp **********************************************
2 // File: MP_constraint.cpp
3 // $Id$
4 // Author: Tim Helge Hultberg (thh@mat.ua.pt)
5 // Copyright (C) 2003 Tim Helge Hultberg
6 // All Rights Reserved.
7 //****************************************************************************
8 
9 #include <iostream>
10 #include <sstream>
11 
12 #include "MP_constraint.hpp"
13 #include "MP_expression.hpp"
14 #include "MP_model.hpp"
15 #include "MP_constant.hpp"
16 #include "MP_data.hpp"
17 
18 using namespace flopc;
19 using namespace std;
20 
21 
operator =(const Constraint & v)22 void MP_constraint::operator=(const Constraint &v) {
23    left = v.left;
24    right = v.right;
25    sense = v.sense;
26 }
27 
row_number() const28 int MP_constraint::row_number() const {
29     int i1 = S1.check(I1->evaluate());
30     int i2 = S2.check(I2->evaluate());
31     int i3 = S3.check(I3->evaluate());
32     int i4 = S4.check(I4->evaluate());
33     int i5 = S5.check(I5->evaluate());
34 
35     if (i1==outOfBound || i2==outOfBound || i3==outOfBound ||
36 	i4==outOfBound || i5==outOfBound) {
37 	return outOfBound;
38     } else {
39 	return offset + f(I1->evaluate(),I2->evaluate(),I3->evaluate(),
40 			  I4->evaluate(),I5->evaluate());
41     }
42 }
43 
price(int i1,int i2,int i3,int i4,int i5) const44 double MP_constraint::price(int i1, int i2, int i3, int i4, int i5) const {
45     return  M->rowPrice[offset + f(i1,i2,i3,i4,i5)];
46 }
47 
MP_constraint(const MP_set_base & s1,const MP_set_base & s2,const MP_set_base & s3,const MP_set_base & s4,const MP_set_base & s5)48 MP_constraint::MP_constraint(
49     const MP_set_base &s1,
50     const MP_set_base &s2,
51     const MP_set_base &s3,
52     const MP_set_base &s4,
53     const MP_set_base &s5) :
54     RowMajor(s1.size(),s2.size(),s3.size(),s4.size(),s5.size()),
55 //     pprice(MP_data(d1->getSet(),d2->getSet(),d3->getSet(),
56 // 		   d4->getSet(),d5->getSet())),
57     M(MP_model::current_model),
58     offset(-1),
59     S1(s1),S2(s2),S3(s3),S4(s4),S5(s5),
60     I1(0),I2(0),I3(0),I4(0),I5(0)
61 {
62     MP_model::current_model->add(*this);
63 }
64 
coefficients(GenerateFunctor & f)65 void MP_constraint::coefficients(GenerateFunctor& f) {
66     f.setConstraint(this);
67 
68     vector<Constant> v;
69 
70         if (I1.operator->()!=0) {
71 	left->generate(S1(I1)*S2(I2)*S3(I3)*S4(I4)*S5(I5).such_that(B),v,f,1.0);
72  	right->generate(S1(I1)*S2(I2)*S3(I3)*S4(I4)*S5(I5).such_that(B),v,f,-1.0);
73 	 } else {
74 		cout<<"FlopCpp Warning: Constraint declared but not defined."<<endl;
75 	}
76 }
77 
insertVariables(set<MP_variable * > & v)78 void MP_constraint::insertVariables(set<MP_variable*>& v) {
79     if (left.operator->()!=0) {
80 	left->insertVariables(v);
81     }
82     if (right.operator->()!=0) {
83 	right->insertVariables(v);
84     }
85 }
86 
display(string s) const87 void MP_constraint::display(string s) const {
88     cout<<s<<endl;
89     if (offset >=0) {
90       for (int i=offset; i<offset+size(); i++) {
91 	cout<<i<<"  "<<M->bl[i]<<"  "<<M->rowActivity[i]<<"  "<<M->bu[i]<<"  "<<M->rowPrice[i]<<endl;
92       }
93     } else {
94       cout<<"No solution available!"<<endl;
95     }
96 }
97