Speed up sol[UnaryExpr] via C-level math and refactor UnaryExpr#1224
Speed up sol[UnaryExpr] via C-level math and refactor UnaryExpr#1224Zeroto521 wants to merge 8 commits into
sol[UnaryExpr] via C-level math and refactor UnaryExpr#1224Conversation
…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
UnaryExpr
UnaryExprUnaryExpr
UnaryExprsol[UnaryExpr] via C-level math and refactor UnaryExpr
|
By the way, @Zeroto521 , out of curiosity. Are you also using PySCIPOpt for your work, or are you just speeding things up for fun? |
|
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. |
| def __abs__(self) -> UnaryExpr: | ||
| if self._op == "abs": | ||
| return <UnaryExpr>self.copy() | ||
| return UnaryExpr(Operator.fabs, self) |
There was a problem hiding this comment.
ExprLike.__abs__ can replace UnaryExpr(Operator.fabs, self)
|
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 |
| cdef class LogExpr(UnaryExpr): | ||
|
|
||
| cpdef double _evaluate(self, Solution sol) except *: | ||
| return c_log((<GenExpr>self.children[0])._evaluate(sol)) |
There was a problem hiding this comment.
Now, things like log of negative numbers will return NaN instead of a ValueError (and log(0) returns -inf). This should probably go into the changelog.
This branch is 1.45x faster than master.