ERGO-Code / HiGHS

Linear optimization software
MIT License
992 stars 184 forks source link

highspy (direct calls via pybind11) incorrectly handles sliced numpy arrays #1903

Closed mathgeekcoder closed 2 months ago

mathgeekcoder commented 2 months ago

Adding issue for visibility, although I've already fixed the code in #1891.

Consider the following:

h = highspy.Highs()

zero, ones = np.zeros(10), np.ones(10)
h.addCols(10, zero, zero, ones, 0, [], [], [])

x = np.arange(10, dtype=np.int32) # [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
tmp = x[1::2]                     # [1, 3, 5, 7, 9]

h.addRow(1, 1, len(tmp), tmp, ones)

We should expect that [1, 3, 5, 7, 9] is added as a row, but currently [1, 2, 3, 4, 5] is added instead.

The issue is in highs_bindings.cpp where we use py::array_t<T>. I've changed this to py::array_t<T, py::array::c_style | py::array::forcecast> instead. This will ensure that the underlying raw data is contiguous and of correct type.

jajhall commented 2 months ago

Closed by #1942