Skip to content

Conversation

@Zeroto521
Copy link
Contributor

Close to #1074. This is a breaking change. We can release the 5.7.0 version first.

Changed the type of _lhs and _rhs attributes in the ExprCons class from unspecified to 'object' to clarify their expected type and improve type safety.
Replaces the __init__ method with __cinit__ in the Variable class and updates the argument type to SCIP_VAR*.
Updated references from 'terms' to 'children' for Expr objects throughout Model methods to reflect changes in the Expr API. This ensures compatibility with the updated data structure and avoids errors when accessing expression terms.
Introduces _to_nodes methods for Expr, PolynomialExpr, and UnaryExpr to convert expressions into node lists for SCIP construction. Refactors Model's constraint creation to use the new node format, simplifying and clarifying the mapping from expression trees to SCIP nonlinear constraints.
Changed Expr from a Cython cdef class to a standard Python class for improved compatibility and maintainability. Removed cdef public dict children, as attribute is now managed in Python.
Converted SumExpr, ProdExpr, and PowExpr from cdef classes to regular Python classes for improved compatibility and maintainability.
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR merges the Expr and GenExpr classes into a unified expression system. The refactoring replaces the previous dual-system (polynomial Expr and general GenExpr) with a single hierarchy based on Expr, using specialized subclasses like PolynomialExpr, SumExpr, ProdExpr, and various function expressions (ExpExpr, LogExpr, etc.).

Key changes:

  • Unified expression representation with children replacing terms
  • Variable class no longer inherits from Expr
  • Simplified expression tree structure with improved type system
  • Refactored constraint creation methods to use the new expression system

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/pyscipopt/scip.pyi Updated Variable class to remove inheritance from Expr
src/pyscipopt/scip.pxi Modified Variable class structure and added operator overloading methods to delegate to expression system; refactored constraint creation methods to use children instead of terms
src/pyscipopt/scip.pxd Updated Variable class declaration to remove Expr inheritance
src/pyscipopt/propagator.pxi Simplified variable creation by removing unnecessary temporary variable
src/pyscipopt/expr.pxi Complete rewrite of expression system replacing Expr/GenExpr duality with unified class hierarchy

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Zeroto521 and others added 3 commits November 18, 2025 18:35
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Deleted the definition of the Expr class, which was not used in the code. This helps clean up the codebase and improves maintainability.
Changed ExprCons from a cdef class to a standard Python class and added type hints to the constructor parameters. This improves code readability and compatibility with Python tooling.
Refactored the __mul__ method in PolynomialExpr to iterate directly over the _children dictionary for improved clarity and efficiency. This change replaces iteration over the object itself with explicit key-value access, making the code more readable and potentially faster.
Simplifies and unifies the logic for merging expression dictionaries in Expr and PolynomialExpr classes. Refactors _to_dict to accept Expr objects directly, adds type annotations, and moves utility methods (_normalize, degree, copy) for better code organization and clarity.
Adjusts the __imul__ method in Expr to ensure that the result of in-place multiplication returns the correct polynomial type, depending on the operand types. This improves type consistency when multiplying PolynomialExpr instances.
Converted _to_polynomial from a Python method to a cdef method for improved performance and type safety. Updated the return type to Expr and adjusted variable declarations accordingly.
Updated type annotations for the 'children' variable and the _to_subclass method in PolynomialExpr for improved type clarity. Also added a check for 'self' in __mul__ to prevent errors when multiplying with empty expressions.
Moved the to_array decorator to a new _decorator.py module for reuse and maintainability. Updated expr.pxi to import and use the new to_array decorator, removing the previous local implementation.
Changed several methods in Term and Expr classes from def to cpdef for improved Cython performance and accessibility from both C and Python. Also refactored _to_node methods to use explicit Cython variable declarations and streamlined node construction logic.
Refactors the internal helper function _to_unaryexpr to _vec_to_unary and updates all usages accordingly for consistency and clarity in naming.
Implements the __neg__ special method for ConstExpr, allowing negation of constant expressions using the unary minus operator.
Added _ExprKey to the accepted key types for Expr.__getitem__, allowing more flexible indexing. This change improves compatibility with different key types when accessing expression children.
Improves the _is_equal method in Expr to more accurately compare ProdExpr and PowExpr instances by checking their specific attributes (coef and expo), and to handle UnaryExpr types. Also changes the method signature to use cdef for better performance.
Changed several internal methods in the Expr class from Python def to cdef for performance and clarity. Updated _fchild, _is_sum, _is_const, and _is_zero to be cdef methods, and improved the logic in _is_const for better type checking.
Replaces direct access to _children.items() with a new items() method throughout the Expr class. This improves encapsulation and consistency when iterating over child elements.
Added explicit type annotations for __radd__, __rmul__, __pow__, and __rpow__ methods in Expr, PolynomialExpr, and ConstExpr classes to improve type safety and code clarity. Also updated _to_subclass method signature in PolynomialExpr for consistency.
Added a static method _is_term to Expr for better identification of term expressions. Updated UnaryExpr's __repr__ to display constants and terms more clearly, improving debugging and readability.
Changed the type of loop variable 'i' from 'Expr' to 'object' in quicksum and quickprod to allow for more flexible input types in the expressions iterator.
Added type annotations to several magic methods in Expr and related classes for improved type safety. Refactored division methods to clarify argument types and method structure, and updated some static methods to use cdef for better Cython integration.
Introduced exp, log, sqrt, sin, and cos methods to the Expr and Variable classes for more convenient mathematical expression building. Updated function signatures to accept np.ndarray as input for element-wise operations.
This commit refactors the Expr and Variable classes to support NumPy universal functions (ufuncs) via __array_ufunc__ and sets __array_priority__ for correct dispatch. Operator overloads in Expr and its subclasses are unified to consistently use a new _from_other method for type conversion, improving type safety and maintainability. Vectorized ufuncs for common operations are added, and Variable now delegates NumPy operations to PolynomialExpr. Rich comparison methods are consolidated for consistency.
Replaces explicit dunder method calls (e.g., __add__, __mul__) with their operator equivalents (e.g., +, *). This improves code readability and consistency in the Expr class implementation.
Refactors the Expr class to simplify and update NumPy ufunc dispatching, replacing cython ufuncs with direct lambda and class-based handlers. Moves and consolidates operator overloads, such as __abs__ and __sub__, and updates Variable to directly return AbsExpr and other unary expressions. Cleans up redundant code and improves consistency in mathematical operation handling for expressions and variables.
Moved the NumPy ufunc dispatch map to a shared EXPR_UFUNC_DISPATCH variable for reuse between Expr and Variable classes. Updated __array_ufunc__ methods to use the shared dispatch, improving maintainability and consistency.
Replaces explicit dunder method calls with operator syntax in Variable class methods, improving readability and consistency with Python operator overloading conventions.
Eliminated explicit isinstance checks and NotImplemented returns from operator overloads in the Expr class, relying on _from_other for type handling. This streamlines the code and centralizes type validation.
`np.ndim(Term(...))` will use `len`
The 'cython' import was not used in expr.pxi and has been removed to clean up the code.
Removed unused degree and _to_node methods from _ExprKey. Updated Expr.degree to iterate directly over self. Refactored _to_node to use _ExprKey.unwrap and renamed variables for clarity.
cython doesn't support decorator well
Replaced lambda functions in EXPR_UFUNC_DISPATCH with corresponding functions from the operator module for improved readability and maintainability.
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