HLPL / VerTeX Modelling Language Documentation

← lessons

Reading the solution

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.

Predict before running

For max 3*x + 2*y with x <= 4 and x + y <= 6, what's the objective at the optimum?

Starter code

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

Hint

print("obj =", elytica.get_best_primal_bound("score"))

What counts as correct

Solution

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

Mastery variant

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.