Skip to content

Commit

Permalink
Merge branch 'master' into fix/mac_actions
Browse files Browse the repository at this point in the history
  • Loading branch information
cdiener authored Apr 26, 2024
2 parents 1df4f16 + a6594a5 commit 6fe804b
Show file tree
Hide file tree
Showing 18 changed files with 65 additions and 140 deletions.
2 changes: 0 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ Dependencies
The following dependencies are needed.

- `sympy >= 1.0.0 <http://sympy.org/en/index.html>`__
- `six >= 1.9.0 <https://pypi.python.org/pypi/six>`__
- `swiglpk >= 1.4.3 <https://pypi.python.org/pypi/swiglpk>`__

The following are optional dependencies that allow other solvers to be used.
Expand All @@ -58,7 +57,6 @@ from `GLPK documentation <http://www.gnu.org/software/glpk>`__):

.. code-block:: python
from __future__ import print_function
from optlang import Model, Variable, Constraint, Objective
# All the (symbolic) variables are declared, with a name and optionally a lower and/or upper bound.
Expand Down
3 changes: 1 addition & 2 deletions examples/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
# 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.
import six

from optlang import Model, Variable, Constraint, Objective

Expand All @@ -29,5 +28,5 @@
status = model.optimize()
print("status:", model.status)
print("objective value:", model.objective.value)
for var_name, var in six.iteritems(model.variables):
for var_name, var in model.variables.items():
print(var_name, "=", var.primal)
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ keywords =
[options]
zip_safe = True
install_requires =
six >=1.9
swiglpk >=5.0.8
sympy >=1.12.0
python_requires = >=3.8
Expand Down
2 changes: 0 additions & 2 deletions src/optlang/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import

import logging
import traceback
from optlang._version import get_versions
Expand Down
27 changes: 10 additions & 17 deletions src/optlang/coinor_cbc_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@
"""
import logging

import six

from optlang.util import inheritdocstring, TemporaryFilename
from optlang.expression_parsing import parse_optimization_expression
from optlang import interface
Expand Down Expand Up @@ -64,7 +62,7 @@
}

_VTYPE_TO_MIP_VTYPE = dict(
[(val, key) for key, val in six.iteritems(_MIP_VTYPE_TO_VTYPE)]
[(val, key) for key, val in _MIP_VTYPE_TO_VTYPE.items()]
)

_DIR_TO_MIP_DIR = {
Expand All @@ -73,7 +71,7 @@
}

_MIP_DIR_TO_DIR = dict(
[(val, key) for key, val in six.iteritems(_DIR_TO_MIP_DIR)]
[(val, key) for key, val in _DIR_TO_MIP_DIR.items()]
)

# Needs to be used for to_bound during _initialize_model_from_problem
Expand All @@ -98,8 +96,7 @@ def to_symbolic_expr(coeffs):
return symbolics.add([var * coef for var, coef in coeffs.items()
if not isclose(0, to_float(coef))])

@six.add_metaclass(inheritdocstring)
class Variable(interface.Variable):
class Variable(interface.Variable, metaclass=inheritdocstring):
def __init__(self, name, *args, **kwargs):
super(Variable, self).__init__(name, **kwargs)

Expand Down Expand Up @@ -162,8 +159,7 @@ def name(self, value):
raise Exception('COIN-OR CBC doesn\'t support variable name change')


@six.add_metaclass(inheritdocstring)
class Constraint(interface.Constraint):
class Constraint(interface.Constraint, metaclass=inheritdocstring):
_INDICATOR_CONSTRAINT_SUPPORT = False

def __init__(self, expression, sloppy=False, *args, **kwargs):
Expand Down Expand Up @@ -289,8 +285,7 @@ def get_linear_coefficients(self, variables):
return {v: expr.get(mip_var('v_' + v.name), 0) for v in variables}


@six.add_metaclass(inheritdocstring)
class Objective(interface.Objective):
class Objective(interface.Objective, metaclass=inheritdocstring):
def __init__(self, expression, sloppy=False, **kwargs):
self._changed_expression = {}
super(Objective, self).__init__(expression, sloppy=sloppy, **kwargs)
Expand Down Expand Up @@ -367,8 +362,7 @@ def get_linear_coefficients(self, variables):
return {v: coeffs.get(mip_var('v_' + v.name), 0) for v in variables}


@six.add_metaclass(inheritdocstring)
class Configuration(interface.MathematicalProgrammingConfiguration):
class Configuration(interface.MathematicalProgrammingConfiguration, metaclass=inheritdocstring):
def __init__(self, verbosity=0, timeout=None, presolve='auto',
max_nodes=None, max_solutions=None, relax=False,
emphasis=0, cuts=-1, threads=0, *args, **kwargs):
Expand Down Expand Up @@ -419,7 +413,7 @@ def __init__(self, verbosity=0, timeout=None, presolve='auto',
self.threads = threads

if 'tolerances' in kwargs:
for key, val in six.iteritems(kwargs['tolerances']):
for key, val in kwargs['tolerances'].items():
if key in self._tolerance_functions():
setattr(self.tolerances, key, val)

Expand Down Expand Up @@ -534,13 +528,13 @@ def __getstate__(self):
}

def __setstate__(self, state):
for key, val in six.iteritems(state):
for key, val in state.items():
if key != 'tolerances':
setattr(self, key, val)
# TODO: remove if check before final merge. Only here for backwards
# compatability for current pickle files stored in cobrapy
if 'tolerances' in state:
for key, val in six.iteritems(state['tolerances']):
for key, val in state['tolerances'].items():
if key in self._tolerance_functions():
setattr(self.tolerances, key, val)

Expand Down Expand Up @@ -592,8 +586,7 @@ def _tolerance_functions(self):
}


@six.add_metaclass(inheritdocstring)
class Model(interface.Model):
class Model(interface.Model, metaclass=inheritdocstring):

def _initialize_problem(self):
self.problem = mip.Model(solver_name=mip.CBC)
Expand Down
28 changes: 11 additions & 17 deletions src/optlang/cplex_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,8 @@
import logging
import sys

import six
import os
from six.moves import StringIO
from io import StringIO

import cplex
from cplex.exceptions import CplexSolverError
Expand Down Expand Up @@ -127,7 +126,7 @@
_CPLEX_VTYPE_TO_VTYPE = {'C': 'continuous', 'I': 'integer', 'B': 'binary'}

_VTYPE_TO_CPLEX_VTYPE = dict(
[(val, key) for key, val in six.iteritems(_CPLEX_VTYPE_TO_VTYPE)]
[(val, key) for key, val in _CPLEX_VTYPE_TO_VTYPE.items()]
)

_CPLEX_MIP_TYPES_TO_CONTINUOUS = {
Expand Down Expand Up @@ -162,8 +161,7 @@ def _constraint_lb_and_ub_to_cplex_sense_rhs_and_range_value(lb, ub):
return sense, rhs, range_value


@six.add_metaclass(inheritdocstring)
class Variable(interface.Variable):
class Variable(interface.Variable, metaclass=inheritdocstring):
def __init__(self, name, *args, **kwargs):
super(Variable, self).__init__(name, **kwargs)

Expand Down Expand Up @@ -210,8 +208,7 @@ def name(self, value):
self.problem.problem.variables.set_names(old_name, value)


@six.add_metaclass(inheritdocstring)
class Constraint(interface.Constraint):
class Constraint(interface.Constraint, metaclass=inheritdocstring):
_INDICATOR_CONSTRAINT_SUPPORT = True

def __init__(self, expression, sloppy=False, *args, **kwargs):
Expand All @@ -220,7 +217,7 @@ 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)]
triplets = [(self.name, var.name, float(coeff)) for var, coeff in coefficients.items()]
self.problem.problem.linear_constraints.set_coefficients(triplets)
else:
raise Exception("Can't change coefficients if constraint is not associated with a model.")
Expand Down Expand Up @@ -337,8 +334,7 @@ def __iadd__(self, other):
return self


@six.add_metaclass(inheritdocstring)
class Objective(interface.Objective):
class Objective(interface.Objective, metaclass=inheritdocstring):
def __init__(self, expression, sloppy=False, **kwargs):
super(Objective, self).__init__(expression, sloppy=sloppy, **kwargs)
self._expression_expired = False
Expand Down Expand Up @@ -390,8 +386,7 @@ def get_linear_coefficients(self, variables):
raise Exception("Can't get coefficients from solver if objective is not in a model")


@six.add_metaclass(inheritdocstring)
class Configuration(interface.MathematicalProgrammingConfiguration):
class Configuration(interface.MathematicalProgrammingConfiguration, metaclass=inheritdocstring):
def __init__(self, lp_method='primal', presolve="auto", verbosity=0, timeout=None,
solution_target="auto", qp_method="primal", *args, **kwargs):
super(Configuration, self).__init__(*args, **kwargs)
Expand All @@ -402,7 +397,7 @@ def __init__(self, lp_method='primal', presolve="auto", verbosity=0, timeout=Non
self.solution_target = solution_target
self.qp_method = qp_method
if "tolerances" in kwargs:
for key, val in six.iteritems(kwargs["tolerances"]):
for key, val in kwargs["tolerances"].items():
try:
setattr(self.tolerances, key, val)
except AttributeError:
Expand Down Expand Up @@ -532,7 +527,7 @@ def __getstate__(self):
}

def __setstate__(self, state):
for key, val in six.iteritems(state):
for key, val in state.items():
if key != "tolerances":
setattr(self, key, val)

Expand Down Expand Up @@ -600,8 +595,7 @@ def _tolerance_functions(self):
}


@six.add_metaclass(inheritdocstring)
class Model(interface.Model):
class Model(interface.Model, metaclass=inheritdocstring):
def _initialize_problem(self):
self.problem = cplex.Cplex()

Expand Down Expand Up @@ -742,7 +736,7 @@ def objective(self, value):

for key, coef in quadratic_coeffients.items():
if len(key) == 1:
var = six.next(iter(key))
var = next(iter(key))
self.problem.objective.set_quadratic_coefficients(var.name, var.name, float(coef) * 2)
else:
var1, var2 = key
Expand Down
17 changes: 5 additions & 12 deletions src/optlang/glpk_exact_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@

import logging

import six

from optlang.util import inheritdocstring
from optlang import interface
from optlang import glpk_interface
Expand All @@ -37,8 +35,7 @@
GLP_SF_AUTO, GLP_ETMLIM, glp_adv_basis, glp_read_lp, glp_scale_prob


@six.add_metaclass(inheritdocstring)
class Variable(glpk_interface.Variable):
class Variable(glpk_interface.Variable, metaclass=inheritdocstring):
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")
Expand All @@ -51,23 +48,19 @@ def type(self, value):
super(Variable, Variable).type.fset(self, value)


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


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


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


@six.add_metaclass(inheritdocstring)
class Model(glpk_interface.Model):
class Model(glpk_interface.Model, metaclass=inheritdocstring):
def _run_glp_exact(self):
return_value = glp_exact(self.problem, self.configuration._smcp)
glpk_status = glp_get_status(self.problem)
Expand Down
Loading

0 comments on commit 6fe804b

Please sign in to comment.