athos@2219
|
1 |
/* -*- C++ -*-
|
athos@2219
|
2 |
*
|
athos@2219
|
3 |
* This file is a part of LEMON, a generic C++ optimization library
|
athos@2219
|
4 |
*
|
athos@2219
|
5 |
* Copyright (C) 2003-2006
|
athos@2219
|
6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
|
athos@2219
|
7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES).
|
athos@2219
|
8 |
*
|
athos@2219
|
9 |
* Permission to use, modify and distribute this software is granted
|
athos@2219
|
10 |
* provided that this copyright notice appears in all copies. For
|
athos@2219
|
11 |
* precise terms see the accompanying LICENSE file.
|
athos@2219
|
12 |
*
|
athos@2219
|
13 |
* This software is provided "AS IS" with no warranty of any kind,
|
athos@2219
|
14 |
* express or implied, and with no claim as to its suitability for any
|
athos@2219
|
15 |
* purpose.
|
athos@2219
|
16 |
*
|
athos@2219
|
17 |
*/
|
athos@2219
|
18 |
|
athos@2219
|
19 |
#ifndef LEMON_MIP_CPLEX_CC
|
athos@2219
|
20 |
#define LEMON_MIP_CPLEX_CC
|
athos@2219
|
21 |
|
athos@2219
|
22 |
///\file
|
athos@2219
|
23 |
///\brief Implementation of the LEMON-CPLEX mip solver interface.
|
athos@2219
|
24 |
|
athos@2219
|
25 |
#include <lemon/mip_cplex.h>
|
athos@2219
|
26 |
|
athos@2219
|
27 |
namespace lemon {
|
athos@2219
|
28 |
|
athos@2219
|
29 |
MipCplex::MipCplex() {
|
athos@2219
|
30 |
//This is unnecessary: setting integrality constraints on
|
athos@2219
|
31 |
//variables will set this, too
|
athos@2219
|
32 |
|
athos@2219
|
33 |
///\todo The constant CPXPROB_MIP is
|
athos@2219
|
34 |
///called CPXPROB_MILP in later versions
|
athos@2219
|
35 |
CPXchgprobtype( env, lp, CPXPROB_MIP);
|
athos@2219
|
36 |
}
|
athos@2219
|
37 |
|
athos@2219
|
38 |
void MipCplex::_colType(int i, MipCplex::ColTypes col_type){
|
athos@2219
|
39 |
|
athos@2219
|
40 |
// Note If a variable is to be changed to binary, a call to CPXchgbds
|
athos@2219
|
41 |
// should also be made to change the bounds to 0 and 1.
|
athos@2219
|
42 |
|
athos@2219
|
43 |
int indices[1];
|
athos@2219
|
44 |
indices[0]=i;
|
athos@2219
|
45 |
char ctype[1];
|
athos@2219
|
46 |
switch (col_type){
|
athos@2219
|
47 |
case LEMON_INTEGER:
|
athos@2219
|
48 |
ctype[0]=CPX_INTEGER;//'I'
|
athos@2219
|
49 |
break;
|
athos@2219
|
50 |
case REAL:
|
athos@2219
|
51 |
ctype[0]=CPX_CONTINUOUS ;//'C'
|
athos@2219
|
52 |
break;
|
athos@2219
|
53 |
default:;
|
athos@2219
|
54 |
//FIXME problem
|
athos@2219
|
55 |
}
|
athos@2219
|
56 |
CPXchgctype (env, lp, 1, indices, ctype);
|
athos@2219
|
57 |
}
|
athos@2219
|
58 |
|
athos@2219
|
59 |
MipCplex::ColTypes MipCplex::_colType(int i){
|
athos@2219
|
60 |
|
athos@2219
|
61 |
char ctype[1];
|
athos@2219
|
62 |
status = CPXgetctype (env, lp, ctype, i, i);
|
athos@2219
|
63 |
switch (ctype[0]){
|
athos@2219
|
64 |
|
athos@2219
|
65 |
case CPX_INTEGER:
|
athos@2219
|
66 |
return LEMON_INTEGER;
|
athos@2219
|
67 |
case CPX_CONTINUOUS:
|
athos@2219
|
68 |
return REAL;
|
athos@2219
|
69 |
default:
|
athos@2219
|
70 |
return REAL;//Error!
|
athos@2219
|
71 |
}
|
athos@2219
|
72 |
|
athos@2219
|
73 |
}
|
athos@2219
|
74 |
|
athos@2219
|
75 |
LpCplex::SolveExitStatus MipCplex::_solve(){
|
athos@2219
|
76 |
|
athos@2219
|
77 |
status = CPXmipopt (env, lp);
|
athos@2219
|
78 |
if (status==0)
|
athos@2219
|
79 |
return SOLVED;
|
athos@2219
|
80 |
else
|
athos@2219
|
81 |
return UNSOLVED;
|
athos@2219
|
82 |
|
athos@2219
|
83 |
}
|
athos@2219
|
84 |
|
athos@2219
|
85 |
|
athos@2219
|
86 |
LpCplex::SolutionStatus MipCplex::_getMipStatus(){
|
athos@2219
|
87 |
|
athos@2219
|
88 |
int stat = CPXgetstat(env, lp);
|
athos@2219
|
89 |
|
athos@2219
|
90 |
//Fortunately, MIP statuses did not change for cplex 8.0
|
athos@2219
|
91 |
switch (stat)
|
athos@2219
|
92 |
{
|
athos@2219
|
93 |
case CPXMIP_OPTIMAL:
|
athos@2219
|
94 |
return OPTIMAL;
|
athos@2219
|
95 |
//This also exists in later issues
|
athos@2219
|
96 |
// case CPXMIP_UNBOUNDED:
|
athos@2219
|
97 |
//return INFINITE;
|
athos@2219
|
98 |
case CPXMIP_INFEASIBLE:
|
athos@2219
|
99 |
return INFEASIBLE;
|
athos@2219
|
100 |
default:
|
athos@2219
|
101 |
return UNDEFINED;
|
athos@2219
|
102 |
}
|
athos@2219
|
103 |
//Unboundedness not treated well: the following is from cplex 9.0 doc
|
athos@2219
|
104 |
// About Unboundedness
|
athos@2219
|
105 |
|
athos@2219
|
106 |
// The treatment of models that are unbounded involves a few
|
athos@2219
|
107 |
// subtleties. Specifically, a declaration of unboundedness means that
|
athos@2219
|
108 |
// ILOG CPLEX has determined that the model has an unbounded
|
athos@2219
|
109 |
// ray. Given any feasible solution x with objective z, a multiple of
|
athos@2219
|
110 |
// the unbounded ray can be added to x to give a feasible solution
|
athos@2219
|
111 |
// with objective z-1 (or z+1 for maximization models). Thus, if a
|
athos@2219
|
112 |
// feasible solution exists, then the optimal objective is
|
athos@2219
|
113 |
// unbounded. Note that ILOG CPLEX has not necessarily concluded that
|
athos@2219
|
114 |
// a feasible solution exists. Users can call the routine CPXsolninfo
|
athos@2219
|
115 |
// to determine whether ILOG CPLEX has also concluded that the model
|
athos@2219
|
116 |
// has a feasible solution.
|
athos@2219
|
117 |
|
athos@2219
|
118 |
}
|
athos@2219
|
119 |
|
athos@2219
|
120 |
MipCplex::Value MipCplex::_getPrimal(int i){
|
athos@2219
|
121 |
Value x;
|
athos@2219
|
122 |
CPXgetmipx(env, lp, &x, i, i);
|
athos@2219
|
123 |
return x;
|
athos@2219
|
124 |
}
|
athos@2219
|
125 |
|
athos@2219
|
126 |
MipCplex::Value MipCplex::_getPrimalValue(){
|
athos@2219
|
127 |
Value objval;
|
athos@2219
|
128 |
status = CPXgetmipobjval(env, lp, &objval);
|
athos@2219
|
129 |
return objval;
|
athos@2219
|
130 |
}
|
athos@2219
|
131 |
} //END OF NAMESPACE LEMON
|
athos@2219
|
132 |
|
athos@2219
|
133 |
#endif //END OF MIP_CPLEX_CC
|