0
5
0
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <iostream> |
| 20 | 20 |
#include <vector> |
| 21 | 21 |
#include <cstring> |
| 22 | 22 |
|
| 23 | 23 |
#include <lemon/cplex.h> |
| 24 | 24 |
|
| 25 | 25 |
extern "C" {
|
| 26 | 26 |
#include <ilcplex/cplex.h> |
| 27 | 27 |
} |
| 28 | 28 |
|
| 29 | 29 |
|
| 30 | 30 |
///\file |
| 31 | 31 |
///\brief Implementation of the LEMON-CPLEX lp solver interface. |
| 32 | 32 |
namespace lemon {
|
| 33 | 33 |
|
| 34 | 34 |
CplexEnv::LicenseError::LicenseError(int status) {
|
| 35 | 35 |
if (!CPXgeterrorstring(0, status, _message)) {
|
| 36 | 36 |
std::strcpy(_message, "Cplex unknown error"); |
| 37 | 37 |
} |
| 38 | 38 |
} |
| 39 | 39 |
|
| 40 | 40 |
CplexEnv::CplexEnv() {
|
| 41 | 41 |
int status; |
| 42 | 42 |
_cnt = new int; |
| 43 | 43 |
_env = CPXopenCPLEX(&status); |
| 44 | 44 |
if (_env == 0) {
|
| 45 | 45 |
delete _cnt; |
| 46 | 46 |
_cnt = 0; |
| 47 | 47 |
throw LicenseError(status); |
| 48 | 48 |
} |
| 49 | 49 |
} |
| 50 | 50 |
|
| 51 | 51 |
CplexEnv::CplexEnv(const CplexEnv& other) {
|
| 52 | 52 |
_env = other._env; |
| 53 | 53 |
_cnt = other._cnt; |
| ... | ... |
@@ -396,103 +396,103 @@ |
| 396 | 396 |
} |
| 397 | 397 |
|
| 398 | 398 |
void CplexBase::_setObjCoeff(int i, Value obj_coef) |
| 399 | 399 |
{
|
| 400 | 400 |
CPXchgobj(cplexEnv(), _prob, 1, &i, &obj_coef); |
| 401 | 401 |
} |
| 402 | 402 |
|
| 403 | 403 |
CplexBase::Value CplexBase::_getObjCoeff(int i) const |
| 404 | 404 |
{
|
| 405 | 405 |
Value x; |
| 406 | 406 |
CPXgetobj(cplexEnv(), _prob, &x, i, i); |
| 407 | 407 |
return x; |
| 408 | 408 |
} |
| 409 | 409 |
|
| 410 | 410 |
void CplexBase::_setSense(CplexBase::Sense sense) {
|
| 411 | 411 |
switch (sense) {
|
| 412 | 412 |
case MIN: |
| 413 | 413 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MIN); |
| 414 | 414 |
break; |
| 415 | 415 |
case MAX: |
| 416 | 416 |
CPXchgobjsen(cplexEnv(), _prob, CPX_MAX); |
| 417 | 417 |
break; |
| 418 | 418 |
} |
| 419 | 419 |
} |
| 420 | 420 |
|
| 421 | 421 |
CplexBase::Sense CplexBase::_getSense() const {
|
| 422 | 422 |
switch (CPXgetobjsen(cplexEnv(), _prob)) {
|
| 423 | 423 |
case CPX_MIN: |
| 424 | 424 |
return MIN; |
| 425 | 425 |
case CPX_MAX: |
| 426 | 426 |
return MAX; |
| 427 | 427 |
default: |
| 428 | 428 |
LEMON_ASSERT(false, "Invalid sense"); |
| 429 | 429 |
return CplexBase::Sense(); |
| 430 | 430 |
} |
| 431 | 431 |
} |
| 432 | 432 |
|
| 433 | 433 |
void CplexBase::_clear() {
|
| 434 | 434 |
CPXfreeprob(cplexEnv(),&_prob); |
| 435 | 435 |
int status; |
| 436 | 436 |
_prob = CPXcreateprob(cplexEnv(), &status, "Cplex problem"); |
| 437 | 437 |
rows.clear(); |
| 438 | 438 |
cols.clear(); |
| 439 | 439 |
} |
| 440 | 440 |
|
| 441 | 441 |
// CplexLp members |
| 442 | 442 |
|
| 443 | 443 |
CplexLp::CplexLp() |
| 444 |
: LpBase(), |
|
| 444 |
: LpBase(), LpSolver(), CplexBase() {}
|
|
| 445 | 445 |
|
| 446 | 446 |
CplexLp::CplexLp(const CplexEnv& env) |
| 447 |
: LpBase(), |
|
| 447 |
: LpBase(), LpSolver(), CplexBase(env) {}
|
|
| 448 | 448 |
|
| 449 | 449 |
CplexLp::CplexLp(const CplexLp& other) |
| 450 |
: LpBase(), |
|
| 450 |
: LpBase(), LpSolver(), CplexBase(other) {}
|
|
| 451 | 451 |
|
| 452 | 452 |
CplexLp::~CplexLp() {}
|
| 453 | 453 |
|
| 454 | 454 |
CplexLp* CplexLp::newSolver() const { return new CplexLp; }
|
| 455 | 455 |
CplexLp* CplexLp::cloneSolver() const {return new CplexLp(*this); }
|
| 456 | 456 |
|
| 457 | 457 |
const char* CplexLp::_solverName() const { return "CplexLp"; }
|
| 458 | 458 |
|
| 459 | 459 |
void CplexLp::_clear_temporals() {
|
| 460 | 460 |
_col_status.clear(); |
| 461 | 461 |
_row_status.clear(); |
| 462 | 462 |
_primal_ray.clear(); |
| 463 | 463 |
_dual_ray.clear(); |
| 464 | 464 |
} |
| 465 | 465 |
|
| 466 | 466 |
// The routine returns zero unless an error occurred during the |
| 467 | 467 |
// optimization. Examples of errors include exhausting available |
| 468 | 468 |
// memory (CPXERR_NO_MEMORY) or encountering invalid data in the |
| 469 | 469 |
// CPLEX problem object (CPXERR_NO_PROBLEM). Exceeding a |
| 470 | 470 |
// user-specified CPLEX limit, or proving the model infeasible or |
| 471 | 471 |
// unbounded, are not considered errors. Note that a zero return |
| 472 | 472 |
// value does not necessarily mean that a solution exists. Use query |
| 473 | 473 |
// routines CPXsolninfo, CPXgetstat, and CPXsolution to obtain |
| 474 | 474 |
// further information about the status of the optimization. |
| 475 | 475 |
CplexLp::SolveExitStatus CplexLp::convertStatus(int status) {
|
| 476 | 476 |
#if CPX_VERSION >= 800 |
| 477 | 477 |
if (status == 0) {
|
| 478 | 478 |
switch (CPXgetstat(cplexEnv(), _prob)) {
|
| 479 | 479 |
case CPX_STAT_OPTIMAL: |
| 480 | 480 |
case CPX_STAT_INFEASIBLE: |
| 481 | 481 |
case CPX_STAT_UNBOUNDED: |
| 482 | 482 |
return SOLVED; |
| 483 | 483 |
default: |
| 484 | 484 |
return UNSOLVED; |
| 485 | 485 |
} |
| 486 | 486 |
} else {
|
| 487 | 487 |
return UNSOLVED; |
| 488 | 488 |
} |
| 489 | 489 |
#else |
| 490 | 490 |
if (status == 0) {
|
| 491 | 491 |
//We want to exclude some cases |
| 492 | 492 |
switch (CPXgetstat(cplexEnv(), _prob)) {
|
| 493 | 493 |
case CPX_OBJ_LIM: |
| 494 | 494 |
case CPX_IT_LIM_FEAS: |
| 495 | 495 |
case CPX_IT_LIM_INFEAS: |
| 496 | 496 |
case CPX_TIME_LIM_FEAS: |
| 497 | 497 |
case CPX_TIME_LIM_INFEAS: |
| 498 | 498 |
return UNSOLVED; |
| ... | ... |
@@ -753,118 +753,118 @@ |
| 753 | 753 |
|
| 754 | 754 |
//9.0-as cplex verzio statusai |
| 755 | 755 |
// CPX_STAT_ABORT_DUAL_OBJ_LIM |
| 756 | 756 |
// CPX_STAT_ABORT_IT_LIM |
| 757 | 757 |
// CPX_STAT_ABORT_OBJ_LIM |
| 758 | 758 |
// CPX_STAT_ABORT_PRIM_OBJ_LIM |
| 759 | 759 |
// CPX_STAT_ABORT_TIME_LIM |
| 760 | 760 |
// CPX_STAT_ABORT_USER |
| 761 | 761 |
// CPX_STAT_FEASIBLE_RELAXED |
| 762 | 762 |
// CPX_STAT_INFEASIBLE |
| 763 | 763 |
// CPX_STAT_INForUNBD |
| 764 | 764 |
// CPX_STAT_NUM_BEST |
| 765 | 765 |
// CPX_STAT_OPTIMAL |
| 766 | 766 |
// CPX_STAT_OPTIMAL_FACE_UNBOUNDED |
| 767 | 767 |
// CPX_STAT_OPTIMAL_INFEAS |
| 768 | 768 |
// CPX_STAT_OPTIMAL_RELAXED |
| 769 | 769 |
// CPX_STAT_UNBOUNDED |
| 770 | 770 |
|
| 771 | 771 |
CplexLp::ProblemType CplexLp::_getDualType() const {
|
| 772 | 772 |
int stat = CPXgetstat(cplexEnv(), _prob); |
| 773 | 773 |
#if CPX_VERSION >= 800 |
| 774 | 774 |
switch (stat) {
|
| 775 | 775 |
case CPX_STAT_OPTIMAL: |
| 776 | 776 |
return OPTIMAL; |
| 777 | 777 |
case CPX_STAT_UNBOUNDED: |
| 778 | 778 |
return INFEASIBLE; |
| 779 | 779 |
default: |
| 780 | 780 |
return UNDEFINED; |
| 781 | 781 |
} |
| 782 | 782 |
#else |
| 783 | 783 |
statusSwitch(cplexEnv(),stat); |
| 784 | 784 |
switch (stat) {
|
| 785 | 785 |
case 0: |
| 786 | 786 |
return UNDEFINED; //Undefined |
| 787 | 787 |
case CPX_OPTIMAL://Optimal |
| 788 | 788 |
return OPTIMAL; |
| 789 | 789 |
case CPX_UNBOUNDED: |
| 790 | 790 |
return INFEASIBLE; |
| 791 | 791 |
default: |
| 792 | 792 |
return UNDEFINED; //Everything else comes here |
| 793 | 793 |
//FIXME error |
| 794 | 794 |
} |
| 795 | 795 |
#endif |
| 796 | 796 |
} |
| 797 | 797 |
|
| 798 | 798 |
// CplexMip members |
| 799 | 799 |
|
| 800 | 800 |
CplexMip::CplexMip() |
| 801 |
: LpBase(), |
|
| 801 |
: LpBase(), MipSolver(), CplexBase() {
|
|
| 802 | 802 |
|
| 803 | 803 |
#if CPX_VERSION < 800 |
| 804 | 804 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP); |
| 805 | 805 |
#else |
| 806 | 806 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP); |
| 807 | 807 |
#endif |
| 808 | 808 |
} |
| 809 | 809 |
|
| 810 | 810 |
CplexMip::CplexMip(const CplexEnv& env) |
| 811 |
: LpBase(), |
|
| 811 |
: LpBase(), MipSolver(), CplexBase(env) {
|
|
| 812 | 812 |
|
| 813 | 813 |
#if CPX_VERSION < 800 |
| 814 | 814 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MIP); |
| 815 | 815 |
#else |
| 816 | 816 |
CPXchgprobtype(cplexEnv(), _prob, CPXPROB_MILP); |
| 817 | 817 |
#endif |
| 818 | 818 |
|
| 819 | 819 |
} |
| 820 | 820 |
|
| 821 | 821 |
CplexMip::CplexMip(const CplexMip& other) |
| 822 |
: LpBase(), |
|
| 822 |
: LpBase(), MipSolver(), CplexBase(other) {}
|
|
| 823 | 823 |
|
| 824 | 824 |
CplexMip::~CplexMip() {}
|
| 825 | 825 |
|
| 826 | 826 |
CplexMip* CplexMip::newSolver() const { return new CplexMip; }
|
| 827 | 827 |
CplexMip* CplexMip::cloneSolver() const {return new CplexMip(*this); }
|
| 828 | 828 |
|
| 829 | 829 |
const char* CplexMip::_solverName() const { return "CplexMip"; }
|
| 830 | 830 |
|
| 831 | 831 |
void CplexMip::_setColType(int i, CplexMip::ColTypes col_type) {
|
| 832 | 832 |
|
| 833 | 833 |
// Note If a variable is to be changed to binary, a call to CPXchgbds |
| 834 | 834 |
// should also be made to change the bounds to 0 and 1. |
| 835 | 835 |
|
| 836 | 836 |
switch (col_type){
|
| 837 | 837 |
case INTEGER: {
|
| 838 | 838 |
const char t = 'I'; |
| 839 | 839 |
CPXchgctype (cplexEnv(), _prob, 1, &i, &t); |
| 840 | 840 |
} break; |
| 841 | 841 |
case REAL: {
|
| 842 | 842 |
const char t = 'C'; |
| 843 | 843 |
CPXchgctype (cplexEnv(), _prob, 1, &i, &t); |
| 844 | 844 |
} break; |
| 845 | 845 |
default: |
| 846 | 846 |
break; |
| 847 | 847 |
} |
| 848 | 848 |
} |
| 849 | 849 |
|
| 850 | 850 |
CplexMip::ColTypes CplexMip::_getColType(int i) const {
|
| 851 | 851 |
char t; |
| 852 | 852 |
CPXgetctype (cplexEnv(), _prob, &t, i, i); |
| 853 | 853 |
switch (t) {
|
| 854 | 854 |
case 'I': |
| 855 | 855 |
return INTEGER; |
| 856 | 856 |
case 'C': |
| 857 | 857 |
return REAL; |
| 858 | 858 |
default: |
| 859 | 859 |
LEMON_ASSERT(false, "Invalid column type"); |
| 860 | 860 |
return ColTypes(); |
| 861 | 861 |
} |
| 862 | 862 |
|
| 863 | 863 |
} |
| 864 | 864 |
|
| 865 | 865 |
CplexMip::SolveExitStatus CplexMip::_solve() {
|
| 866 | 866 |
int status; |
| 867 | 867 |
status = CPXmipopt (cplexEnv(), _prob); |
| 868 | 868 |
if (status==0) |
| 869 | 869 |
return SOLVED; |
| 870 | 870 |
else |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#ifndef LEMON_CPLEX_H |
| 20 | 20 |
#define LEMON_CPLEX_H |
| 21 | 21 |
|
| 22 | 22 |
///\file |
| 23 | 23 |
///\brief Header of the LEMON-CPLEX lp solver interface. |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/lp_base.h> |
| 26 | 26 |
|
| 27 | 27 |
struct cpxenv; |
| 28 | 28 |
struct cpxlp; |
| 29 | 29 |
|
| 30 | 30 |
namespace lemon {
|
| 31 | 31 |
|
| 32 | 32 |
/// \brief Reference counted wrapper around cpxenv pointer |
| 33 | 33 |
/// |
| 34 | 34 |
/// The cplex uses environment object which is responsible for |
| 35 | 35 |
/// checking the proper license usage. This class provides a simple |
| 36 | 36 |
/// interface for share the environment object between different |
| 37 | 37 |
/// problems. |
| 38 | 38 |
class CplexEnv {
|
| 39 | 39 |
friend class CplexBase; |
| 40 | 40 |
private: |
| 41 | 41 |
cpxenv* _env; |
| 42 | 42 |
mutable int* _cnt; |
| 43 | 43 |
|
| 44 | 44 |
public: |
| 45 | 45 |
|
| 46 | 46 |
/// \brief This exception is thrown when the license check is not |
| 47 | 47 |
/// sufficient |
| 48 | 48 |
class LicenseError : public Exception {
|
| 49 | 49 |
friend class CplexEnv; |
| 50 | 50 |
private: |
| 51 | 51 |
|
| 52 | 52 |
LicenseError(int status); |
| 53 | 53 |
char _message[510]; |
| 54 | 54 |
|
| 55 | 55 |
public: |
| 56 | 56 |
|
| 57 | 57 |
/// The short error message |
| 58 | 58 |
virtual const char* what() const throw() {
|
| 59 | 59 |
return _message; |
| 60 | 60 |
} |
| 61 | 61 |
}; |
| 62 | 62 |
|
| 63 | 63 |
/// Constructor |
| 64 | 64 |
CplexEnv(); |
| 65 | 65 |
/// Shallow copy constructor |
| 66 | 66 |
CplexEnv(const CplexEnv&); |
| 67 | 67 |
/// Shallow assignement |
| 68 | 68 |
CplexEnv& operator=(const CplexEnv&); |
| 69 | 69 |
/// Destructor |
| 70 | 70 |
virtual ~CplexEnv(); |
| 71 | 71 |
|
| 72 | 72 |
protected: |
| 73 | 73 |
|
| 74 | 74 |
cpxenv* cplexEnv() { return _env; }
|
| 75 | 75 |
const cpxenv* cplexEnv() const { return _env; }
|
| 76 | 76 |
}; |
| 77 | 77 |
|
| 78 | 78 |
/// \brief Base interface for the CPLEX LP and MIP solver |
| 79 | 79 |
/// |
| 80 | 80 |
/// This class implements the common interface of the CPLEX LP and |
| 81 |
/// MIP solvers. |
|
| 81 |
/// MIP solvers. |
|
| 82 | 82 |
/// \ingroup lp_group |
| 83 | 83 |
class CplexBase : virtual public LpBase {
|
| 84 | 84 |
protected: |
| 85 | 85 |
|
| 86 | 86 |
CplexEnv _env; |
| 87 | 87 |
cpxlp* _prob; |
| 88 | 88 |
|
| 89 | 89 |
CplexBase(); |
| 90 | 90 |
CplexBase(const CplexEnv&); |
| 91 | 91 |
CplexBase(const CplexBase &); |
| 92 | 92 |
virtual ~CplexBase(); |
| 93 | 93 |
|
| 94 | 94 |
virtual int _addCol(); |
| 95 | 95 |
virtual int _addRow(); |
| 96 | 96 |
|
| 97 | 97 |
virtual void _eraseCol(int i); |
| 98 | 98 |
virtual void _eraseRow(int i); |
| 99 | 99 |
|
| 100 | 100 |
virtual void _eraseColId(int i); |
| 101 | 101 |
virtual void _eraseRowId(int i); |
| 102 | 102 |
|
| 103 | 103 |
virtual void _getColName(int col, std::string& name) const; |
| 104 | 104 |
virtual void _setColName(int col, const std::string& name); |
| 105 | 105 |
virtual int _colByName(const std::string& name) const; |
| 106 | 106 |
|
| 107 | 107 |
virtual void _getRowName(int row, std::string& name) const; |
| 108 | 108 |
virtual void _setRowName(int row, const std::string& name); |
| 109 | 109 |
virtual int _rowByName(const std::string& name) const; |
| 110 | 110 |
|
| 111 | 111 |
virtual void _setRowCoeffs(int i, ExprIterator b, ExprIterator e); |
| 112 | 112 |
virtual void _getRowCoeffs(int i, InsertIterator b) const; |
| 113 | 113 |
|
| 114 | 114 |
virtual void _setColCoeffs(int i, ExprIterator b, ExprIterator e); |
| 115 | 115 |
virtual void _getColCoeffs(int i, InsertIterator b) const; |
| 116 | 116 |
|
| 117 | 117 |
virtual void _setCoeff(int row, int col, Value value); |
| 118 | 118 |
virtual Value _getCoeff(int row, int col) const; |
| 119 | 119 |
|
| 120 | 120 |
virtual void _setColLowerBound(int i, Value value); |
| 121 | 121 |
virtual Value _getColLowerBound(int i) const; |
| 122 | 122 |
|
| 123 | 123 |
virtual void _setColUpperBound(int i, Value value); |
| 124 | 124 |
virtual Value _getColUpperBound(int i) const; |
| 125 | 125 |
|
| 126 | 126 |
private: |
| 127 | 127 |
void _set_row_bounds(int i, Value lb, Value ub); |
| 128 | 128 |
protected: |
| 129 | 129 |
|
| ... | ... |
@@ -190,69 +190,72 @@ |
| 190 | 190 |
SolveExitStatus convertStatus(int status); |
| 191 | 191 |
|
| 192 | 192 |
protected: |
| 193 | 193 |
|
| 194 | 194 |
virtual const char* _solverName() const; |
| 195 | 195 |
|
| 196 | 196 |
virtual SolveExitStatus _solve(); |
| 197 | 197 |
virtual Value _getPrimal(int i) const; |
| 198 | 198 |
virtual Value _getDual(int i) const; |
| 199 | 199 |
virtual Value _getPrimalValue() const; |
| 200 | 200 |
|
| 201 | 201 |
virtual VarStatus _getColStatus(int i) const; |
| 202 | 202 |
virtual VarStatus _getRowStatus(int i) const; |
| 203 | 203 |
|
| 204 | 204 |
virtual Value _getPrimalRay(int i) const; |
| 205 | 205 |
virtual Value _getDualRay(int i) const; |
| 206 | 206 |
|
| 207 | 207 |
virtual ProblemType _getPrimalType() const; |
| 208 | 208 |
virtual ProblemType _getDualType() const; |
| 209 | 209 |
|
| 210 | 210 |
public: |
| 211 | 211 |
|
| 212 | 212 |
/// Solve with primal simplex method |
| 213 | 213 |
SolveExitStatus solvePrimal(); |
| 214 | 214 |
|
| 215 | 215 |
/// Solve with dual simplex method |
| 216 | 216 |
SolveExitStatus solveDual(); |
| 217 | 217 |
|
| 218 | 218 |
/// Solve with barrier method |
| 219 | 219 |
SolveExitStatus solveBarrier(); |
| 220 | 220 |
|
| 221 | 221 |
}; |
| 222 | 222 |
|
| 223 | 223 |
/// \brief Interface for the CPLEX MIP solver |
| 224 | 224 |
/// |
| 225 | 225 |
/// This class implements an interface for the CPLEX MIP solver. |
| 226 | 226 |
///\ingroup lp_group |
| 227 | 227 |
class CplexMip : public MipSolver, public CplexBase {
|
| 228 | 228 |
public: |
| 229 | 229 |
/// \e |
| 230 | 230 |
CplexMip(); |
| 231 | 231 |
/// \e |
| 232 | 232 |
CplexMip(const CplexEnv&); |
| 233 | 233 |
/// \e |
| 234 | 234 |
CplexMip(const CplexMip&); |
| 235 | 235 |
/// \e |
| 236 | 236 |
virtual ~CplexMip(); |
| 237 | 237 |
|
| 238 |
/// \e |
|
| 239 |
virtual CplexMip* cloneSolver() const; |
|
| 240 |
/// \e |
|
| 241 |
virtual CplexMip* newSolver() const; |
|
| 242 |
|
|
| 238 | 243 |
protected: |
| 239 | 244 |
|
| 240 |
virtual CplexMip* _cloneSolver() const; |
|
| 241 |
virtual CplexMip* _newSolver() const; |
|
| 242 | 245 |
|
| 243 | 246 |
virtual const char* _solverName() const; |
| 244 | 247 |
|
| 245 | 248 |
virtual ColTypes _getColType(int col) const; |
| 246 | 249 |
virtual void _setColType(int col, ColTypes col_type); |
| 247 | 250 |
|
| 248 | 251 |
virtual SolveExitStatus _solve(); |
| 249 | 252 |
virtual ProblemType _getType() const; |
| 250 | 253 |
virtual Value _getSol(int i) const; |
| 251 | 254 |
virtual Value _getSolValue() const; |
| 252 | 255 |
|
| 253 | 256 |
}; |
| 254 | 257 |
|
| 255 | 258 |
} //END OF NAMESPACE LEMON |
| 256 | 259 |
|
| 257 | 260 |
#endif //LEMON_CPLEX_H |
| 258 | 261 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
///\file |
| 20 | 20 |
///\brief Implementation of the LEMON GLPK LP and MIP solver interface. |
| 21 | 21 |
|
| 22 | 22 |
#include <lemon/glpk.h> |
| 23 | 23 |
#include <glpk.h> |
| 24 | 24 |
|
| 25 | 25 |
#include <lemon/assert.h> |
| 26 | 26 |
|
| 27 | 27 |
namespace lemon {
|
| 28 | 28 |
|
| 29 | 29 |
// GlpkBase members |
| 30 | 30 |
|
| 31 | 31 |
GlpkBase::GlpkBase() : LpBase() {
|
| 32 | 32 |
lp = glp_create_prob(); |
| 33 | 33 |
glp_create_index(lp); |
| 34 | 34 |
} |
| 35 | 35 |
|
| 36 | 36 |
GlpkBase::GlpkBase(const GlpkBase &other) : LpBase() {
|
| 37 | 37 |
lp = glp_create_prob(); |
| 38 | 38 |
glp_copy_prob(lp, other.lp, GLP_ON); |
| 39 | 39 |
glp_create_index(lp); |
| 40 | 40 |
rows = other.rows; |
| 41 | 41 |
cols = other.cols; |
| 42 | 42 |
} |
| 43 | 43 |
|
| 44 | 44 |
GlpkBase::~GlpkBase() {
|
| 45 | 45 |
glp_delete_prob(lp); |
| 46 | 46 |
} |
| 47 | 47 |
|
| 48 | 48 |
int GlpkBase::_addCol() {
|
| 49 | 49 |
int i = glp_add_cols(lp, 1); |
| 50 | 50 |
glp_set_col_bnds(lp, i, GLP_FR, 0.0, 0.0); |
| 51 | 51 |
return i; |
| 52 | 52 |
} |
| 53 | 53 |
|
| ... | ... |
@@ -486,102 +486,102 @@ |
| 486 | 486 |
void GlpkBase::_setObjCoeff(int i, Value obj_coef) {
|
| 487 | 487 |
//i = 0 means the constant term (shift) |
| 488 | 488 |
glp_set_obj_coef(lp, i, obj_coef); |
| 489 | 489 |
} |
| 490 | 490 |
|
| 491 | 491 |
GlpkBase::Value GlpkBase::_getObjCoeff(int i) const {
|
| 492 | 492 |
//i = 0 means the constant term (shift) |
| 493 | 493 |
return glp_get_obj_coef(lp, i); |
| 494 | 494 |
} |
| 495 | 495 |
|
| 496 | 496 |
void GlpkBase::_setSense(GlpkBase::Sense sense) {
|
| 497 | 497 |
switch (sense) {
|
| 498 | 498 |
case MIN: |
| 499 | 499 |
glp_set_obj_dir(lp, GLP_MIN); |
| 500 | 500 |
break; |
| 501 | 501 |
case MAX: |
| 502 | 502 |
glp_set_obj_dir(lp, GLP_MAX); |
| 503 | 503 |
break; |
| 504 | 504 |
} |
| 505 | 505 |
} |
| 506 | 506 |
|
| 507 | 507 |
GlpkBase::Sense GlpkBase::_getSense() const {
|
| 508 | 508 |
switch(glp_get_obj_dir(lp)) {
|
| 509 | 509 |
case GLP_MIN: |
| 510 | 510 |
return MIN; |
| 511 | 511 |
case GLP_MAX: |
| 512 | 512 |
return MAX; |
| 513 | 513 |
default: |
| 514 | 514 |
LEMON_ASSERT(false, "Wrong sense"); |
| 515 | 515 |
return GlpkBase::Sense(); |
| 516 | 516 |
} |
| 517 | 517 |
} |
| 518 | 518 |
|
| 519 | 519 |
void GlpkBase::_clear() {
|
| 520 | 520 |
glp_erase_prob(lp); |
| 521 | 521 |
rows.clear(); |
| 522 | 522 |
cols.clear(); |
| 523 | 523 |
} |
| 524 | 524 |
|
| 525 | 525 |
void GlpkBase::freeEnv() {
|
| 526 | 526 |
glp_free_env(); |
| 527 | 527 |
} |
| 528 | 528 |
|
| 529 | 529 |
GlpkBase::FreeEnvHelper GlpkBase::freeEnvHelper; |
| 530 | 530 |
|
| 531 | 531 |
// GlpkLp members |
| 532 | 532 |
|
| 533 | 533 |
GlpkLp::GlpkLp() |
| 534 |
: LpBase(), |
|
| 534 |
: LpBase(), LpSolver(), GlpkBase() {
|
|
| 535 | 535 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 536 | 536 |
} |
| 537 | 537 |
|
| 538 | 538 |
GlpkLp::GlpkLp(const GlpkLp& other) |
| 539 |
: LpBase(other), |
|
| 539 |
: LpBase(other), LpSolver(other), GlpkBase(other) {
|
|
| 540 | 540 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 541 | 541 |
} |
| 542 | 542 |
|
| 543 | 543 |
GlpkLp* GlpkLp::newSolver() const { return new GlpkLp; }
|
| 544 | 544 |
GlpkLp* GlpkLp::cloneSolver() const { return new GlpkLp(*this); }
|
| 545 | 545 |
|
| 546 | 546 |
const char* GlpkLp::_solverName() const { return "GlpkLp"; }
|
| 547 | 547 |
|
| 548 | 548 |
void GlpkLp::_clear_temporals() {
|
| 549 | 549 |
_primal_ray.clear(); |
| 550 | 550 |
_dual_ray.clear(); |
| 551 | 551 |
} |
| 552 | 552 |
|
| 553 | 553 |
GlpkLp::SolveExitStatus GlpkLp::_solve() {
|
| 554 | 554 |
return solvePrimal(); |
| 555 | 555 |
} |
| 556 | 556 |
|
| 557 | 557 |
GlpkLp::SolveExitStatus GlpkLp::solvePrimal() {
|
| 558 | 558 |
_clear_temporals(); |
| 559 | 559 |
|
| 560 | 560 |
glp_smcp smcp; |
| 561 | 561 |
glp_init_smcp(&smcp); |
| 562 | 562 |
|
| 563 | 563 |
switch (_message_level) {
|
| 564 | 564 |
case MESSAGE_NO_OUTPUT: |
| 565 | 565 |
smcp.msg_lev = GLP_MSG_OFF; |
| 566 | 566 |
break; |
| 567 | 567 |
case MESSAGE_ERROR_MESSAGE: |
| 568 | 568 |
smcp.msg_lev = GLP_MSG_ERR; |
| 569 | 569 |
break; |
| 570 | 570 |
case MESSAGE_NORMAL_OUTPUT: |
| 571 | 571 |
smcp.msg_lev = GLP_MSG_ON; |
| 572 | 572 |
break; |
| 573 | 573 |
case MESSAGE_FULL_OUTPUT: |
| 574 | 574 |
smcp.msg_lev = GLP_MSG_ALL; |
| 575 | 575 |
break; |
| 576 | 576 |
} |
| 577 | 577 |
|
| 578 | 578 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
| 579 | 579 |
return SOLVED; |
| 580 | 580 |
} |
| 581 | 581 |
|
| 582 | 582 |
GlpkLp::SolveExitStatus GlpkLp::solveDual() {
|
| 583 | 583 |
_clear_temporals(); |
| 584 | 584 |
|
| 585 | 585 |
glp_smcp smcp; |
| 586 | 586 |
glp_init_smcp(&smcp); |
| 587 | 587 |
|
| ... | ... |
@@ -785,102 +785,102 @@ |
| 785 | 785 |
return UNDEFINED; |
| 786 | 786 |
case GLP_FEAS: |
| 787 | 787 |
case GLP_INFEAS: |
| 788 | 788 |
if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
|
| 789 | 789 |
return UNBOUNDED; |
| 790 | 790 |
} else {
|
| 791 | 791 |
return UNDEFINED; |
| 792 | 792 |
} |
| 793 | 793 |
case GLP_NOFEAS: |
| 794 | 794 |
return INFEASIBLE; |
| 795 | 795 |
default: |
| 796 | 796 |
LEMON_ASSERT(false, "Wrong primal type"); |
| 797 | 797 |
return GlpkLp::ProblemType(); |
| 798 | 798 |
} |
| 799 | 799 |
} |
| 800 | 800 |
|
| 801 | 801 |
GlpkLp::ProblemType GlpkLp::_getDualType() const {
|
| 802 | 802 |
if (glp_get_status(lp) == GLP_OPT) |
| 803 | 803 |
return OPTIMAL; |
| 804 | 804 |
switch (glp_get_dual_stat(lp)) {
|
| 805 | 805 |
case GLP_UNDEF: |
| 806 | 806 |
return UNDEFINED; |
| 807 | 807 |
case GLP_FEAS: |
| 808 | 808 |
case GLP_INFEAS: |
| 809 | 809 |
if (glp_get_prim_stat(lp) == GLP_NOFEAS) {
|
| 810 | 810 |
return UNBOUNDED; |
| 811 | 811 |
} else {
|
| 812 | 812 |
return UNDEFINED; |
| 813 | 813 |
} |
| 814 | 814 |
case GLP_NOFEAS: |
| 815 | 815 |
return INFEASIBLE; |
| 816 | 816 |
default: |
| 817 | 817 |
LEMON_ASSERT(false, "Wrong primal type"); |
| 818 | 818 |
return GlpkLp::ProblemType(); |
| 819 | 819 |
} |
| 820 | 820 |
} |
| 821 | 821 |
|
| 822 | 822 |
void GlpkLp::presolver(bool b) {
|
| 823 | 823 |
lpx_set_int_parm(lp, LPX_K_PRESOL, b ? 1 : 0); |
| 824 | 824 |
} |
| 825 | 825 |
|
| 826 | 826 |
void GlpkLp::messageLevel(MessageLevel m) {
|
| 827 | 827 |
_message_level = m; |
| 828 | 828 |
} |
| 829 | 829 |
|
| 830 | 830 |
// GlpkMip members |
| 831 | 831 |
|
| 832 | 832 |
GlpkMip::GlpkMip() |
| 833 |
: LpBase(), |
|
| 833 |
: LpBase(), MipSolver(), GlpkBase() {
|
|
| 834 | 834 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 835 | 835 |
} |
| 836 | 836 |
|
| 837 | 837 |
GlpkMip::GlpkMip(const GlpkMip& other) |
| 838 |
: LpBase(), |
|
| 838 |
: LpBase(), MipSolver(), GlpkBase(other) {
|
|
| 839 | 839 |
messageLevel(MESSAGE_NO_OUTPUT); |
| 840 | 840 |
} |
| 841 | 841 |
|
| 842 | 842 |
void GlpkMip::_setColType(int i, GlpkMip::ColTypes col_type) {
|
| 843 | 843 |
switch (col_type) {
|
| 844 | 844 |
case INTEGER: |
| 845 | 845 |
glp_set_col_kind(lp, i, GLP_IV); |
| 846 | 846 |
break; |
| 847 | 847 |
case REAL: |
| 848 | 848 |
glp_set_col_kind(lp, i, GLP_CV); |
| 849 | 849 |
break; |
| 850 | 850 |
} |
| 851 | 851 |
} |
| 852 | 852 |
|
| 853 | 853 |
GlpkMip::ColTypes GlpkMip::_getColType(int i) const {
|
| 854 | 854 |
switch (glp_get_col_kind(lp, i)) {
|
| 855 | 855 |
case GLP_IV: |
| 856 | 856 |
case GLP_BV: |
| 857 | 857 |
return INTEGER; |
| 858 | 858 |
default: |
| 859 | 859 |
return REAL; |
| 860 | 860 |
} |
| 861 | 861 |
|
| 862 | 862 |
} |
| 863 | 863 |
|
| 864 | 864 |
GlpkMip::SolveExitStatus GlpkMip::_solve() {
|
| 865 | 865 |
glp_smcp smcp; |
| 866 | 866 |
glp_init_smcp(&smcp); |
| 867 | 867 |
|
| 868 | 868 |
switch (_message_level) {
|
| 869 | 869 |
case MESSAGE_NO_OUTPUT: |
| 870 | 870 |
smcp.msg_lev = GLP_MSG_OFF; |
| 871 | 871 |
break; |
| 872 | 872 |
case MESSAGE_ERROR_MESSAGE: |
| 873 | 873 |
smcp.msg_lev = GLP_MSG_ERR; |
| 874 | 874 |
break; |
| 875 | 875 |
case MESSAGE_NORMAL_OUTPUT: |
| 876 | 876 |
smcp.msg_lev = GLP_MSG_ON; |
| 877 | 877 |
break; |
| 878 | 878 |
case MESSAGE_FULL_OUTPUT: |
| 879 | 879 |
smcp.msg_lev = GLP_MSG_ALL; |
| 880 | 880 |
break; |
| 881 | 881 |
} |
| 882 | 882 |
smcp.meth = GLP_DUAL; |
| 883 | 883 |
|
| 884 | 884 |
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED; |
| 885 | 885 |
if (glp_get_status(lp) != GLP_OPT) return SOLVED; |
| 886 | 886 |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include <sstream> |
| 20 | 20 |
#include <lemon/lp_skeleton.h> |
| 21 | 21 |
#include "test_tools.h" |
| 22 | 22 |
#include <lemon/tolerance.h> |
| 23 | 23 |
|
| 24 | 24 |
#ifdef HAVE_CONFIG_H |
| 25 | 25 |
#include <lemon/config.h> |
| 26 | 26 |
#endif |
| 27 | 27 |
|
| 28 | 28 |
#ifdef HAVE_GLPK |
| 29 | 29 |
#include <lemon/glpk.h> |
| 30 | 30 |
#endif |
| 31 | 31 |
|
| 32 | 32 |
#ifdef HAVE_CPLEX |
| 33 | 33 |
#include <lemon/cplex.h> |
| 34 | 34 |
#endif |
| 35 | 35 |
|
| 36 | 36 |
#ifdef HAVE_SOPLEX |
| 37 | 37 |
#include <lemon/soplex.h> |
| 38 | 38 |
#endif |
| 39 | 39 |
|
| 40 | 40 |
#ifdef HAVE_CLP |
| 41 | 41 |
#include <lemon/clp.h> |
| 42 | 42 |
#endif |
| 43 | 43 |
|
| 44 | 44 |
using namespace lemon; |
| 45 | 45 |
|
| 46 | 46 |
void lpTest(LpSolver& lp) |
| 47 | 47 |
{
|
| 48 | 48 |
|
| 49 | 49 |
typedef LpSolver LP; |
| 50 | 50 |
|
| 51 | 51 |
std::vector<LP::Col> x(10); |
| 52 | 52 |
// for(int i=0;i<10;i++) x.push_back(lp.addCol()); |
| 53 | 53 |
lp.addColSet(x); |
| ... | ... |
@@ -320,107 +320,107 @@ |
| 320 | 320 |
"The lower bound for variable x1 should be 0."); |
| 321 | 321 |
check(clp->colUpperBound(x1)==LpSolver::INF, |
| 322 | 322 |
"The upper bound for variable x1 should be infty."); |
| 323 | 323 |
|
| 324 | 324 |
check(lp.rowLowerBound(upright)==-LpSolver::INF, |
| 325 | 325 |
"The lower bound for the first row should be -infty."); |
| 326 | 326 |
check(lp.rowUpperBound(upright)==1, |
| 327 | 327 |
"The upper bound for the first row should be 1."); |
| 328 | 328 |
e = clp->row(upright); |
| 329 | 329 |
check(e[x1] == 1, "The first coefficient should 1."); |
| 330 | 330 |
check(e[x2] == 1, "The second coefficient should 1."); |
| 331 | 331 |
|
| 332 | 332 |
de = clp->col(x1); |
| 333 | 333 |
check(de[upright] == 1, "The first coefficient should 1."); |
| 334 | 334 |
|
| 335 | 335 |
delete clp; |
| 336 | 336 |
|
| 337 | 337 |
//Maximization of x1+x2 |
| 338 | 338 |
//over the triangle with vertices (0,0) (0,1) (1,0) |
| 339 | 339 |
double expected_opt=1; |
| 340 | 340 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 341 | 341 |
|
| 342 | 342 |
//Minimization |
| 343 | 343 |
lp.sense(lp.MIN); |
| 344 | 344 |
expected_opt=0; |
| 345 | 345 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 346 | 346 |
|
| 347 | 347 |
//Vertex (-1,0) instead of (0,0) |
| 348 | 348 |
lp.colLowerBound(x1, -LpSolver::INF); |
| 349 | 349 |
expected_opt=-1; |
| 350 | 350 |
solveAndCheck(lp, LpSolver::OPTIMAL, expected_opt); |
| 351 | 351 |
|
| 352 | 352 |
//Erase one constraint and return to maximization |
| 353 | 353 |
lp.erase(upright); |
| 354 | 354 |
lp.sense(lp.MAX); |
| 355 | 355 |
expected_opt=LpSolver::INF; |
| 356 | 356 |
solveAndCheck(lp, LpSolver::UNBOUNDED, expected_opt); |
| 357 | 357 |
|
| 358 | 358 |
//Infeasibilty |
| 359 | 359 |
lp.addRow(x1+x2 <=-2); |
| 360 | 360 |
solveAndCheck(lp, LpSolver::INFEASIBLE, expected_opt); |
| 361 | 361 |
|
| 362 | 362 |
} |
| 363 | 363 |
|
| 364 | 364 |
template<class LP> |
| 365 | 365 |
void cloneTest() |
| 366 | 366 |
{
|
| 367 | 367 |
//Test for clone/new |
| 368 |
|
|
| 368 |
|
|
| 369 | 369 |
LP* lp = new LP(); |
| 370 | 370 |
LP* lpnew = lp->newSolver(); |
| 371 | 371 |
LP* lpclone = lp->cloneSolver(); |
| 372 | 372 |
delete lp; |
| 373 | 373 |
delete lpnew; |
| 374 | 374 |
delete lpclone; |
| 375 | 375 |
} |
| 376 | 376 |
|
| 377 | 377 |
int main() |
| 378 | 378 |
{
|
| 379 | 379 |
LpSkeleton lp_skel; |
| 380 | 380 |
lpTest(lp_skel); |
| 381 | 381 |
|
| 382 | 382 |
#ifdef HAVE_GLPK |
| 383 | 383 |
{
|
| 384 | 384 |
GlpkLp lp_glpk1,lp_glpk2; |
| 385 | 385 |
lpTest(lp_glpk1); |
| 386 | 386 |
aTest(lp_glpk2); |
| 387 | 387 |
cloneTest<GlpkLp>(); |
| 388 | 388 |
} |
| 389 | 389 |
#endif |
| 390 | 390 |
|
| 391 | 391 |
#ifdef HAVE_CPLEX |
| 392 | 392 |
try {
|
| 393 | 393 |
CplexLp lp_cplex1,lp_cplex2; |
| 394 | 394 |
lpTest(lp_cplex1); |
| 395 | 395 |
aTest(lp_cplex2); |
| 396 |
cloneTest<CplexLp>(); |
|
| 396 | 397 |
} catch (CplexEnv::LicenseError& error) {
|
| 397 | 398 |
#ifdef LEMON_FORCE_CPLEX_CHECK |
| 398 | 399 |
check(false, error.what()); |
| 399 | 400 |
#else |
| 400 | 401 |
std::cerr << error.what() << std::endl; |
| 401 | 402 |
std::cerr << "Cplex license check failed, lp check skipped" << std::endl; |
| 402 | 403 |
#endif |
| 403 | 404 |
} |
| 404 |
cloneTest<CplexLp>(); |
|
| 405 | 405 |
#endif |
| 406 | 406 |
|
| 407 | 407 |
#ifdef HAVE_SOPLEX |
| 408 | 408 |
{
|
| 409 | 409 |
SoplexLp lp_soplex1,lp_soplex2; |
| 410 | 410 |
lpTest(lp_soplex1); |
| 411 | 411 |
aTest(lp_soplex2); |
| 412 | 412 |
cloneTest<SoplexLp>(); |
| 413 | 413 |
} |
| 414 | 414 |
#endif |
| 415 | 415 |
|
| 416 | 416 |
#ifdef HAVE_CLP |
| 417 | 417 |
{
|
| 418 | 418 |
ClpLp lp_clp1,lp_clp2; |
| 419 | 419 |
lpTest(lp_clp1); |
| 420 | 420 |
aTest(lp_clp2); |
| 421 | 421 |
cloneTest<ClpLp>(); |
| 422 | 422 |
} |
| 423 | 423 |
#endif |
| 424 | 424 |
|
| 425 | 425 |
return 0; |
| 426 | 426 |
} |
| 1 | 1 |
/* -*- mode: C++; indent-tabs-mode: nil; -*- |
| 2 | 2 |
* |
| 3 | 3 |
* This file is a part of LEMON, a generic C++ optimization library. |
| 4 | 4 |
* |
| 5 |
* Copyright (C) 2003- |
|
| 5 |
* Copyright (C) 2003-2009 |
|
| 6 | 6 |
* Egervary Jeno Kombinatorikus Optimalizalasi Kutatocsoport |
| 7 | 7 |
* (Egervary Research Group on Combinatorial Optimization, EGRES). |
| 8 | 8 |
* |
| 9 | 9 |
* Permission to use, modify and distribute this software is granted |
| 10 | 10 |
* provided that this copyright notice appears in all copies. For |
| 11 | 11 |
* precise terms see the accompanying LICENSE file. |
| 12 | 12 |
* |
| 13 | 13 |
* This software is provided "AS IS" with no warranty of any kind, |
| 14 | 14 |
* express or implied, and with no claim as to its suitability for any |
| 15 | 15 |
* purpose. |
| 16 | 16 |
* |
| 17 | 17 |
*/ |
| 18 | 18 |
|
| 19 | 19 |
#include "test_tools.h" |
| 20 | 20 |
|
| 21 | 21 |
|
| 22 | 22 |
#ifdef HAVE_CONFIG_H |
| 23 | 23 |
#include <lemon/config.h> |
| 24 | 24 |
#endif |
| 25 | 25 |
|
| 26 | 26 |
#ifdef HAVE_CPLEX |
| 27 | 27 |
#include <lemon/cplex.h> |
| 28 | 28 |
#endif |
| 29 | 29 |
|
| 30 | 30 |
#ifdef HAVE_GLPK |
| 31 | 31 |
#include <lemon/glpk.h> |
| 32 | 32 |
#endif |
| 33 | 33 |
|
| 34 | 34 |
|
| 35 | 35 |
using namespace lemon; |
| 36 | 36 |
|
| 37 | 37 |
void solveAndCheck(MipSolver& mip, MipSolver::ProblemType stat, |
| 38 | 38 |
double exp_opt) {
|
| 39 | 39 |
using std::string; |
| 40 | 40 |
|
| 41 | 41 |
mip.solve(); |
| 42 | 42 |
//int decimal,sign; |
| 43 | 43 |
std::ostringstream buf; |
| 44 | 44 |
buf << "Type should be: " << int(stat)<<" and it is "<<int(mip.type()); |
| 45 | 45 |
|
| 46 | 46 |
|
| 47 | 47 |
// itoa(stat,buf1, 10); |
| 48 | 48 |
check(mip.type()==stat, buf.str()); |
| 49 | 49 |
|
| 50 | 50 |
if (stat == MipSolver::OPTIMAL) {
|
| 51 | 51 |
std::ostringstream sbuf; |
| 52 | 52 |
buf << "Wrong optimal value: the right optimum is " << exp_opt; |
| 53 | 53 |
check(std::abs(mip.solValue()-exp_opt) < 1e-3, sbuf.str()); |
| ... | ... |
@@ -64,86 +64,86 @@ |
| 64 | 64 |
typedef MipSolver::Col Col; |
| 65 | 65 |
|
| 66 | 66 |
|
| 67 | 67 |
|
| 68 | 68 |
Col x1 = mip.addCol(); |
| 69 | 69 |
Col x2 = mip.addCol(); |
| 70 | 70 |
|
| 71 | 71 |
|
| 72 | 72 |
//Objective function |
| 73 | 73 |
mip.obj(x1); |
| 74 | 74 |
|
| 75 | 75 |
mip.max(); |
| 76 | 76 |
|
| 77 | 77 |
|
| 78 | 78 |
//Unconstrained optimization |
| 79 | 79 |
mip.solve(); |
| 80 | 80 |
//Check it out! |
| 81 | 81 |
|
| 82 | 82 |
//Constraints |
| 83 | 83 |
mip.addRow(2*x1+x2 <=2); |
| 84 | 84 |
mip.addRow(x1-2*x2 <=0); |
| 85 | 85 |
|
| 86 | 86 |
//Nonnegativity of the variable x1 |
| 87 | 87 |
mip.colLowerBound(x1, 0); |
| 88 | 88 |
|
| 89 | 89 |
//Maximization of x1 |
| 90 | 90 |
//over the triangle with vertices (0,0),(4/5,2/5),(0,2) |
| 91 | 91 |
double expected_opt=4.0/5.0; |
| 92 | 92 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 93 | 93 |
|
| 94 | 94 |
//Restrict x2 to integer |
| 95 | 95 |
mip.colType(x2,MipSolver::INTEGER); |
| 96 | 96 |
expected_opt=1.0/2.0; |
| 97 | 97 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 98 | 98 |
|
| 99 | 99 |
|
| 100 | 100 |
//Restrict both to integer |
| 101 | 101 |
mip.colType(x1,MipSolver::INTEGER); |
| 102 | 102 |
expected_opt=0; |
| 103 | 103 |
solveAndCheck(mip, MipSolver::OPTIMAL, expected_opt); |
| 104 | 104 |
|
| 105 | 105 |
|
| 106 | 106 |
|
| 107 | 107 |
} |
| 108 | 108 |
|
| 109 | 109 |
template<class MIP> |
| 110 | 110 |
void cloneTest() |
| 111 | 111 |
{
|
| 112 |
|
|
| 112 |
|
|
| 113 | 113 |
MIP* mip = new MIP(); |
| 114 | 114 |
MIP* mipnew = mip->newSolver(); |
| 115 | 115 |
MIP* mipclone = mip->cloneSolver(); |
| 116 | 116 |
delete mip; |
| 117 | 117 |
delete mipnew; |
| 118 | 118 |
delete mipclone; |
| 119 | 119 |
} |
| 120 | 120 |
|
| 121 | 121 |
int main() |
| 122 | 122 |
{
|
| 123 | 123 |
|
| 124 | 124 |
#ifdef HAVE_GLPK |
| 125 | 125 |
{
|
| 126 | 126 |
GlpkMip mip1; |
| 127 | 127 |
aTest(mip1); |
| 128 | 128 |
cloneTest<GlpkMip>(); |
| 129 | 129 |
} |
| 130 | 130 |
#endif |
| 131 | 131 |
|
| 132 | 132 |
#ifdef HAVE_CPLEX |
| 133 | 133 |
try {
|
| 134 | 134 |
CplexMip mip2; |
| 135 | 135 |
aTest(mip2); |
| 136 |
cloneTest<CplexMip>(); |
|
| 136 | 137 |
} catch (CplexEnv::LicenseError& error) {
|
| 137 | 138 |
#ifdef LEMON_FORCE_CPLEX_CHECK |
| 138 | 139 |
check(false, error.what()); |
| 139 | 140 |
#else |
| 140 | 141 |
std::cerr << error.what() << std::endl; |
| 141 | 142 |
std::cerr << "Cplex license check failed, lp check skipped" << std::endl; |
| 142 | 143 |
#endif |
| 143 | 144 |
} |
| 144 |
cloneTest<CplexMip>(); |
|
| 145 | 145 |
#endif |
| 146 | 146 |
|
| 147 | 147 |
return 0; |
| 148 | 148 |
|
| 149 | 149 |
} |
0 comments (0 inline)