Skip to content

Add addConsLinear() to avoid creating expressions in MILPs - #1273

Open
Joao-Dionisio wants to merge 1 commit into
masterfrom
add-cons-linear
Open

Joao-Dionisio wants to merge 1 commit into
masterfrom
add-cons-linear

Conversation

@Joao-Dionisio

Copy link
Copy Markdown
Member

I've been running some experiments comparing the performance of different Python interfaces. PySCIPOpt lands in a pretty nice second place, behind only OR-Tools. Still, why not try to get first place? :)

For each nonzero, quicksum(a * x[j] for ...) builds a new Expr, hashes a Term, and inserts it into a dict. addCons then goes through ExprCons and iterates that dict again to fill the C arrays. OR-Tools does a single C++ call per coefficient (ct.SetCoefficient(x[j], a)) and creates no Python objects.

In this PR, I'm adding an addConsLinear() function, similar to what addConsKnapsack() and addConsSOS1() do, adding the constraints directly while skipping the expression building.

I locally timed the building 13 MIPLIB 2017 instances with more than 100k nonzeros each. Here are the results:

instance vars conss nnz quicksum + addCons (s) addConsLinear (s) OR-Tools (s)
geometric mean 0.706 0.181 0.312
30n20b8 18,380 576 109,706 0.048 0.022 0.024
neos-1122047 5,100 57,791 163,640 0.193 0.046 0.060
atlanta-ip 48,738 21,732 257,532 0.173 0.080 0.081
rail01 117,527 46,843 392,086 0.355 0.181 0.151
supportcase6 130,052 771 584,976 0.351 0.183 0.177
roi2alpha3n4 6,816 1,251 878,812 0.249 0.032 0.132
neos-5049753-cuanza 242,736 322,248 1,440,672 1.50 0.517 0.525
ns1644855 30,200 40,698 2,110,696 0.727 0.112 0.360
supportcase7 138,844 6,532 2,845,545 0.998 0.265 0.503
neos-5052403-cygnet 32,868 38,268 4,898,304 1.52 0.191 0.783
neos-2075418-temuka 122,304 349,602 7,610,261 3.24 0.689 1.42
neos-3402454-bohle 2,904 2,897,380 8,953,800 10.22 2.45 3.01
square41 62,234 40,160 13,566,426 3.99 0.538 2.05

PS: In my tests, the matrix API is not faster than just using normal loops.

@Zeroto521 would you mind taking a look, please?

@Joao-Dionisio Joao-Dionisio self-assigned this Oct 16, 2026
@Joao-Dionisio Joao-Dionisio changed the title Add addConsLinear() for avoiding creating expressions in MILPs Add addConsLinear() to avoid creating expressions in MILPs Oct 16, 2026
@Joao-Dionisio

Copy link
Copy Markdown
Member Author

Despite the numbers we started from being already pretty small, this often matters in industrial applications. We academics may be used to the actual solving to take most of the solving time, but often the problems are actually super easy if presolving detects a certain structure, they're just huge.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant