xref: /haiku/src/libs/linprog/Variable.cpp (revision a381c8a06378de22ff08adf4282b4e3f7e50d250)
1 /*
2  * Copyright 2007-2008, Christof Lutteroth, lutteroth@cs.auckland.ac.nz
3  * Copyright 2007-2008, James Kim, jkim202@ec.auckland.ac.nz
4  * Distributed under the terms of the MIT License.
5  */
6 
7 #include "Variable.h"
8 #include "Constraint.h"
9 #include "LinearSpec.h"
10 #include "OperatorType.h"
11 
12 #include "lp_lib.h"
13 
14 #include <float.h>	// for DBL_MAX
15 
16 
17 /**
18  * Gets index of the variable.
19  *
20  * @return the index of the variable
21  */
22 int32
23 Variable::Index()
24 {
25 	int32 i = fLS->Variables()->IndexOf(this);
26 	if (i == -1)
27 		printf("Variable not part of fLS->Variables().");
28 	return i + 1;
29 }
30 
31 
32 /**
33  * Gets the current linear specification.
34  *
35  * @return the current linear specification
36  */
37 LinearSpec*
38 Variable::LS() const
39 {
40 	return fLS;
41 }
42 
43 
44 /**
45  * Sets the current linear specification.
46  *
47  * @param value	the current linear specification
48  */
49 void
50 Variable::SetLS(LinearSpec* value)
51 {
52 	fLS = value;
53 }
54 
55 
56 /**
57  * Gets the value.
58  *
59  * @return the value
60  */
61 double
62 Variable::Value() const
63 {
64 	return fValue;
65 }
66 
67 
68 /**
69  * Sets the value.
70  *
71  * @param value	the value
72  */
73 void
74 Variable::SetValue(double value)
75 {
76 	fValue = value;
77 }
78 
79 
80 /**
81  * Gets the minimum value of the variable.
82  *
83  * @return the minimum value of variable
84  */
85 double
86 Variable::Min() const
87 {
88 	return fMin;
89 }
90 
91 
92 /**
93  * Sets the minimum value of the variable.
94  *
95  * @param min	minimum value
96  */
97 void
98 Variable::SetMin(double min)
99 {
100 	fMin = min;
101 	set_bounds(fLS->LP(), this->Index(), fMin, fMax);
102 }
103 
104 
105 /**
106  * Gets the maximum value of the variable.
107  *
108  * @return the maximum value of variable
109  */
110 double
111 Variable::Max() const
112 {
113 	return fMax;
114 }
115 
116 
117 /**
118  * Sets the maximum value of the variable.
119  *
120  * @param max	maximum value
121  */
122 void
123 Variable::SetMax(double max)
124 {
125 	fMax = max;
126 	set_bounds(fLS->LP(), this->Index(), fMin, fMax);
127 }
128 
129 
130 /**
131  * Sets the minimum and maximum values of the variable.
132  *
133  * @param min	minimum value
134  * @param max	maximum value
135  */
136 void
137 Variable::SetRange(double min, double max)
138 {
139 	fMin = min;
140 	fMax = max;
141 	set_bounds(fLS->LP(), this->Index(), fMin, fMax);
142 }
143 
144 
145 /**
146  * Returns index of the variable as String.
147  * E.g. "Var2"
148  *
149  * @return the <code>String</code> index of the variable
150  */
151 //~ string Variable::ToString() {
152 	//~ return "Var" + Index();
153 //~ }
154 
155 
156 /**
157  * Adds a constraint that sets this variable equal to the given one.
158  *
159  * @param var	variable that should have the same value
160  * @return the new equality constraint
161  */
162 Constraint*
163 Variable::IsEqual(Variable* var)
164 {
165 	return fLS->AddConstraint(1.0, this, -1.0, var, OperatorType(EQ), 0.0);
166 }
167 
168 
169 /**
170  * Adds a constraint that sets this variable smaller or equal to the given one.
171  *
172  * @param var	variable that should have a larger or equal value
173  * @return the new constraint
174  */
175 Constraint*
176 Variable::IsSmallerOrEqual(Variable* var)
177 {
178 	return fLS->AddConstraint(1.0, this, -1.0, var, OperatorType(LE), 0.0);
179 }
180 
181 
182 /**
183  * Adds a constraint that sets this variable greater or equal to the given one.
184  *
185  * @param var	variable that should have a smaller or equal value
186  * @return the new constraint
187  */
188 Constraint*
189 Variable::IsGreaterorEqual(Variable* var)
190 {
191 	return fLS->AddConstraint(-1.0, var, 1.0, this, OperatorType(GE), 0.0);
192 }
193 
194 
195 /**
196  * Constructor.
197  */
198 Variable::Variable(LinearSpec* ls)
199 {
200 	fMin = 0;
201 	fMax = DBL_MAX;
202 	fValue = NULL;
203 	fLS = ls;
204 
205 	ls->Variables()->AddItem(this);
206 
207 	int32 size = ls->Variables()->CountItems();
208 	if (size > ls->Columns()) {
209 		double d = 0;
210 		int i = 0;
211 		if (!add_columnex(ls->LP(), 0, &d, &i))
212 			printf("Error in add_columnex.");
213 	}
214 }
215 
216 
217 /**
218  * Destructor.
219  * Removes the variable from its specification.
220  */
221 Variable::~Variable()
222 {
223 	del_column(fLS->LP(), this->Index());
224 	fLS->Variables()->RemoveItem(this);
225 }
226 
227