OMMX Python SDK 1.5.0#
This notebook describes the new features. Please refer the GitHub release note for the detailed information.
Evaluation and Partial Evaluation#
From the first release of OMMX, ommx.v1.Instance
supports evaluate
method to produce Solution
message
from ommx.v1 import Instance, DecisionVariable
# Create an instance of the OMMX API
x = DecisionVariable.binary(1)
y = DecisionVariable.binary(2)
instance = Instance.from_components(
decision_variables=[x, y],
objective=x + y,
constraints=[x + y <= 1],
sense=Instance.MINIMIZE
)
solution = instance.evaluate({1: 1, 2: 0})
solution.decision_variables
kind | lower | upper | name | subscripts | description | substituted_value | value | |
---|---|---|---|---|---|---|---|---|
id | ||||||||
1 | binary | 0.0 | 1.0 | <NA> | [] | <NA> | <NA> | 1.0 |
2 | binary | 0.0 | 1.0 | <NA> | [] | <NA> | <NA> | 0.0 |
From Python SDK 1.5.0, Function
and its base classes, Linear
, Quadratic
, and Polynomial
also support evaluate
method:
f = 2*x + 3*y
value, used_ids = f.evaluate({1: 1, 2: 0})
print(f"{value=}, {used_ids=}")
value=2.0, used_ids={1, 2}
This returns evaluated value of the function and used decision variable IDs. If some decision variables are lacking, the evaluate
method raises an exception:
try:
f.evaluate({3: 1})
except RuntimeError as e:
print(e)
Variable id (1) is not found in the solution
In addition, there is partial_evaluate
method
f2, used_ids = f.partial_evaluate({1: 1})
print(f"{f2=}, {used_ids=}")
f2=Linear(3*x2 + 2), used_ids={1}
This creates a new function by substituting x = 1
. partial_evaluate
is also added to ommx.v1.Instance
class:
new_instance = instance.partial_evaluate({1: 1})
new_instance.objective
Function(x2 + 1)
This method will be useful for creating a problem with fixing specific decision variables.