Variables and the objective value.
Chapter: II — Reading the solver · lesson id read-solution · practice lesson
Two APIs return what the solver found: elytica.get_variable_value(model, name) pulls one variable at a time, and elytica.get_best_primal_bound(model) returns the value of the objective at the best primal solution.
You could recompute the objective in Python (3*x + 2*y here) but it's brittle — change the model and you'd have to mirror the change in Python by hand. Use the API.
Add a line that prints obj = followed by the best primal bound.
For max 3*x + 2*y with x <= 4 and x + y <= 6, what's the objective at the optimum?
model score
var 0 <= x <= 4
var 0 <= y
constr x + y <= 6
max 3*x + 2*y
end
import elytica
def main():
elytica.init_model("score")
elytica.run_model("score")
x = elytica.get_variable_value("score", "x")
y = elytica.get_variable_value("score", "y")
print("x =", x, "y =", y)
# TODO: print "obj =" using elytica.get_best_primal_bound("score")
return 0
print("obj =", elytica.get_best_primal_bound("score"))
no-error — Model solvesnumeric-match — Objective reaches 16 (x=4, y=2)code-contains — Reads the objective from the solver (no hardcoding)model score
var 0 <= x <= 4
var 0 <= y
constr x + y <= 6
max 3*x + 2*y
end
import elytica
def main():
elytica.init_model("score")
elytica.run_model("score")
x = elytica.get_variable_value("score", "x")
y = elytica.get_variable_value("score", "y")
print("x =", x, "y =", y)
print("obj =", elytica.get_best_primal_bound("score"))
return 0
Different bounds and a different budget. Read all three outputs back from the solver.
model score_v
var 0 <= a <= 5
var 0 <= b
constr a + b <= 9
max 4*a + b
end
import elytica
def main():
elytica.init_model("score_v")
elytica.run_model("score_v")
a = elytica.get_variable_value("score_v", "a")
b = elytica.get_variable_value("score_v", "b")
print("a =", a, "b =", b)
# TODO: print "obj =" using elytica.get_best_primal_bound("score_v")
return 0
Read this through MCP: read with path: "lessons/read-solution", or resource hlpl://lessons/read-solution.