1 /* -*- mode: C++; indent-tabs-mode: nil; -*-
3 * This file is a part of LEMON, a generic C++ optimization library.
5 * Copyright (C) 2003-2013
6 * Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport
7 * (Egervary Research Group on Combinatorial Optimization, EGRES).
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.
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
23 #include <lemon/cplex.h>
26 #include <ilcplex/cplex.h>
31 ///\brief Implementation of the LEMON-CPLEX lp solver interface.
34 CplexEnv::LicenseError::LicenseError(int status) {
35 if (!CPXgeterrorstring(0, status, _message)) {
36 std::strcpy(_message, "Cplex unknown error");
40 void CplexEnv::incCnt()
47 void CplexEnv::decCnt()
57 else _cnt_lock->unlock();
60 CplexEnv::CplexEnv() {
62 _env = CPXopenCPLEX(&status);
64 throw LicenseError(status);
67 _cnt_lock = new bits::Lock;
70 CplexEnv::CplexEnv(const CplexEnv& other) {
73 _cnt_lock = other._cnt_lock;
77 CplexEnv& CplexEnv::operator=(const CplexEnv& other) {
81 _cnt_lock = other._cnt_lock;
86 CplexEnv::~CplexEnv() {
90 CplexBase::CplexBase() : LpBase() {
92 _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
93 messageLevel(MESSAGE_NOTHING);
96 CplexBase::CplexBase(const CplexEnv& env)
97 : LpBase(), _env(env) {
99 _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
100 messageLevel(MESSAGE_NOTHING);
103 CplexBase::CplexBase(const CplexBase& cplex)
106 _prob = CPXcloneprob(cplexEnv(), cplex._prob, &status);
109 messageLevel(MESSAGE_NOTHING);
112 CplexBase::~CplexBase() {
113 CPXfreeprob(cplexEnv(),&_prob);
116 int CplexBase::_addCol() {
117 int i = CPXgetnumcols(cplexEnv(), _prob);
118 double lb = -INF, ub = INF;
119 CPXnewcols(cplexEnv(), _prob, 1, 0, &lb, &ub, 0, 0);
124 int CplexBase::_addRow() {
125 int i = CPXgetnumrows(cplexEnv(), _prob);
126 const double ub = INF;
128 CPXnewrows(cplexEnv(), _prob, 1, &ub, &s, 0, 0);
132 int CplexBase::_addRow(Value lb, ExprIterator b,
133 ExprIterator e, Value ub) {
134 int i = CPXgetnumrows(cplexEnv(), _prob);
138 std::vector<int> indices;
139 std::vector<Value> values;
141 for(ExprIterator it=b; it!=e; ++it) {
142 indices.push_back(it->first);
143 values.push_back(it->second);
148 CPXaddrows(cplexEnv(), _prob, 0, 1, values.size(), &ub, &s,
149 &rmatbeg, &indices.front(), &values.front(), 0, 0);
150 } else if (ub == INF) {
152 CPXaddrows(cplexEnv(), _prob, 0, 1, values.size(), &lb, &s,
153 &rmatbeg, &indices.front(), &values.front(), 0, 0);
154 } else if (lb == ub){
156 CPXaddrows(cplexEnv(), _prob, 0, 1, values.size(), &lb, &s,
157 &rmatbeg, &indices.front(), &values.front(), 0, 0);
160 double len = ub - lb;
161 CPXaddrows(cplexEnv(), _prob, 0, 1, values.size(), &ub, &s,
162 &rmatbeg, &indices.front(), &values.front(), 0, 0);
163 CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
169 void CplexBase::_eraseCol(int i) {
170 CPXdelcols(cplexEnv(), _prob, i, i);
173 void CplexBase::_eraseRow(int i) {
174 CPXdelrows(cplexEnv(), _prob, i, i);
177 void CplexBase::_eraseColId(int i) {
179 _cols.shiftIndices(i);
181 void CplexBase::_eraseRowId(int i) {
183 _rows.shiftIndices(i);
186 void CplexBase::_getColName(int col, std::string &name) const {
188 CPXgetcolname(cplexEnv(), _prob, 0, 0, 0, &size, col, col);
195 std::vector<char> buf(size);
198 CPXgetcolname(cplexEnv(), _prob, &cname, &buf.front(), size,
203 void CplexBase::_setColName(int col, const std::string &name) {
205 cname = const_cast<char*>(name.c_str());
206 CPXchgcolname(cplexEnv(), _prob, 1, &col, &cname);
209 int CplexBase::_colByName(const std::string& name) const {
211 if (CPXgetcolindex(cplexEnv(), _prob,
212 const_cast<char*>(name.c_str()), &index) == 0) {
218 void CplexBase::_getRowName(int row, std::string &name) const {
220 CPXgetrowname(cplexEnv(), _prob, 0, 0, 0, &size, row, row);
227 std::vector<char> buf(size);
230 CPXgetrowname(cplexEnv(), _prob, &cname, &buf.front(), size,
235 void CplexBase::_setRowName(int row, const std::string &name) {
237 cname = const_cast<char*>(name.c_str());
238 CPXchgrowname(cplexEnv(), _prob, 1, &row, &cname);
241 int CplexBase::_rowByName(const std::string& name) const {
243 if (CPXgetrowindex(cplexEnv(), _prob,
244 const_cast<char*>(name.c_str()), &index) == 0) {
250 void CplexBase::_setRowCoeffs(int i, ExprIterator b,
253 std::vector<int> indices;
254 std::vector<int> rowlist;
255 std::vector<Value> values;
257 for(ExprIterator it=b; it!=e; ++it) {
258 indices.push_back(it->first);
259 values.push_back(it->second);
260 rowlist.push_back(i);
263 CPXchgcoeflist(cplexEnv(), _prob, values.size(),
264 &rowlist.front(), &indices.front(), &values.front());
267 void CplexBase::_getRowCoeffs(int i, InsertIterator b) const {
268 int tmp1, tmp2, tmp3, length;
269 CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
272 std::vector<int> indices(length);
273 std::vector<double> values(length);
275 CPXgetrows(cplexEnv(), _prob, &tmp1, &tmp2,
276 &indices.front(), &values.front(),
277 length, &tmp3, i, i);
279 for (int i = 0; i < length; ++i) {
280 *b = std::make_pair(indices[i], values[i]);
285 void CplexBase::_setColCoeffs(int i, ExprIterator b, ExprIterator e) {
286 std::vector<int> indices;
287 std::vector<int> collist;
288 std::vector<Value> values;
290 for(ExprIterator it=b; it!=e; ++it) {
291 indices.push_back(it->first);
292 values.push_back(it->second);
293 collist.push_back(i);
296 CPXchgcoeflist(cplexEnv(), _prob, values.size(),
297 &indices.front(), &collist.front(), &values.front());
300 void CplexBase::_getColCoeffs(int i, InsertIterator b) const {
302 int tmp1, tmp2, tmp3, length;
303 CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2, 0, 0, 0, &length, i, i);
306 std::vector<int> indices(length);
307 std::vector<double> values(length);
309 CPXgetcols(cplexEnv(), _prob, &tmp1, &tmp2,
310 &indices.front(), &values.front(),
311 length, &tmp3, i, i);
313 for (int i = 0; i < length; ++i) {
314 *b = std::make_pair(indices[i], values[i]);
320 void CplexBase::_setCoeff(int row, int col, Value value) {
321 CPXchgcoef(cplexEnv(), _prob, row, col, value);
324 CplexBase::Value CplexBase::_getCoeff(int row, int col) const {
325 CplexBase::Value value;
326 CPXgetcoef(cplexEnv(), _prob, row, col, &value);
330 void CplexBase::_setColLowerBound(int i, Value value) {
332 CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
335 CplexBase::Value CplexBase::_getColLowerBound(int i) const {
336 CplexBase::Value res;
337 CPXgetlb(cplexEnv(), _prob, &res, i, i);
338 return res <= -CPX_INFBOUND ? -INF : res;
341 void CplexBase::_setColUpperBound(int i, Value value)
344 CPXchgbds(cplexEnv(), _prob, 1, &i, &s, &value);
347 CplexBase::Value CplexBase::_getColUpperBound(int i) const {
348 CplexBase::Value res;
349 CPXgetub(cplexEnv(), _prob, &res, i, i);
350 return res >= CPX_INFBOUND ? INF : res;
353 CplexBase::Value CplexBase::_getRowLowerBound(int i) const {
355 CPXgetsense(cplexEnv(), _prob, &s, i, i);
356 CplexBase::Value res;
362 CPXgetrhs(cplexEnv(), _prob, &res, i, i);
363 return res <= -CPX_INFBOUND ? -INF : res;
369 CplexBase::Value CplexBase::_getRowUpperBound(int i) const {
371 CPXgetsense(cplexEnv(), _prob, &s, i, i);
372 CplexBase::Value res;
377 CPXgetrhs(cplexEnv(), _prob, &res, i, i);
378 return res >= CPX_INFBOUND ? INF : res;
380 CPXgetrhs(cplexEnv(), _prob, &res, i, i);
383 CPXgetrngval(cplexEnv(), _prob, &rng, i, i);
386 return res >= CPX_INFBOUND ? INF : res;
392 //This is easier to implement
393 void CplexBase::_set_row_bounds(int i, Value lb, Value ub) {
396 CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
397 CPXchgrhs(cplexEnv(), _prob, 1, &i, &ub);
398 } else if (ub == INF) {
400 CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
401 CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
402 } else if (lb == ub){
404 CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
405 CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
408 CPXchgsense(cplexEnv(), _prob, 1, &i, &s);
409 CPXchgrhs(cplexEnv(), _prob, 1, &i, &lb);
410 double len = ub - lb;
411 CPXchgrngval(cplexEnv(), _prob, 1, &i, &len);
415 void CplexBase::_setRowLowerBound(int i, Value lb)
417 LEMON_ASSERT(lb != INF, "Invalid bound");
418 _set_row_bounds(i, lb, CplexBase::_getRowUpperBound(i));
421 void CplexBase::_setRowUpperBound(int i, Value ub)
424 LEMON_ASSERT(ub != -INF, "Invalid bound");
425 _set_row_bounds(i, CplexBase::_getRowLowerBound(i), ub);
428 void CplexBase::_setObjCoeffs(ExprIterator b, ExprIterator e)
430 std::vector<int> indices;
431 std::vector<Value> values;
432 for(ExprIterator it=b; it!=e; ++it) {
433 indices.push_back(it->first);
434 values.push_back(it->second);
436 CPXchgobj(cplexEnv(), _prob, values.size(),
437 &indices.front(), &values.front());
441 void CplexBase::_getObjCoeffs(InsertIterator b) const
443 int num = CPXgetnumcols(cplexEnv(), _prob);
444 std::vector<Value> x(num);
446 CPXgetobj(cplexEnv(), _prob, &x.front(), 0, num - 1);
447 for (int i = 0; i < num; ++i) {
449 *b = std::make_pair(i, x[i]);
455 void CplexBase::_setObjCoeff(int i, Value obj_coef)
457 CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef);
460 CplexBase::Value CplexBase::_getObjCoeff(int i) const
463 CPXgetobj(cplexEnv(), _prob, &x, i, i);
467 void CplexBase::_setSense(CplexBase::Sense sense) {
470 CPXchgobjsen(cplexEnv(), _prob, CPX_MIN);
473 CPXchgobjsen(cplexEnv(), _prob, CPX_MAX);
478 CplexBase::Sense CplexBase::_getSense() const {
479 switch (CPXgetobjsen(cplexEnv(), _prob)) {
485 LEMON_ASSERT(false, "Invalid sense");
486 return CplexBase::Sense();
490 void CplexBase::_clear() {
491 CPXfreeprob(cplexEnv(),&_prob);
493 _prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem");
496 void CplexBase::_messageLevel(MessageLevel level) {
498 case MESSAGE_NOTHING:
499 _message_enabled = false;
502 case MESSAGE_WARNING:
504 case MESSAGE_VERBOSE:
505 _message_enabled = true;
510 void CplexBase::_applyMessageLevel() {
511 CPXsetintparam(cplexEnv(), CPX_PARAM_SCRIND,
512 _message_enabled ? CPX_ON : CPX_OFF);
515 void CplexBase::_write(std::string file, std::string format) const
517 if(format == "MPS" || format == "LP")
518 CPXwriteprob(cplexEnv(), cplexLp(), file.c_str(), format.c_str());
519 else if(format == "SOL")
520 CPXsolwrite(cplexEnv(), cplexLp(), file.c_str());
521 else throw UnsupportedFormatError(format);
529 : LpBase(), LpSolver(), CplexBase() {}
531 CplexLp::CplexLp(const CplexEnv& env)
532 : LpBase(), LpSolver(), CplexBase(env) {}
534 CplexLp::CplexLp(const CplexLp& other)
535 : LpBase(), LpSolver(), CplexBase(other) {}
537 CplexLp::~CplexLp() {}
539 CplexLp* CplexLp::newSolver() const { return new CplexLp; }
540 CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }
542 const char* CplexLp::_solverName() const { return "CplexLp"; }
544 void CplexLp::_clear_temporals() {
551 // The routine returns zero unless an error occurred during the
552 // optimization. Examples of errors include exhausting available
553 // memory (CPXERR_NO_MEMORY) or encountering invalid data in the
554 // CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a
555 // user-specified CPLEX limit, or proving the model infeasible or
556 // unbounded, are not considered errors. Note that a zero return
557 // value does not necessarily mean that a solution exists. Use query
558 // routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain
559 // further information about the status of the optimization.
560 CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
561 #if CPX_VERSION >= 800
563 switch (CPXgetstat(cplexEnv(), _prob)) {
564 case CPX_STAT_OPTIMAL:
565 case CPX_STAT_INFEASIBLE:
566 case CPX_STAT_UNBOUNDED:
576 //We want to exclude some cases
577 switch (CPXgetstat(cplexEnv(), _prob)) {
579 case CPX_IT_LIM_FEAS:
580 case CPX_IT_LIM_INFEAS:
581 case CPX_TIME_LIM_FEAS:
582 case CPX_TIME_LIM_INFEAS:
593 CplexLp::SolveExitStatus CplexLp::_solve() {
595 _applyMessageLevel();
596 return convertStatus(CPXlpopt(cplexEnv(), _prob));
599 CplexLp::SolveExitStatus CplexLp::solvePrimal() {
601 _applyMessageLevel();
602 return convertStatus(CPXprimopt(cplexEnv(), _prob));
605 CplexLp::SolveExitStatus CplexLp::solveDual() {
607 _applyMessageLevel();
608 return convertStatus(CPXdualopt(cplexEnv(), _prob));
611 CplexLp::SolveExitStatus CplexLp::solveBarrier() {
613 _applyMessageLevel();
614 return convertStatus(CPXbaropt(cplexEnv(), _prob));
617 CplexLp::Value CplexLp::_getPrimal(int i) const {
619 CPXgetx(cplexEnv(), _prob, &x, i, i);
623 CplexLp::Value CplexLp::_getDual(int i) const {
625 CPXgetpi(cplexEnv(), _prob, &y, i, i);
629 CplexLp::Value CplexLp::_getPrimalValue() const {
631 CPXgetobjval(cplexEnv(), _prob, &objval);
635 CplexLp::VarStatus CplexLp::_getColStatus(int i) const {
636 if (_col_status.empty()) {
637 _col_status.resize(CPXgetnumcols(cplexEnv(), _prob));
638 CPXgetbase(cplexEnv(), _prob, &_col_status.front(), 0);
640 switch (_col_status[i]) {
650 LEMON_ASSERT(false, "Wrong column status");
651 return CplexLp::VarStatus();
655 CplexLp::VarStatus CplexLp::_getRowStatus(int i) const {
656 if (_row_status.empty()) {
657 _row_status.resize(CPXgetnumrows(cplexEnv(), _prob));
658 CPXgetbase(cplexEnv(), _prob, 0, &_row_status.front());
660 switch (_row_status[i]) {
666 CPXgetsense(cplexEnv(), _prob, &s, i, i);
667 return s != 'L' ? LOWER : UPPER;
672 LEMON_ASSERT(false, "Wrong row status");
673 return CplexLp::VarStatus();
677 CplexLp::Value CplexLp::_getPrimalRay(int i) const {
678 if (_primal_ray.empty()) {
679 _primal_ray.resize(CPXgetnumcols(cplexEnv(), _prob));
680 CPXgetray(cplexEnv(), _prob, &_primal_ray.front());
682 return _primal_ray[i];
685 CplexLp::Value CplexLp::_getDualRay(int i) const {
686 if (_dual_ray.empty()) {
692 // Cplex 7.0 status values
693 // This table lists the statuses, returned by the CPXgetstat()
694 // routine, for solutions to LP problems or mixed integer problems. If
695 // no solution exists, the return value is zero.
697 // For Simplex, Barrier
699 // Optimal solution found
701 // Problem infeasible
705 // Objective limit exceeded in Phase II
707 // Iteration limit exceeded in Phase II
708 // 6 CPX_IT_LIM_INFEAS
709 // Iteration limit exceeded in Phase I
710 // 7 CPX_TIME_LIM_FEAS
711 // Time limit exceeded in Phase II
712 // 8 CPX_TIME_LIM_INFEAS
713 // Time limit exceeded in Phase I
714 // 9 CPX_NUM_BEST_FEAS
715 // Problem non-optimal, singularities in Phase II
716 // 10 CPX_NUM_BEST_INFEAS
717 // Problem non-optimal, singularities in Phase I
718 // 11 CPX_OPTIMAL_INFEAS
719 // Optimal solution found, unscaled infeasibilities
721 // Aborted in Phase II
722 // 13 CPX_ABORT_INFEAS
723 // Aborted in Phase I
724 // 14 CPX_ABORT_DUAL_INFEAS
725 // Aborted in barrier, dual infeasible
726 // 15 CPX_ABORT_PRIM_INFEAS
727 // Aborted in barrier, primal infeasible
728 // 16 CPX_ABORT_PRIM_DUAL_INFEAS
729 // Aborted in barrier, primal and dual infeasible
730 // 17 CPX_ABORT_PRIM_DUAL_FEAS
731 // Aborted in barrier, primal and dual feasible
732 // 18 CPX_ABORT_CROSSOVER
733 // Aborted in crossover
735 // Infeasible or unbounded
739 // Pending return values
740 // ??case CPX_ABORT_DUAL_INFEAS
741 // ??case CPX_ABORT_CROSSOVER
742 // ??case CPX_INForUNBD
745 //Some more interesting stuff:
747 // CPX_PARAM_PROBMETHOD 1062 int LPMETHOD
752 // 4 Standard Barrier
754 // Description: Method for linear optimization.
755 // Determines which algorithm is used when CPXlpopt() (or "optimize"
756 // in the Interactive Optimizer) is called. Currently the behavior of
757 // the "Automatic" setting is that CPLEX simply invokes the dual
758 // simplex method, but this capability may be expanded in the future
759 // so that CPLEX chooses the method based on problem characteristics
760 #if CPX_VERSION < 900
761 void statusSwitch(CPXENVptr cplexEnv(),int& stat){
763 CPXgetintparam (cplexEnv(),CPX_PARAM_PROBMETHOD,&lpmethod);
765 if (stat==CPX_UNBOUNDED){
769 if (stat==CPX_INFEASIBLE)
775 void statusSwitch(CPXENVptr,int&){}
778 CplexLp::ProblemType CplexLp::_getPrimalType() const {
779 // Unboundedness not treated well: the following is from cplex 9.0 doc
780 // About Unboundedness
782 // The treatment of models that are unbounded involves a few
783 // subtleties. Specifically, a declaration of unboundedness means that
784 // ILOG CPLEX has determined that the model has an unbounded
785 // ray. Given any feasible solution x with objective z, a multiple of
786 // the unbounded ray can be added to x to give a feasible solution
787 // with objective z-1 (or z+1 for maximization models). Thus, if a
788 // feasible solution exists, then the optimal objective is
789 // unbounded. Note that ILOG CPLEX has not necessarily concluded that
790 // a feasible solution exists. Users can call the routine CPXsolninfo
791 // to determine whether ILOG CPLEX has also concluded that the model
792 // has a feasible solution.
794 int stat = CPXgetstat(cplexEnv(), _prob);
795 #if CPX_VERSION >= 800
798 case CPX_STAT_OPTIMAL:
800 case CPX_STAT_UNBOUNDED:
802 case CPX_STAT_INFEASIBLE:
808 statusSwitch(cplexEnv(),stat);
809 //CPXgetstat(cplexEnv(), _prob);
812 return UNDEFINED; //Undefined
813 case CPX_OPTIMAL://Optimal
815 case CPX_UNBOUNDED://Unbounded
816 return INFEASIBLE;//In case of dual simplex
818 case CPX_INFEASIBLE://Infeasible
819 // case CPX_IT_LIM_INFEAS:
820 // case CPX_TIME_LIM_INFEAS:
821 // case CPX_NUM_BEST_INFEAS:
822 // case CPX_OPTIMAL_INFEAS:
823 // case CPX_ABORT_INFEAS:
824 // case CPX_ABORT_PRIM_INFEAS:
825 // case CPX_ABORT_PRIM_DUAL_INFEAS:
826 return UNBOUNDED;//In case of dual simplex
829 // case CPX_IT_LIM_FEAS:
830 // case CPX_TIME_LIM_FEAS:
831 // case CPX_NUM_BEST_FEAS:
832 // case CPX_ABORT_FEAS:
833 // case CPX_ABORT_PRIM_DUAL_FEAS:
836 return UNDEFINED; //Everything else comes here
842 // Cplex 9.0 status values
843 // CPX_STAT_ABORT_DUAL_OBJ_LIM
844 // CPX_STAT_ABORT_IT_LIM
845 // CPX_STAT_ABORT_OBJ_LIM
846 // CPX_STAT_ABORT_PRIM_OBJ_LIM
847 // CPX_STAT_ABORT_TIME_LIM
848 // CPX_STAT_ABORT_USER
849 // CPX_STAT_FEASIBLE_RELAXED
850 // CPX_STAT_INFEASIBLE
851 // CPX_STAT_INForUNBD
854 // CPX_STAT_OPTIMAL_FACE_UNBOUNDED
855 // CPX_STAT_OPTIMAL_INFEAS
856 // CPX_STAT_OPTIMAL_RELAXED
857 // CPX_STAT_UNBOUNDED
859 CplexLp::ProblemType CplexLp::_getDualType() const {
860 int stat = CPXgetstat(cplexEnv(), _prob);
861 #if CPX_VERSION >= 800
863 case CPX_STAT_OPTIMAL:
865 case CPX_STAT_UNBOUNDED:
871 statusSwitch(cplexEnv(),stat);
874 return UNDEFINED; //Undefined
875 case CPX_OPTIMAL://Optimal
880 return UNDEFINED; //Everything else comes here
889 : LpBase(), MipSolver(), CplexBase() {
891 #if CPX_VERSION < 800
892 CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP);
894 CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP);
898 CplexMip::CplexMip(const CplexEnv& env)
899 : LpBase(), MipSolver(), CplexBase(env) {
901 #if CPX_VERSION < 800
902 CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP);
904 CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP);
909 CplexMip::CplexMip(const CplexMip& other)
910 : LpBase(), MipSolver(), CplexBase(other) {}
912 CplexMip::~CplexMip() {}
914 CplexMip* CplexMip::newSolver() const { return new CplexMip; }
915 CplexMip* CplexMip::cloneSolver() const {return new CplexMip(*this); }
917 const char* CplexMip::_solverName() const { return "CplexMip"; }
919 void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {
921 // Note If a variable is to be changed to binary, a call to CPXchgbds
922 // should also be made to change the bounds to 0 and 1.
927 CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
931 CPXchgctype (cplexEnv(), _prob, 1, &i, &t);
938 CplexMip::ColTypes CplexMip::_getColType(int i) const {
940 CPXgetctype (cplexEnv(), _prob, &t, i, i);
947 LEMON_ASSERT(false, "Invalid column type");
953 CplexMip::SolveExitStatus CplexMip::_solve() {
955 _applyMessageLevel();
956 status = CPXmipopt (cplexEnv(), _prob);
965 CplexMip::ProblemType CplexMip::_getType() const {
967 int stat = CPXgetstat(cplexEnv(), _prob);
969 //Fortunately, MIP statuses did not change for cplex 8.0
972 // Optimal integer solution has been found.
973 case CPXMIP_OPTIMAL_TOL:
974 // Optimal soluton with the tolerance defined by epgap or epagap has
977 //This also exists in later issues
978 // case CPXMIP_UNBOUNDED:
980 case CPXMIP_INFEASIBLE:
985 //Unboundedness not treated well: the following is from cplex 9.0 doc
986 // About Unboundedness
988 // The treatment of models that are unbounded involves a few
989 // subtleties. Specifically, a declaration of unboundedness means that
990 // ILOG CPLEX has determined that the model has an unbounded
991 // ray. Given any feasible solution x with objective z, a multiple of
992 // the unbounded ray can be added to x to give a feasible solution
993 // with objective z-1 (or z+1 for maximization models). Thus, if a
994 // feasible solution exists, then the optimal objective is
995 // unbounded. Note that ILOG CPLEX has not necessarily concluded that
996 // a feasible solution exists. Users can call the routine CPXsolninfo
997 // to determine whether ILOG CPLEX has also concluded that the model
998 // has a feasible solution.
1001 CplexMip::Value CplexMip::_getSol(int i) const {
1003 CPXgetmipx(cplexEnv(), _prob, &x, i, i);
1007 CplexMip::Value CplexMip::_getSolValue() const {
1009 CPXgetmipobjval(cplexEnv(), _prob, &objval);