2 * src/lemon/lp_cplex.cc
3 * - Part of LEMON, a generic C++ optimization library
5 * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
6 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
18 #include<lemon/lp_cplex.h>
21 ///\brief Implementation of the LEMON-CPLEX lp solver interface.
24 LpCplex::LpCplex() : LpSolverBase() {
27 env = CPXopenCPLEXdevelop(&status);
30 // fprintf(stderr,"A CPLEX környezet megnyitása sikertelen.\n");
31 // CPXgeterrorstring(Env, Status, ErrorMsg);
32 // fprintf(stderr,"%s",ErrorMsg);
36 // *** A problema létrehozása ***
37 lp = CPXcreateprob(env, &status, "LP problem");
39 // if (Problem == NULL)
41 // fprintf(stderr,"Az LP létrehozása sikertelen");
48 status = CPXfreeprob(env,&lp);
51 // fprintf(stderr,"A CPLEX feladat törlése sikertelen.\n");
52 // CPXgeterrorstring(Env, Status, ErrorMsg);
53 // fprintf(stderr,"%s",ErrorMsg);
57 status = CPXcloseCPLEX(&env);
60 // fprintf(stderr,"A CPLEX környezet bezárása sikertelen.\n");
61 // CPXgeterrorstring(Env, Status, ErrorMsg);
62 // fprintf(stderr,"%s",ErrorMsg);
68 LpSolverBase &LpCplex::_newLp()
70 return *(LpSolverBase*)0;
72 LpSolverBase &LpCplex::_copyLp() {
73 return *(LpSolverBase*)0;
74 //Ez lesz majd CPXcloneprob (env, lp, &status);
77 int LpCplex::_addCol()
79 int i = CPXgetnumcols (env, lp);
81 lb[0]=-INF;//-CPX_INFBOUND;
82 ub[0]=INF;//CPX_INFBOUND;
83 status = CPXnewcols (env, lp, 1, NULL, lb, ub, NULL, NULL);
87 int LpCplex::_addRow()
89 //We want a row that is not constrained
91 sense[0]='L';//<= constraint
94 int i = CPXgetnumrows (env, lp);
95 status = CPXnewrows (env, lp, 1, rhs, sense, NULL, NULL);
99 ///\warning Data at index 0 is ignored in the arrays.
100 void LpCplex::_setRowCoeffs(int i,
103 Value const * values )
105 int rowlist[length+1];
107 for (int k=1;k<=length;++k){
110 status = CPXchgcoeflist(env, lp,
113 const_cast<int * >(indices+1),
114 const_cast<Value * >(values+1));
117 void LpCplex::_setColCoeffs(int i,
120 Value const * values)
122 int collist[length+1];
124 for (int k=1;k<=length;++k){
127 status = CPXchgcoeflist(env, lp,
129 const_cast<int * >(indices+1),
131 const_cast<Value * >(values+1));
134 void LpCplex::_setColLowerBound(int i, Value value)
142 status = CPXchgbds (env, lp, 1, indices, lu, bd);
146 void LpCplex::_setColUpperBound(int i, Value value)
154 status = CPXchgbds (env, lp, 1, indices, lu, bd);
157 //This will be easier to implement
158 void LpCplex::_setRowBounds(int i, Value lb, Value ub)
161 if (lb==INF || ub==-INF) {
172 CPXchgsense (env, lp, cnt, indices, sense);
173 CPXchgcoef (env, lp, i, -1, ub);
179 CPXchgsense (env, lp, cnt, indices, sense);
180 CPXchgcoef (env, lp, i, -1, lb);
185 CPXchgsense (env, lp, cnt, indices, sense);
186 CPXchgcoef (env, lp, i, -1, lb);
190 CPXchgsense (env, lp, cnt, indices, sense);
191 CPXchgcoef (env, lp, i, -1, lb);
192 CPXchgcoef (env, lp, i, -2, ub-lb);
198 // void LpCplex::_setRowLowerBound(int i, Value value)
200 // //Not implemented, obsolete
203 // void LpCplex::_setRowUpperBound(int i, Value value)
205 // //Not implemented, obsolete
206 // // //TODO Ezt kell meg megirni
207 // // //type of the problem
209 // // status = CPXgetsense (env, lp, sense, i, i);
211 // // status = CPXgetrhs (env, lp, rhs, i, i);
213 // // switch (sense[0]) {
214 // // case 'L'://<= constraint
216 // // case 'E'://= constraint
218 // // case 'G'://>= constraint
220 // // case 'R'://ranged constraint
226 // // status = CPXchgcoef (env, lp, i, -2, value_rng);
229 void LpCplex::_setObjCoeff(int i, Value obj_coef)
231 CPXchgcoef (env, lp, -1, i, obj_coef);
234 void LpCplex::_clearObj()
236 for (int i=0;i< CPXgetnumcols (env, lp);++i){
237 CPXchgcoef (env, lp, -1, i, 0);
242 LpCplex::SolveExitStatus LpCplex::_solve()
245 status = CPXlpopt (env, lp);
252 // int i= lpx_simplex(lp);
262 LpCplex::SolutionStatus LpCplex::_getPrimalStatus()
264 //7.5-os cplex statusai
265 // #define CPX_OPTIMAL 1
266 // #define CPX_INFEASIBLE 2
267 // #define CPX_UNBOUNDED 3
268 // #define CPX_OBJ_LIM 4
269 // #define CPX_IT_LIM_FEAS 5
270 // #define CPX_IT_LIM_INFEAS 6
271 // #define CPX_TIME_LIM_FEAS 7
272 // #define CPX_TIME_LIM_INFEAS 8
273 // #define CPX_NUM_BEST_FEAS 9
274 // #define CPX_NUM_BEST_INFEAS 10
275 // #define CPX_OPTIMAL_INFEAS 11
276 // #define CPX_ABORT_FEAS 12
277 // #define CPX_ABORT_INFEAS 13
278 // #define CPX_ABORT_DUAL_INFEAS 14
279 // #define CPX_ABORT_PRIM_INFEAS 15
280 // #define CPX_ABORT_PRIM_DUAL_INFEAS 16
281 // #define CPX_ABORT_PRIM_DUAL_FEAS 17
282 // #define CPX_ABORT_CROSSOVER 18
283 // #define CPX_INForUNBD 19
284 // #define CPX_PIVOT 20
286 // Ezeket hova tegyem:
287 // ??case CPX_ABORT_DUAL_INFEAS
288 // ??case CPX_ABORT_CROSSOVER
289 // ??case CPX_INForUNBD
292 int stat = CPXgetstat (env, lp);
295 return UNDEFINED; //Undefined
297 case CPX_OPTIMAL://Optimal
300 case CPX_UNBOUNDED://Unbounded
303 case CPX_INFEASIBLE://Infeasible
304 case CPX_IT_LIM_INFEAS:
305 case CPX_TIME_LIM_INFEAS:
306 case CPX_NUM_BEST_INFEAS:
307 case CPX_OPTIMAL_INFEAS:
308 case CPX_ABORT_INFEAS:
309 case CPX_ABORT_PRIM_INFEAS:
310 case CPX_ABORT_PRIM_DUAL_INFEAS:
314 case CPX_IT_LIM_FEAS:
315 case CPX_TIME_LIM_FEAS:
316 case CPX_NUM_BEST_FEAS:
318 case CPX_ABORT_PRIM_DUAL_FEAS:
322 return UNDEFINED; //Everything else comes here
327 //Nem tudom, hanyas cplex verzio statusai
328 // CPX_STAT_ABORT_DUAL_OBJ_LIM
329 // CPX_STAT_ABORT_IT_LIM
330 // CPX_STAT_ABORT_OBJ_LIM
331 // CPX_STAT_ABORT_PRIM_OBJ_LIM
332 // CPX_STAT_ABORT_TIME_LIM
333 // CPX_STAT_ABORT_USER
334 // CPX_STAT_FEASIBLE_RELAXED
335 // CPX_STAT_INFEASIBLE
336 // CPX_STAT_INForUNBD
339 // CPX_STAT_OPTIMAL_FACE_UNBOUNDED
340 // CPX_STAT_OPTIMAL_INFEAS
341 // CPX_STAT_OPTIMAL_RELAXED
342 // CPX_STAT_UNBOUNDED
344 // int stat = CPXgetstat (env, lp);
346 // case CPX_STAT_OPTIMAL://Optimal
349 // case CPX_STAT_INFEASIBLE://Infeasible
350 // return INFEASIBLE;
352 // case CPX_STAT_UNBOUNDED://Unbounded
355 // case CPX_STAT_NUM_BEST://Feasible
359 // return UNDEFINED; //Everything else comes here
365 LpCplex::Value LpCplex::_getPrimal(int i)
368 CPXgetx (env, lp, &x, i, i);
372 LpCplex::Value LpCplex::_getPrimalValue()
375 //method = CPXgetmethod (env, lp);
376 status = CPXgetobjval (env, lp, &objval);
383 void LpCplex::_setMax()
385 CPXchgobjsen (env, lp, CPX_MAX);
387 void LpCplex::_setMin()
389 CPXchgobjsen (env, lp, CPX_MIN);