/* -*- mode: C++; indent-tabs-mode: nil; -*-
* This file is a part of LEMON, a generic C++ optimization library.
* Copyright (C) 2003-2008
* 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
///\brief Implementation of the LEMON GLPK LP and MIP solver interface.
#include <lemon/assert.h>
GlpkBase::GlpkBase() : LpBase() {
GlpkBase::GlpkBase(const GlpkBase &other) : LpBase() {
glp_copy_prob(lp, other.lp, GLP_ON);
int GlpkBase::_addCol() {
int i = glp_add_cols(lp, 1);
glp_set_col_bnds(lp, i, GLP_FR, 0.0, 0.0);
int GlpkBase::_addRow() {
int i = glp_add_rows(lp, 1);
glp_set_row_bnds(lp, i, GLP_FR, 0.0, 0.0);
void GlpkBase::_eraseCol(int i) {
void GlpkBase::_eraseRow(int i) {
void GlpkBase::_eraseColId(int i) {
void GlpkBase::_eraseRowId(int i) {
void GlpkBase::_getColName(int c, std::string& name) const {
const char *str = glp_get_col_name(lp, c);
void GlpkBase::_setColName(int c, const std::string & name) {
glp_set_col_name(lp, c, const_cast<char*>(name.c_str()));
int GlpkBase::_colByName(const std::string& name) const {
int k = glp_find_col(lp, const_cast<char*>(name.c_str()));
void GlpkBase::_getRowName(int r, std::string& name) const {
const char *str = glp_get_row_name(lp, r);
void GlpkBase::_setRowName(int r, const std::string & name) {
glp_set_row_name(lp, r, const_cast<char*>(name.c_str()));
int GlpkBase::_rowByName(const std::string& name) const {
int k = glp_find_row(lp, const_cast<char*>(name.c_str()));
void GlpkBase::_setRowCoeffs(int i, ExprIterator b, ExprIterator e) {
std::vector<int> indexes;
std::vector<Value> values;
for(ExprIterator it = b; it != e; ++it) {
indexes.push_back(it->first);
values.push_back(it->second);
glp_set_mat_row(lp, i, values.size() - 1,
&indexes.front(), &values.front());
void GlpkBase::_getRowCoeffs(int ix, InsertIterator b) const {
int length = glp_get_mat_row(lp, ix, 0, 0);
std::vector<int> indexes(length + 1);
std::vector<Value> values(length + 1);
glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
for (int i = 1; i <= length; ++i) {
*b = std::make_pair(indexes[i], values[i]);
void GlpkBase::_setColCoeffs(int ix, ExprIterator b,
std::vector<int> indexes;
std::vector<Value> values;
for(ExprIterator it = b; it != e; ++it) {
indexes.push_back(it->first);
values.push_back(it->second);
glp_set_mat_col(lp, ix, values.size() - 1,
&indexes.front(), &values.front());
void GlpkBase::_getColCoeffs(int ix, InsertIterator b) const {
int length = glp_get_mat_col(lp, ix, 0, 0);
std::vector<int> indexes(length + 1);
std::vector<Value> values(length + 1);
glp_get_mat_col(lp, ix, &indexes.front(), &values.front());
for (int i = 1; i <= length; ++i) {
*b = std::make_pair(indexes[i], values[i]);
void GlpkBase::_setCoeff(int ix, int jx, Value value) {
if (glp_get_num_cols(lp) < glp_get_num_rows(lp)) {
int length = glp_get_mat_row(lp, ix, 0, 0);
std::vector<int> indexes(length + 2);
std::vector<Value> values(length + 2);
glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
//The following code does not suppose that the elements of the
//array indexes are sorted
for (int i = 1; i <= length; ++i) {
glp_set_mat_row(lp, ix, length, &indexes.front(), &values.front());
int length = glp_get_mat_col(lp, jx, 0, 0);
std::vector<int> indexes(length + 2);
std::vector<Value> values(length + 2);
glp_get_mat_col(lp, jx, &indexes.front(), &values.front());
//The following code does not suppose that the elements of the
//array indexes are sorted
for (int i = 1; i <= length; ++i) {
glp_set_mat_col(lp, jx, length, &indexes.front(), &values.front());
GlpkBase::Value GlpkBase::_getCoeff(int ix, int jx) const {
int length = glp_get_mat_row(lp, ix, 0, 0);
std::vector<int> indexes(length + 1);
std::vector<Value> values(length + 1);
glp_get_mat_row(lp, ix, &indexes.front(), &values.front());
for (int i = 1; i <= length; ++i) {
void GlpkBase::_setColLowerBound(int i, Value lo) {
LEMON_ASSERT(lo != INF, "Invalid bound");
int b = glp_get_col_type(lp, i);
double up = glp_get_col_ub(lp, i);
glp_set_col_bnds(lp, i, GLP_FR, lo, up);
glp_set_col_bnds(lp, i, GLP_UP, lo, up);
glp_set_col_bnds(lp, i, GLP_LO, lo, up);
glp_set_col_bnds(lp, i, GLP_FX, lo, up);
glp_set_col_bnds(lp, i, GLP_DB, lo, up);
GlpkBase::Value GlpkBase::_getColLowerBound(int i) const {
int b = glp_get_col_type(lp, i);
return glp_get_col_lb(lp, i);
void GlpkBase::_setColUpperBound(int i, Value up) {
LEMON_ASSERT(up != -INF, "Invalid bound");
int b = glp_get_col_type(lp, i);
double lo = glp_get_col_lb(lp, i);
glp_set_col_bnds(lp, i, GLP_FR, lo, up);
glp_set_col_bnds(lp, i, GLP_LO, lo, up);
glp_set_col_bnds(lp, i, GLP_UP, lo, up);
glp_set_col_bnds(lp, i, GLP_UP, lo, up);
glp_set_col_bnds(lp, i, GLP_FX, lo, up);
glp_set_col_bnds(lp, i, GLP_DB, lo, up);
GlpkBase::Value GlpkBase::_getColUpperBound(int i) const {
int b = glp_get_col_type(lp, i);
return glp_get_col_ub(lp, i);
void GlpkBase::_setRowLowerBound(int i, Value lo) {
LEMON_ASSERT(lo != INF, "Invalid bound");
int b = glp_get_row_type(lp, i);
double up = glp_get_row_ub(lp, i);
glp_set_row_bnds(lp, i, GLP_FR, lo, up);
glp_set_row_bnds(lp, i, GLP_UP, lo, up);
glp_set_row_bnds(lp, i, GLP_LO, lo, up);
glp_set_row_bnds(lp, i, GLP_FX, lo, up);
glp_set_row_bnds(lp, i, GLP_DB, lo, up);
GlpkBase::Value GlpkBase::_getRowLowerBound(int i) const {
int b = glp_get_row_type(lp, i);
return glp_get_row_lb(lp, i);
void GlpkBase::_setRowUpperBound(int i, Value up) {
LEMON_ASSERT(up != -INF, "Invalid bound");
int b = glp_get_row_type(lp, i);
double lo = glp_get_row_lb(lp, i);
glp_set_row_bnds(lp, i, GLP_FR, lo, up);
glp_set_row_bnds(lp, i, GLP_LO, lo, up);
glp_set_row_bnds(lp, i, GLP_UP, lo, up);
glp_set_row_bnds(lp, i, GLP_UP, lo, up);
glp_set_row_bnds(lp, i, GLP_FX, lo, up);
glp_set_row_bnds(lp, i, GLP_DB, lo, up);
GlpkBase::Value GlpkBase::_getRowUpperBound(int i) const {
int b = glp_get_row_type(lp, i);
return glp_get_row_ub(lp, i);
void GlpkBase::_setObjCoeffs(ExprIterator b, ExprIterator e) {
for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
glp_set_obj_coef(lp, i, 0.0);
for (ExprIterator it = b; it != e; ++it) {
glp_set_obj_coef(lp, it->first, it->second);
void GlpkBase::_getObjCoeffs(InsertIterator b) const {
for (int i = 1; i <= glp_get_num_cols(lp); ++i) {
Value val = glp_get_obj_coef(lp, i);
*b = std::make_pair(i, val);
void GlpkBase::_setObjCoeff(int i, Value obj_coef) {
//i = 0 means the constant term (shift)
glp_set_obj_coef(lp, i, obj_coef);
GlpkBase::Value GlpkBase::_getObjCoeff(int i) const {
//i = 0 means the constant term (shift)
return glp_get_obj_coef(lp, i);
void GlpkBase::_setSense(GlpkBase::Sense sense) {
glp_set_obj_dir(lp, GLP_MIN);
glp_set_obj_dir(lp, GLP_MAX);
GlpkBase::Sense GlpkBase::_getSense() const {
switch(glp_get_obj_dir(lp)) {
LEMON_ASSERT(false, "Wrong sense");
return GlpkBase::Sense();
void GlpkBase::_clear() {
: LpBase(), GlpkBase(), LpSolver() {
messageLevel(MESSAGE_NO_OUTPUT);
LpGlpk::LpGlpk(const LpGlpk& other)
: LpBase(other), GlpkBase(other), LpSolver(other) {
messageLevel(MESSAGE_NO_OUTPUT);
LpGlpk* LpGlpk::_newSolver() const { return new LpGlpk; }
LpGlpk* LpGlpk::_cloneSolver() const { return new LpGlpk(*this); }
const char* LpGlpk::_solverName() const { return "LpGlpk"; }
void LpGlpk::_clear_temporals() {
LpGlpk::SolveExitStatus LpGlpk::_solve() {
LpGlpk::SolveExitStatus LpGlpk::solvePrimal() {
switch (_message_level) {
smcp.msg_lev = GLP_MSG_OFF;
case MESSAGE_ERROR_MESSAGE:
smcp.msg_lev = GLP_MSG_ERR;
case MESSAGE_NORMAL_OUTPUT:
smcp.msg_lev = GLP_MSG_ON;
case MESSAGE_FULL_OUTPUT:
smcp.msg_lev = GLP_MSG_ALL;
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
LpGlpk::SolveExitStatus LpGlpk::solveDual() {
switch (_message_level) {
smcp.msg_lev = GLP_MSG_OFF;
case MESSAGE_ERROR_MESSAGE:
smcp.msg_lev = GLP_MSG_ERR;
case MESSAGE_NORMAL_OUTPUT:
smcp.msg_lev = GLP_MSG_ON;
case MESSAGE_FULL_OUTPUT:
smcp.msg_lev = GLP_MSG_ALL;
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
LpGlpk::Value LpGlpk::_getPrimal(int i) const {
return glp_get_col_prim(lp, i);
LpGlpk::Value LpGlpk::_getDual(int i) const {
return glp_get_row_dual(lp, i);
LpGlpk::Value LpGlpk::_getPrimalValue() const {
return glp_get_obj_val(lp);
LpGlpk::VarStatus LpGlpk::_getColStatus(int i) const {
switch (glp_get_col_stat(lp, i)) {
LEMON_ASSERT(false, "Wrong column status");
return LpGlpk::VarStatus();
LpGlpk::VarStatus LpGlpk::_getRowStatus(int i) const {
switch (glp_get_row_stat(lp, i)) {
LEMON_ASSERT(false, "Wrong row status");
return LpGlpk::VarStatus();
LpGlpk::Value LpGlpk::_getPrimalRay(int i) const {
if (_primal_ray.empty()) {
int row_num = glp_get_num_rows(lp);
int col_num = glp_get_num_cols(lp);
_primal_ray.resize(col_num + 1, 0.0);
int index = glp_get_unbnd_ray(lp);
// The primal ray is found in primal simplex second phase
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) :
glp_get_col_stat(lp, index - row_num)) != GLP_BS,
bool negate = glp_get_obj_dir(lp) == GLP_MAX;
_primal_ray[index - row_num] = 1.0;
if (glp_get_col_dual(lp, index - row_num) > 0) {
if (glp_get_row_dual(lp, index) > 0) {
std::vector<int> ray_indexes(row_num + 1);
std::vector<Value> ray_values(row_num + 1);
int ray_length = glp_eval_tab_col(lp, index, &ray_indexes.front(),
for (int i = 1; i <= ray_length; ++i) {
if (ray_indexes[i] > row_num) {
_primal_ray[ray_indexes[i] - row_num] = ray_values[i];
for (int i = 1; i <= col_num; ++i) {
_primal_ray[i] = - _primal_ray[i];
for (int i = 1; i <= col_num; ++i) {
_primal_ray[i] = glp_get_col_prim(lp, i);
LpGlpk::Value LpGlpk::_getDualRay(int i) const {
int row_num = glp_get_num_rows(lp);
_dual_ray.resize(row_num + 1, 0.0);
int index = glp_get_unbnd_ray(lp);
// The dual ray is found in dual simplex second phase
LEMON_ASSERT((index <= row_num ? glp_get_row_stat(lp, index) :
glp_get_col_stat(lp, index - row_num)) == GLP_BS,
idx = glp_get_col_bind(lp, index - row_num);
if (glp_get_col_prim(lp, index - row_num) >
glp_get_col_ub(lp, index - row_num)) {
idx = glp_get_row_bind(lp, index);
if (glp_get_row_prim(lp, index) > glp_get_row_ub(lp, index)) {
_dual_ray[idx] = negate ? - 1.0 : 1.0;
glp_btran(lp, &_dual_ray.front());
// The dual ray is found in primal simplex first phase
// We assume that the glpk minimizes the slack to get feasible solution
for (int i = 1; i <= row_num; ++i) {
int index = glp_get_bhead(lp, i);
double res = glp_get_row_prim(lp, index);
if (res > glp_get_row_ub(lp, index) + eps) {
} else if (res < glp_get_row_lb(lp, index) - eps) {
_dual_ray[i] *= glp_get_rii(lp, index);
double res = glp_get_col_prim(lp, index - row_num);
if (res > glp_get_col_ub(lp, index - row_num) + eps) {
} else if (res < glp_get_col_lb(lp, index - row_num) - eps) {
_dual_ray[i] /= glp_get_sjj(lp, index - row_num);
glp_btran(lp, &_dual_ray.front());
for (int i = 1; i <= row_num; ++i) {
_dual_ray[i] /= glp_get_rii(lp, i);
LpGlpk::ProblemType LpGlpk::_getPrimalType() const {
if (glp_get_status(lp) == GLP_OPT)
switch (glp_get_prim_stat(lp)) {
if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
LEMON_ASSERT(false, "Wrong primal type");
return LpGlpk::ProblemType();
LpGlpk::ProblemType LpGlpk::_getDualType() const {
if (glp_get_status(lp) == GLP_OPT)
switch (glp_get_dual_stat(lp)) {
if (glp_get_prim_stat(lp) == GLP_NOFEAS) {
LEMON_ASSERT(false, "Wrong primal type");
return LpGlpk::ProblemType();
void LpGlpk::presolver(bool b) {
lpx_set_int_parm(lp, LPX_K_PRESOL, b ? 1 : 0);
void LpGlpk::messageLevel(MessageLevel m) {
: LpBase(), GlpkBase(), MipSolver() {
messageLevel(MESSAGE_NO_OUTPUT);
MipGlpk::MipGlpk(const MipGlpk& other)
: LpBase(), GlpkBase(other), MipSolver() {
messageLevel(MESSAGE_NO_OUTPUT);
void MipGlpk::_setColType(int i, MipGlpk::ColTypes col_type) {
glp_set_col_kind(lp, i, GLP_IV);
glp_set_col_kind(lp, i, GLP_CV);
MipGlpk::ColTypes MipGlpk::_getColType(int i) const {
switch (glp_get_col_kind(lp, i)) {
MipGlpk::SolveExitStatus MipGlpk::_solve() {
switch (_message_level) {
smcp.msg_lev = GLP_MSG_OFF;
case MESSAGE_ERROR_MESSAGE:
smcp.msg_lev = GLP_MSG_ERR;
case MESSAGE_NORMAL_OUTPUT:
smcp.msg_lev = GLP_MSG_ON;
case MESSAGE_FULL_OUTPUT:
smcp.msg_lev = GLP_MSG_ALL;
if (glp_simplex(lp, &smcp) != 0) return UNSOLVED;
if (glp_get_status(lp) != GLP_OPT) return SOLVED;
switch (_message_level) {
iocp.msg_lev = GLP_MSG_OFF;
case MESSAGE_ERROR_MESSAGE:
iocp.msg_lev = GLP_MSG_ERR;
case MESSAGE_NORMAL_OUTPUT:
iocp.msg_lev = GLP_MSG_ON;
case MESSAGE_FULL_OUTPUT:
iocp.msg_lev = GLP_MSG_ALL;
if (glp_intopt(lp, &iocp) != 0) return UNSOLVED;
MipGlpk::ProblemType MipGlpk::_getType() const {
switch (glp_get_status(lp)) {
switch (glp_mip_status(lp)) {
LEMON_ASSERT(false, "Wrong problem type.");
return MipGlpk::ProblemType();
if (glp_get_dual_stat(lp) == GLP_NOFEAS) {
LEMON_ASSERT(false, "Wrong problem type.");
return MipGlpk::ProblemType();
MipGlpk::Value MipGlpk::_getSol(int i) const {
return glp_mip_col_val(lp, i);
MipGlpk::Value MipGlpk::_getSolValue() const {
return glp_mip_obj_val(lp);
MipGlpk* MipGlpk::_newSolver() const { return new MipGlpk; }
MipGlpk* MipGlpk::_cloneSolver() const {return new MipGlpk(*this); }
const char* MipGlpk::_solverName() const { return "MipGlpk"; }
void MipGlpk::messageLevel(MessageLevel m) {
} //END OF NAMESPACE LEMON