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 method to evaluate SCIP expressions without variables #771

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Unreleased
### Added
- Added eval method to evaluate constant expressions
- Added methods for getting the names of the current stage and of an event
### Fixed
- Fixed outdated time.clock call in gcp.py
Expand Down
12 changes: 12 additions & 0 deletions src/pyscipopt/scip.pxi
Original file line number Diff line number Diff line change
Expand Up @@ -4671,6 +4671,18 @@ cdef class Model:
raise Warning("method cannot be called before problem is solved")
return self.getSolVal(self._bestSol, expr)

def evalConstantExpression(self, str const):
"""Evaluates expression without variables
:param string const: Constant expression as string"""
import math
for operation in ["log", "sin", "cos", "exp", "sqrt"]:
const = const.replace(operation, "math."+operation)
const = const.replace("prod(", "math.prod([")
const = const.replace("sum(", "sum([")
const = const.replace("))", ")])]")

return eval(const)

def hasPrimalRay(self):
"""
Returns whether a primal ray is stored that proves unboundedness of the LP relaxation
Expand Down
8 changes: 8 additions & 0 deletions tests/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,3 +286,11 @@ def test_getStage():
print(m.getStage())
assert m.getStage() == SCIP_STAGE.SOLVED
assert m.getStageName() == "SOLVED"

def test_eval():
import math
m = Model()

assert m.isEQ(math.log(2), m.evalConstantExpression("log(2)"))
assert m.isEQ(sum([21.68,math.prod([0.007446285387417584,sum([996.47,math.prod([-1.0,math.exp(1.234317963583726),math.log(1.42)])])])]) ,m.evalConstantExpression("sum(21.68,prod(0.007446285387417584,sum(996.47,prod(-1.0,exp(1.234317963583726),log(1.42)))))"))

Loading