/* -*- C++ -*- * lemon/lp_cplex.cc - Part of LEMON, a generic C++ optimization library * * Copyright (C) 2005 Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport * (Egervary Research Group on Combinatorial Optimization, EGRES). * * Permission to use, modify and distribute this software is granted * provided that this copyright notice appears in all copies. For * precise terms see the accompanying LICENSE file. * * This software is provided "AS IS" with no warranty of any kind, * express or implied, and with no claim as to its suitability for any * purpose. * */ #include #include ///\file ///\brief Implementation of the LEMON-CPLEX lp solver interface. namespace lemon { LpCplex::LpCplex() : LpSolverBase() { // env = CPXopenCPLEXdevelop(&status); env = CPXopenCPLEX(&status); lp = CPXcreateprob(env, &status, "LP problem"); } LpCplex::~LpCplex() { CPXfreeprob(env,&lp); CPXcloseCPLEX(&env); } LpSolverBase &LpCplex::_newLp() { //The first approach opens a new environment LpCplex* newlp=new LpCplex(); return *newlp; } LpSolverBase &LpCplex::_copyLp() { //The first approach opens a new environment LpCplex* newlp=new LpCplex(); //The routine CPXcloneprob can be used to create a new CPLEX problem //object and copy all the problem data from an existing problem //object to it. Solution and starting information is not copied. newlp->lp = CPXcloneprob(env, lp, &status); return *newlp; } int LpCplex::_addCol() { int i = CPXgetnumcols(env, lp); Value lb[1],ub[1]; lb[0]=-INF;//-CPX_INFBOUND; ub[0]=INF;//CPX_INFBOUND; status = CPXnewcols(env, lp, 1, NULL, lb, ub, NULL, NULL); return i; } int LpCplex::_addRow() { //We want a row that is not constrained char sense[1]; sense[0]='L';//<= constraint Value rhs[1]; rhs[0]=INF; int i = CPXgetnumrows(env, lp); status = CPXnewrows(env, lp, 1, rhs, sense, NULL, NULL); return i; } void LpCplex::_eraseCol(int i) { CPXdelcols(env, lp, i, i); } void LpCplex::_eraseRow(int i) { CPXdelrows(env, lp, i, i); } ///\warning Data at index 0 is ignored in the arrays. void LpCplex::_setRowCoeffs(int i, int length, int const * indices, Value const * values ) { int rowlist[length+1]; int* p=rowlist; for (int k=1;k<=length;++k){ rowlist[k]=i; } status = CPXchgcoeflist(env, lp, length, p+1, const_cast(indices+1), const_cast(values+1)); } void LpCplex::_setColCoeffs(int i, int length, int const * indices, Value const * values) { int collist[length+1]; int* p=collist; for (int k=1;k<=length;++k){ collist[k]=i; } status = CPXchgcoeflist(env, lp, length, const_cast(indices+1), p+1, const_cast(values+1)); } void LpCplex::_setCoeff(int row, int col, Value value) { CPXchgcoef(env, lp, row, col, value); } void LpCplex::_setColLowerBound(int i, Value value) { int indices[1]; indices[0]=i; char lu[1]; lu[0]='L'; Value bd[1]; bd[0]=value; status = CPXchgbds(env, lp, 1, indices, lu, bd); } void LpCplex::_setColUpperBound(int i, Value value) { int indices[1]; indices[0]=i; char lu[1]; lu[0]='U'; Value bd[1]; bd[0]=value; status = CPXchgbds(env, lp, 1, indices, lu, bd); } //This will be easier to implement void LpCplex::_setRowBounds(int i, Value lb, Value ub) { //Bad parameter if (lb==INF || ub==-INF) { //FIXME error } int cnt=1; int indices[1]; indices[0]=i; char sense[1]; if (lb==-INF){ sense[0]='L'; CPXchgsense(env, lp, cnt, indices, sense); CPXchgcoef(env, lp, i, -1, ub); } else{ if (ub==INF){ sense[0]='G'; CPXchgsense(env, lp, cnt, indices, sense); CPXchgcoef(env, lp, i, -1, lb); } else{ if (lb == ub){ sense[0]='E'; CPXchgsense(env, lp, cnt, indices, sense); CPXchgcoef(env, lp, i, -1, lb); } else{ sense[0]='R'; CPXchgsense(env, lp, cnt, indices, sense); CPXchgcoef(env, lp, i, -1, lb); CPXchgcoef(env, lp, i, -2, ub-lb); } } } } // void LpCplex::_setRowLowerBound(int i, Value value) // { // //Not implemented, obsolete // } // void LpCplex::_setRowUpperBound(int i, Value value) // { // //Not implemented, obsolete // // //TODO Ezt kell meg megirni // // //type of the problem // // char sense[1]; // // status = CPXgetsense(env, lp, sense, i, i); // // Value rhs[1]; // // status = CPXgetrhs(env, lp, rhs, i, i); // // switch (sense[0]) { // // case 'L'://<= constraint // // break; // // case 'E'://= constraint // // break; // // case 'G'://>= constraint // // break; // // case 'R'://ranged constraint // // break; // // default: ; // // //FIXME error // // } // // status = CPXchgcoef(env, lp, i, -2, value_rng); // } void LpCplex::_setObjCoeff(int i, Value obj_coef) { CPXchgcoef(env, lp, -1, i, obj_coef); } void LpCplex::_clearObj() { for (int i=0;i< CPXgetnumcols(env, lp);++i){ CPXchgcoef(env, lp, -1, i, 0); } } // The routine returns zero unless an error occurred during the // optimization. Examples of errors include exhausting available // memory (CPXERR_NO_MEMORY) or encountering invalid data in the // CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a // user-specified CPLEX limit, or proving the model infeasible or // unbounded, are not considered errors. Note that a zero return // value does not necessarily mean that a solution exists. Use query // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain // further information about the status of the optimization. LpCplex::SolveExitStatus LpCplex::_solve() { //CPX_PARAM_LPMETHOD status = CPXlpopt(env, lp); //status = CPXprimopt(env, lp); if (status == 0){ //We want to exclude some cases switch (CPXgetstat(env, lp)){ case CPX_OBJ_LIM: case CPX_IT_LIM_FEAS: case CPX_IT_LIM_INFEAS: case CPX_TIME_LIM_FEAS: case CPX_TIME_LIM_INFEAS: return UNSOLVED; default: return SOLVED; } } else{ return UNSOLVED; } } LpCplex::Value LpCplex::_getPrimal(int i) { Value x; CPXgetx(env, lp, &x, i, i); return x; } LpCplex::Value LpCplex::_getPrimalValue() { Value objval; //method = CPXgetmethod (env, lp); //printf("CPXgetprobtype %d \n",CPXgetprobtype(env,lp)); status = CPXgetobjval(env, lp, &objval); //printf("Objective value: %g \n",objval); return objval; } //7.5-os cplex statusai (Vigyazat: a 9.0-asei masok!) // This table lists the statuses, returned by the CPXgetstat() routine, for solutions to LP problems or mixed integer problems. If no solution exists, the return value is zero. // For Simplex, Barrier // 1 CPX_OPTIMAL // Optimal solution found // 2 CPX_INFEASIBLE // Problem infeasible // 3 CPX_UNBOUNDED // Problem unbounded // 4 CPX_OBJ_LIM // Objective limit exceeded in Phase II // 5 CPX_IT_LIM_FEAS // Iteration limit exceeded in Phase II // 6 CPX_IT_LIM_INFEAS // Iteration limit exceeded in Phase I // 7 CPX_TIME_LIM_FEAS // Time limit exceeded in Phase II // 8 CPX_TIME_LIM_INFEAS // Time limit exceeded in Phase I // 9 CPX_NUM_BEST_FEAS // Problem non-optimal, singularities in Phase II // 10 CPX_NUM_BEST_INFEAS // Problem non-optimal, singularities in Phase I // 11 CPX_OPTIMAL_INFEAS // Optimal solution found, unscaled infeasibilities // 12 CPX_ABORT_FEAS // Aborted in Phase II // 13 CPX_ABORT_INFEAS // Aborted in Phase I // 14 CPX_ABORT_DUAL_INFEAS // Aborted in barrier, dual infeasible // 15 CPX_ABORT_PRIM_INFEAS // Aborted in barrier, primal infeasible // 16 CPX_ABORT_PRIM_DUAL_INFEAS // Aborted in barrier, primal and dual infeasible // 17 CPX_ABORT_PRIM_DUAL_FEAS // Aborted in barrier, primal and dual feasible // 18 CPX_ABORT_CROSSOVER // Aborted in crossover // 19 CPX_INForUNBD // Infeasible or unbounded // 20 CPX_PIVOT // User pivot used // // Ezeket hova tegyem: // ??case CPX_ABORT_DUAL_INFEAS // ??case CPX_ABORT_CROSSOVER // ??case CPX_INForUNBD // ??case CPX_PIVOT //Some more interesting stuff: // CPX_PARAM_LPMETHOD 1062 int LPMETHOD // 0 Automatic // 1 Primal Simplex // 2 Dual Simplex // 3 Network Simplex // 4 Standard Barrier // Default: 0 // Description: Method for linear optimization. // Determines which algorithm is used when CPXlpopt() (or "optimize" in the Interactive Optimizer) is called. Currently the behavior of the "Automatic" setting is that CPLEX simply invokes the dual simplex method, but this capability may be expanded in the future so that CPLEX chooses the method based on problem characteristics //Hulye cplex void statusSwitch(CPXENVptr env,int& stat){ int lpmethod; CPXgetintparam (env,CPX_PARAM_LPMETHOD,&lpmethod); if (lpmethod==2){ if (stat==CPX_UNBOUNDED){ stat=CPX_INFEASIBLE; } else{ if (stat==CPX_INFEASIBLE) stat=CPX_UNBOUNDED; } } } LpCplex::SolutionStatus LpCplex::_getPrimalStatus() { int stat = CPXgetstat(env, lp); statusSwitch(env,stat); //CPXgetstat(env, lp); //printf("A primal status: %d, CPX_OPTIMAL=%d \n",stat,CPX_OPTIMAL); switch (stat) { case 0: return UNDEFINED; //Undefined case CPX_OPTIMAL://Optimal return OPTIMAL; case CPX_UNBOUNDED://Unbounded return INFEASIBLE;//In case of dual simplex //return INFINITE; case CPX_INFEASIBLE://Infeasible // case CPX_IT_LIM_INFEAS: // case CPX_TIME_LIM_INFEAS: // case CPX_NUM_BEST_INFEAS: // case CPX_OPTIMAL_INFEAS: // case CPX_ABORT_INFEAS: // case CPX_ABORT_PRIM_INFEAS: // case CPX_ABORT_PRIM_DUAL_INFEAS: return INFINITE;//In case of dual simplex //return INFEASIBLE; // case CPX_OBJ_LIM: // case CPX_IT_LIM_FEAS: // case CPX_TIME_LIM_FEAS: // case CPX_NUM_BEST_FEAS: // case CPX_ABORT_FEAS: // case CPX_ABORT_PRIM_DUAL_FEAS: // return FEASIBLE; default: return UNDEFINED; //Everything else comes here //FIXME error } } //9.0-as cplex verzio statusai // CPX_STAT_ABORT_DUAL_OBJ_LIM // CPX_STAT_ABORT_IT_LIM // CPX_STAT_ABORT_OBJ_LIM // CPX_STAT_ABORT_PRIM_OBJ_LIM // CPX_STAT_ABORT_TIME_LIM // CPX_STAT_ABORT_USER // CPX_STAT_FEASIBLE_RELAXED // CPX_STAT_INFEASIBLE // CPX_STAT_INForUNBD // CPX_STAT_NUM_BEST // CPX_STAT_OPTIMAL // CPX_STAT_OPTIMAL_FACE_UNBOUNDED // CPX_STAT_OPTIMAL_INFEAS // CPX_STAT_OPTIMAL_RELAXED // CPX_STAT_UNBOUNDED LpCplex::SolutionStatus LpCplex::_getDualStatus() { int stat = CPXgetstat(env, lp); statusSwitch(env,stat); switch (stat) { case 0: return UNDEFINED; //Undefined case CPX_OPTIMAL://Optimal return OPTIMAL; case CPX_UNBOUNDED: return INFEASIBLE; default: return UNDEFINED; //Everything else comes here //FIXME error } } LpCplex::ProblemTypes LpCplex::_getProblemType() { int stat = CPXgetstat(env, lp); switch (stat) { case CPX_OPTIMAL://Optimal return PRIMAL_DUAL_FEASIBLE; case CPX_UNBOUNDED: return PRIMAL_FEASIBLE_DUAL_INFEASIBLE; // return PRIMAL_INFEASIBLE_DUAL_FEASIBLE; // return PRIMAL_DUAL_INFEASIBLE; //Seems to be that this is all we can say for sure default: //In all other cases return UNKNOWN; //FIXME error } } void LpCplex::_setMax() { CPXchgobjsen(env, lp, CPX_MAX); } void LpCplex::_setMin() { CPXchgobjsen(env, lp, CPX_MIN); } } //namespace lemon