Skip to content

Commit 1adefe4

Browse files
jstacclaude
andauthored
[schelling] Address editorial items from #419 (#756)
- Rename misleading "Jump Chain Algorithm" title to "Move algorithm" - Wrap the main-loop pseudocode in a prf:algorithm admonition (was code-block:: none) - Drop noisy `print('Entering loop ', count)` from run_simulation - Add Type 0 / Type 1 legends to both scatter plots - Move solution-end so the closing prose and sim_random_select sample run sit inside the dropdown solution Co-authored-by: Claude Opus 4.7 <[email protected]>
1 parent 29b5e23 commit 1adefe4

1 file changed

Lines changed: 18 additions & 21 deletions

File tree

‎lectures/schelling.md‎

Lines changed: 18 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ Each agent stays if they are happy and moves if they are unhappy.
131131

132132
The algorithm for moving is as follows
133133

134-
```{prf:algorithm} Jump Chain Algorithm
134+
```{prf:algorithm} Move algorithm
135135
:label: move_algo
136136
137137
1. Draw a random location in $S$
@@ -244,24 +244,26 @@ def plot_distribution(agents, cycle_num):
244244
plot_args = {'markersize': 8, 'alpha': 0.8}
245245
ax.set_facecolor('azure')
246246
ax.plot(x_values_0, y_values_0,
247-
'o', markerfacecolor='orange', **plot_args)
247+
'o', markerfacecolor='orange', label='Type 0', **plot_args)
248248
ax.plot(x_values_1, y_values_1,
249-
'o', markerfacecolor='green', **plot_args)
249+
'o', markerfacecolor='green', label='Type 1', **plot_args)
250250
ax.set_title(f'Cycle {cycle_num-1}')
251+
ax.legend()
251252
plt.show()
252253
```
253254

254-
And here's some pseudocode for the main loop, where we cycle through the
255-
agents until no one wishes to move.
255+
Here's the main loop, where we cycle through the agents until no one wishes
256+
to move.
256257

257-
The pseudocode is
258+
```{prf:algorithm} Main loop
259+
:label: schelling_main_loop
260+
261+
1. plot the distribution
262+
1. while agents are still moving
263+
1. for agent in agents
264+
1. give agent the opportunity to move
265+
1. plot the distribution
258266
259-
```{code-block} none
260-
plot the distribution
261-
while agents are still moving
262-
for agent in agents
263-
give agent the opportunity to move
264-
plot the distribution
265267
```
266268

267269
The real code is below
@@ -288,7 +290,6 @@ def run_simulation(num_of_type_0=600,
288290
289291
# Loop until no agent wishes to move
290292
while count < max_iter:
291-
print('Entering loop ', count)
292293
count += 1
293294
no_one_moved = True
294295
for agent in agents:
@@ -428,8 +429,10 @@ def plot_distribution(locations, types, title, savepdf=False):
428429
'o',
429430
markersize=8,
430431
markerfacecolor=color,
431-
alpha=0.8)
432+
alpha=0.8,
433+
label=f'Type {agent_type}')
432434
ax.set_title(title)
435+
ax.legend()
433436
plt.show()
434437
435438
def sim_random_select(max_iter=100_000, flip_prob=0.01, test_freq=10_000):
@@ -470,11 +473,6 @@ def sim_random_select(max_iter=100_000, flip_prob=0.01, test_freq=10_000):
470473
print(f"Terminating at iteration {current_iter}")
471474
```
472475

473-
```{solution-end}
474-
```
475-
476-
+++
477-
478476
When we run this we again find that mixed neighborhoods break down and segregation emerges.
479477

480478
Here's a sample run.
@@ -483,6 +481,5 @@ Here's a sample run.
483481
sim_random_select(max_iter=50_000, flip_prob=0.01, test_freq=10_000)
484482
```
485483

486-
```{code-cell} ipython3
487-
484+
```{solution-end}
488485
```

0 commit comments

Comments
 (0)