Skip to content

Speed up sol[UnaryExpr] via C-level math and refactor UnaryExpr - #1224

Open
Zeroto521 wants to merge 18 commits into
scipopt:masterfrom
Zeroto521:UnaryExpr
Open

Speed up sol[UnaryExpr] via C-level math and refactor UnaryExpr#1224
Zeroto521 wants to merge 18 commits into
scipopt:masterfrom
Zeroto521:UnaryExpr

Conversation

@Zeroto521

Copy link
Copy Markdown
Contributor

This branch is 1.45x faster than master.

  • master branch: 2.9803s
  • this branch: 4.3398s
from timeit import timeit

from pyscipopt import Model, sqrt

m = Model()

n = 1000
x = m.addMatrixVar(n, lb=0, ub=1, vtype="C", name="x")
e = sqrt(x)
obj = m.addVar(lb=0, ub=None, vtype="C", name="obj")
m.addCons(obj == e.sum())

m.setObjective(obj, "maximize")
m.hideOutput()
m.optimize()
sol = m.getBestSol()

number = 10_000
cost = timeit(lambda: sol[e], number=number)
print(f"Cost of squaring an expression over {number:,} runs: {cost:.4f} seconds")

Zeroto521 added 3 commits June 8, 2026 21:50
…r unary expressions

Remove Python math module and use libc.math for C-level functions (fabs, exp, log, sqrt, sin, cos). Refactor unary expression evaluation by introducing specific subclasses (AbsExpr, ExpExpr, LogExpr, SqrtExpr, SinExpr, CosExpr) with dedicated evaluate methods using C functions, replacing the generic UnaryExpr implementation.
Update UnaryExpr type stubs to include concrete expression subclasses for
more precise type annotations. This change refines the return type of
__abs__ and introduces new expression classes.

- Change __abs__ return type from GenExpr to AbsExpr
- Add AbsExpr, ExpExpr, LogExpr, SqrtExpr, SinExpr, and CosExpr classes
- All new classes inherit from UnaryExpr
@Zeroto521 Zeroto521 changed the title Replace Python math with C-level math functions and refactor unary expressions Speed up via C-level math functions and refactor UnaryExpr Jun 8, 2026
@Zeroto521 Zeroto521 changed the title Speed up via C-level math functions and refactor UnaryExpr Speed up via C-level math and refactor UnaryExpr Jun 8, 2026
@Zeroto521 Zeroto521 changed the title Speed up via C-level math and refactor UnaryExpr Speed up sol[UnaryExpr] via C-level math and refactor UnaryExpr Jun 8, 2026
@Joao-Dionisio
Joao-Dionisio self-requested a review June 8, 2026 14:43
@Joao-Dionisio

Copy link
Copy Markdown
Member

By the way, @Zeroto521 , out of curiosity. Are you also using PySCIPOpt for your work, or are you just speeding things up for fun?

Comment thread src/pyscipopt/expr.pxi
@Zeroto521

Zeroto521 commented Jun 9, 2026

Copy link
Copy Markdown
Contributor Author

80% of the OR problems I encounter use commercial solvers or our closure algorithms for large-scale, speed-critical cases, while the rest use open-source ones for small tasks and research.

Comment thread src/pyscipopt/expr.pxi
def __abs__(self) -> UnaryExpr:
if self._op == "abs":
return <UnaryExpr>self.copy()
return UnaryExpr(Operator.fabs, self)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExprLike.__abs__ can replace UnaryExpr(Operator.fabs, self)

@Joao-Dionisio

Copy link
Copy Markdown
Member

I see @Zeroto521 , thank you! We're actually in the process of collecting users of SCIP "in the wild", particularly in industry. Would it be possible for us to reference the work you're doing? We can talk via email, if you prefer: joao.goncalves.dionisio@gmail.com

Comment thread src/pyscipopt/expr.pxi Outdated
@Joao-Dionisio

Copy link
Copy Markdown
Member

Hey @Zeroto521 , just wanted to let you know that I'm going to be quite busy until the end of August, but by then I will be able to take a proper look at the repo. Are there other changes you're planning on making?

@Zeroto521

Copy link
Copy Markdown
Contributor Author

Hey @Zeroto521 , just wanted to let you know that I'm going to be quite busy until the end of August, but by then I will be able to take a proper look at the repo. Are there other changes you're planning on making?

Yeah, I've already finished the basic version (drop OP, Split Variable and Expr). But they depend on this PR, so I haven't submitted it yet.

Copilot AI lite review requested due to automatic review settings September 6, 2026 08:24

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

Comment thread src/pyscipopt/expr.pxi
from libc.math cimport cos as c_cos
from libc.math cimport exp as c_exp
from libc.math cimport fabs as c_fabs
from libc.math cimport INFINITY

@Zeroto521 Zeroto521 Sep 6, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This version of INFINITY doesn't have any Python object runtime cost

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants