-
Notifications
You must be signed in to change notification settings - Fork 274
Feature: Merge Expr and GenExpr
#1114
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
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.
c13b346 to
8719b91
Compare
This reverts commit 28e6673.
Converted SumExpr, ProdExpr, and PowExpr from cdef classes to regular Python classes for improved compatibility and maintainability.
There was a problem hiding this 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
childrenreplacingterms Variableclass no longer inherits fromExpr- 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.
Co-authored-by: Copilot <[email protected]>
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.
Implemented __imul__ and __isub__ methods for the Variable class, delegating to MonomialExpr. This allows in-place multiplication and subtraction operations on Variable instances.
Implements the __imul__ method for the Expr class, enabling in-place multiplication. For sum expressions multiplied by a constant, the operation is performed directly on the children; otherwise, it falls back to regular multiplication.
Implemented the __iadd__ method for the ProdExpr class to support in-place addition. Added a new 'config' file with setup and test commands for the project environment.
Implements the __imul__ method for ProdExpr to support in-place multiplication with constants and other expressions. Handles the special case where multiplying by zero returns a ConstExpr(0.0).
Introduces _hash_child and _is_child_equal methods to FuncExpr for improved child comparison and hashing. Updates ProdExpr addition logic to use the new _is_child_equal method for cleaner and more robust equality checks.
Added __mul__, __imul__, and __truediv__ methods to PowExpr for combining exponents when multiplying or dividing similar expressions. Implemented __add__ in LogExpr to combine logs of the same base. These changes improve symbolic manipulation and algebraic simplification capabilities.
Reformatted code for consistency, improved readability by reducing line breaks and simplifying expressions, and replaced double quotes with single quotes in docstrings. No functional changes were made.
Corrects the sign in the calculation of 'coeffs' by multiplying with -self.M[j] instead of self.M[j]. This ensures the coefficients are computed as intended for the Benders cut test.
Deleted the test_eq.py and test_ge.py files, which contained test scripts for expression operator behavior in pyscipopt. These files are no longer needed.
Updated the Expr constructor to clarify the allowed key types in the error message. Added tests to verify TypeError is raised for invalid keys and to check slot behavior in the Expr class.
Introduces a test to verify that Expr and its components enforce __slots__ by preventing dynamic attribute assignment, ensuring memory efficiency and attribute control.
Moved and grouped assertions in test_expr_op_expr for better logical flow. The sequence of operations and their corresponding type checks were reorganized, but the test coverage remains the same.
Introduces tests to verify the equality and type checking behavior of the Term class, ensuring correct comparison with other Term instances and raising TypeError for invalid comparisons.
Introduces test_getitem to verify correct indexing of Term objects, including valid access, type errors, and index errors.
Renamed test_init_error to test_Expr_init_error and test_slots to test_Expr_slots to improve test function naming consistency and clarity.
Added a new test 'test_Expr_getitem' to verify the __getitem__ behavior of Expr objects, including key lookups and error handling. Also added a .github/copilot-instructions.md file with architecture, development, and coding guidelines for contributors.
Introduces a new test to verify that applying abs() to an Expr returns an AbsExpr instance with the correct string representation and child expression.
Introduces unit tests to verify the behavior of the Expr._fchild() method with various expression compositions.
Adds tests to ensure that adding unsupported types (such as strings and lists) to Expr instances raises a TypeError.
Introduces tests to ensure multiplying an Expr by unsupported types (such as a string or list) raises a TypeError.
Introduces test_Expr_div to verify division behavior for Expr objects, including division by zero, scalar division, reciprocal, and division of expressions. Ensures correct exceptions and string representations are produced.
Introduces a test to verify that raising an expression to the power of 0 returns an expression equivalent to 1. Ensures correct handling of the power operation edge case.
Introduces a new test to verify the behavior of the Expr class when used as the exponent in the power operation (rpow). The test checks correct string output, and ensures appropriate exceptions are raised for invalid base types and negative bases.
Improves multiplication logic in Expr by handling cases where either operand has no children, returning a constant zero. Updates __slots__ in ProdExpr and PowExpr to only include relevant attributes, removing 'children' from both classes.
Replaces equality check with identity check in test_getitem to ensure the variable returned by Term is the same object as the original variable.
Renamed test_Expr_mul_unsupported_type to test_Expr_mul and added additional assertions to cover valid multiplication cases, including multiplication by zero, by an empty Expr, and by itself. This improves test coverage for the Expr multiplication operator.
Close to #1074. This is a breaking change. We can release the 5.7.0 version first.