Skip to content

Commit 3833085

Browse files
Joao-DionisiommghannamOpt-Mucca
authored
Add addExprNonlinear (#760)
* Add addExprNonlinear * Add test addExprNonlinear * Update CHANGELOG * Add TODO * EVENTTYPE Overhaul * Directly use SCIP macros instead of their value * Update CHANGELOG * Update CHANGELOG * Add NODEDELETE to scip.pxi * Add test prototype for events * Add more SCIP events * Add all event types as of SCIP 8 * Add test for event identification * Update CHANGELOG * Add method for getting Event names * Add ability to print stage name as string. Add test. * Update CHANGELOG * Add __str__ to Event class * Update src/pyscipopt/scip.pxi Co-authored-by: Mohammed Ghannam <mohammad.m.ghannam@gmail.com> * Add function getconshdlrname * Remove commented code * Update version * Rename * Add check to test_cons * Change assert to SOS2 * Update INSTALL.md * Change manylinux Co-authored-by: Mohammed Ghannam <mohammad.m.ghannam@gmail.com> * Update CHANGELOG.md * Update CHANGELOG.md * Fix outdated time.clock * Update README * Update CHANGELOG --------- Co-authored-by: Mohammed Ghannam <mohammad.m.ghannam@gmail.com> Co-authored-by: Mark Turner <turner@zib.de> Co-authored-by: Mark Turner <64978342+Opt-Mucca@users.noreply.github.com>
1 parent 5a166be commit 3833085

File tree

3 files changed

+40
-2
lines changed

3 files changed

+40
-2
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
# CHANGELOG
22

3-
## Unreleased
3+
## 4.4.0 - 2023-12-04
44
### Added
5+
- Added SCIP function addExprNonlinear
56
- Add support for Cython 3
67
- Added methods for getting the names of the current stage and of an event
78
### Fixed

src/pyscipopt/scip.pxi

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2365,6 +2365,7 @@ cdef class Model:
23652365
free(monomials)
23662366
free(termcoefs)
23672367
return PyCons
2368+
23682369

23692370
def _addGenNonlinearCons(self, ExprCons cons, **kwargs):
23702371
cdef SCIP_EXPR** childrenexpr
@@ -2489,6 +2490,24 @@ cdef class Model:
24892490

24902491
return PyCons
24912492

2493+
# TODO Find a better way to retrieve a scip expression from a python expression. Consider making GenExpr include Expr, to avoid using Union. See PR #760.
2494+
from typing import Union
2495+
def addExprNonlinear(self, Constraint cons, expr: Union[Expr,GenExpr], float coef):
2496+
"""
2497+
Add coef*expr to nonlinear constraint.
2498+
"""
2499+
assert self.getStage() == 1, "addExprNonlinear cannot be called in stage %i." % self.getStage()
2500+
assert cons.isNonlinear(), "addExprNonlinear can only be called with nonlinear constraints."
2501+
2502+
cdef Constraint temp_cons
2503+
cdef SCIP_EXPR* scip_expr
2504+
2505+
temp_cons = self.addCons(expr <= 0)
2506+
scip_expr = SCIPgetExprNonlinear(temp_cons.scip_cons)
2507+
2508+
PY_SCIP_CALL(SCIPaddExprNonlinear(self._scip, cons.scip_cons, scip_expr, coef))
2509+
self.delCons(temp_cons)
2510+
24922511
def addConsCoeff(self, Constraint cons, Variable var, coeff):
24932512
"""Add coefficient to the linear constraint (if non-zero).
24942513

tests/test_nonlinear.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,4 +284,22 @@ def test_quad_coeffs():
284284
assert quadterms[0][1] == 0.5
285285

286286
assert linterms[0][0].name == z.name
287-
assert linterms[0][1] == 4
287+
assert linterms[0][1] == 4
288+
289+
def test_addExprNonLinear():
290+
m = Model()
291+
x = m.addVar("x", lb=0, ub=1, obj=10)
292+
y = m.addVar("y", obj=1)
293+
z = m.addVar("z", obj=1)
294+
295+
c = m.addCons(x**2 >= 9)
296+
c1 = m.addCons(x**3 >= 4)
297+
m.addExprNonlinear(c, y**2, 2)
298+
m.addExprNonlinear(c1, z**(1/3), 1)
299+
300+
m.setParam("numerics/epsilon", 10**(-5)) # bigger eps due to nonlinearities
301+
m.optimize()
302+
303+
assert m.getNSols() > 0
304+
assert m.isEQ(m.getVal(y), 2)
305+
assert m.isEQ(m.getVal(z), 27)

0 commit comments

Comments
 (0)