-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSchelling.py
More file actions
305 lines (227 loc) · 10.9 KB
/
Copy pathSchelling.py
File metadata and controls
305 lines (227 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
import random
from tokenize import group
import matplotlib.pyplot as plt
from IPython import display
import time
random.seed(32)
class Agent():
def __init__(self, xlocation, ylocation):
self.x = xlocation
self.y = ylocation
agent1 = Agent(22, 55)
agent2 = Agent(66, 88)
def map_all_agents(listofagents):
agents_XCoordinate = []
agents_YCoordinate = []
for item in listofagents:
agents_XCoordinate.append(item.x)
agents_YCoordinate.append(item.y)
fig, ax = plt.subplots(figsize=(5, 5))
ax.set_facecolor('azure')
ax.plot(agents_XCoordinate, agents_YCoordinate, 'o', markerfacecolor='purple')
plt.xlim(-5,105)
plt.ylim(-5,105)
ax.set_title("Here's our map of the agents we have created:")
plt.show()
agents_list = [agent1, agent2]
def moveagents(listofagents):
for each_agent in listofagents:
each_agent.x = random.randint(0,100),
each_agent.y = random.randint(0,100)
return listofagents
def make_agents_dance(agentslist, num_steps=10):
for i in range(num_steps):
moveagents(agentslist)
map_all_agents(agentslist)
time.sleep(1)
display.clear_output(wait=True)
New_List_of_Agents = [Agent(random.randint(0,100), random.randint(0,100)) for i in range(12)]
class AgentNew(Agent):
def __init__(self, xlocation, ylocation, group, status="unhappy"):
super().__init__(xlocation, ylocation)
self.group = group
self.status = status
# The class AgentNew randomly picks whether an agent will be purple or gold.
# We may want to specify that instead, so lets create two subclasses of AgentNew.
# Let's call them PurpleAgents and GoldAgents.
class PurpleAgents(AgentNew):
def __init__(self, xlocation, ylocation, group="Purple", status="unhappy"):
super().__init__(xlocation, ylocation, group)
self.group = group
self.status = status
b1 = PurpleAgents(3,6)
print(b1.group)
class GoldAgents(AgentNew):
def __init__(self, xlocation, ylocation, group="Gold", status="unhappy"):
super().__init__(xlocation, ylocation, group)
self.group = group
self.status = status
b2 = GoldAgents(3,6)
print(b2.group)
random.seed(15)
List_of_PurpleAgents = [PurpleAgents(random.randint(0,100), random.randint(0,100)) for x in range(12)] ### using list comprehension, make 12 PurpleAgents
List_of_GoldAgents = [GoldAgents(random.randint(0,100), random.randint(0,100)) for x in range(12)] ### using list comprehension, make 12 GoldAgents
Combined_List = List_of_PurpleAgents + List_of_GoldAgents
def map_colorful_agents(CombinedList): # what argument should go in this function?
Purple_XCoordinate = [CombinedList[i].x for i in range(len(CombinedList)) if CombinedList[i].group =="Purple"] ### fill in the blanks to make this list comprehension work
Purple_YCoordinate = [CombinedList[i].y for i in range(len(CombinedList)) if CombinedList[i].group =="Purple"] # model this after the above list comprehension
Gold_XCoordinate = [CombinedList[i].x for i in range(len(CombinedList)) if CombinedList[i].group =="Gold"]
Gold_YCoordinate = [CombinedList[i].y for i in range(len(CombinedList)) if CombinedList[i].group =="Gold"]
fig, ax = plt.subplots(figsize=(5, 5))
ax.set_facecolor('azure')
ax.plot(Purple_XCoordinate, Purple_YCoordinate, 'o', markerfacecolor='purple')
ax.plot(Gold_XCoordinate, Gold_YCoordinate, 'o', markerfacecolor='gold')
plt.xlim(-5,105)
plt.ylim(-5,105)
ax.set_title("Here's our map of the agents we have created:")
plt.show()
x = map_colorful_agents(Combined_List)
### call the function appropriately
# Now that we have some agents of different groups
random.seed(38)
class AgentNew(Agent):
def __init__(self, xlocation, ylocation, group, status='unhappy'):
super().__init__(xlocation, ylocation)
self.group = group
self.status = status
def move_if_unhappy (self): # what arguement(s) are needed here?
if self.status == "unhappy": ### there is a mistake in this command - fix it
self.x = random.randint(0,100)
self.y = random.randint(0,100)
a55 = AgentNew(24,11,"Purple")
'''Problem #4'''
# Now we are going to make our most complicated method.
# We're going to build a method called 'check_neighbors' which will identify
# the agents that are within 10 x-coordinate AND 10 y-coordinate spaces of a
# given agent. Once those agents are identified, we'll calculate if enough of
# them are of the same group to meet our pre-determined threshold.
class AgentNew(Agent):
def __init__(self, xlocation, ylocation, group, status='unhappy'):
super().__init__(xlocation, ylocation)
self.group = group
self.status = status
def move_if_unhappy (self):
if self.status == "unhappy":
self.x = random.randint(0,100)
self.y = random.randint(0,100)
def check_neighbors(self, agentlist):
zlist = list(filter(lambda t: abs(t.x - self.x) <10, agentlist))
zlist = list(filter(lambda t: abs(t.y - self.y) <10, zlist))
same_group_neighbor = [t for t in zlist if t.group == self.group]
opposite_group_neighbor = [t for t in zlist if t.group != self.group]
if (len(same_group_neighbor)+.01)/(len(zlist)+.01) > group_affinity_threshold:
self.status="happy"
else:
self.status="unhappy"
class PurpleAgents(AgentNew):
def __init__(self, xlocation, ylocation, group="Purple"):
super().__init__(xlocation, ylocation, group, status='unhappy')
self.group = group
def move_if_unhappy(self):
return super().move_if_unhappy()
def check_neighbors(self, agentlist):
return super().check_neighbors(agentlist)
class GoldAgents(AgentNew):
def __init__(self, xlocation, ylocation, group="Gold"):
super().__init__(xlocation, ylocation, group, status='unhappy')
self.group = group
def move_if_unhappy(self):
return super().move_if_unhappy()
def check_neighbors(self, agentlist):
return super().check_neighbors(agentlist)
random.seed(34)
p32 = PurpleAgents(14,55)
p32.move_if_unhappy()
print(p32.x) # what do you expect the output to be?
# Now we're ready to put it all together and run a few simulations.
# First, let's run a simulation in which there are 200 Purple agents and
# 200 Gold agents.
# Notice the group_affinity_threshold is set at .51.
# This means each purple or gold agent wants to be in a 'block' in which they
# are in the majority group.
random.seed(2021)
group_affinity_threshold = .51
testlist = [PurpleAgents(random.randint(0,100), random.randint(0,100)) for x in range(200)] + [GoldAgents(random.randint(0,100), random.randint(0,100)) for x in range(200)]
map_colorful_agents(testlist)
for x in range(15):
for agent in (testlist):
agent.check_neighbors(testlist) # does this need any arguments?
for agent in (testlist):
agent.move_if_unhappy() # does this need any arguments?
map_colorful_agents(testlist)
print(x)
time.sleep(.5)
display.clear_output(wait=True)
random.seed(202)
group_affinity_threshold = .4
testlist = [PurpleAgents(random.randint(0,100), random.randint(0,100)) for x in range(400)] + [GoldAgents(random.randint(0,100), random.randint(0,100)) for x in range(400)] ### create a testlist that has 200 PurpleAgents and 200 GoldAgents###### create a testlist that has 400 PurpleAgents and 400 GoldAgents
map_colorful_agents(testlist)
for x in range(15):
for agent in (testlist):
agent.check_neighbors(testlist) #does this need any arguments?
for agent in (testlist):
agent.move_if_unhappy() # does this need any arguments?
map_colorful_agents(testlist)
print(x)
time.sleep(.5)
display.clear_output(wait=True)
'''Problem #7'''
# Even if people don't mind being a minority in their neighborhood, you still
# get segregation pretty easily according to this model. For a long time models
# such as this were used to argue that some degree of segregation was
# inevitable, and therefore it should not be a target of policy.
# Lets make 2 new subclasses, 'PurpleDiversitySeekers' and 'GoldDiversitySeekers'.
# Please use those exact names to allow for autograding.
# For these subclasses, make them seek out diversity instead of avoid it.
# Run some simulations with 300 traditional PurpleAgents, 300 traditional
# GoldAgents, 100 PurpleDiversitySeekers, and 100 GoldDiversitySeekers.
# What happens now?
random.seed(11)
class PurpleDiversitySeekers(Agent):
def __init__(self, xlocation, ylocation, group='Purple', status='unhappy'):
super().__init__(xlocation, ylocation)
self.group = group
self.status = status
def move_if_unhappy (self):
if self.status == "unhappy":
self.x = random.randint(0,100)
self.y = random.randint(0,100)
def check_neighbors(self, agentlist):
zlist = list(filter(lambda x: abs(x.x - self.x) <10, agentlist))
zlist = list(filter(lambda x: abs(x.y - self.y) <10, zlist))
same_group_neighbor = [t for t in zlist if t.group != self.group]
opposite_group_neighbor = [t for t in zlist if t.group == self.group]
if (len(same_group_neighbor)+.01)/(len(zlist)+.01) > group_affinity_threshold:
self.status="happy"
else:
self.status="unhappy"
class GoldDiversitySeekers(Agent):
def __init__(self, xlocation, ylocation, group='Gold', status='unhappy'):
super().__init__(xlocation, ylocation)
self.group = group
self.status = status
def move_if_unhappy (self):
if self.status == "unhappy":
self.x = random.randint(0,100)
self.y = random.randint(0,100)
def check_neighbors(self, agentlist):
zlist = list(filter(lambda x: abs(x.x - self.x) <10, agentlist))
zlist = list(filter(lambda x: abs(x.y - self.y) <10, zlist))
same_group_neighbor = [t for t in zlist if t.group != self.group]
opposite_group_neighbor = [t for t in zlist if t.group == self.group]
if (len(same_group_neighbor)+.01)/(len(zlist)+.01) > group_affinity_threshold:
self.status="happy"
else:
self.status="unhappy"
group_affinity_threshold = .51
testlist = [AgentNew(random.randint(0,100), random.randint(0,100), group='Purple') for x in range(300)] + [AgentNew(random.randint(0,100), random.randint(0,100), group='Gold') for x in range(300)] + [PurpleDiversitySeekers(random.randint(0,100), random.randint(0,100)) for x in range(100)] + [GoldDiversitySeekers(random.randint(0,100), random.randint(0,100)) for x in range(100)]
map_colorful_agents(testlist)
for x in range(15):
for agent in (testlist):
agent.check_neighbors(testlist)
for agent in (testlist):
agent.move_if_unhappy()
map_colorful_agents(testlist)
print(x)
time.sleep(.5)
display.clear_output(wait=True)