Skip to content

Commit

Permalink
[fix] Make the hybrid interface work for numpy>=2.0 (#266)
Browse files Browse the repository at this point in the history
* switch to np.inf

* update changelog

* circuimvent safety bug

* circuimvent safety bug
  • Loading branch information
cdiener authored Jul 15, 2024
1 parent 11a6295 commit 0443f19
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Next Release
1.8.2
-----
* fix the feasibility check in the hybrid solver
* make optlang compatible with numpy>=2.0

1.8.1
-----
Expand Down
18 changes: 9 additions & 9 deletions src/optlang/matrix_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
from collections import defaultdict
from typing import NamedTuple

from numpy import Infinity, array, concatenate, zeros
from numpy import inf, array, concatenate, zeros
from scipy.sparse import csc_matrix

from optlang import available_solvers, interface, symbolics
Expand Down Expand Up @@ -495,15 +495,15 @@ def name(self, value):
def lb(self, value):
self._check_valid_lower_bound(value)
if getattr(self, "problem", None) is not None:
lb = -Infinity if value is None else float(value)
lb = -inf if value is None else float(value)
self.problem.problem.constraint_lbs[self.name] = lb
self._lb = value

@interface.Constraint.ub.setter
def ub(self, value):
self._check_valid_upper_bound(value)
if getattr(self, "problem", None) is not None:
ub = Infinity if value is None else float(value)
ub = inf if value is None else float(value)
self.problem.problem.constraint_ubs[self.name] = ub
self._ub = value

Expand Down Expand Up @@ -899,18 +899,18 @@ def optimize(self):
def _set_variable_bounds_on_problem(self, var_lb, var_ub):
self.problem.reset()
for var, val in var_lb:
lb = -Infinity if val is None else float(val)
lb = -inf if val is None else float(val)
self.problem.variable_lbs[var.name] = lb
for var, val in var_ub:
ub = Infinity if val is None else val
ub = inf if val is None else val
self.problem.variable_ubs[var.name] = float(ub)

def _add_variables(self, variables):
super()._add_variables(variables)
self.problem.reset()
for variable in variables:
lb = -Infinity if variable.lb is None else float(variable.lb)
ub = Infinity if variable.ub is None else float(variable.ub)
lb = -inf if variable.lb is None else float(variable.lb)
ub = inf if variable.ub is None else float(variable.ub)
self.problem.variables.add(variable.name)
self.problem.variable_lbs[variable.name] = lb
self.problem.variable_ubs[variable.name] = ub
Expand Down Expand Up @@ -940,8 +940,8 @@ def _add_constraints(self, constraints, sloppy=False):
constraint._problem = None
if constraint.is_Linear:
_, coeff_dict, _ = parse_optimization_expression(constraint)
lb = -Infinity if constraint.lb is None else float(constraint.lb)
ub = Infinity if constraint.ub is None else float(constraint.ub)
lb = -inf if constraint.lb is None else float(constraint.lb)
ub = inf if constraint.ub is None else float(constraint.ub)
self.problem.constraints.add(constraint.name)
self.problem.constraint_coefs.update(
{
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ deps=
pip>=21.1
safety
commands=
safety check --full-report
safety check --full-report -i 70612

[testenv:mypy]
skip_install = True
Expand Down

0 comments on commit 0443f19

Please sign in to comment.