Skip to content

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

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

Speed up sol[UnaryExpr] via C-level math and refactor UnaryExpr#1224
Zeroto521 wants to merge 8 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)

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.

2 participants