HLPL / VerTeX Modelling Language Documentation

← lessons

Shortest path — edges and distances

Start with what you decide.

Chapter: X — Shortest path · lesson id sp-edges · practice lesson


Network: 4 nodes, 5 directed edges. The plan is to route a unit of flow from node 1 (source) to node 4 (sink) along the cheapest path.

Edges: 1→2 (d=4), 1→3 (d=2), 3→2 (d=1), 2→4 (d=3), 3→4 (d=5). Binary x_e per edge.

For now: just declare the decision variables and the objective. With no constraints, the solver picks zero edges — cost 0. Run it, see the trivial answer, understand why we need flow conservation in the next lesson.

Predict before running

Edge variables and a minimise-cost objective, but no conservation yet. What's the optimum?

Starter code

model sp
set E = {12, 13, 32, 24, 34}
const d = {4, 2, 1, 3, 5}, forall e in E
bin x, forall e in E
min sum_{e in E}{d_{e} * x_{e}}
end

import elytica
def main():
  elytica.init_model("sp")
  elytica.run_model("sp")
  for i in [12,13,32,24,34]:
    print("x"+str(i), "=", elytica.get_variable_value("sp", "x"+str(i)))
  print("obj =", elytica.get_best_primal_bound("sp"))
  return 0

Hint

No constraints yet → solver picks nothing → cost 0. That's the point.

What counts as correct

Mastery variant

Same network shape, different costs. Without conservation, still nothing picked.

model sp_v
set E = {12, 13, 32, 24, 34}
const d = {6, 3, 2, 4, 8}, forall e in E
bin x, forall e in E
min sum_{e in E}{d_{e} * x_{e}}
end

import elytica
def main():
  elytica.init_model("sp_v")
  elytica.run_model("sp_v")
  for i in [12,13,32,24,34]:
    print("x"+str(i), "=", elytica.get_variable_value("sp_v", "x"+str(i)))
  print("obj =", elytica.get_best_primal_bound("sp_v"))
  return 0

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