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