alpar@484
|
1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*-
|
alpar@484
|
2 |
*
|
alpar@484
|
3 |
* This file is a part of LEMON, a generic C++ optimization library.
|
alpar@484
|
4 |
*
|
alpar@956
|
5 |
* Copyright (C) 2003-2010
|
alpar@484
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
alpar@484
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
alpar@484
|
8 |
*
|
alpar@484
|
9 |
* Permission to use, modify and distribute this software is granted
|
alpar@484
|
10 |
* provided that this copyright notice appears in all copies. For
|
alpar@484
|
11 |
* precise terms see the accompanying LICENSE file.
|
alpar@484
|
12 |
*
|
alpar@484
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
alpar@484
|
14 |
* express or implied, and with no claim as to its suitability for any
|
alpar@484
|
15 |
* purpose.
|
alpar@484
|
16 |
*
|
alpar@484
|
17 |
*/
|
alpar@484
|
18 |
|
alpar@484
|
19 |
#include <iostream>
|
alpar@484
|
20 |
#include <vector>
|
alpar@484
|
21 |
#include <cstring>
|
alpar@484
|
22 |
|
alpar@484
|
23 |
#include <lemon/cplex.h>
|
alpar@484
|
24 |
|
alpar@484
|
25 |
extern "C" {
|
alpar@484
|
26 |
#include <ilcplex/cplex.h>
|
alpar@484
|
27 |
}
|
alpar@484
|
28 |
|
alpar@484
|
29 |
|
alpar@484
|
30 |
///\file
|
alpar@484
|
31 |
///\brief Implementation of the LEMON-CPLEX lp solver interface.
|
alpar@484
|
32 |
namespace lemon {
|
alpar@484
|
33 |
|
alpar@484
|
34 |
CplexEnv::LicenseError::LicenseError(int status) {
|
alpar@484
|
35 |
if (!CPXgeterrorstring(0, status, _message)) {
|
alpar@484
|
36 |
std::strcpy(_message, "Cplex unknown error");
|
alpar@484
|
37 |
}
|
alpar@484
|
38 |
}
|
alpar@484
|
39 |
|
alpar@484
|
40 |
CplexEnv::CplexEnv() {
|
alpar@484
|
41 |
int status;
|
alpar@484
|
42 |
_cnt = new int;
|
alpar@1181
|
43 |
(*_cnt) = 1;
|
alpar@484
|
44 |
_env = CPXopenCPLEX(&status);
|
alpar@484
|
45 |
if (_env == 0) {
|
alpar@484
|
46 |
delete _cnt;
|
alpar@484
|
47 |
_cnt = 0;
|
alpar@484
|
48 |
throw LicenseError(status);
|
alpar@484
|
49 |
}
|
alpar@484
|
50 |
}
|
alpar@484
|
51 |
|
alpar@484
|
52 |
CplexEnv::CplexEnv(const CplexEnv& other) {
|
alpar@484
|
53 |
_env = other._env;
|
alpar@484
|
54 |
_cnt = other._cnt;
|
alpar@484
|
55 |
++(*_cnt);
|
alpar@484
|
56 |
}
|
alpar@484
|
57 |
|
alpar@484
|
58 |
CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
|
alpar@484
|
59 |
_env = other._env;
|
alpar@484
|
60 |
_cnt = other._cnt;
|
alpar@484
|
61 |
++(*_cnt);
|
alpar@484
|
62 |
return *this;
|
alpar@484
|
63 |
}
|
alpar@484
|
64 |
|
alpar@484
|
65 |
CplexEnv::~CplexEnv() {
|
alpar@484
|
66 |
--(*_cnt);
|
alpar@484
|
67 |
if (*_cnt == 0) {
|
alpar@484
|
68 |
delete _cnt;
|
alpar@484
|
69 |
CPXcloseCPLEX(&_env);
|
alpar@484
|
70 |
}
|
alpar@484
|
71 |
}
|
alpar@484
|
72 |
|
alpar@484
|
73 |
CplexBase::CplexBase() : LpBase() {
|
alpar@484
|
74 |
int status;
|
alpar@484
|
75 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
|
deba@623
|
76 |
messageLevel(MESSAGE_NOTHING);
|
alpar@484
|
77 |
}
|
alpar@484
|
78 |
|
alpar@484
|
79 |
CplexBase::CplexBase(const CplexEnv& env)
|
alpar@484
|
80 |
: LpBase(), _env(env) {
|
alpar@484
|
81 |
int status;
|
alpar@484
|
82 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
|
deba@623
|
83 |
messageLevel(MESSAGE_NOTHING);
|
alpar@484
|
84 |
}
|
alpar@484
|
85 |
|
alpar@484
|
86 |
CplexBase::CplexBase(const CplexBase& cplex)
|
alpar@484
|
87 |
: LpBase() {
|
alpar@484
|
88 |
int status;
|
alpar@484
|
89 |
_prob = CPXcloneprob(cplexEnv(), cplex._prob, &status);
|
alpar@484
|
90 |
rows = cplex.rows;
|
alpar@484
|
91 |
cols = cplex.cols;
|
deba@623
|
92 |
messageLevel(MESSAGE_NOTHING);
|
alpar@484
|
93 |
}
|
alpar@484
|
94 |
|
alpar@484
|
95 |
CplexBase::~CplexBase() {
|
alpar@484
|
96 |
CPXfreeprob(cplexEnv(),&_prob);
|
alpar@484
|
97 |
}
|
alpar@484
|
98 |
|
alpar@484
|
99 |
int CplexBase::_addCol() {
|
alpar@484
|
100 |
int i = CPXgetnumcols(cplexEnv(), _prob);
|
alpar@484
|
101 |
double lb = -INF, ub = INF;
|
alpar@484
|
102 |
CPXnewcols(cplexEnv(), _prob, 1, 0, &lb, &ub, 0, 0);
|
alpar@484
|
103 |
return i;
|
alpar@484
|
104 |
}
|
alpar@484
|
105 |
|
alpar@484
|
106 |
|
alpar@484
|
107 |
int CplexBase::_addRow() {
|
alpar@484
|
108 |
int i = CPXgetnumrows(cplexEnv(), _prob);
|
alpar@484
|
109 |
const double ub = INF;
|
alpar@484
|
110 |
const char s = 'L';
|
alpar@484
|
111 |
CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
|
alpar@484
|
112 |
return i;
|
alpar@484
|
113 |
}
|
alpar@484
|
114 |
|
alpar@956
|
115 |
int CplexBase::_addRow(Value lb, ExprIterator b,
|
deba@793
|
116 |
ExprIterator e, Value ub) {
|
deba@793
|
117 |
int i = CPXgetnumrows(cplexEnv(), _prob);
|
deba@793
|
118 |
if (lb == -INF) {
|
deba@793
|
119 |
const char s = 'L';
|
deba@793
|
120 |
CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
|
deba@793
|
121 |
} else if (ub == INF) {
|
deba@793
|
122 |
const char s = 'G';
|
deba@793
|
123 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
|
deba@793
|
124 |
} else if (lb == ub){
|
deba@793
|
125 |
const char s = 'E';
|
deba@793
|
126 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, 0, 0);
|
deba@793
|
127 |
} else {
|
deba@793
|
128 |
const char s = 'R';
|
deba@793
|
129 |
double len = ub - lb;
|
deba@793
|
130 |
CPXnewrows(cplexEnv(), _prob, 1, &lb, &s, &len, 0);
|
deba@793
|
131 |
}
|
deba@793
|
132 |
|
deba@793
|
133 |
std::vector<int> indices;
|
deba@793
|
134 |
std::vector<int> rowlist;
|
deba@793
|
135 |
std::vector<Value> values;
|
deba@793
|
136 |
|
deba@793
|
137 |
for(ExprIterator it=b; it!=e; ++it) {
|
deba@793
|
138 |
indices.push_back(it->first);
|
deba@793
|
139 |
values.push_back(it->second);
|
deba@793
|
140 |
rowlist.push_back(i);
|
deba@793
|
141 |
}
|
deba@793
|
142 |
|
deba@793
|
143 |
CPXchgcoeflist(cplexEnv(), _prob, values.size(),
|
deba@793
|
144 |
&rowlist.front(), &indices.front(), &values.front());
|
deba@793
|
145 |
|
deba@793
|
146 |
return i;
|
deba@793
|
147 |
}
|
alpar@484
|
148 |
|
alpar@484
|
149 |
void CplexBase::_eraseCol(int i) {
|
alpar@484
|
150 |
CPXdelcols(cplexEnv(), _prob, i, i);
|
alpar@484
|
151 |
}
|
alpar@484
|
152 |
|
alpar@484
|
153 |
void CplexBase::_eraseRow(int i) {
|
alpar@484
|
154 |
CPXdelrows(cplexEnv(), _prob, i, i);
|
alpar@484
|
155 |
}
|
alpar@484
|
156 |
|
alpar@484
|
157 |
void CplexBase::_eraseColId(int i) {
|
alpar@484
|
158 |
cols.eraseIndex(i);
|
alpar@484
|
159 |
cols.shiftIndices(i);
|
alpar@484
|
160 |
}
|
alpar@484
|
161 |
void CplexBase::_eraseRowId(int i) {
|
alpar@484
|
162 |
rows.eraseIndex(i);
|
alpar@484
|
163 |
rows.shiftIndices(i);
|
alpar@484
|
164 |
}
|
alpar@484
|
165 |
|
alpar@484
|
166 |
void CplexBase::_getColName(int col, std::string &name) const {
|
alpar@484
|
167 |
int size;
|
alpar@484
|
168 |
CPXgetcolname(cplexEnv(), _prob, 0, 0, 0, &size, col, col);
|
alpar@484
|
169 |
if (size == 0) {
|
alpar@484
|
170 |
name.clear();
|
alpar@484
|
171 |
return;
|
alpar@484
|
172 |
}
|
alpar@484
|
173 |
|
alpar@484
|
174 |
size *= -1;
|
alpar@484
|
175 |
std::vector<char> buf(size);
|
alpar@484
|
176 |
char *cname;
|
alpar@484
|
177 |
int tmp;
|
alpar@484
|
178 |
CPXgetcolname(cplexEnv(), _prob, &cname, &buf.front(), size,
|
alpar@484
|
179 |
&tmp, col, col);
|
alpar@484
|
180 |
name = cname;
|
alpar@484
|
181 |
}
|
alpar@484
|
182 |
|
alpar@484
|
183 |
void CplexBase::_setColName(int col, const std::string &name) {
|
alpar@484
|
184 |
char *cname;
|
alpar@484
|
185 |
cname = const_cast<char*>(name.c_str());
|
alpar@484
|
186 |
CPXchgcolname(cplexEnv(), _prob, 1, &col, &cname);
|
alpar@484
|
187 |
}
|
alpar@484
|
188 |
|
alpar@484
|
189 |
int CplexBase::_colByName(const std::string& name) const {
|
alpar@484
|
190 |
int index;
|
alpar@484
|
191 |
if (CPXgetcolindex(cplexEnv(), _prob,
|
alpar@484
|
192 |
const_cast<char*>(name.c_str()), &index) == 0) {
|
alpar@484
|
193 |
return index;
|
alpar@484
|
194 |
}
|
alpar@484
|
195 |
return -1;
|
alpar@484
|
196 |
}
|
alpar@484
|
197 |
|
alpar@484
|
198 |
void CplexBase::_getRowName(int row, std::string &name) const {
|
alpar@484
|
199 |
int size;
|
alpar@484
|
200 |
CPXgetrowname(cplexEnv(), _prob, 0, 0, 0, &size, row, row);
|
alpar@484
|
201 |
if (size == 0) {
|
alpar@484
|
202 |
name.clear();
|
alpar@484
|
203 |
return;
|
alpar@484
|
204 |
}
|
alpar@484
|
205 |
|
alpar@484
|
206 |
size *= -1;
|
alpar@484
|
207 |
std::vector<char> buf(size);
|
alpar@484
|
208 |
char *cname;
|
alpar@484
|
209 |
int tmp;
|
alpar@484
|
210 |
CPXgetrowname(cplexEnv(), _prob, &cname, &buf.front(), size,
|
alpar@484
|
211 |
&tmp, row, row);
|
alpar@484
|
212 |
name = cname;
|
alpar@484
|
213 |
}
|
alpar@484
|
214 |
|
alpar@484
|
215 |
void CplexBase::_setRowName(int row, const std::string &name) {
|
alpar@484
|
216 |
char *cname;
|
alpar@484
|
217 |
cname = const_cast<char*>(name.c_str());
|
alpar@484
|
218 |
CPXchgrowname(cplexEnv(), _prob, 1, &row, &cname);
|
alpar@484
|
219 |
}
|
alpar@484
|
220 |
|
alpar@484
|
221 |
int CplexBase::_rowByName(const std::string& name) const {
|
alpar@484
|
222 |
int index;
|
alpar@484
|
223 |
if (CPXgetrowindex(cplexEnv(), _prob,
|
alpar@484
|
224 |
const_cast<char*>(name.c_str()), &index) == 0) {
|
alpar@484
|
225 |
return index;
|
alpar@484
|
226 |
}
|
alpar@484
|
227 |
return -1;
|
alpar@484
|
228 |
}
|
alpar@484
|
229 |
|
alpar@484
|
230 |
void CplexBase::_setRowCoeffs(int i, ExprIterator b,
|
alpar@484
|
231 |
ExprIterator e)
|
alpar@484
|
232 |
{
|
alpar@484
|
233 |
std::vector<int> indices;
|
alpar@484
|
234 |
std::vector<int> rowlist;
|
alpar@484
|
235 |
std::vector<Value> values;
|
alpar@484
|
236 |
|
alpar@484
|
237 |
for(ExprIterator it=b; it!=e; ++it) {
|
alpar@484
|
238 |
indices.push_back(it->first);
|
alpar@484
|
239 |
values.push_back(it->second);
|
alpar@484
|
240 |
rowlist.push_back(i);
|
alpar@484
|
241 |
}
|
alpar@484
|
242 |
|
alpar@484
|
243 |
CPXchgcoeflist(cplexEnv(), _prob, values.size(),
|
alpar@484
|
244 |
&rowlist.front(), &indices.front(), &values.front());
|
alpar@484
|
245 |
}
|
alpar@484
|
246 |
|
alpar@484
|
247 |
void CplexBase::_getRowCoeffs(int i, InsertIterator b) const {
|
alpar@484
|
248 |
int tmp1, tmp2, tmp3, length;
|
alpar@484
|
249 |
CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
|
alpar@484
|
250 |
|
alpar@484
|
251 |
length = -length;
|
alpar@484
|
252 |
std::vector<int> indices(length);
|
alpar@484
|
253 |
std::vector<double> values(length);
|
alpar@484
|
254 |
|
alpar@484
|
255 |
CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2,
|
alpar@484
|
256 |
&indices.front(), &values.front(),
|
alpar@484
|
257 |
length, &tmp3, i, i);
|
alpar@484
|
258 |
|
alpar@484
|
259 |
for (int i = 0; i < length; ++i) {
|
alpar@484
|
260 |
*b = std::make_pair(indices[i], values[i]);
|
alpar@484
|
261 |
++b;
|
alpar@484
|
262 |
}
|
alpar@484
|
263 |
}
|
alpar@484
|
264 |
|
alpar@484
|
265 |
void CplexBase::_setColCoeffs(int i, ExprIterator b, ExprIterator e) {
|
alpar@484
|
266 |
std::vector<int> indices;
|
alpar@484
|
267 |
std::vector<int> collist;
|
alpar@484
|
268 |
std::vector<Value> values;
|
alpar@484
|
269 |
|
alpar@484
|
270 |
for(ExprIterator it=b; it!=e; ++it) {
|
alpar@484
|
271 |
indices.push_back(it->first);
|
alpar@484
|
272 |
values.push_back(it->second);
|
alpar@484
|
273 |
collist.push_back(i);
|
alpar@484
|
274 |
}
|
alpar@484
|
275 |
|
alpar@484
|
276 |
CPXchgcoeflist(cplexEnv(), _prob, values.size(),
|
alpar@484
|
277 |
&indices.front(), &collist.front(), &values.front());
|
alpar@484
|
278 |
}
|
alpar@484
|
279 |
|
alpar@484
|
280 |
void CplexBase::_getColCoeffs(int i, InsertIterator b) const {
|
alpar@484
|
281 |
|
alpar@484
|
282 |
int tmp1, tmp2, tmp3, length;
|
alpar@484
|
283 |
CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
|
alpar@484
|
284 |
|
alpar@484
|
285 |
length = -length;
|
alpar@484
|
286 |
std::vector<int> indices(length);
|
alpar@484
|
287 |
std::vector<double> values(length);
|
alpar@484
|
288 |
|
alpar@484
|
289 |
CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2,
|
alpar@484
|
290 |
&indices.front(), &values.front(),
|
alpar@484
|
291 |
length, &tmp3, i, i);
|
alpar@484
|
292 |
|
alpar@484
|
293 |
for (int i = 0; i < length; ++i) {
|
alpar@484
|
294 |
*b = std::make_pair(indices[i], values[i]);
|
alpar@484
|
295 |
++b;
|
alpar@484
|
296 |
}
|
alpar@484
|
297 |
|
alpar@484
|
298 |
}
|
alpar@484
|
299 |
|
alpar@484
|
300 |
void CplexBase::_setCoeff(int row, int col, Value value) {
|
alpar@484
|
301 |
CPXchgcoef(cplexEnv(), _prob, row, col, value);
|
alpar@484
|
302 |
}
|
alpar@484
|
303 |
|
alpar@484
|
304 |
CplexBase::Value CplexBase::_getCoeff(int row, int col) const {
|
alpar@484
|
305 |
CplexBase::Value value;
|
alpar@484
|
306 |
CPXgetcoef(cplexEnv(), _prob, row, col, &value);
|
alpar@484
|
307 |
return value;
|
alpar@484
|
308 |
}
|
alpar@484
|
309 |
|
alpar@484
|
310 |
void CplexBase::_setColLowerBound(int i, Value value) {
|
alpar@484
|
311 |
const char s = 'L';
|
alpar@484
|
312 |
CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
|
alpar@484
|
313 |
}
|
alpar@484
|
314 |
|
alpar@484
|
315 |
CplexBase::Value CplexBase::_getColLowerBound(int i) const {
|
alpar@484
|
316 |
CplexBase::Value res;
|
alpar@484
|
317 |
CPXgetlb(cplexEnv(), _prob, &res, i, i);
|
alpar@484
|
318 |
return res <= -CPX_INFBOUND ? -INF : res;
|
alpar@484
|
319 |
}
|
alpar@484
|
320 |
|
alpar@484
|
321 |
void CplexBase::_setColUpperBound(int i, Value value)
|
alpar@484
|
322 |
{
|
alpar@484
|
323 |
const char s = 'U';
|
alpar@484
|
324 |
CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
|
alpar@484
|
325 |
}
|
alpar@484
|
326 |
|
alpar@484
|
327 |
CplexBase::Value CplexBase::_getColUpperBound(int i) const {
|
alpar@484
|
328 |
CplexBase::Value res;
|
alpar@484
|
329 |
CPXgetub(cplexEnv(), _prob, &res, i, i);
|
alpar@484
|
330 |
return res >= CPX_INFBOUND ? INF : res;
|
alpar@484
|
331 |
}
|
alpar@484
|
332 |
|
alpar@484
|
333 |
CplexBase::Value CplexBase::_getRowLowerBound(int i) const {
|
alpar@484
|
334 |
char s;
|
alpar@484
|
335 |
CPXgetsense(cplexEnv(), _prob, &s, i, i);
|
alpar@484
|
336 |
CplexBase::Value res;
|
alpar@484
|
337 |
|
alpar@484
|
338 |
switch (s) {
|
alpar@484
|
339 |
case 'G':
|
alpar@484
|
340 |
case 'R':
|
alpar@484
|
341 |
case 'E':
|
alpar@484
|
342 |
CPXgetrhs(cplexEnv(), _prob, &res, i, i);
|
alpar@484
|
343 |
return res <= -CPX_INFBOUND ? -INF : res;
|
alpar@484
|
344 |
default:
|
alpar@484
|
345 |
return -INF;
|
alpar@484
|
346 |
}
|
alpar@484
|
347 |
}
|
alpar@484
|
348 |
|
alpar@484
|
349 |
CplexBase::Value CplexBase::_getRowUpperBound(int i) const {
|
alpar@484
|
350 |
char s;
|
alpar@484
|
351 |
CPXgetsense(cplexEnv(), _prob, &s, i, i);
|
alpar@484
|
352 |
CplexBase::Value res;
|
alpar@484
|
353 |
|
alpar@484
|
354 |
switch (s) {
|
alpar@484
|
355 |
case 'L':
|
alpar@484
|
356 |
case 'E':
|
alpar@484
|
357 |
CPXgetrhs(cplexEnv(), _prob, &res, i, i);
|
alpar@484
|
358 |
return res >= CPX_INFBOUND ? INF : res;
|
alpar@484
|
359 |
case 'R':
|
alpar@484
|
360 |
CPXgetrhs(cplexEnv(), _prob, &res, i, i);
|
alpar@484
|
361 |
{
|
alpar@484
|
362 |
double rng;
|
alpar@484
|
363 |
CPXgetrngval(cplexEnv(), _prob, &rng, i, i);
|
alpar@484
|
364 |
res += rng;
|
alpar@484
|
365 |
}
|
alpar@484
|
366 |
return res >= CPX_INFBOUND ? INF : res;
|
alpar@484
|
367 |
default:
|
alpar@484
|
368 |
return INF;
|
alpar@484
|
369 |
}
|
alpar@484
|
370 |
}
|
alpar@484
|
371 |
|
alpar@484
|
372 |
//This is easier to implement
|
alpar@484
|
373 |
void CplexBase::_set_row_bounds(int i, Value lb, Value ub) {
|
alpar@484
|
374 |
if (lb == -INF) {
|
alpar@484
|
375 |
const char s = 'L';
|
alpar@484
|
376 |
CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
|
alpar@484
|
377 |
CPXchgrhs(cplexEnv(), _prob, 1, &i, &ub);
|
alpar@484
|
378 |
} else if (ub == INF) {
|
alpar@484
|
379 |
const char s = 'G';
|
alpar@484
|
380 |
CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
|
alpar@484
|
381 |
CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
|
alpar@484
|
382 |
} else if (lb == ub){
|
alpar@484
|
383 |
const char s = 'E';
|
alpar@484
|
384 |
CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
|
alpar@484
|
385 |
CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
|
alpar@484
|
386 |
} else {
|
alpar@484
|
387 |
const char s = 'R';
|
alpar@484
|
388 |
CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
|
alpar@484
|
389 |
CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
|
alpar@484
|
390 |
double len = ub - lb;
|
alpar@484
|
391 |
CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
|
alpar@484
|
392 |
}
|
alpar@484
|
393 |
}
|
alpar@484
|
394 |
|
alpar@484
|
395 |
void CplexBase::_setRowLowerBound(int i, Value lb)
|
alpar@484
|
396 |
{
|
alpar@484
|
397 |
LEMON_ASSERT(lb != INF, "Invalid bound");
|
alpar@484
|
398 |
_set_row_bounds(i, lb, CplexBase::_getRowUpperBound(i));
|
alpar@484
|
399 |
}
|
alpar@484
|
400 |
|
alpar@484
|
401 |
void CplexBase::_setRowUpperBound(int i, Value ub)
|
alpar@484
|
402 |
{
|
alpar@484
|
403 |
|
alpar@484
|
404 |
LEMON_ASSERT(ub != -INF, "Invalid bound");
|
alpar@484
|
405 |
_set_row_bounds(i, CplexBase::_getRowLowerBound(i), ub);
|
alpar@484
|
406 |
}
|
alpar@484
|
407 |
|
alpar@484
|
408 |
void CplexBase::_setObjCoeffs(ExprIterator b, ExprIterator e)
|
alpar@484
|
409 |
{
|
alpar@484
|
410 |
std::vector<int> indices;
|
alpar@484
|
411 |
std::vector<Value> values;
|
alpar@484
|
412 |
for(ExprIterator it=b; it!=e; ++it) {
|
alpar@484
|
413 |
indices.push_back(it->first);
|
alpar@484
|
414 |
values.push_back(it->second);
|
alpar@484
|
415 |
}
|
alpar@484
|
416 |
CPXchgobj(cplexEnv(), _prob, values.size(),
|
alpar@484
|
417 |
&indices.front(), &values.front());
|
alpar@484
|
418 |
|
alpar@484
|
419 |
}
|
alpar@484
|
420 |
|
alpar@484
|
421 |
void CplexBase::_getObjCoeffs(InsertIterator b) const
|
alpar@484
|
422 |
{
|
alpar@484
|
423 |
int num = CPXgetnumcols(cplexEnv(), _prob);
|
alpar@484
|
424 |
std::vector<Value> x(num);
|
alpar@484
|
425 |
|
alpar@484
|
426 |
CPXgetobj(cplexEnv(), _prob, &x.front(), 0, num - 1);
|
alpar@484
|
427 |
for (int i = 0; i < num; ++i) {
|
alpar@484
|
428 |
if (x[i] != 0.0) {
|
alpar@484
|
429 |
*b = std::make_pair(i, x[i]);
|
alpar@484
|
430 |
++b;
|
alpar@484
|
431 |
}
|
alpar@484
|
432 |
}
|
alpar@484
|
433 |
}
|
alpar@484
|
434 |
|
alpar@484
|
435 |
void CplexBase::_setObjCoeff(int i, Value obj_coef)
|
alpar@484
|
436 |
{
|
alpar@484
|
437 |
CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef);
|
alpar@484
|
438 |
}
|
alpar@484
|
439 |
|
alpar@484
|
440 |
CplexBase::Value CplexBase::_getObjCoeff(int i) const
|
alpar@484
|
441 |
{
|
alpar@484
|
442 |
Value x;
|
alpar@484
|
443 |
CPXgetobj(cplexEnv(), _prob, &x, i, i);
|
alpar@484
|
444 |
return x;
|
alpar@484
|
445 |
}
|
alpar@484
|
446 |
|
alpar@484
|
447 |
void CplexBase::_setSense(CplexBase::Sense sense) {
|
alpar@484
|
448 |
switch (sense) {
|
alpar@484
|
449 |
case MIN:
|
alpar@484
|
450 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MIN);
|
alpar@484
|
451 |
break;
|
alpar@484
|
452 |
case MAX:
|
alpar@484
|
453 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MAX);
|
alpar@484
|
454 |
break;
|
alpar@484
|
455 |
}
|
alpar@484
|
456 |
}
|
alpar@484
|
457 |
|
alpar@484
|
458 |
CplexBase::Sense CplexBase::_getSense() const {
|
alpar@484
|
459 |
switch (CPXgetobjsen(cplexEnv(), _prob)) {
|
alpar@484
|
460 |
case CPX_MIN:
|
alpar@484
|
461 |
return MIN;
|
alpar@484
|
462 |
case CPX_MAX:
|
alpar@484
|
463 |
return MAX;
|
alpar@484
|
464 |
default:
|
alpar@484
|
465 |
LEMON_ASSERT(false, "Invalid sense");
|
alpar@484
|
466 |
return CplexBase::Sense();
|
alpar@484
|
467 |
}
|
alpar@484
|
468 |
}
|
alpar@484
|
469 |
|
alpar@484
|
470 |
void CplexBase::_clear() {
|
alpar@484
|
471 |
CPXfreeprob(cplexEnv(),&_prob);
|
alpar@484
|
472 |
int status;
|
alpar@484
|
473 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
|
alpar@484
|
474 |
}
|
alpar@484
|
475 |
|
deba@623
|
476 |
void CplexBase::_messageLevel(MessageLevel level) {
|
deba@623
|
477 |
switch (level) {
|
deba@623
|
478 |
case MESSAGE_NOTHING:
|
deba@623
|
479 |
_message_enabled = false;
|
deba@623
|
480 |
break;
|
deba@623
|
481 |
case MESSAGE_ERROR:
|
deba@623
|
482 |
case MESSAGE_WARNING:
|
deba@623
|
483 |
case MESSAGE_NORMAL:
|
deba@623
|
484 |
case MESSAGE_VERBOSE:
|
deba@623
|
485 |
_message_enabled = true;
|
deba@623
|
486 |
break;
|
deba@623
|
487 |
}
|
deba@623
|
488 |
}
|
deba@623
|
489 |
|
deba@623
|
490 |
void CplexBase::_applyMessageLevel() {
|
alpar@956
|
491 |
CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND,
|
deba@623
|
492 |
_message_enabled ? CPX_ON : CPX_OFF);
|
deba@623
|
493 |
}
|
deba@623
|
494 |
|
alpar@485
|
495 |
// CplexLp members
|
alpar@484
|
496 |
|
alpar@485
|
497 |
CplexLp::CplexLp()
|
deba@598
|
498 |
: LpBase(), LpSolver(), CplexBase() {}
|
alpar@484
|
499 |
|
alpar@485
|
500 |
CplexLp::CplexLp(const CplexEnv& env)
|
deba@598
|
501 |
: LpBase(), LpSolver(), CplexBase(env) {}
|
alpar@484
|
502 |
|
alpar@485
|
503 |
CplexLp::CplexLp(const CplexLp& other)
|
deba@598
|
504 |
: LpBase(), LpSolver(), CplexBase(other) {}
|
alpar@484
|
505 |
|
alpar@485
|
506 |
CplexLp::~CplexLp() {}
|
alpar@484
|
507 |
|
alpar@587
|
508 |
CplexLp* CplexLp::newSolver() const { return new CplexLp; }
|
alpar@587
|
509 |
CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }
|
alpar@484
|
510 |
|
alpar@485
|
511 |
const char* CplexLp::_solverName() const { return "CplexLp"; }
|
alpar@484
|
512 |
|
alpar@485
|
513 |
void CplexLp::_clear_temporals() {
|
alpar@484
|
514 |
_col_status.clear();
|
alpar@484
|
515 |
_row_status.clear();
|
alpar@484
|
516 |
_primal_ray.clear();
|
alpar@484
|
517 |
_dual_ray.clear();
|
alpar@484
|
518 |
}
|
alpar@484
|
519 |
|
alpar@484
|
520 |
// The routine returns zero unless an error occurred during the
|
alpar@484
|
521 |
// optimization. Examples of errors include exhausting available
|
alpar@484
|
522 |
// memory (CPXERR_NO_MEMORY) or encountering invalid data in the
|
alpar@484
|
523 |
// CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a
|
alpar@484
|
524 |
// user-specified CPLEX limit, or proving the model infeasible or
|
alpar@484
|
525 |
// unbounded, are not considered errors. Note that a zero return
|
alpar@484
|
526 |
// value does not necessarily mean that a solution exists. Use query
|
alpar@484
|
527 |
// routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
|
alpar@484
|
528 |
// further information about the status of the optimization.
|
alpar@485
|
529 |
CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
|
alpar@484
|
530 |
#if CPX_VERSION >= 800
|
alpar@484
|
531 |
if (status == 0) {
|
alpar@484
|
532 |
switch (CPXgetstat(cplexEnv(), _prob)) {
|
alpar@484
|
533 |
case CPX_STAT_OPTIMAL:
|
alpar@484
|
534 |
case CPX_STAT_INFEASIBLE:
|
alpar@484
|
535 |
case CPX_STAT_UNBOUNDED:
|
alpar@484
|
536 |
return SOLVED;
|
alpar@484
|
537 |
default:
|
alpar@484
|
538 |
return UNSOLVED;
|
alpar@484
|
539 |
}
|
alpar@484
|
540 |
} else {
|
alpar@484
|
541 |
return UNSOLVED;
|
alpar@484
|
542 |
}
|
alpar@484
|
543 |
#else
|
alpar@484
|
544 |
if (status == 0) {
|
alpar@484
|
545 |
//We want to exclude some cases
|
alpar@484
|
546 |
switch (CPXgetstat(cplexEnv(), _prob)) {
|
alpar@484
|
547 |
case CPX_OBJ_LIM:
|
alpar@484
|
548 |
case CPX_IT_LIM_FEAS:
|
alpar@484
|
549 |
case CPX_IT_LIM_INFEAS:
|
alpar@484
|
550 |
case CPX_TIME_LIM_FEAS:
|
alpar@484
|
551 |
case CPX_TIME_LIM_INFEAS:
|
alpar@484
|
552 |
return UNSOLVED;
|
alpar@484
|
553 |
default:
|
alpar@484
|
554 |
return SOLVED;
|
alpar@484
|
555 |
}
|
alpar@484
|
556 |
} else {
|
alpar@484
|
557 |
return UNSOLVED;
|
alpar@484
|
558 |
}
|
alpar@484
|
559 |
#endif
|
alpar@484
|
560 |
}
|
alpar@484
|
561 |
|
alpar@485
|
562 |
CplexLp::SolveExitStatus CplexLp::_solve() {
|
alpar@484
|
563 |
_clear_temporals();
|
deba@623
|
564 |
_applyMessageLevel();
|
alpar@484
|
565 |
return convertStatus(CPXlpopt(cplexEnv(), _prob));
|
alpar@484
|
566 |
}
|
alpar@484
|
567 |
|
alpar@485
|
568 |
CplexLp::SolveExitStatus CplexLp::solvePrimal() {
|
alpar@484
|
569 |
_clear_temporals();
|
deba@623
|
570 |
_applyMessageLevel();
|
alpar@484
|
571 |
return convertStatus(CPXprimopt(cplexEnv(), _prob));
|
alpar@484
|
572 |
}
|
alpar@484
|
573 |
|
alpar@485
|
574 |
CplexLp::SolveExitStatus CplexLp::solveDual() {
|
alpar@484
|
575 |
_clear_temporals();
|
deba@623
|
576 |
_applyMessageLevel();
|
alpar@484
|
577 |
return convertStatus(CPXdualopt(cplexEnv(), _prob));
|
alpar@484
|
578 |
}
|
alpar@484
|
579 |
|
alpar@485
|
580 |
CplexLp::SolveExitStatus CplexLp::solveBarrier() {
|
alpar@484
|
581 |
_clear_temporals();
|
deba@623
|
582 |
_applyMessageLevel();
|
alpar@484
|
583 |
return convertStatus(CPXbaropt(cplexEnv(), _prob));
|
alpar@484
|
584 |
}
|
alpar@484
|
585 |
|
alpar@485
|
586 |
CplexLp::Value CplexLp::_getPrimal(int i) const {
|
alpar@484
|
587 |
Value x;
|
alpar@484
|
588 |
CPXgetx(cplexEnv(), _prob, &x, i, i);
|
alpar@484
|
589 |
return x;
|
alpar@484
|
590 |
}
|
alpar@484
|
591 |
|
alpar@485
|
592 |
CplexLp::Value CplexLp::_getDual(int i) const {
|
alpar@484
|
593 |
Value y;
|
alpar@484
|
594 |
CPXgetpi(cplexEnv(), _prob, &y, i, i);
|
alpar@484
|
595 |
return y;
|
alpar@484
|
596 |
}
|
alpar@484
|
597 |
|
alpar@485
|
598 |
CplexLp::Value CplexLp::_getPrimalValue() const {
|
alpar@484
|
599 |
Value objval;
|
alpar@484
|
600 |
CPXgetobjval(cplexEnv(), _prob, &objval);
|
alpar@484
|
601 |
return objval;
|
alpar@484
|
602 |
}
|
alpar@484
|
603 |
|
alpar@485
|
604 |
CplexLp::VarStatus CplexLp::_getColStatus(int i) const {
|
alpar@484
|
605 |
if (_col_status.empty()) {
|
alpar@484
|
606 |
_col_status.resize(CPXgetnumcols(cplexEnv(), _prob));
|
alpar@484
|
607 |
CPXgetbase(cplexEnv(), _prob, &_col_status.front(), 0);
|
alpar@484
|
608 |
}
|
alpar@484
|
609 |
switch (_col_status[i]) {
|
alpar@484
|
610 |
case CPX_BASIC:
|
alpar@484
|
611 |
return BASIC;
|
alpar@484
|
612 |
case CPX_FREE_SUPER:
|
alpar@484
|
613 |
return FREE;
|
alpar@484
|
614 |
case CPX_AT_LOWER:
|
alpar@484
|
615 |
return LOWER;
|
alpar@484
|
616 |
case CPX_AT_UPPER:
|
alpar@484
|
617 |
return UPPER;
|
alpar@484
|
618 |
default:
|
alpar@484
|
619 |
LEMON_ASSERT(false, "Wrong column status");
|
alpar@485
|
620 |
return CplexLp::VarStatus();
|
alpar@484
|
621 |
}
|
alpar@484
|
622 |
}
|
alpar@484
|
623 |
|
alpar@485
|
624 |
CplexLp::VarStatus CplexLp::_getRowStatus(int i) const {
|
alpar@484
|
625 |
if (_row_status.empty()) {
|
alpar@484
|
626 |
_row_status.resize(CPXgetnumrows(cplexEnv(), _prob));
|
alpar@484
|
627 |
CPXgetbase(cplexEnv(), _prob, 0, &_row_status.front());
|
alpar@484
|
628 |
}
|
alpar@484
|
629 |
switch (_row_status[i]) {
|
alpar@484
|
630 |
case CPX_BASIC:
|
alpar@484
|
631 |
return BASIC;
|
alpar@484
|
632 |
case CPX_AT_LOWER:
|
alpar@484
|
633 |
{
|
alpar@484
|
634 |
char s;
|
alpar@484
|
635 |
CPXgetsense(cplexEnv(), _prob, &s, i, i);
|
alpar@484
|
636 |
return s != 'L' ? LOWER : UPPER;
|
alpar@484
|
637 |
}
|
alpar@484
|
638 |
case CPX_AT_UPPER:
|
alpar@484
|
639 |
return UPPER;
|
alpar@484
|
640 |
default:
|
alpar@484
|
641 |
LEMON_ASSERT(false, "Wrong row status");
|
alpar@485
|
642 |
return CplexLp::VarStatus();
|
alpar@484
|
643 |
}
|
alpar@484
|
644 |
}
|
alpar@484
|
645 |
|
alpar@485
|
646 |
CplexLp::Value CplexLp::_getPrimalRay(int i) const {
|
alpar@484
|
647 |
if (_primal_ray.empty()) {
|
alpar@484
|
648 |
_primal_ray.resize(CPXgetnumcols(cplexEnv(), _prob));
|
alpar@484
|
649 |
CPXgetray(cplexEnv(), _prob, &_primal_ray.front());
|
alpar@484
|
650 |
}
|
alpar@484
|
651 |
return _primal_ray[i];
|
alpar@484
|
652 |
}
|
alpar@484
|
653 |
|
alpar@485
|
654 |
CplexLp::Value CplexLp::_getDualRay(int i) const {
|
alpar@484
|
655 |
if (_dual_ray.empty()) {
|
alpar@484
|
656 |
|
alpar@484
|
657 |
}
|
alpar@484
|
658 |
return _dual_ray[i];
|
alpar@484
|
659 |
}
|
alpar@484
|
660 |
|
deba@623
|
661 |
// Cplex 7.0 status values
|
alpar@484
|
662 |
// This table lists the statuses, returned by the CPXgetstat()
|
alpar@484
|
663 |
// routine, for solutions to LP problems or mixed integer problems. If
|
alpar@484
|
664 |
// no solution exists, the return value is zero.
|
alpar@484
|
665 |
|
alpar@484
|
666 |
// For Simplex, Barrier
|
alpar@484
|
667 |
// 1 CPX_OPTIMAL
|
alpar@484
|
668 |
// Optimal solution found
|
alpar@484
|
669 |
// 2 CPX_INFEASIBLE
|
alpar@484
|
670 |
// Problem infeasible
|
alpar@484
|
671 |
// 3 CPX_UNBOUNDED
|
alpar@484
|
672 |
// Problem unbounded
|
alpar@484
|
673 |
// 4 CPX_OBJ_LIM
|
alpar@484
|
674 |
// Objective limit exceeded in Phase II
|
alpar@484
|
675 |
// 5 CPX_IT_LIM_FEAS
|
alpar@484
|
676 |
// Iteration limit exceeded in Phase II
|
alpar@484
|
677 |
// 6 CPX_IT_LIM_INFEAS
|
alpar@484
|
678 |
// Iteration limit exceeded in Phase I
|
alpar@484
|
679 |
// 7 CPX_TIME_LIM_FEAS
|
alpar@484
|
680 |
// Time limit exceeded in Phase II
|
alpar@484
|
681 |
// 8 CPX_TIME_LIM_INFEAS
|
alpar@484
|
682 |
// Time limit exceeded in Phase I
|
alpar@484
|
683 |
// 9 CPX_NUM_BEST_FEAS
|
alpar@484
|
684 |
// Problem non-optimal, singularities in Phase II
|
alpar@484
|
685 |
// 10 CPX_NUM_BEST_INFEAS
|
alpar@484
|
686 |
// Problem non-optimal, singularities in Phase I
|
alpar@484
|
687 |
// 11 CPX_OPTIMAL_INFEAS
|
alpar@484
|
688 |
// Optimal solution found, unscaled infeasibilities
|
alpar@484
|
689 |
// 12 CPX_ABORT_FEAS
|
alpar@484
|
690 |
// Aborted in Phase II
|
alpar@484
|
691 |
// 13 CPX_ABORT_INFEAS
|
alpar@484
|
692 |
// Aborted in Phase I
|
alpar@484
|
693 |
// 14 CPX_ABORT_DUAL_INFEAS
|
alpar@484
|
694 |
// Aborted in barrier, dual infeasible
|
alpar@484
|
695 |
// 15 CPX_ABORT_PRIM_INFEAS
|
alpar@484
|
696 |
// Aborted in barrier, primal infeasible
|
alpar@484
|
697 |
// 16 CPX_ABORT_PRIM_DUAL_INFEAS
|
alpar@484
|
698 |
// Aborted in barrier, primal and dual infeasible
|
alpar@484
|
699 |
// 17 CPX_ABORT_PRIM_DUAL_FEAS
|
alpar@484
|
700 |
// Aborted in barrier, primal and dual feasible
|
alpar@484
|
701 |
// 18 CPX_ABORT_CROSSOVER
|
alpar@484
|
702 |
// Aborted in crossover
|
alpar@484
|
703 |
// 19 CPX_INForUNBD
|
alpar@484
|
704 |
// Infeasible or unbounded
|
alpar@484
|
705 |
// 20 CPX_PIVOT
|
alpar@484
|
706 |
// User pivot used
|
alpar@484
|
707 |
//
|
deba@623
|
708 |
// Pending return values
|
alpar@484
|
709 |
// ??case CPX_ABORT_DUAL_INFEAS
|
alpar@484
|
710 |
// ??case CPX_ABORT_CROSSOVER
|
alpar@484
|
711 |
// ??case CPX_INForUNBD
|
alpar@484
|
712 |
// ??case CPX_PIVOT
|
alpar@484
|
713 |
|
alpar@484
|
714 |
//Some more interesting stuff:
|
alpar@484
|
715 |
|
alpar@484
|
716 |
// CPX_PARAM_PROBMETHOD 1062 int LPMETHOD
|
alpar@484
|
717 |
// 0 Automatic
|
alpar@484
|
718 |
// 1 Primal Simplex
|
alpar@484
|
719 |
// 2 Dual Simplex
|
alpar@484
|
720 |
// 3 Network Simplex
|
alpar@484
|
721 |
// 4 Standard Barrier
|
alpar@484
|
722 |
// Default: 0
|
alpar@484
|
723 |
// Description: Method for linear optimization.
|
alpar@484
|
724 |
// Determines which algorithm is used when CPXlpopt() (or "optimize"
|
alpar@484
|
725 |
// in the Interactive Optimizer) is called. Currently the behavior of
|
alpar@484
|
726 |
// the "Automatic" setting is that CPLEX simply invokes the dual
|
alpar@484
|
727 |
// simplex method, but this capability may be expanded in the future
|
alpar@484
|
728 |
// so that CPLEX chooses the method based on problem characteristics
|
alpar@484
|
729 |
#if CPX_VERSION < 900
|
alpar@484
|
730 |
void statusSwitch(CPXENVptr cplexEnv(),int& stat){
|
alpar@484
|
731 |
int lpmethod;
|
alpar@484
|
732 |
CPXgetintparam (cplexEnv(),CPX_PARAM_PROBMETHOD,&lpmethod);
|
alpar@484
|
733 |
if (lpmethod==2){
|
alpar@484
|
734 |
if (stat==CPX_UNBOUNDED){
|
alpar@484
|
735 |
stat=CPX_INFEASIBLE;
|
alpar@484
|
736 |
}
|
alpar@484
|
737 |
else{
|
alpar@484
|
738 |
if (stat==CPX_INFEASIBLE)
|
alpar@484
|
739 |
stat=CPX_UNBOUNDED;
|
alpar@484
|
740 |
}
|
alpar@484
|
741 |
}
|
alpar@484
|
742 |
}
|
alpar@484
|
743 |
#else
|
alpar@484
|
744 |
void statusSwitch(CPXENVptr,int&){}
|
alpar@484
|
745 |
#endif
|
alpar@484
|
746 |
|
alpar@485
|
747 |
CplexLp::ProblemType CplexLp::_getPrimalType() const {
|
alpar@484
|
748 |
// Unboundedness not treated well: the following is from cplex 9.0 doc
|
alpar@484
|
749 |
// About Unboundedness
|
alpar@484
|
750 |
|
alpar@484
|
751 |
// The treatment of models that are unbounded involves a few
|
alpar@484
|
752 |
// subtleties. Specifically, a declaration of unboundedness means that
|
alpar@484
|
753 |
// ILOG CPLEX has determined that the model has an unbounded
|
alpar@484
|
754 |
// ray. Given any feasible solution x with objective z, a multiple of
|
alpar@484
|
755 |
// the unbounded ray can be added to x to give a feasible solution
|
alpar@484
|
756 |
// with objective z-1 (or z+1 for maximization models). Thus, if a
|
alpar@484
|
757 |
// feasible solution exists, then the optimal objective is
|
alpar@484
|
758 |
// unbounded. Note that ILOG CPLEX has not necessarily concluded that
|
alpar@484
|
759 |
// a feasible solution exists. Users can call the routine CPXsolninfo
|
alpar@484
|
760 |
// to determine whether ILOG CPLEX has also concluded that the model
|
alpar@484
|
761 |
// has a feasible solution.
|
alpar@484
|
762 |
|
alpar@484
|
763 |
int stat = CPXgetstat(cplexEnv(), _prob);
|
alpar@484
|
764 |
#if CPX_VERSION >= 800
|
alpar@484
|
765 |
switch (stat)
|
alpar@484
|
766 |
{
|
alpar@484
|
767 |
case CPX_STAT_OPTIMAL:
|
alpar@484
|
768 |
return OPTIMAL;
|
alpar@484
|
769 |
case CPX_STAT_UNBOUNDED:
|
alpar@484
|
770 |
return UNBOUNDED;
|
alpar@484
|
771 |
case CPX_STAT_INFEASIBLE:
|
alpar@484
|
772 |
return INFEASIBLE;
|
alpar@484
|
773 |
default:
|
alpar@484
|
774 |
return UNDEFINED;
|
alpar@484
|
775 |
}
|
alpar@484
|
776 |
#else
|
alpar@484
|
777 |
statusSwitch(cplexEnv(),stat);
|
alpar@484
|
778 |
//CPXgetstat(cplexEnv(), _prob);
|
alpar@484
|
779 |
switch (stat) {
|
alpar@484
|
780 |
case 0:
|
alpar@484
|
781 |
return UNDEFINED; //Undefined
|
alpar@484
|
782 |
case CPX_OPTIMAL://Optimal
|
alpar@484
|
783 |
return OPTIMAL;
|
alpar@484
|
784 |
case CPX_UNBOUNDED://Unbounded
|
alpar@484
|
785 |
return INFEASIBLE;//In case of dual simplex
|
alpar@484
|
786 |
//return UNBOUNDED;
|
alpar@484
|
787 |
case CPX_INFEASIBLE://Infeasible
|
alpar@484
|
788 |
// case CPX_IT_LIM_INFEAS:
|
alpar@484
|
789 |
// case CPX_TIME_LIM_INFEAS:
|
alpar@484
|
790 |
// case CPX_NUM_BEST_INFEAS:
|
alpar@484
|
791 |
// case CPX_OPTIMAL_INFEAS:
|
alpar@484
|
792 |
// case CPX_ABORT_INFEAS:
|
alpar@484
|
793 |
// case CPX_ABORT_PRIM_INFEAS:
|
alpar@484
|
794 |
// case CPX_ABORT_PRIM_DUAL_INFEAS:
|
alpar@484
|
795 |
return UNBOUNDED;//In case of dual simplex
|
alpar@484
|
796 |
//return INFEASIBLE;
|
alpar@484
|
797 |
// case CPX_OBJ_LIM:
|
alpar@484
|
798 |
// case CPX_IT_LIM_FEAS:
|
alpar@484
|
799 |
// case CPX_TIME_LIM_FEAS:
|
alpar@484
|
800 |
// case CPX_NUM_BEST_FEAS:
|
alpar@484
|
801 |
// case CPX_ABORT_FEAS:
|
alpar@484
|
802 |
// case CPX_ABORT_PRIM_DUAL_FEAS:
|
alpar@484
|
803 |
// return FEASIBLE;
|
alpar@484
|
804 |
default:
|
alpar@484
|
805 |
return UNDEFINED; //Everything else comes here
|
alpar@484
|
806 |
//FIXME error
|
alpar@484
|
807 |
}
|
alpar@484
|
808 |
#endif
|
alpar@484
|
809 |
}
|
alpar@484
|
810 |
|
deba@623
|
811 |
// Cplex 9.0 status values
|
alpar@484
|
812 |
// CPX_STAT_ABORT_DUAL_OBJ_LIM
|
alpar@484
|
813 |
// CPX_STAT_ABORT_IT_LIM
|
alpar@484
|
814 |
// CPX_STAT_ABORT_OBJ_LIM
|
alpar@484
|
815 |
// CPX_STAT_ABORT_PRIM_OBJ_LIM
|
alpar@484
|
816 |
// CPX_STAT_ABORT_TIME_LIM
|
alpar@484
|
817 |
// CPX_STAT_ABORT_USER
|
alpar@484
|
818 |
// CPX_STAT_FEASIBLE_RELAXED
|
alpar@484
|
819 |
// CPX_STAT_INFEASIBLE
|
alpar@484
|
820 |
// CPX_STAT_INForUNBD
|
alpar@484
|
821 |
// CPX_STAT_NUM_BEST
|
alpar@484
|
822 |
// CPX_STAT_OPTIMAL
|
alpar@484
|
823 |
// CPX_STAT_OPTIMAL_FACE_UNBOUNDED
|
alpar@484
|
824 |
// CPX_STAT_OPTIMAL_INFEAS
|
alpar@484
|
825 |
// CPX_STAT_OPTIMAL_RELAXED
|
alpar@484
|
826 |
// CPX_STAT_UNBOUNDED
|
alpar@484
|
827 |
|
alpar@485
|
828 |
CplexLp::ProblemType CplexLp::_getDualType() const {
|
alpar@484
|
829 |
int stat = CPXgetstat(cplexEnv(), _prob);
|
alpar@484
|
830 |
#if CPX_VERSION >= 800
|
alpar@484
|
831 |
switch (stat) {
|
alpar@484
|
832 |
case CPX_STAT_OPTIMAL:
|
alpar@484
|
833 |
return OPTIMAL;
|
alpar@484
|
834 |
case CPX_STAT_UNBOUNDED:
|
alpar@484
|
835 |
return INFEASIBLE;
|
alpar@484
|
836 |
default:
|
alpar@484
|
837 |
return UNDEFINED;
|
alpar@484
|
838 |
}
|
alpar@484
|
839 |
#else
|
alpar@484
|
840 |
statusSwitch(cplexEnv(),stat);
|
alpar@484
|
841 |
switch (stat) {
|
alpar@484
|
842 |
case 0:
|
alpar@484
|
843 |
return UNDEFINED; //Undefined
|
alpar@484
|
844 |
case CPX_OPTIMAL://Optimal
|
alpar@484
|
845 |
return OPTIMAL;
|
alpar@484
|
846 |
case CPX_UNBOUNDED:
|
alpar@484
|
847 |
return INFEASIBLE;
|
alpar@484
|
848 |
default:
|
alpar@484
|
849 |
return UNDEFINED; //Everything else comes here
|
alpar@484
|
850 |
//FIXME error
|
alpar@484
|
851 |
}
|
alpar@484
|
852 |
#endif
|
alpar@484
|
853 |
}
|
alpar@484
|
854 |
|
alpar@485
|
855 |
// CplexMip members
|
alpar@484
|
856 |
|
alpar@485
|
857 |
CplexMip::CplexMip()
|
deba@598
|
858 |
: LpBase(), MipSolver(), CplexBase() {
|
alpar@484
|
859 |
|
alpar@484
|
860 |
#if CPX_VERSION < 800
|
alpar@484
|
861 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP);
|
alpar@484
|
862 |
#else
|
alpar@484
|
863 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP);
|
alpar@484
|
864 |
#endif
|
alpar@484
|
865 |
}
|
alpar@484
|
866 |
|
alpar@485
|
867 |
CplexMip::CplexMip(const CplexEnv& env)
|
deba@598
|
868 |
: LpBase(), MipSolver(), CplexBase(env) {
|
alpar@484
|
869 |
|
alpar@484
|
870 |
#if CPX_VERSION < 800
|
alpar@484
|
871 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP);
|
alpar@484
|
872 |
#else
|
alpar@484
|
873 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP);
|
alpar@484
|
874 |
#endif
|
alpar@484
|
875 |
|
alpar@484
|
876 |
}
|
alpar@484
|
877 |
|
alpar@485
|
878 |
CplexMip::CplexMip(const CplexMip& other)
|
deba@598
|
879 |
: LpBase(), MipSolver(), CplexBase(other) {}
|
alpar@484
|
880 |
|
alpar@485
|
881 |
CplexMip::~CplexMip() {}
|
alpar@484
|
882 |
|
alpar@587
|
883 |
CplexMip* CplexMip::newSolver() const { return new CplexMip; }
|
alpar@587
|
884 |
CplexMip* CplexMip::cloneSolver() const {return new CplexMip(*this); }
|
alpar@484
|
885 |
|
alpar@485
|
886 |
const char* CplexMip::_solverName() const { return "CplexMip"; }
|
alpar@484
|
887 |
|
alpar@485
|
888 |
void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {
|
alpar@484
|
889 |
|
alpar@484
|
890 |
// Note If a variable is to be changed to binary, a call to CPXchgbds
|
alpar@484
|
891 |
// should also be made to change the bounds to 0 and 1.
|
alpar@484
|
892 |
|
alpar@484
|
893 |
switch (col_type){
|
alpar@484
|
894 |
case INTEGER: {
|
alpar@484
|
895 |
const char t = 'I';
|
alpar@484
|
896 |
CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
|
alpar@484
|
897 |
} break;
|
alpar@484
|
898 |
case REAL: {
|
alpar@484
|
899 |
const char t = 'C';
|
alpar@484
|
900 |
CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
|
alpar@484
|
901 |
} break;
|
alpar@484
|
902 |
default:
|
alpar@484
|
903 |
break;
|
alpar@484
|
904 |
}
|
alpar@484
|
905 |
}
|
alpar@484
|
906 |
|
alpar@485
|
907 |
CplexMip::ColTypes CplexMip::_getColType(int i) const {
|
alpar@484
|
908 |
char t;
|
alpar@484
|
909 |
CPXgetctype (cplexEnv(), _prob, &t, i, i);
|
alpar@484
|
910 |
switch (t) {
|
alpar@484
|
911 |
case 'I':
|
alpar@484
|
912 |
return INTEGER;
|
alpar@484
|
913 |
case 'C':
|
alpar@484
|
914 |
return REAL;
|
alpar@484
|
915 |
default:
|
alpar@484
|
916 |
LEMON_ASSERT(false, "Invalid column type");
|
alpar@484
|
917 |
return ColTypes();
|
alpar@484
|
918 |
}
|
alpar@484
|
919 |
|
alpar@484
|
920 |
}
|
alpar@484
|
921 |
|
alpar@485
|
922 |
CplexMip::SolveExitStatus CplexMip::_solve() {
|
alpar@484
|
923 |
int status;
|
deba@623
|
924 |
_applyMessageLevel();
|
alpar@484
|
925 |
status = CPXmipopt (cplexEnv(), _prob);
|
alpar@484
|
926 |
if (status==0)
|
alpar@484
|
927 |
return SOLVED;
|
alpar@484
|
928 |
else
|
alpar@484
|
929 |
return UNSOLVED;
|
alpar@484
|
930 |
|
alpar@484
|
931 |
}
|
alpar@484
|
932 |
|
alpar@484
|
933 |
|
alpar@485
|
934 |
CplexMip::ProblemType CplexMip::_getType() const {
|
alpar@484
|
935 |
|
alpar@484
|
936 |
int stat = CPXgetstat(cplexEnv(), _prob);
|
alpar@484
|
937 |
|
alpar@484
|
938 |
//Fortunately, MIP statuses did not change for cplex 8.0
|
alpar@484
|
939 |
switch (stat) {
|
alpar@484
|
940 |
case CPXMIP_OPTIMAL:
|
alpar@484
|
941 |
// Optimal integer solution has been found.
|
alpar@484
|
942 |
case CPXMIP_OPTIMAL_TOL:
|
alpar@484
|
943 |
// Optimal soluton with the tolerance defined by epgap or epagap has
|
alpar@484
|
944 |
// been found.
|
alpar@484
|
945 |
return OPTIMAL;
|
alpar@484
|
946 |
//This also exists in later issues
|
alpar@484
|
947 |
// case CPXMIP_UNBOUNDED:
|
alpar@484
|
948 |
//return UNBOUNDED;
|
alpar@484
|
949 |
case CPXMIP_INFEASIBLE:
|
alpar@484
|
950 |
return INFEASIBLE;
|
alpar@484
|
951 |
default:
|
alpar@484
|
952 |
return UNDEFINED;
|
alpar@484
|
953 |
}
|
alpar@484
|
954 |
//Unboundedness not treated well: the following is from cplex 9.0 doc
|
alpar@484
|
955 |
// About Unboundedness
|
alpar@484
|
956 |
|
alpar@484
|
957 |
// The treatment of models that are unbounded involves a few
|
alpar@484
|
958 |
// subtleties. Specifically, a declaration of unboundedness means that
|
alpar@484
|
959 |
// ILOG CPLEX has determined that the model has an unbounded
|
alpar@484
|
960 |
// ray. Given any feasible solution x with objective z, a multiple of
|
alpar@484
|
961 |
// the unbounded ray can be added to x to give a feasible solution
|
alpar@484
|
962 |
// with objective z-1 (or z+1 for maximization models). Thus, if a
|
alpar@484
|
963 |
// feasible solution exists, then the optimal objective is
|
alpar@484
|
964 |
// unbounded. Note that ILOG CPLEX has not necessarily concluded that
|
alpar@484
|
965 |
// a feasible solution exists. Users can call the routine CPXsolninfo
|
alpar@484
|
966 |
// to determine whether ILOG CPLEX has also concluded that the model
|
alpar@484
|
967 |
// has a feasible solution.
|
alpar@484
|
968 |
}
|
alpar@484
|
969 |
|
alpar@485
|
970 |
CplexMip::Value CplexMip::_getSol(int i) const {
|
alpar@484
|
971 |
Value x;
|
alpar@484
|
972 |
CPXgetmipx(cplexEnv(), _prob, &x, i, i);
|
alpar@484
|
973 |
return x;
|
alpar@484
|
974 |
}
|
alpar@484
|
975 |
|
alpar@485
|
976 |
CplexMip::Value CplexMip::_getSolValue() const {
|
alpar@484
|
977 |
Value objval;
|
alpar@484
|
978 |
CPXgetmipobjval(cplexEnv(), _prob, &objval);
|
alpar@484
|
979 |
return objval;
|
alpar@484
|
980 |
}
|
alpar@484
|
981 |
|
alpar@484
|
982 |
} //namespace lemon
|
alpar@484
|
983 |
|