Skip to content
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

Add addExprNonlinear #760

Merged
merged 35 commits into from
Feb 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
b0bc416
Add addExprNonlinear
Joao-Dionisio Dec 2, 2023
a9fd34f
Add test addExprNonlinear
Joao-Dionisio Dec 2, 2023
ab1b304
Update CHANGELOG
Joao-Dionisio Dec 2, 2023
8bb0e9b
Merge branch 'master' into scipAddExprNonlinear
Joao-Dionisio Dec 2, 2023
d9f9ae9
Add TODO
Joao-Dionisio Feb 2, 2024
b5fa4a2
EVENTTYPE Overhaul
Joao-Dionisio Nov 28, 2023
e23a7cf
Directly use SCIP macros instead of their value
Joao-Dionisio Nov 28, 2023
b02d39a
Update CHANGELOG
Joao-Dionisio Nov 28, 2023
32bea3a
Update CHANGELOG
Joao-Dionisio Nov 28, 2023
b8a1581
Add NODEDELETE to scip.pxi
Joao-Dionisio Nov 29, 2023
c7ae651
Add test prototype for events
Joao-Dionisio Dec 3, 2023
546719f
Add more SCIP events
Joao-Dionisio Dec 3, 2023
0ca137b
Add all event types as of SCIP 8
Joao-Dionisio Dec 3, 2023
5d38184
Add test for event identification
Joao-Dionisio Dec 3, 2023
074827d
Update CHANGELOG
Joao-Dionisio Dec 3, 2023
db98078
Add method for getting Event names
Joao-Dionisio Dec 17, 2023
197bf6e
Add ability to print stage name as string. Add test.
Joao-Dionisio Dec 17, 2023
dfff808
Update CHANGELOG
Joao-Dionisio Dec 17, 2023
7d55074
Add __str__ to Event class
Joao-Dionisio Dec 27, 2023
ff3ee8d
Update src/pyscipopt/scip.pxi
Joao-Dionisio Dec 27, 2023
19ffc22
Add function getconshdlrname
Opt-Mucca Dec 7, 2023
d3d5cee
Remove commented code
Joao-Dionisio Dec 4, 2023
8734cbf
Update version
mmghannam Dec 4, 2023
e6d094d
Rename
mmghannam Dec 4, 2023
8222c30
Add check to test_cons
Opt-Mucca Dec 8, 2023
c130270
Change assert to SOS2
Opt-Mucca Dec 8, 2023
df52aa0
Update INSTALL.md
Opt-Mucca Dec 8, 2023
75bc219
Change manylinux
Opt-Mucca Dec 9, 2023
cef48c9
Update CHANGELOG.md
Joao-Dionisio Dec 28, 2023
5ec3773
Update CHANGELOG.md
Joao-Dionisio Dec 28, 2023
f5d7cf0
Fix outdated time.clock
Joao-Dionisio Jan 9, 2024
8c82281
Update README
Joao-Dionisio Jan 9, 2024
9942a0c
Update CHANGELOG
Joao-Dionisio Feb 2, 2024
43f0151
Merge branch 'master' into scipAddExprNonlinear
Joao-Dionisio Feb 2, 2024
676939d
Merge branch 'master' into scipAddExprNonlinear
mmghannam Feb 25, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
# CHANGELOG

## Unreleased
## 4.4.0 - 2023-12-04
### Added
- Added SCIP function addExprNonlinear
- Add support for Cython 3
- Added methods for getting the names of the current stage and of an event
### Fixed
Expand Down
19 changes: 19 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@
if rc == SCIP_OKAY:
pass
elif rc == SCIP_ERROR:
raise Exception('SCIP: unspecified error!')

Check failure on line 261 in src/pyscipopt/scip.pxi

View workflow job for this annotation

GitHub Actions / test-coverage (3.11)

SCIP: unspecified error!
elif rc == SCIP_NOMEMORY:
raise MemoryError('SCIP: insufficient memory error!')
elif rc == SCIP_READERROR:
Expand Down Expand Up @@ -2365,6 +2365,7 @@
free(monomials)
free(termcoefs)
return PyCons


def _addGenNonlinearCons(self, ExprCons cons, **kwargs):
cdef SCIP_EXPR** childrenexpr
Expand Down Expand Up @@ -2489,6 +2490,24 @@

return PyCons

# 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.
from typing import Union
def addExprNonlinear(self, Constraint cons, expr: Union[Expr,GenExpr], float coef):

Check warning on line 2495 in src/pyscipopt/scip.pxi

View check run for this annotation

Codecov / codecov/patch

src/pyscipopt/scip.pxi#L2494-L2495

Added lines #L2494 - L2495 were not covered by tests
"""
Add coef*expr to nonlinear constraint.
"""
assert self.getStage() == 1, "addExprNonlinear cannot be called in stage %i." % self.getStage()
assert cons.isNonlinear(), "addExprNonlinear can only be called with nonlinear constraints."

cdef Constraint temp_cons
cdef SCIP_EXPR* scip_expr

temp_cons = self.addCons(expr <= 0)
scip_expr = SCIPgetExprNonlinear(temp_cons.scip_cons)

PY_SCIP_CALL(SCIPaddExprNonlinear(self._scip, cons.scip_cons, scip_expr, coef))
self.delCons(temp_cons)

def addConsCoeff(self, Constraint cons, Variable var, coeff):
"""Add coefficient to the linear constraint (if non-zero).

Expand Down
20 changes: 19 additions & 1 deletion tests/test_nonlinear.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,22 @@ def test_quad_coeffs():
assert quadterms[0][1] == 0.5

assert linterms[0][0].name == z.name
assert linterms[0][1] == 4
assert linterms[0][1] == 4

def test_addExprNonLinear():
m = Model()
x = m.addVar("x", lb=0, ub=1, obj=10)
y = m.addVar("y", obj=1)
z = m.addVar("z", obj=1)

c = m.addCons(x**2 >= 9)
c1 = m.addCons(x**3 >= 4)
m.addExprNonlinear(c, y**2, 2)
m.addExprNonlinear(c1, z**(1/3), 1)

m.setParam("numerics/epsilon", 10**(-5)) # bigger eps due to nonlinearities
m.optimize()

assert m.getNSols() > 0
assert m.isEQ(m.getVal(y), 2)
assert m.isEQ(m.getVal(z), 27)
Loading