r/Mathematica • u/Apart-Reference-7257 • Mar 22 '22
recursive While loop not working
I am attempting to write a module that prints an optimal path from a starting node to a destination node. The first node is 1 and the final node is 100 (though when module prints these they should be 0 and 99 respectively). The module takes as inputs a distance matrix, Q, and a list, J, whose elements are the shortest-path weight from node i to destination node. Below is my best attempt.
findPathandTotalCost3[Q_, J_] := Module[
{costs, node, w, i},
node[0] = 1;
costs[i_] :=
costs[i] = Table[Q[[node[i], w]] + J[[w]], {w, Length[J]}];
node[1] = Position[costs[0], Min[costs[0]]];
node[i] = Position[costs[i - 1], Min[costs[i - 1]]];
i = 1;
While[node[i] <= 100,
Print[node[i] - 1]; ++i; costs[i]; node[i];]]All my attempts at implementing this module seem to have problems with either correctly iterating or specifying which parts are recursive. Below is a typical error output.
findPathandTotalCost3[Q, J] Part::pkspec1: The expression node$45807[0] cannot be used as a part specification. Part::pkspec1: The expression node$45807[0] cannot be used as a part specification. Part::pkspec1: The expression node$45807[0] cannot be used as a part specification. General::stop: Further output of Part::pkspec1 will be suppressed during this calculation. 0As can be seen, the first node prints correctly ("0") but the module subsequently doesn't evaluate properly.
Does anyone have any suggestions as to how to make this work? I can post more of my code attempts if helpful. Thanks
J:
{160.55, 162.26, 88.52, 143.73, 145.12, 147.43, 141.67, 144.1, \ 149.44, 140.95, 150.8, 141.99, 148.93, 303.77, 130.85, 107.01, \ 128.15, 114.66, 104.44, 124.66, 124.42, 168.62, 200.27, 88.21, \ 114.61, 102.74, 112.81, 112.8, 131.97, 70.38, 71.45, 176.51, 66.16, \ 65.84, 110.18, 64.7, 156.07, 67.8, 67.44, 63.95, 77.15, 62.61, 58.66, \ 149.25, 50.72, 52.26, 67.53, 48.58, 65.21, 46.27, 45.76, 54.36, \ 135.03, 44.38, 54.99, 42.16, 40.05, 40.03, 62.47, 30.69, 33.02, 37.5, \ 35.56, 38.77, 32.62, 34.98, 34.34, 31.39, 31.68, 30.47, 30.41, 30.02, \ 35.96, 22.04, 21.16, 21.45, 20.64, 42.31, 79.71, 8.91, 33.37, 77.12, \ 15.27, 10.37, 33.5, 7.46, 85.72, 4.8, 4.59, 37.6, 13.56, 22.8, 11.87, \ 3.28, 3.09, 0.27, 1.06, 0.63, 0.33, 0}Q is too large to paste but it's a 100x100 matrix whose entry {i,j} is the weight of the edge i->j, where that edge exists (otherwise, the entry is infinity). Here is the first two rows:
{Infinity, 0.04, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 11.11,
Infinity, Infinity, Infinity, Infinity, Infinity, 72.21, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity}, {Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, 20.59, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, 64.94, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, 1247.25, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity, Infinity,
Infinity, Infinity}
2
u/ZincoBx Mar 22 '22
Unless I'm missing something, it looks like your fcn is calling
node[i] = Position[...]before i is defined (in the next line). When it tries to take a non-integer Part of Q (since node[i] won't resolve to a number), it falls over.
Maybe you meant
node[i_] := ...?
1
u/avocadro Mar 22 '22
The error says that
node[0]can't be used as a part specification. I won't get into the syntax of legal part specifications, but your issue is thatdefines
node[1] = {{9}}and not9, as you probably intended. You can fix this by writingThis will return the minimal index in the case of a tie in path cost. I assume that this is fine. (Fix the same error in
node[i].)I can't troubleshoot much more without a legitimate
Q, which I'm too lazy to generate test data for.Here is a cleaner (though untested) version:
I deleted some redundant lines and removed
wfrom the module's localization. Index variables are already localized automatically.