Skip to content

Commit 5337573

Browse files
authored
Merge pull request #477 from QuantEcon/update_short_path
[short_path] Update editorial suggestions
2 parents 53e0713 + dff3acd commit 5337573

File tree

1 file changed

+12
-20
lines changed

1 file changed

+12
-20
lines changed

lectures/short_path.md

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ Possible interpretations of the graph include
6969

7070
* Minimum cost for supplier to reach a destination.
7171
* Routing of packets on the internet (minimize time).
72-
* Etc., etc.
72+
* etc., etc.
7373

7474
For this simple graph, a quick scan of the edges shows that the optimal paths are
7575

@@ -91,7 +91,7 @@ For large graphs, we need a systematic solution.
9191

9292
Let $J(v)$ denote the minimum cost-to-go from node $v$, understood as the total cost from $v$ if we take the best route.
9393

94-
Suppose that we know $J(v)$ for each node $v$, as shown below for the graph from the preceding example
94+
Suppose that we know $J(v)$ for each node $v$, as shown below for the graph from the preceding example.
9595

9696
```{figure} /_static/lecture_specific/short_path/graph2.png
9797
@@ -128,7 +128,7 @@ the function $J$ satisfies
128128
J(v) = \min_{w \in F_v} \{ c(v, w) + J(w) \}
129129
```
130130

131-
This is known as the *Bellman equation*, after the mathematician Richard Bellman.
131+
This is known as the **Bellman equation**, after the mathematician [Richard Bellman](https://en.wikipedia.org/wiki/Richard_E._Bellman).
132132

133133
The Bellman equation can be thought of as a restriction that $J$ must
134134
satisfy.
@@ -209,7 +209,7 @@ Q = np.array([[inf, 1, 5, 3, inf, inf, inf],
209209

210210
Notice that the cost of staying still (on the principle diagonal) is set to
211211

212-
* np.inf for non-destination nodes --- moving on is required.
212+
* `np.inf` for non-destination nodes --- moving on is required.
213213
* 0 for the destination node --- here is where we stop.
214214

215215
For the sequence of approximations $\{J_n\}$ of the cost-to-go functions, we can use NumPy arrays.
@@ -226,18 +226,14 @@ i = 0
226226
227227
while i < max_iter:
228228
for v in nodes:
229-
# minimize Q[v, w] + J[w] over all choices of w
230-
lowest_cost = inf
231-
for w in nodes:
232-
cost = Q[v, w] + J[w]
233-
if cost < lowest_cost:
234-
lowest_cost = cost
235-
next_J[v] = lowest_cost
236-
if np.equal(next_J, J).all():
229+
# Minimize Q[v, w] + J[w] over all choices of w
230+
next_J[v] = np.min(Q[v, :] + J)
231+
232+
if np.array_equal(next_J, J):
237233
break
238-
else:
239-
J[:] = next_J # Copy contents of next_J to J
240-
i += 1
234+
235+
J[:] = next_J # Copy contents of next_J to J
236+
i += 1
241237
242238
print("The cost-to-go function is", J)
243239
```
@@ -420,11 +416,7 @@ The minimization step is vectorized to make it faster.
420416

421417
```{code-cell} python3
422418
def bellman(J, Q):
423-
num_nodes = Q.shape[0]
424-
next_J = np.empty_like(J)
425-
for v in range(num_nodes):
426-
next_J[v] = np.min(Q[v, :] + J)
427-
return next_J
419+
return np.min(Q + J, axis=1)
428420
429421
430422
def compute_cost_to_go(Q):

0 commit comments

Comments
 (0)