Skip to content

Commit

Permalink
1.3.0
Browse files Browse the repository at this point in the history
- Symengine-based symbolics is now a fully supported feature
- Added the first exact LP solver, available as glpk_exact_interface
- Fixed an issue with indicator variables in cplex
- Minor bugfixes
  • Loading branch information
KristianJensen authored Nov 22, 2017
1 parent 102437f commit 3b8dcb5
Show file tree
Hide file tree
Showing 15 changed files with 858 additions and 105 deletions.
85 changes: 44 additions & 41 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
language: python
sudo: false
python:
- '2.7'
- '3.4'
- '3.5'
- '2.7'
- '3.4'
- '3.5'
env:
global:
- secure: PBcwrLg4ZwVi9Gw25Q2adNd0uK+NCqFSiYl+ZuumkEXs4NQBbSXVd719wrWKUFdIqBy+h9ETo4Vsz/QsTZzI2mSG4zqTkm+oUxMW1tJxYEeKZvCo7GfZ883VlKFgdqvh6iouEvHSjfGbwc5cUp98CbjgI5ni01vGpDQh2hgokkI=
matrix:
- OPTLANG_USE_SYMENGINE=False
- OPTLANG_USE_SYMENGINE=True
cache:
- pip: true
- pip: true
addons:
apt:
packages:
Expand All @@ -16,49 +22,46 @@ addons:
- glpk-utils
- pandoc
before_install:
- export SYMPY_USE_CACHE=no
- export OPTLANG_USE_SYMENGINE=no
- pip install pip --upgrade
- 'echo "this is a build for: $TRAVIS_BRANCH"'
- 'if [[ "$TRAVIS_PYTHON_VERSION" != "3.5" ]]; then bash ./.travis/install_cplex.sh; fi'
- export SYMPY_USE_CACHE=no
- pip install pip --upgrade
- 'echo "this is a build for: $TRAVIS_BRANCH"'
- 'if [[ "$TRAVIS_PYTHON_VERSION" != "3.5" ]]; then bash ./.travis/install_cplex.sh; fi'
install:
- pip install nose nose-progressive rednose coverage docutils flake8 codecov jsonschema
- pip install -r requirements.txt
- pip install inspyred
- pip install pypandoc
- pip install swiglpk
- pip install scipy
- python setup.py install
- pip install nose nose-progressive rednose coverage docutils flake8 codecov jsonschema
- pip install -r requirements.txt
- pip install inspyred
- pip install pypandoc
- pip install swiglpk
- pip install scipy
- pip install symengine
- python setup.py install
before_script:
- flake8 .
- flake8 .
script: nosetests
after_success:
- codecov
- codecov
notifications:
slack:
secure: s8Dj0MFreNwZ3Zhb0+5yJiHPL33JsxLjmoRo8f0ohLdD15L//E4VjkCsYkNEcLzid6HarEL/1JSmzAuGl40fCdLqTAoDRy01shT1zmfWQPXQlaALh5f8ExBAlyDHxKhd/B2SytYu6uhe0WOuxu/oo4c33a7pKhuV1piNcevPZew=
before_deploy:
- pip install twine
- python setup.py sdist bdist_wheel
env:
global:
- secure: PBcwrLg4ZwVi9Gw25Q2adNd0uK+NCqFSiYl+ZuumkEXs4NQBbSXVd719wrWKUFdIqBy+h9ETo4Vsz/QsTZzI2mSG4zqTkm+oUxMW1tJxYEeKZvCo7GfZ883VlKFgdqvh6iouEvHSjfGbwc5cUp98CbjgI5ni01vGpDQh2hgokkI=
- pip install twine
- python setup.py sdist bdist_wheel
deploy:
- provider: releases
api_key:
secure: u4aJv+5YoH3gjJpyiVoq33SqKIUtx8LWPp15pIh8hKHmUgJNyjGm7ELXOeczfQ5W7ZpnWj+ogewaes2oA0NLxBB1/MBPL7kr77hmzp+XhZomh73DzFKegbpBTgqpioBRxvPlq3HYNIWqrLkeg/HYlBW1WM6mKifFUwqbIaL+++4=
file_glob: true
file: dist/optlang*.whl
skip_cleanup: true
on:
branch: master
tags: true
repo: biosustain/optlang
- provider: pypi
user: Nikolaus.Sonnenschein
password:
secure: Gn23MUvzP1DPJXxRXUOXGBJjyMamawxey5ByrOd+JT90roljHKSk8v1wdBMH7+s1DB/ygUJqB2Zy0cBC3mr0waY6HmxKpXhddgzQzG56Eua/npTxpz58Y8xfSYF+5QqS3gcyBrYEXmeHWuEURERy0b7uYKMx/QcHAHYhTaVy4zE=
on:
branch: master
tags: true
repo: biosustain/optlang
- provider: releases
api_key:
secure: u4aJv+5YoH3gjJpyiVoq33SqKIUtx8LWPp15pIh8hKHmUgJNyjGm7ELXOeczfQ5W7ZpnWj+ogewaes2oA0NLxBB1/MBPL7kr77hmzp+XhZomh73DzFKegbpBTgqpioBRxvPlq3HYNIWqrLkeg/HYlBW1WM6mKifFUwqbIaL+++4=
file_glob: true
file: dist/optlang*.whl
skip_cleanup: true
on:
branch: master
tags: true
repo: biosustain/optlang
- provider: pypi
user: Nikolaus.Sonnenschein
password:
secure: Gn23MUvzP1DPJXxRXUOXGBJjyMamawxey5ByrOd+JT90roljHKSk8v1wdBMH7+s1DB/ygUJqB2Zy0cBC3mr0waY6HmxKpXhddgzQzG56Eua/npTxpz58Y8xfSYF+5QqS3gcyBrYEXmeHWuEURERy0b7uYKMx/QcHAHYhTaVy4zE=
on:
branch: master
tags: true
repo: biosustain/optlang
30 changes: 30 additions & 0 deletions examples/simple_numpy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import numpy as np
from optlang import Model, Variable, Constraint, Objective

# All the (symbolic) variables are declared, with a name and optionally a lower
# and/or upper bound.
x = np.array([Variable('x{}'.format(i), lb=0) for i in range(1, 4)])

bounds = [100, 600, 300]

A = np.array([[1, 1, 1],
[10, 4, 5],
[2, 2, 6]])

w = np.array([10, 6, 4])

obj = Objective(w.dot(x), direction='max')

c = np.array([Constraint(row, ub=bound) for row, bound in zip(A.dot(x), bounds)])

model = Model(name='Numpy model')
model.objective = obj
model.add(c)

status = model.optimize()

print("status:", model.status)
print("objective value:", model.objective.value)
print("----------")
for var_name, var in model.variables.iteritems():
print(var_name, "=", var.primal)
5 changes: 3 additions & 2 deletions optlang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
if available_solvers['GLPK']:
try:
from optlang import glpk_interface
from optlang import glpk_exact_interface
except Exception:
log.error('GLPK is available but could not load with error:\n ' + str(traceback.format_exc()).strip().replace('\n','\n '))

Expand All @@ -57,10 +58,10 @@


# Go through and find the best solver that loaded. Load that one as the default
for engine_str in ['cplex_interface','gurobi_interface','glpk_interface','scipy_interface']:
for engine_str in ['cplex_interface', 'gurobi_interface', 'glpk_interface', 'scipy_interface']:
# Must check globals since not all interface variables will be defined
if engine_str in globals():
engine=globals()[engine_str]
engine = globals()[engine_str]
Model = engine.Model
Variable = engine.Variable
Constraint = engine.Constraint
Expand Down
26 changes: 17 additions & 9 deletions optlang/cplex_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,13 +219,15 @@ def __init__(self, expression, sloppy=False, *args, **kwargs):

def set_linear_coefficients(self, coefficients):
if self.problem is not None:
self.problem.update()
triplets = [(self.name, var.name, float(coeff)) for var, coeff in six.iteritems(coefficients)]
self.problem.problem.linear_constraints.set_coefficients(triplets)
else:
raise Exception("Can't change coefficients if constraint is not associated with a model.")

def get_linear_coefficients(self, variables):
if self.problem is not None:
self.problem.update()
coefs = self.problem.problem.linear_constraints.get_coefficients([(self.name, v.name) for v in variables])
return {v: c for v, c in zip(variables, coefs)}
else:
Expand All @@ -234,7 +236,13 @@ def get_linear_coefficients(self, variables):
def _get_expression(self):
if self.problem is not None:
cplex_problem = self.problem.problem
cplex_row = cplex_problem.linear_constraints.get_rows(self.name)
try:
cplex_row = cplex_problem.linear_constraints.get_rows(self.name)
except CplexSolverError as e:
if 'CPLEX Error 1219:' not in str(e):
raise e
else:
cplex_row = cplex_problem.indicator_constraints.get_linear_components(self.name)
variables = self.problem._variables
expression = add(
[mul((symbolics.Real(cplex_row.val[i]), variables[ind])) for i, ind in
Expand Down Expand Up @@ -367,13 +375,15 @@ def _get_expression(self):

def set_linear_coefficients(self, coefficients):
if self.problem is not None:
self.problem.update()
self.problem.problem.objective.set_linear([(variable.name, float(coefficient)) for variable, coefficient in coefficients.items()])
self._expression_expired = True
else:
raise Exception("Can't change coefficients if objective is not associated with a model.")

def get_linear_coefficients(self, variables):
if self.problem is not None:
self.problem.update()
coefs = self.problem.problem.objective.get_linear([v.name for v in variables])
return {v: c for v, c in zip(variables, coefs)}
else:
Expand Down Expand Up @@ -608,11 +618,7 @@ def __init__(self, problem=None, *args, **kwargs):

# Since constraint expressions are lazily retrieved from the solver they don't have to be built here
# lhs = _unevaluated_Add(*[val * variables[i - 1] for i, val in zip(row.ind, row.val)])
lhs = 0
if isinstance(lhs, int):
lhs = symbolics.Integer(lhs)
elif isinstance(lhs, float):
lhs = symbolics.Real(lhs)
lhs = symbolics.Integer(0)
if sense == 'E':
constr = Constraint(lhs, lb=rhs, ub=rhs, name=name, problem=self)
elif sense == 'G':
Expand All @@ -625,7 +631,7 @@ def __init__(self, problem=None, *args, **kwargs):
constr = Constraint(lhs, lb=rhs, ub=rhs + range_val, name=name, problem=self)
else:
constr = Constraint(lhs, lb=rhs + range_val, ub=rhs, name=name, problem=self)
else:
else: # pragma: no cover
raise Exception('%s is not a recognized constraint sense.' % sense)

for variable in constraint_variables:
Expand All @@ -640,7 +646,7 @@ def __init__(self, problem=None, *args, **kwargs):
)
try:
objective_name = self.problem.objective.get_name()
except cplex.exceptions.CplexSolverError as e:
except CplexSolverError as e:
if 'CPLEX Error 1219:' not in str(e):
raise e
else:
Expand Down Expand Up @@ -885,7 +891,9 @@ def _add_constraints(self, constraints, sloppy=False):
def _remove_constraints(self, constraints):
super(Model, self)._remove_constraints(constraints)
for constraint in constraints:
if constraint.is_Linear:
if constraint.indicator_variable is not None:
self.problem.indicator_constraints.delete(constraint.name)
elif constraint.is_Linear:
self.problem.linear_constraints.delete(constraint.name)
elif constraint.is_Quadratic:
self.problem.quadratic_constraints.delete(constraint.name)
Expand Down
145 changes: 145 additions & 0 deletions optlang/glpk_exact_interface.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
# Copyright 2017 Novo Nordisk Foundation Center for Biosustainability,
# Technical University of Denmark.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.


"""
Interface for the GNU Linear Programming Kit (GLPK)
GLPK is an open source LP solver, with MILP capabilities. This interface exposes its GLPK's exact solver.
To use GLPK you need to install the 'swiglpk' python package (with pip or from http://github.com/biosustain/swiglpk)
and make sure that 'import swiglpk' runs without error.
"""

import logging

import six

from optlang.util import inheritdocstring
from optlang import interface
from optlang import glpk_interface
from optlang.glpk_interface import _GLPK_STATUS_TO_STATUS

log = logging.getLogger(__name__)

from swiglpk import glp_exact, glp_create_prob, glp_get_status, \
GLP_SF_AUTO, GLP_ETMLIM, glp_adv_basis, glp_read_lp, glp_scale_prob


@six.add_metaclass(inheritdocstring)
class Variable(glpk_interface.Variable):
def __init__(self, name, index=None, type="continuous", **kwargs):
if type in ("integer", "binary"):
raise ValueError("The GLPK exact solver does not support integer and mixed integer problems")
super(Variable, self).__init__(name, index, type=type, **kwargs)

@glpk_interface.Variable.type.setter
def type(self, value):
if value in ("integer", "binary"):
raise ValueError("The GLPK exact solver does not support integer and mixed integer problems")
super(Variable, Variable).type.fset(self, value)


@six.add_metaclass(inheritdocstring)
class Constraint(glpk_interface.Constraint):
pass


@six.add_metaclass(inheritdocstring)
class Objective(glpk_interface.Objective):
pass


@six.add_metaclass(inheritdocstring)
class Configuration(glpk_interface.Configuration):
pass


@six.add_metaclass(inheritdocstring)
class Model(glpk_interface.Model):
def _run_glp_exact(self):
return_value = glp_exact(self.problem, self.configuration._smcp)
glpk_status = glp_get_status(self.problem)
if return_value == 0:
status = _GLPK_STATUS_TO_STATUS[glpk_status]
elif return_value == GLP_ETMLIM:
status = interface.TIME_LIMIT
else:
status = _GLPK_STATUS_TO_STATUS[glpk_status]
if status == interface.UNDEFINED:
log.debug("Status undefined. GLPK status code returned by glp_simplex was %d" % return_value)
return status

def _optimize(self):
# Solving inexact first per GLPK manual
# Computations in exact arithmetic are very time consuming, so solving LP
# problem with the routine glp_exact from the very beginning is not a good
# idea. It is much better at first to find an optimal basis with the routine
# glp_simplex and only then to call glp_exact, in which case only a few
# simplex iterations need to be performed in exact arithmetic.
status = super(Model, self)._optimize()
if status != interface.OPTIMAL:
return status
else:
status = self._run_glp_exact()

if status == interface.UNDEFINED and self.configuration.presolve is True:
# If presolve is on, status will be undefined if not optimal
self.configuration.presolve = False
status = self._run_glp_exact()
self.configuration.presolve = True
return status


if __name__ == '__main__':
import pickle

x1 = Variable('x1', lb=0)
x2 = Variable('x2', lb=0)
x3 = Variable('x3', lb=0, ub=1, type='binary')
c1 = Constraint(x1 + x2 + x3, lb=-100, ub=100, name='c1')
c2 = Constraint(10 * x1 + 4 * x2 + 5 * x3, ub=600, name='c2')
c3 = Constraint(2 * x1 + 2 * x2 + 6 * x3, ub=300, name='c3')
obj = Objective(10 * x1 + 6 * x2 + 4 * x3, direction='max')
model = Model(name='Simple model')
model.objective = obj
model.add([c1, c2, c3])
model.configuration.verbosity = 3
status = model.optimize()
print("status:", model.status)
print("objective value:", model.objective.value)

for var_name, var in model.variables.items():
print(var_name, "=", var.primal)

print(model)

problem = glp_create_prob()
glp_read_lp(problem, None, "tests/data/model.lp")

solver = Model(problem=problem)
print(solver.optimize())
print(solver.objective)

import time

t1 = time.time()
print("pickling")
pickle_string = pickle.dumps(solver)
resurrected_solver = pickle.loads(pickle_string)
t2 = time.time()
print("Execution time: %s" % (t2 - t1))

resurrected_solver.optimize()
print("Halelujah!", resurrected_solver.objective.value)
Loading

0 comments on commit 3b8dcb5

Please sign in to comment.