1 | /* -*- C++ -*- |
---|
2 | * src/lemon/lp_cplex.cc |
---|
3 | * - Part of LEMON, a generic C++ optimization library |
---|
4 | * |
---|
5 | * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
---|
6 | * (Egervary Combinatorial Optimization Research Group, EGRES). |
---|
7 | * |
---|
8 | * Permission to use, modify and distribute this software is granted |
---|
9 | * provided that this copyright notice appears in all copies. For |
---|
10 | * precise terms see the accompanying LICENSE file. |
---|
11 | * |
---|
12 | * This software is provided "AS IS" with no warranty of any kind, |
---|
13 | * express or implied, and with no claim as to its suitability for any |
---|
14 | * purpose. |
---|
15 | * |
---|
16 | */ |
---|
17 | |
---|
18 | #include"lp_cplex.h" |
---|
19 | |
---|
20 | ///\file |
---|
21 | ///\brief Implementation of the LEMON-CPLEX lp solver interface. |
---|
22 | namespace lemon { |
---|
23 | |
---|
24 | int LpCplex::_addCol() |
---|
25 | { |
---|
26 | int i = CPXgetnumcols (env, lp); |
---|
27 | Value lb[1],ub[1]; |
---|
28 | lb[0]=-INF;//-CPX_INFBOUND; |
---|
29 | ub[0]=INF;//CPX_INFBOUND; |
---|
30 | status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL); |
---|
31 | return i; |
---|
32 | } |
---|
33 | |
---|
34 | int LpCplex::_addRow() |
---|
35 | { |
---|
36 | //We want a ranged row |
---|
37 | char sense[1]; |
---|
38 | sense[0]='R'; |
---|
39 | |
---|
40 | int i = CPXgetnumrows (env, lp); |
---|
41 | status = CPXnewrows (env, lp, 1, NULL, sense, NULL, NULL); |
---|
42 | return i; |
---|
43 | } |
---|
44 | |
---|
45 | ///\warning Data at index 0 is ignored iin the arrays. |
---|
46 | void LpCplex::_setRowCoeffs(int i, |
---|
47 | int length, |
---|
48 | int const * indices, |
---|
49 | Value const * values ) |
---|
50 | { |
---|
51 | int rowlist[length+1]; |
---|
52 | int* p=rowlist; |
---|
53 | for (int k=1;k<=length;++k){ |
---|
54 | rowlist[k]=i; |
---|
55 | } |
---|
56 | status = CPXchgcoeflist(env, lp, |
---|
57 | length, |
---|
58 | p++, |
---|
59 | const_cast<int * >(indices++), |
---|
60 | const_cast<Value * >(values++)); |
---|
61 | } |
---|
62 | |
---|
63 | void LpCplex::_setColCoeffs(int i, |
---|
64 | int length, |
---|
65 | int const * indices, |
---|
66 | Value const * values) |
---|
67 | { |
---|
68 | int collist[length+1]; |
---|
69 | int* p=collist; |
---|
70 | for (int k=1;k<=length;++k){ |
---|
71 | collist[k]=i; |
---|
72 | } |
---|
73 | status = CPXchgcoeflist(env, lp, |
---|
74 | length, |
---|
75 | const_cast<int * >(indices++), |
---|
76 | p++, |
---|
77 | const_cast<Value * >(values++)); |
---|
78 | } |
---|
79 | |
---|
80 | void LpCplex::_setColLowerBound(int i, Value value) |
---|
81 | { |
---|
82 | int indices[1]; |
---|
83 | indices[0]=i; |
---|
84 | char lu[1]; |
---|
85 | lu[0]='L'; |
---|
86 | Value bd[1]; |
---|
87 | bd[0]=value; |
---|
88 | status = CPXchgbds (env, lp, 1, indices, lu, bd); |
---|
89 | |
---|
90 | } |
---|
91 | |
---|
92 | void LpCplex::_setColUpperBound(int i, Value value) |
---|
93 | { |
---|
94 | int indices[1]; |
---|
95 | indices[0]=i; |
---|
96 | char lu[1]; |
---|
97 | lu[0]='U'; |
---|
98 | Value bd[1]; |
---|
99 | bd[0]=value; |
---|
100 | status = CPXchgbds (env, lp, 1, indices, lu, bd); |
---|
101 | } |
---|
102 | |
---|
103 | void LpCplex::_setRowLowerBound(int i, Value value) |
---|
104 | { |
---|
105 | status = CPXchgcoef (env, lp, i, -1, value); |
---|
106 | |
---|
107 | } |
---|
108 | |
---|
109 | void LpCplex::_setRowUpperBound(int i, Value value) |
---|
110 | { |
---|
111 | //TODO Ezt kell meg megirni |
---|
112 | //type of the problem |
---|
113 | char sense[1]; |
---|
114 | status = CPXgetsense (env, lp, sense, i, i); |
---|
115 | Value rhs[1]; |
---|
116 | status = CPXgetrhs (env, lp, rhs, i, i); |
---|
117 | |
---|
118 | switch (sense[0]) { |
---|
119 | case 'L'://<= constraint |
---|
120 | break; |
---|
121 | case 'E'://= constraint |
---|
122 | break; |
---|
123 | case 'G'://>= constraint |
---|
124 | break; |
---|
125 | case 'R'://ranged constraint |
---|
126 | break; |
---|
127 | default: ; |
---|
128 | //FIXME error |
---|
129 | } |
---|
130 | |
---|
131 | status = CPXchgcoef (env, lp, i, -2, value_rng); |
---|
132 | } |
---|
133 | |
---|
134 | void LpCplex::_setObjCoeff(int i, Value obj_coef) |
---|
135 | { |
---|
136 | status = CPXchgcoef (env, lp, -1, i, obj_coef); |
---|
137 | } |
---|
138 | |
---|
139 | LpCplex::SolveExitStatus LpCplex::_solve() |
---|
140 | { |
---|
141 | return SOLVED; |
---|
142 | // int i= lpx_simplex(lp); |
---|
143 | // switch (i) { |
---|
144 | // case LPX_E_OK: |
---|
145 | // return SOLVED; |
---|
146 | // break; |
---|
147 | // default: |
---|
148 | // return UNSOLVED; |
---|
149 | // } |
---|
150 | } |
---|
151 | |
---|
152 | LpCplex::Value LpCplex::_getPrimal(int i) |
---|
153 | { |
---|
154 | return 0; |
---|
155 | } |
---|
156 | |
---|
157 | LpCplex::Value LpCplex::_getPrimalValue() |
---|
158 | { |
---|
159 | return 0; |
---|
160 | } |
---|
161 | |
---|
162 | |
---|
163 | LpCplex::SolutionStatus LpCplex::_getPrimalStatus() |
---|
164 | { |
---|
165 | return OPTIMAL; |
---|
166 | // int stat= lpx_get_status(lp); |
---|
167 | // switch (stat) { |
---|
168 | // case LPX_UNDEF://Undefined (no solve has been run yet) |
---|
169 | // return UNDEFINED; |
---|
170 | // break; |
---|
171 | // case LPX_NOFEAS://There is no feasible solution (primal, I guess) |
---|
172 | // case LPX_INFEAS://Infeasible |
---|
173 | // return INFEASIBLE; |
---|
174 | // break; |
---|
175 | // case LPX_UNBND://Unbounded |
---|
176 | // return INFINITE; |
---|
177 | // break; |
---|
178 | // case LPX_FEAS://Feasible |
---|
179 | // return FEASIBLE; |
---|
180 | // break; |
---|
181 | // case LPX_OPT://Feasible |
---|
182 | // return OPTIMAL; |
---|
183 | // break; |
---|
184 | // default: |
---|
185 | // return UNDEFINED; //to avoid gcc warning |
---|
186 | // //FIXME error |
---|
187 | // } |
---|
188 | } |
---|
189 | |
---|
190 | |
---|
191 | void LpCplex::_setMax() |
---|
192 | { |
---|
193 | CPXchgobjsen (env, lp, CPX_MAX); |
---|
194 | } |
---|
195 | void LpCplex::_setMin() |
---|
196 | { |
---|
197 | CPXchgobjsen (env, lp, CPX_MIN); |
---|
198 | } |
---|
199 | |
---|
200 | } //namespace lemon |
---|
201 | |
---|