Just the decisions.
Chapter: XIV — Travelling salesman · lesson id tsp-vars · practice lesson
A salesman must visit four cities and return home, minimising total distance.
Variable x_{i,j} = 1 if the tour goes directly from i to j. The objective is the sum of travelled distances. One subtle thing: we forbid self-loops (x_{i,i} = 0) — no city goes to itself.
Run it. With only the no-self-loops constraint, the solver picks no edges — cost 0. The next lesson adds the degree constraints that force a tour.
TSP variables defined, no-self-loops, min cost. But no degree constraints yet. What's the optimum?
model tsp
set C = {1, 2, 3, 4}
const d = {{0, 4, 5, 7}, {4, 0, 3, 6}, {5, 3, 0, 2}, {7, 6, 2, 0}}, forall i in C, forall j in C
bin x, forall i in C, forall j in C
constr x_{i,i} = 0, forall i in C
min sum_{i in C}{sum_{j in C}{d_{i,j} * x_{i,j}}}
end
import elytica
def main():
elytica.init_model("tsp")
elytica.run_model("tsp")
print("obj =", elytica.get_best_primal_bound("tsp"))
return 0
No structural constraints yet → solver picks zero edges → cost 0.
no-error — Model solvesnumeric-match — Without degree constraints, cost is 0code-contains — Reads the objective from the solver (no hardcoding)Different distance matrix, same naked-skeleton model. Still 0.
model tsp_v
set C = {1, 2, 3, 4}
const d = {{0, 3, 8, 6}, {3, 0, 5, 9}, {8, 5, 0, 2}, {6, 9, 2, 0}}, forall i in C, forall j in C
bin x, forall i in C, forall j in C
constr x_{i,i} = 0, forall i in C
min sum_{i in C}{sum_{j in C}{d_{i,j} * x_{i,j}}}
end
import elytica
def main():
elytica.init_model("tsp_v")
elytica.run_model("tsp_v")
print("obj =", elytica.get_best_primal_bound("tsp_v"))
return 0
Read this through MCP: read with path: "lessons/tsp-vars", or resource hlpl://lessons/tsp-vars.