Skip to content

Commit 4512852

Browse files
committed
add var_get_column
1 parent 8d7286d commit 4512852

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

mip/highs.py

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,48 @@ def var_set_var_type(self: "SolverHighs", var: "mip.Var", value: str):
735735
self._var_type[var.idx] = value
736736

737737
def var_get_column(self: "SolverHighs", var: "mip.Var") -> "Column":
738-
# TODO
739-
raise NotImplementedError()
738+
# Call method twice:
739+
# - first, to get the sizes for coefficients,
740+
num_col = ffi.new("int*")
741+
costs = ffi.new("double[]", 1)
742+
lower = ffi.new("double[]", 1)
743+
upper = ffi.new("double[]", 1)
744+
num_nz = ffi.new("int*")
745+
status = self._lib.Highs_getColsByRange(
746+
self._model,
747+
var.idx, # from_col
748+
var.idx, # to_col
749+
num_col,
750+
costs,
751+
lower,
752+
upper,
753+
num_nz,
754+
ffi.NULL, # matrix_start
755+
ffi.NULL, # matrix_index
756+
ffi.NULL, # matrix_value
757+
)
758+
# - second, to get the coefficients in pre-allocated arrays.
759+
matrix_start = ffi.new("int[]", 1)
760+
matrix_index = ffi.new("int[]", num_nz[0])
761+
matrix_value = ffi.new("double[]", num_nz[0])
762+
status = self._lib.Highs_getColsByRange(
763+
self._model,
764+
var.idx, # from_col
765+
var.idx, # to_col
766+
num_col,
767+
costs,
768+
lower,
769+
upper,
770+
num_nz,
771+
matrix_start,
772+
matrix_index,
773+
matrix_value,
774+
)
775+
776+
return mip.Column(
777+
constrs=[self.model.constrs[matrix_index[i]] for i in range(num_nz[0])],
778+
coeffs=[matrix_value[i] for i in range(num_nz[0])],
779+
)
740780

741781
def var_set_column(self: "SolverHighs", var: "mip.Var", value: "Column"):
742782
# TODO

0 commit comments

Comments
 (0)