Skip to content

Commit de8fab1

Browse files
committed
Remove hard numpy dep
1 parent 5c09bed commit de8fab1

1 file changed

Lines changed: 15 additions & 18 deletions

File tree

pyomo/contrib/solver/solvers/xpress/xpress_base.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
from typing import Any, Iterator, Mapping, Optional, Sequence, cast
4141

4242
from pyomo.common.collections import ComponentMap
43-
from pyomo.common.dependencies import attempt_import, numpy as np
43+
from pyomo.common.dependencies import attempt_import
4444
from pyomo.common.errors import InfeasibleConstraintException
4545
from pyomo.common.tee import capture_output, TeeStream
4646
from pyomo.common.timing import HierarchicalTimer
@@ -93,7 +93,7 @@
9393

9494
# ---- Pure-Python module-level constants (no xp dependency) ------------------
9595

96-
_BOUND_TYPE_CODES = np.array([76, 85], dtype=np.int8) # 'L', 'U'
96+
_BOUND_TYPE_CODES = ['L', 'U']
9797

9898
_VAR_TYPE_CODES: dict[tuple[bool, bool], int] = {
9999
(True, True): 66, # 'B' -- binary (implies integer)
@@ -515,7 +515,8 @@ def _set_var_bounds(self, prob, pyo_vars: list[VarData], xp_vars) -> None:
515515
"""Bulk-set variable bounds for pyo_vars, respecting fixed-var pinning."""
516516
n = len(pyo_vars)
517517
cbounds = [b for var in pyo_vars for b in self._var_bounds(var)]
518-
prob.chgBounds(np.repeat(xp_vars, 2), np.tile(_BOUND_TYPE_CODES, n), cbounds)
518+
cols = [v for v in xp_vars for _ in range(2)]
519+
prob.chgBounds(cols, _BOUND_TYPE_CODES * n, cbounds)
519520

520521
def _add_vars_impl(self, prob, pyo_vars: list[VarData], symbolic_labels: bool):
521522
"""Add columns, set types and bounds. Returns the xp.var array.
@@ -579,16 +580,15 @@ def _add_sos_impl(
579580
n = len(pyo_sos)
580581
if n == 0:
581582
return []
582-
settype = np.empty(n, dtype=np.int8)
583-
setstart = np.empty(n + 1, dtype=np.int64)
584-
setstart[0] = 0
583+
settype: list[int] = []
584+
setstart: list[int] = [0]
585585
setind: list[int] = []
586586
refval: list[float] = []
587-
for i, con in enumerate(pyo_sos):
587+
for con in pyo_sos:
588588
setind.extend(var_map[id(var)].index for var in con.variables)
589589
refval.extend(float(w) for _, w in con.get_items())
590-
settype[i] = ord('1' if con.level == 1 else '2')
591-
setstart[i + 1] = len(setind)
590+
settype.append(ord('1' if con.level == 1 else '2'))
591+
setstart.append(len(setind))
592592
nsos = prob.attributes.sets
593593
prob.addSets(settype, setstart, setind, refval)
594594
if symbolic_labels:
@@ -597,18 +597,15 @@ def _add_sos_impl(
597597
return prob.getSOS(first=nsos, last=nsos + n - 1)
598598

599599
def _warmstart(self, prob, vars: list[VarData], entind: list[int]) -> None:
600-
n = len(entind)
601-
ws_vals = np.empty(n, dtype=np.float64)
602-
ws_cols = np.empty(n, dtype=np.int32)
603-
count = 0
600+
ws_vals: list[float] = []
601+
ws_cols: list[int] = []
604602
for j in entind:
605603
var = vars[j]
606604
if var.value is not None:
607-
ws_vals[count] = var.value
608-
ws_cols[count] = j
609-
count += 1
610-
if count > 0:
611-
prob.addMipSol(ws_vals[:count], ws_cols[:count])
605+
ws_vals.append(var.value)
606+
ws_cols.append(j)
607+
if ws_vals:
608+
prob.addMipSol(ws_vals, ws_cols)
612609

613610
def _apply_solver_controls(self, prob, config: BranchAndBoundConfig) -> None:
614611
if config.time_limit is not None:

0 commit comments

Comments
 (0)