|
17 | 17 | }, |
18 | 18 | { |
19 | 19 | "cell_type": "code", |
| 20 | + "execution_count": null, |
20 | 21 | "metadata": {}, |
| 22 | + "outputs": [], |
21 | 23 | "source": [ |
22 | 24 | "import matplotlib.pyplot as plt\n", |
23 | 25 | "import pandas as pd\n", |
24 | 26 | "\n", |
25 | 27 | "%matplotlib inline\n", |
26 | 28 | "\n", |
27 | 29 | "from model import Schelling" |
28 | | - ], |
29 | | - "outputs": [], |
30 | | - "execution_count": null |
| 30 | + ] |
31 | 31 | }, |
32 | 32 | { |
33 | 33 | "cell_type": "markdown", |
|
38 | 38 | }, |
39 | 39 | { |
40 | 40 | "cell_type": "code", |
| 41 | + "execution_count": null, |
41 | 42 | "metadata": {}, |
42 | | - "source": "model = Schelling(height=10, width=10, homophily=3, density=0.8, minority_pc=0.2)", |
43 | 43 | "outputs": [], |
44 | | - "execution_count": null |
| 44 | + "source": [ |
| 45 | + "schelling_model = Schelling(\n", |
| 46 | + " height=10, width=10, homophily=3, density=0.8, minority_pc=0.2\n", |
| 47 | + ")" |
| 48 | + ] |
45 | 49 | }, |
46 | 50 | { |
47 | 51 | "cell_type": "markdown", |
|
52 | 56 | }, |
53 | 57 | { |
54 | 58 | "cell_type": "code", |
| 59 | + "execution_count": null, |
55 | 60 | "metadata": {}, |
56 | | - "source": [ |
57 | | - "while model.running and model.steps < 100:\n", |
58 | | - " model.step()\n", |
59 | | - "print(model.steps) # Show how many steps have actually run" |
60 | | - ], |
61 | 61 | "outputs": [], |
62 | | - "execution_count": null |
| 62 | + "source": [ |
| 63 | + "while schelling_model.running and schelling_model.steps < 100:\n", |
| 64 | + " schelling_model.step()\n", |
| 65 | + "print(schelling_model.steps) # Show how many steps have actually run" |
| 66 | + ] |
63 | 67 | }, |
64 | 68 | { |
65 | 69 | "cell_type": "markdown", |
|
70 | 74 | }, |
71 | 75 | { |
72 | 76 | "cell_type": "code", |
| 77 | + "execution_count": null, |
73 | 78 | "metadata": {}, |
74 | | - "source": [ |
75 | | - "model_out = model.datacollector.get_model_vars_dataframe()" |
76 | | - ], |
77 | 79 | "outputs": [], |
78 | | - "execution_count": null |
| 80 | + "source": [ |
| 81 | + "model_out = schelling_model.datacollector.get_model_vars_dataframe()" |
| 82 | + ] |
79 | 83 | }, |
80 | 84 | { |
81 | 85 | "cell_type": "code", |
| 86 | + "execution_count": null, |
82 | 87 | "metadata": {}, |
| 88 | + "outputs": [], |
83 | 89 | "source": [ |
84 | 90 | "model_out.head()" |
85 | | - ], |
86 | | - "outputs": [], |
87 | | - "execution_count": null |
| 91 | + ] |
88 | 92 | }, |
89 | 93 | { |
90 | 94 | "cell_type": "markdown", |
|
95 | 99 | }, |
96 | 100 | { |
97 | 101 | "cell_type": "code", |
| 102 | + "execution_count": null, |
98 | 103 | "metadata": {}, |
| 104 | + "outputs": [], |
99 | 105 | "source": [ |
100 | 106 | "model_out.happy.plot()" |
101 | | - ], |
102 | | - "outputs": [], |
103 | | - "execution_count": null |
| 107 | + ] |
104 | 108 | }, |
105 | 109 | { |
106 | 110 | "cell_type": "markdown", |
|
115 | 119 | }, |
116 | 120 | { |
117 | 121 | "cell_type": "code", |
| 122 | + "execution_count": null, |
118 | 123 | "metadata": {}, |
119 | | - "source": "from mesa.batchrunner import batch_run", |
120 | 124 | "outputs": [], |
121 | | - "execution_count": null |
| 125 | + "source": [ |
| 126 | + "from mesa.batchrunner import batch_run" |
| 127 | + ] |
122 | 128 | }, |
123 | 129 | { |
124 | 130 | "cell_type": "markdown", |
|
129 | 135 | }, |
130 | 136 | { |
131 | 137 | "cell_type": "code", |
| 138 | + "execution_count": null, |
132 | 139 | "metadata": {}, |
| 140 | + "outputs": [], |
133 | 141 | "source": [ |
134 | 142 | "fixed_params = {\"height\": 10, \"width\": 10, \"density\": 0.8, \"minority_pc\": 0.2}\n", |
135 | 143 | "variable_parms = {\"homophily\": range(1, 9)}\n", |
136 | 144 | "all_params = fixed_params | variable_parms" |
137 | | - ], |
138 | | - "outputs": [], |
139 | | - "execution_count": null |
| 145 | + ] |
140 | 146 | }, |
141 | 147 | { |
142 | 148 | "cell_type": "code", |
| 149 | + "execution_count": null, |
143 | 150 | "metadata": {}, |
| 151 | + "outputs": [], |
144 | 152 | "source": [ |
145 | 153 | "results = batch_run(\n", |
146 | 154 | " Schelling,\n", |
147 | 155 | " parameters=all_params,\n", |
148 | 156 | " iterations=10,\n", |
149 | 157 | " max_steps=200,\n", |
150 | 158 | ")" |
151 | | - ], |
152 | | - "outputs": [], |
153 | | - "execution_count": null |
| 159 | + ] |
154 | 160 | }, |
155 | 161 | { |
156 | | - "metadata": {}, |
157 | 162 | "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "metadata": {}, |
| 165 | + "outputs": [], |
158 | 166 | "source": [ |
159 | 167 | "df = pd.DataFrame(results)\n", |
160 | 168 | "df" |
161 | | - ], |
162 | | - "outputs": [], |
163 | | - "execution_count": null |
| 169 | + ] |
164 | 170 | }, |
165 | 171 | { |
166 | 172 | "cell_type": "code", |
| 173 | + "execution_count": null, |
167 | 174 | "metadata": {}, |
| 175 | + "outputs": [], |
168 | 176 | "source": [ |
169 | 177 | "plt.scatter(df.homophily, df.happy)\n", |
170 | 178 | "plt.xlabel(\"Homophily\")\n", |
171 | 179 | "plt.ylabel(\"Happy Agents\")\n", |
172 | 180 | "plt.grid()\n", |
173 | 181 | "plt.title(\"Effect of Homophily on segregation\")\n", |
174 | 182 | "plt.show()" |
175 | | - ], |
176 | | - "outputs": [], |
177 | | - "execution_count": null |
| 183 | + ] |
178 | 184 | } |
179 | 185 | ], |
180 | 186 | "metadata": { |
|
0 commit comments