1 #ifndef OPENGM_LP_SOLVER_CPLEX_HXX_ 2 #define OPENGM_LP_SOLVER_CPLEX_HXX_ 6 #include <ilcplex/ilocplex.h> 28 const IloNum&
operator[](
const IloInt n)
const;
70 static CplexValueType infinity_impl();
73 void addContinuousVariables_impl(
const CplexIndexType numVariables,
const CplexValueType lowerBound,
const CplexValueType upperBound);
74 void addIntegerVariables_impl(
const CplexIndexType numVariables,
const CplexValueType lowerBound,
const CplexValueType upperBound);
75 void addBinaryVariables_impl(
const CplexIndexType numVariables);
78 void setObjective_impl(
const Objective objective);
79 void setObjectiveValue_impl(
const CplexIndexType variable,
const CplexValueType value);
80 template<
class ITERATOR_TYPE>
81 void setObjectiveValue_impl(ITERATOR_TYPE begin,
const ITERATOR_TYPE end);
82 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
83 void setObjectiveValue_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin);
86 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
87 void addEqualityConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin,
const CplexValueType bound,
const std::string& constraintName =
"");
88 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
89 void addLessEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin,
const CplexValueType bound,
const std::string& constraintName =
"");
90 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
91 void addGreaterEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin,
const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin,
const CplexValueType bound,
const std::string& constraintName =
"");
93 void addConstraintsFinished_impl();
94 void addConstraintsFinished_impl(CplexTimingType& timing);
97 template <
class PARAMETER_TYPE,
class PARAMETER_VALUE_TYPE>
98 void setParameter_impl(
const PARAMETER_TYPE parameter,
const PARAMETER_VALUE_TYPE value);
102 bool solve_impl(CplexTimingType& timing);
105 CplexSolutionIteratorType solutionBegin_impl()
const;
106 CplexSolutionIteratorType solutionEnd_impl()
const;
107 CplexValueType solution_impl(
const CplexIndexType variable)
const;
109 CplexValueType objectiveFunctionValue_impl()
const;
110 CplexValueType objectiveFunctionValueBound_impl()
const;
113 void exportModel_impl(
const std::string& filename)
const;
116 void updateSolution()
const;
630 return static_cast<difference_type
>(
position_) - static_cast<difference_type>(iter.
position_);
635 cplexModel_(cplexEnvironment_), cplexVariables_(cplexEnvironment_),
636 cplexObjective_(cplexEnvironment_), cplexConstraints_(cplexEnvironment_),
637 cplexSolution_(cplexEnvironment_), cplexSolutionValid_(false),
644 }
catch(
const IloException& e) {
645 std::cout << e << std::endl;
646 throw std::runtime_error(
"CPLEX exception");
714 throw std::runtime_error(
"Unknown Root Algorithm");
749 throw std::runtime_error(
"Unknown Node Algorithm");
776 throw std::runtime_error(
"Unknown Presolve Option");
803 throw std::runtime_error(
"Unknown MIP Emphasis Option");
859 }
catch(
const IloException& e) {
860 std::cout << e << std::endl;
861 throw std::runtime_error(
"CPLEX exception");
877 }
catch(
const IloException& e) {
878 std::cout << e << std::endl;
879 throw std::runtime_error(
"CPLEX exception");
887 }
catch(
const IloException& e) {
888 std::cout << e << std::endl;
889 throw std::runtime_error(
"CPLEX exception");
897 }
catch(
const IloException& e) {
898 std::cout << e << std::endl;
899 throw std::runtime_error(
"CPLEX exception");
908 }
catch(
const IloException& e) {
909 std::cout << e << std::endl;
910 throw std::runtime_error(
"CPLEX exception");
917 }
catch(
const IloException& e) {
918 std::cout << e << std::endl;
919 throw std::runtime_error(
"CPLEX exception");
924 throw std::runtime_error(
"Unknown Objective");
932 }
catch(
const IloException& e) {
933 std::cout << e << std::endl;
934 throw std::runtime_error(
"CPLEX exception");
938 template<
class ITERATOR_TYPE>
940 const CplexIndexType numObjectiveVariables = std::distance(begin, end);
945 objective[i] = *begin;
951 }
catch(
const IloException& e) {
952 std::cout << e << std::endl;
953 throw std::runtime_error(
"CPLEX exception");
957 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
959 const CplexIndexType numObjectiveVariables = std::distance(variableIDsBegin, variableIDsEnd);
965 objective[i] = *coefficientsBegin;
973 }
catch(
const IloException& e) {
974 std::cout << e << std::endl;
975 throw std::runtime_error(
"CPLEX exception");
979 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
982 while(variableIDsBegin != variableIDsEnd) {
983 constraint.setLinearCoef(
cplexVariables_[*variableIDsBegin], *coefficientsBegin);
990 }
catch(
const IloException& e) {
991 std::cout << e << std::endl;
992 throw std::runtime_error(
"CPLEX exception");
996 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
998 IloRange constraint(
cplexEnvironment_, -IloInfinity, bound, constraintName.c_str());
999 while(variableIDsBegin != variableIDsEnd) {
1000 constraint.setLinearCoef(
cplexVariables_[*variableIDsBegin], *coefficientsBegin);
1002 ++coefficientsBegin;
1007 }
catch(
const IloException& e) {
1008 std::cout << e << std::endl;
1009 throw std::runtime_error(
"CPLEX exception");
1013 template<
class VARIABLES_ITERATOR_TYPE,
class COEFFICIENTS_ITERATOR_TYPE>
1015 IloRange constraint(
cplexEnvironment_, bound, IloInfinity, constraintName.c_str());
1016 while(variableIDsBegin != variableIDsEnd) {
1017 constraint.setLinearCoef(
cplexVariables_[*variableIDsBegin], *coefficientsBegin);
1019 ++coefficientsBegin;
1024 }
catch(
const IloException& e) {
1025 std::cout << e << std::endl;
1026 throw std::runtime_error(
"CPLEX exception");
1037 }
catch(
const IloException& e) {
1038 std::cout << e << std::endl;
1039 throw std::runtime_error(
"CPLEX exception");
1052 timing = end - begin;
1053 }
catch(
const IloException& e) {
1054 std::cout << e << std::endl;
1055 throw std::runtime_error(
"CPLEX exception");
1059 template <
class PARAMETER_TYPE,
class PARAMETER_VALUE_TYPE>
1063 }
catch(
const IloException& e) {
1064 std::cout << e << std::endl;
1065 throw std::runtime_error(
"CPLEX exception");
1074 IloCplex::CplexStatus status =
cplexSolver_.getCplexStatus();
1075 std::cout <<
"failed to optimize(CPLEX Status: " << status <<
")." << std::endl;
1080 }
catch(
const IloException& e) {
1081 std::cout <<
"caught CPLEX exception: " << e << std::endl;
1092 IloCplex::CplexStatus status =
cplexSolver_.getCplexStatus();
1093 std::cout <<
"failed to optimize(CPLEX Status: " << status <<
")." << std::endl;
1095 timing = end - begin;
1099 timing = end - begin;
1102 }
catch(
const IloException& e) {
1103 std::cout <<
"caught CPLEX exception: " << e << std::endl;
1122 }
catch(
const IloException& e) {
1123 std::cout << e << std::endl;
1124 throw std::runtime_error(
"CPLEX exception");
1131 }
catch(
const IloException& e) {
1132 std::cout << e << std::endl;
1133 throw std::runtime_error(
"CPLEX exception");
1144 }
catch(
const IloException& e) {
1145 std::cout << e << std::endl;
1146 throw std::runtime_error(
"CPLEX exception");
1153 }
catch(
const IloException& e) {
1154 std::cout << e << std::endl;
1155 throw std::runtime_error(
"CPLEX exception");
1164 }
catch(
const IloException& e) {
1165 std::cout << e << std::endl;
1166 throw std::runtime_error(
"CPLEX exception");
1185 throw std::runtime_error(
"Unknown Cut level.");
void addBinaryVariables_impl(const CplexIndexType numVariables)
Add new binary variables to the model.
void addLessEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const CplexValueType bound, const std::string &constraintName="")
Add a new less equal constraint to the model.
CplexValueType objectiveFunctionValueBound_impl() const
Get the best known bound for the optimal solution of the current model.
void setObjective_impl(const Objective objective)
Set objective to minimize or maximize.
Objective function will be maximized.
void setObjectiveValue_impl(const CplexIndexType variable, const CplexValueType value)
Set the coefficient of a variable in the objective function.
~LPSolverCplex()
Destructor for LPSolverCplex.
void addEqualityConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const CplexValueType bound, const std::string &constraintName="")
Add a new equality constraint to the model.
Objective
This enum defines the type of the objective. It is used to select either to minimize or to maxime the...
IloCplex cplexSolver_
The CPLEX solver.
const IloNum & operator[](const IloInt n) const
The subscript operator provides constant access to the element in the sequence which is n elements be...
IloInt CplexIndexType
Defines the index type used by CPLEX.
IloObjective cplexObjective_
The objective function.
LPSolverCplex(const Parameter ¶meter=Parameter())
Default constructor for LPSolverCplex.
IloRangeArray cplexConstraints_
Puffer for the constraints which are added by CPlexSolver::addEqualityConstraint, CPlexSolver::addLes...
Provides Interface definition for wrapper of LP Solvers like CPLEX and Gurobi.
void updateSolution() const
Update solution if required.
bool cplexSolutionValid_
Tell if the currently stored solution is valid.
IloNum CplexTimingType
Defines the timing type used by CPLEX.
Interface definition for wrapper of LP Solvers like CPLEX and Gurobi.
difference_type operator-(const IloNumArrayIterator &iter) const
The difference operator computes the difference of two IloNumArrayIterators.
CplexValueType solution_impl(const CplexIndexType variable) const
Get the solution value of a variable computed by CPLEX.
Wrapper class for the IBM ILOG CPLEX optimizer.
IloNum CplexValueType
Defines the value type used by CPLEX.
IloNumVarArray cplexVariables_
The variables which are present in the model.
CplexSolutionIteratorType solutionEnd_impl() const
Get an iterator which is pointing to the end of the solution computed by CPLEX.
const Parameter parameter_
Storage for parameter.
bool solve_impl()
Solve the current model.
void setParameter_impl(const PARAMETER_TYPE parameter, const PARAMETER_VALUE_TYPE value)
Set CPLEX parameter.
CplexValueType objectiveFunctionValue_impl() const
Get the objective function value from CPLEX.
IloNumArrayIterator & operator++()
The increment operator increases the position at which the iterator is pointing by one...
IloNumArray cplexSolution_
Storage for the solution computed by CPLEX.
Iterator to iterate over an array of type IloNumArray.
IloNumArrayIterator()
Default constructor to create an empty IloNumArrayIterator.
Objective function will be minimized.
IloEnv cplexEnvironment_
The CPLEX environment.
void exportModel_impl(const std::string &filename) const
Export model to file.
LPSolverInterface< LPSolverCplex, CplexValueType, CplexIndexType, CplexSolutionIteratorType, CplexTimingType > LPSolverBaseClass
Defines the type of the base class.
void addContinuousVariables_impl(const CplexIndexType numVariables, const CplexValueType lowerBound, const CplexValueType upperBound)
Add new continuous variables to the model.
IloInt position_
Index of the element in the sequence at which the iterator is pointing at the moment.
void addIntegerVariables_impl(const CplexIndexType numVariables, const CplexValueType lowerBound, const CplexValueType upperBound)
Add new integer variables to the model.
IloNumArrayIterator CplexSolutionIteratorType
Defines the iterator type which can be used to iterate over the solution of CPLEX.
void addGreaterEqualConstraint_impl(VARIABLES_ITERATOR_TYPE variableIDsBegin, const VARIABLES_ITERATOR_TYPE variableIDsEnd, COEFFICIENTS_ITERATOR_TYPE coefficientsBegin, const CplexValueType bound, const std::string &constraintName="")
Add a new greater equal constraint to the model.
bool operator!=(const IloNumArrayIterator &iter) const
Comparison operator to test if two iterators point to different elements.
IloModel cplexModel_
The CPLEX model of the LP/MIP problem.
void addConstraintsFinished_impl()
Join all constraints added via LPSolverCplex::addEqualityConstraint, LPSolverCplex::addLessEqualConst...
static CplexValueType infinity_impl()
Get the value which is used by CPLEX to represent infinity.
bool operator==(const IloNumArrayIterator &iter) const
Comparison operator to test if two iterators point to the same element.
CplexSolutionIteratorType solutionBegin_impl() const
Get an iterator which is pointing to the begin of the solution computed by CPLEX. ...
static int getCutLevelValue(const LPDef::MIP_CUT cutLevel)
Translate LPDef::MIP_CUT into corresponding CPLEX int value.
const IloNumArray * array_
Constant pointer to the IloNumArray over which the iterator will iterate.
const IloNum & operator*() const
The dereference operator provides constant access to the element at which the iterator is pointing...