2 * lemon/lp_cplex.cc - Part of LEMON, a generic C++ optimization library
4 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
5 * (Egervary Research Group on Combinatorial Optimization, EGRES).
7 * Permission to use, modify and distribute this software is granted
8 * provided that this copyright notice appears in all copies. For
9 * precise terms see the accompanying LICENSE file.
11 * This software is provided "AS IS" with no warranty of any kind,
12 * express or implied, and with no claim as to its suitability for any
17 #include<lemon/lp_cplex.h>
20 ///\brief Implementation of the LEMON-CPLEX lp solver interface.
23 LpCplex::LpCplex() : LpSolverBase() {
25 // env = CPXopenCPLEXdevelop(&status);
26 env = CPXopenCPLEX(&status);
27 lp = CPXcreateprob(env, &status, "LP problem");
28 //CPXmsg (cpxresults, "Hello-bello\n");
36 LpSolverBase &LpCplex::_newLp()
38 //The first approach opens a new environment
39 LpCplex* newlp=new LpCplex();
43 LpSolverBase &LpCplex::_copyLp() {
44 //The first approach opens a new environment
45 LpCplex* newlp=new LpCplex();
46 //The routine CPXcloneprob can be used to create a new CPLEX problem
47 //object and copy all the problem data from an existing problem
48 //object to it. Solution and starting information is not copied.
49 newlp->lp = CPXcloneprob (env, lp, &status);
53 int LpCplex::_addCol()
55 int i = CPXgetnumcols (env, lp);
57 lb[0]=-INF;//-CPX_INFBOUND;
58 ub[0]=INF;//CPX_INFBOUND;
59 status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
64 int LpCplex::_addRow()
66 //We want a row that is not constrained
68 sense[0]='L';//<= constraint
71 int i = CPXgetnumrows (env, lp);
72 status = CPXnewrows (env, lp, 1, rhs, sense, NULL, NULL);
77 void LpCplex::_eraseCol(int i) {
78 CPXdelcols (env, lp, i, i);
81 void LpCplex::_eraseRow(int i) {
82 CPXdelrows (env, lp, i, i);
86 ///\warning Data at index 0 is ignored in the arrays.
87 void LpCplex::_setRowCoeffs(int i,
90 Value const * values )
92 int rowlist[length+1];
94 for (int k=1;k<=length;++k){
97 status = CPXchgcoeflist(env, lp,
100 const_cast<int * >(indices+1),
101 const_cast<Value * >(values+1));
104 void LpCplex::_setColCoeffs(int i,
107 Value const * values)
109 int collist[length+1];
111 for (int k=1;k<=length;++k){
114 status = CPXchgcoeflist(env, lp,
116 const_cast<int * >(indices+1),
118 const_cast<Value * >(values+1));
121 void LpCplex::_setCoeff(int row, int col, Value value)
123 CPXchgcoef (env, lp, row, col, value);
126 void LpCplex::_setColLowerBound(int i, Value value)
134 status = CPXchgbds (env, lp, 1, indices, lu, bd);
138 void LpCplex::_setColUpperBound(int i, Value value)
146 status = CPXchgbds (env, lp, 1, indices, lu, bd);
149 //This will be easier to implement
150 void LpCplex::_setRowBounds(int i, Value lb, Value ub)
153 if (lb==INF || ub==-INF) {
164 CPXchgsense (env, lp, cnt, indices, sense);
165 CPXchgcoef (env, lp, i, -1, ub);
171 CPXchgsense (env, lp, cnt, indices, sense);
172 CPXchgcoef (env, lp, i, -1, lb);
177 CPXchgsense (env, lp, cnt, indices, sense);
178 CPXchgcoef (env, lp, i, -1, lb);
182 CPXchgsense (env, lp, cnt, indices, sense);
183 CPXchgcoef (env, lp, i, -1, lb);
184 CPXchgcoef (env, lp, i, -2, ub-lb);
190 // void LpCplex::_setRowLowerBound(int i, Value value)
192 // //Not implemented, obsolete
195 // void LpCplex::_setRowUpperBound(int i, Value value)
197 // //Not implemented, obsolete
198 // // //TODO Ezt kell meg megirni
199 // // //type of the problem
201 // // status = CPXgetsense (env, lp, sense, i, i);
203 // // status = CPXgetrhs (env, lp, rhs, i, i);
205 // // switch (sense[0]) {
206 // // case 'L'://<= constraint
208 // // case 'E'://= constraint
210 // // case 'G'://>= constraint
212 // // case 'R'://ranged constraint
218 // // status = CPXchgcoef (env, lp, i, -2, value_rng);
221 void LpCplex::_setObjCoeff(int i, Value obj_coef)
223 CPXchgcoef (env, lp, -1, i, obj_coef);
226 void LpCplex::_clearObj()
228 for (int i=0;i< CPXgetnumcols (env, lp);++i){
229 CPXchgcoef (env, lp, -1, i, 0);
234 LpCplex::SolveExitStatus LpCplex::_solve()
237 status = CPXlpopt (env, lp);
244 // int i= lpx_simplex(lp);
254 LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
256 //7.5-os cplex statusai
257 // #define CPX_OPTIMAL 1
258 // #define CPX_INFEASIBLE 2
259 // #define CPX_UNBOUNDED 3
260 // #define CPX_OBJ_LIM 4
261 // #define CPX_IT_LIM_FEAS 5
262 // #define CPX_IT_LIM_INFEAS 6
263 // #define CPX_TIME_LIM_FEAS 7
264 // #define CPX_TIME_LIM_INFEAS 8
265 // #define CPX_NUM_BEST_FEAS 9
266 // #define CPX_NUM_BEST_INFEAS 10
267 // #define CPX_OPTIMAL_INFEAS 11
268 // #define CPX_ABORT_FEAS 12
269 // #define CPX_ABORT_INFEAS 13
270 // #define CPX_ABORT_DUAL_INFEAS 14
271 // #define CPX_ABORT_PRIM_INFEAS 15
272 // #define CPX_ABORT_PRIM_DUAL_INFEAS 16
273 // #define CPX_ABORT_PRIM_DUAL_FEAS 17
274 // #define CPX_ABORT_CROSSOVER 18
275 // #define CPX_INForUNBD 19
276 // #define CPX_PIVOT 20
278 // Ezeket hova tegyem:
279 // ??case CPX_ABORT_DUAL_INFEAS
280 // ??case CPX_ABORT_CROSSOVER
281 // ??case CPX_INForUNBD
284 int stat = CPXgetstat (env, lp);
287 return UNDEFINED; //Undefined
289 case CPX_OPTIMAL://Optimal
292 case CPX_UNBOUNDED://Unbounded
295 case CPX_INFEASIBLE://Infeasible
296 case CPX_IT_LIM_INFEAS:
297 case CPX_TIME_LIM_INFEAS:
298 case CPX_NUM_BEST_INFEAS:
299 case CPX_OPTIMAL_INFEAS:
300 case CPX_ABORT_INFEAS:
301 case CPX_ABORT_PRIM_INFEAS:
302 case CPX_ABORT_PRIM_DUAL_INFEAS:
306 case CPX_IT_LIM_FEAS:
307 case CPX_TIME_LIM_FEAS:
308 case CPX_NUM_BEST_FEAS:
310 case CPX_ABORT_PRIM_DUAL_FEAS:
314 return UNDEFINED; //Everything else comes here
319 //Nem tudom, hanyas cplex verzio statusai
320 // CPX_STAT_ABORT_DUAL_OBJ_LIM
321 // CPX_STAT_ABORT_IT_LIM
322 // CPX_STAT_ABORT_OBJ_LIM
323 // CPX_STAT_ABORT_PRIM_OBJ_LIM
324 // CPX_STAT_ABORT_TIME_LIM
325 // CPX_STAT_ABORT_USER
326 // CPX_STAT_FEASIBLE_RELAXED
327 // CPX_STAT_INFEASIBLE
328 // CPX_STAT_INForUNBD
331 // CPX_STAT_OPTIMAL_FACE_UNBOUNDED
332 // CPX_STAT_OPTIMAL_INFEAS
333 // CPX_STAT_OPTIMAL_RELAXED
334 // CPX_STAT_UNBOUNDED
336 // int stat = CPXgetstat (env, lp);
338 // case CPX_STAT_OPTIMAL://Optimal
341 // case CPX_STAT_INFEASIBLE://Infeasible
342 // return INFEASIBLE;
344 // case CPX_STAT_UNBOUNDED://Unbounded
347 // case CPX_STAT_NUM_BEST://Feasible
351 // return UNDEFINED; //Everything else comes here
357 LpCplex::Value LpCplex::_getPrimal(int i)
360 CPXgetx (env, lp, &x, i, i);
364 LpCplex::Value LpCplex::_getPrimalValue()
367 //method = CPXgetmethod (env, lp);
368 status = CPXgetobjval (env, lp, &objval);
375 void LpCplex::_setMax()
377 CPXchgobjsen (env, lp, CPX_MAX);
379 void LpCplex::_setMin()
381 CPXchgobjsen (env, lp, CPX_MIN);