HLPL / VerTeX Modelling Language Documentation

← lessons

Binary variables

Yes or no, on or off.

Chapter: IV — Discrete decisions · lesson id binary-variables · practice lesson


bin x declares a 0/1 variable — you either pick it or you don't. This is the cornerstone of discrete decision-making.

Three binary variables compete under a sum cap. The objective tells the solver which to prefer.

Predict before running

For max 5*a + 3*b + 4*c with a + b + c <= 2, which two binaries flip on?

Starter code

model binary_pick
bin a
bin b
bin c
constr a + b + c <= 2
max 5*a + 3*b + 4*c
end

import elytica
def main():
  elytica.init_model("binary_pick")
  elytica.run_model("binary_pick")
  print("a =", elytica.get_variable_value("binary_pick", "a"))
  print("b =", elytica.get_variable_value("binary_pick", "b"))
  print("c =", elytica.get_variable_value("binary_pick", "c"))
  print("obj =", elytica.get_best_primal_bound("binary_pick"))
  return 0

Hint

The constraint allows at most two of the three. Pick the two with the largest weights.

What counts as correct

Mastery variant

Four binaries this time. Pick at most three. Which one gets dropped?

model binary_pick_v
bin p
bin q
bin r
bin s
constr p + q + r + s <= 3
max 7*p + 4*q + 6*r + 5*s
end

import elytica
def main():
  elytica.init_model("binary_pick_v")
  elytica.run_model("binary_pick_v")
  print("p =", elytica.get_variable_value("binary_pick_v", "p"))
  print("q =", elytica.get_variable_value("binary_pick_v", "q"))
  print("r =", elytica.get_variable_value("binary_pick_v", "r"))
  print("s =", elytica.get_variable_value("binary_pick_v", "s"))
  print("obj =", elytica.get_best_primal_bound("binary_pick_v"))
  return 0

Read this through MCP: read with path: "lessons/binary-variables", or resource hlpl://lessons/binary-variables.