diff --git a/traveling_salesman/tsp.ipynb b/traveling_salesman/tsp.ipynb index 1c9654f..58ef03a 100644 --- a/traveling_salesman/tsp.ipynb +++ b/traveling_salesman/tsp.ipynb @@ -234,9 +234,8 @@ "# Variables: is city 'i' adjacent to city 'j' on the tour?\n", "vars = m.addVars(dist.keys(), obj=dist, vtype=GRB.BINARY, name='x')\n", "\n", - "# Symmetric direction: Copy the object\n", - "for i, j in vars.keys():\n", - " vars[j, i] = vars[i, j] # edge in opposite direction\n", + "# Symmetric direction: use dict.update to alias variable with new key\n", + "vars.update({(j,i):vars[i,j] for i,j in vars.keys()})\n", "\n", "# Constraints: two edges incident to each city\n", "cons = m.addConstrs(vars.sum(c, '*') == 2 for c in capitals)"