From 0443f19970cd4d96d6df72e27bc9133edb305b07 Mon Sep 17 00:00:00 2001 From: Christian Diener Date: Mon, 15 Jul 2024 14:11:44 +0200 Subject: [PATCH] [fix] Make the hybrid interface work for numpy>=2.0 (#266) * switch to np.inf * update changelog * circuimvent safety bug * circuimvent safety bug --- CHANGELOG.rst | 1 + src/optlang/matrix_interface.py | 18 +++++++++--------- tox.ini | 2 +- 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4c61b3c..da17f2e 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -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 ----- diff --git a/src/optlang/matrix_interface.py b/src/optlang/matrix_interface.py index 4933c4d..fbc021e 100644 --- a/src/optlang/matrix_interface.py +++ b/src/optlang/matrix_interface.py @@ -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 @@ -495,7 +495,7 @@ 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 @@ -503,7 +503,7 @@ def lb(self, value): 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 @@ -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 @@ -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( { diff --git a/tox.ini b/tox.ini index cdf2780..4e09b02 100644 --- a/tox.ini +++ b/tox.ini @@ -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