HiGHS icon indicating copy to clipboard operation
HiGHS copied to clipboard

Segmentation Fault on windows 10 python 3.12 highspy>1.9

Open Ymiros0 opened this issue 2 months ago • 9 comments

As the title says I'm running highspy in python 3.12 on a windows 10 operating system and even the simplest examples from the documentation segfault when I try to run them. For example

import highspy
import numpy as np
h = highspy.Highs()
x0 = h.addVariable(lb = 0, ub = 4)
x1 = h.addVariable(lb = 1, ub = 7)

h.addConstr(5 <=   x0 + 2*x1 <= 15)
h.addConstr(6 <= 3*x0 + 2*x1)

h.minimize(x0 + x1)

gives me the output

Running HiGHS 1.12.0 (git hash: 755a8e0): Copyright (c) 2025 HiGHS under MIT licence terms
LP has 2 rows; 2 cols; 4 nonzeros
Coefficient ranges:
  Matrix  [1e+00, 3e+00]
  Cost    [1e+00, 1e+00]
  Bound   [1e+00, 7e+00]
  RHS     [5e+00, 2e+01]
Presolving model
2 rows, 2 cols, 4 nonzeros  0s
2 rows, 2 cols, 4 nonzeros  0s
Presolve reductions: rows 2(-0); columns 2(-0); nonzeros 4(-0) - Not reduced
Problem not reduced by presolve: solving the LP
Using EKK dual simplex solver - serial
  Iteration        Objective     Infeasibilities num(sum)
          0     1.0000013886e+00 Pr: 2(7) 0s
          2     2.7500000000e+00 Pr: 0(0) 0s

Model status        : Optimal
Simplex   iterations: 2
Objective value     :  2.7500000000e+00
P-D objective error :  0.0000000000e+00
HiGHS run time      :          0.00
Segmentation fault

Now this case isn't too bad as it still finishes just fine and segfaults only at the end, but for the more complicated problem I use highs for it segfaults right away, output:

Running HiGHS 1.12.0 (git hash: 755a8e0): Copyright (c) 2025 HiGHS under MIT licence terms
MIP has 671 rows; 2951 cols; 15034 nonzeros; 2951 integer variables (0 binary)
Coefficient ranges:
  Matrix  [1e-01, 7e+01]
  Cost    [1e+00, 5e+03]
  Bound   [1e+01, 1e+04]
  RHS     [1e+01, 6e+03]
Presolving model
Segmentation fault

If I use highspy 1.9 none of the examples and also my real problem work just fine.

An another note I'm sorry, this probably doesn't belong here, but I'm honestly lost; I'm trying to hotstart the solver with setSolution, but failing to, is there anywhere or anyone I could ask about this?

Edit: Fix Formatting

Ymiros0 avatar Oct 26 '25 10:10 Ymiros0

As a sanity check, I've run your code using a fresh pip (local) installation of v1.12 but I can't reproduce the segfault. Perhaps it's due to you using a version of Python that we don't support? @galabovaa will know much better than me!

As for the use of setSolution, here's an extension of your code illustrating its use and anticipating that you're only setting the (primal) values of the decision variables.

import numpy as np
import highspy
h = highspy.Highs()
x0 = h.addVariable(lb = 0, ub = 4)
x1 = h.addVariable(lb = 1, ub = 7)

h.addConstr(5 <=   x0 + 2*x1 <= 15)
h.addConstr(6 <= 3*x0 + 2*x1)

h.minimize(x0 + x1)

print("Cold start required ", h.getInfo().simplex_iteration_count, " iterations")

solution0 = h.getSolution()

col_value = solution0.col_value

solution1 = highspy.HighsSolution()

solution1.col_value = col_value

h.clearSolver()

h.setSolution(solution1)

h.run()

print("Hot start required ", h.getInfo().simplex_iteration_count, " iterations")

In this case, after starting with the optimal values of the decision variables, no simplex iterations are required. This is because your LP has a non-degenerate solution. If an LP has a degenerate solution, multiple iterations may be necessary to find an optimal basis.

jajhall avatar Oct 26 '25 10:10 jajhall

Python 3.12 should be fine. I wonder if you can debug the solve somehow, to give us more information about where the segfault is happening

Could you, for example, try running it with faulthandler, e.g.

import faulthandler
faulthandler.enable()
import highspy
...

I can try modifying the code to compile locally with debug info too

galabovaa avatar Oct 26 '25 22:10 galabovaa

Running with faulthandler gives me this output:

Presolving model
Windows fatal exception: 552 rows, 2951 cols, 14882 nonzeros  0s
access violation

Thread 0x00002434 (most recent call first):
  File "<file>", line 1296 in build_and_solve_big_model
  File "<file>", line 1578 in <module>
Segmentation fault

I see, thanks for the quick reply. I had been trying to use the other definition of setSolution that allows me to provide 3 args, num_entries, indices and values, but I couldn't get it to work, it would always say something like

ERROR:   setSolution: User solution index 1 has value 49301504 out of range [0, 2)

Apparently it works fine if I supply all indices and all values which I think kinda defeats the purpose of supplying the indices. For my real problem many variables are zero, so it seems hotstarting doesn't help me much. For some reason I also cannot get it to work properly. If I just do two runs right after another like you did it in your example it works, if I save the col_value in a file and then read the file it also works, but if I rerun the file it tells me the solution is infeasible. I can only imagine this if the order of the columns is not deterministic, but the creation is all done from static data, so I don't understand how that could be the case.

Ymiros0 avatar Oct 27 '25 10:10 Ymiros0

Firstly, the following code (using avgas.mps.txt) runs fine

import numpy as np
import highspy
h = highspy.Highs()

h.readModel('avgas.mps')

status = h.run()

print('h.run returns status = ', status)
print('h.run yields model status = ', h.getModelStatus())

h.writeSolution("", 1)

index = np.array([0, 1, 7])
value = np.array([0, 1, 0], dtype=np.float64)

h.setSolution(3, index, value)
h.run()


h.setSolution(6, np.array([0, 1, 2, 3, 4, 7]), np.array([0, 1, 0.75, 0.25, 0.5, 0]))
h.run()

as does knapsack.py

jajhall avatar Oct 27 '25 12:10 jajhall

If I just do two runs right after another like you did it in your example it works, if I save the col_value in a file and then read the file it also works, but if I rerun the file it tells me the solution is infeasible. I can only imagine this if the order of the columns is not deterministic, but the creation is all done from static data, so I don't understand how that could be the case.

I see that you're using v1.12, so I don't see how this can happen - there was an issue with saving models as .lp files and trying to read a saved solution (search for #2463 in the release notes. Could you share the code where you observe that you "save the col_value in a file and then read the file it also works, but if I rerun the file it tells me the solution is infeasible."

jajhall avatar Oct 27 '25 12:10 jajhall

I see, the first was just user error, I misunderstood what value I had to assign to num_var, it works now for my small test file and the file you shared. I now realise that there are probably better ways of saving and reading the solution, but I tried it like this:

highs.run()
sol = highs.getSolution()
np.save(f"test.npy", sol.col_value)

sol = HighsSolution()
sol.col_value = np.load(f"test.npy")
highs.setSolution(sol)
print("Test rerun")
highs.run()

This works fine, if I comment out the saving on a later run it doesn't work anymore. I can't reproduce it with a small model at the moment though. But what confuses me is that it always claims different rows are infeasible for different reasons despite the columns and rows looking completely consistent when I print them. From the feasability report it seems to me like it's not reading the solution in the correct order as the row constraints align with the ones I see in the printed solution, just the sums don't. I am using 1.9 by the way due to the Segmentation Fault I experience on 1.10-1.12.

Ymiros0 avatar Oct 27 '25 15:10 Ymiros0

Other than the use of np.save and np.load, what you're doing here is exactly what I'm doing when saving the col_value in code. Hence I'm tempted to assume that there's something odd in the way that np.save and np.load work, but I'm sure that they are widely-used methods...

jajhall avatar Oct 27 '25 15:10 jajhall

I also checked the value obtained from loading with np.load and it aligns perfectly with the printed solution and also still closely aligns with the solution printed from the solve after the detected infeasability. If you wish I can share the entire file, it contains no sensitive data. Well I'd probably have to make a git repo because it requires some other files as well to run.

Ymiros0 avatar Oct 27 '25 15:10 Ymiros0

Sorry got a bit busy, but I now uploaded the relevant code to https://github.com/Ymiros0/Highsissue It should run as it is as long as you have the relevant packages installed. If you have a debug version I'd be willing to try that as well @galabovaa

Ymiros0 avatar Nov 19 '25 20:11 Ymiros0