Question: MODEL import mesa class SchellingAgent(mesa.Agent): Schelling segregation agent def __init__(self, pos, model, agent_type): Create a new Schelling agent. Args: unique_id: Unique identifier

MODEL
import mesa
class SchellingAgent(mesa.Agent): """ Schelling segregation agent """
def __init__(self, pos, model, agent_type): """ Create a new Schelling agent. Args: unique_id: Unique identifier for the agent. x, y: Agent initial location. agent_type: Indicator for the agent's type (minority=1, majority=0) """ super().__init__(pos, model) self.pos = pos self.type = agent_type
def step(self): similar = 0 for neighbor in self.model.grid.iter_neighbors(self.pos, True): if neighbor.type == self.type: similar += 1
# If unhappy, move: if similar
class Schelling(mesa.Model): """ Model class for the Schelling segregation model. """
def __init__(self, width=20, height=20, density=0.8, minority_pc=0.2, homophily=3): """ """
self.width = width self.height = height self.density = density self.minority_pc = minority_pc self.homophily = homophily
self.schedule = mesa.time.RandomActivation(self) self.grid = mesa.space.SingleGrid(width, height, torus=True)
self.happy = 0 self.datacollector = mesa.DataCollector( {"happy": "happy"}, # Model-level count of happy agents # For testing purposes, agent's individual x and y {"x": lambda a: a.pos[0], "y": lambda a: a.pos[1]}, )
# Set up agents # We use a grid iterator that returns # the coordinates of a cell as well as # its contents. (coord_iter) for cell in self.grid.coord_iter(): x = cell[1] y = cell[2] if self.random.random()
agent = SchellingAgent((x, y), self, agent_type) self.grid.position_agent(agent, (x, y)) self.schedule.add(agent)
self.running = True self.datacollector.collect(self)
def step(self): """ Run one step of the model. If All agents are happy, halt the model. """ self.happy = 0 # Reset counter of happy agents self.schedule.step() # collect data self.datacollector.collect(self)
if self.happy == self.schedule.get_agent_count(): self.running = False
RUN
from server import server
server.launch()
SERVER
import mesa
from model import Schelling
def get_happy_agents(model): """ Display a text count of how many happy agents there are. """ return f"Happy agents: {model.happy}"
def schelling_draw(agent): """ Portrayal Method for canvas """ if agent is None: return portrayal = {"Shape": "circle", "r": 0.5, "Filled": "true", "Layer": 0}
if agent.type == 0: portrayal["Color"] = ["#FF0000", "#FF9999"] portrayal["stroke_color"] = "#00FF00" else: portrayal["Color"] = ["#0000FF", "#9999FF"] portrayal["stroke_color"] = "#000000" return portrayal
canvas_element = mesa.visualization.CanvasGrid(schelling_draw, 20, 20, 500, 500) happy_chart = mesa.visualization.ChartModule([{"Label": "happy", "Color": "Black"}])
model_params = { "height": 20, "width": 20, "density": mesa.visualization.Slider("Agent density", 0.8, 0.1, 1.0, 0.1), "minority_pc": mesa.visualization.Slider("Fraction minority", 0.2, 0.00, 1.0, 0.05), "homophily": mesa.visualization.Slider("Homophily", 3, 0, 8, 1), }
server = mesa.visualization.ModularServer( Schelling, [canvas_element, get_happy_agents, happy_chart], "Schelling", model_params, )
In this exam, you are asked to change the structure of the Schelling agent program which has been uploaded to this course moodle page previously. For ease aforementioned document link is also given here; a) Model.py b) Run.pv c) Server.py As was requested before, you were supposed to set up python, then an IDE for python development like PyCharm or any other python development environment in your computer, then run these codes on your computer to obtain expected outcomes. If you have finished these procedures previously, now you only need to carry out the following steps, otherwise, you first need to accomplish the previous steps and then go to the next phase which in fact is the take-home exam question. As a take-home exam question you are kindly requested to reconfigure the developed model in a way it satisfies the following demands:The model should include three different types of agents (type_0, type_1,and type_2). 1. The model should include three different types of agents (type_0, type_1, and type_2). 2. The third agent type should be distinguished with a different color, preferably green color. 3. The population of agents should be different from each other and the sum of agents' population share should be equal to one. (e.g. red agents =0.3, blue agents =0.3, and green agents =0.4 ) 4. The homophily value for all agent types is the same (homophily value =3 ) 5. Run your program, and make sure your code is running smoothly and you get the final result which is a model with happy agents located suitably on your grid
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
