Question: Complete the code as required ( You should not modify the code given as partial solution ) : Agent implementation: Initialize the agent attributes with

Complete the code as required (You should not modify the code given as partial solution):
Agent implementation:
Initialize the agent attributes with the field health initialized as a random value between 60 and 100.
Define code to simulate agents movement by changing the agents pos assigning the value of new_pos.
Update health values decreasing by 5 on each step.
Use an appropriate method to record the agents health on each step.
Model implementation:
Create an object of type Grid using as height and width the size value in the initialization parameters. Assign this object to a model's field called room.
Create a list of 10 agents of type MyAgent. Add them to the grid environment at random positions.
Update all agents health and position. If an agents health is 0, it must be removed from the grid.
Verify if there are healthy agents left in the room. If there are no agents left, stop the simulation.
Report the last positions of each agent using theend method of MyModel.
At the end of the execution, get the records of health for each agent and draw a plot.
Extra activity: Before removing an agent (step 7), select its nearest neighbor and send a goodbye message. Add necessary methods to perform direct communication. Code: !pip install agentpy
# Model design
import agentpy as ap
import numpy as np
# Visualization
import matplotlib.pyplot as plt
import IPython class MyAgent(ap.Agent):
def setup(self):
# 1. Initialize agent's attribute: health
# Setting initial position
def setup_pos(self, env):
self.pos = env.positions[self]
# Movement simulation
def move(self):
new_pos =(self.pos[0]+ self.model.random.randint(-1,2),
self.pos[1]+ self.model.random.randint(-1,2))
# 2. Add code to simulate agents movement
def update(self):
# 3. Update health values
# Updating position values using the move method
self.move()
# 4. Record health data
class MyModel(ap.Model):
def setup(self):
# 5. Initialize a grid with the given values
# 6. Add 10 agents of type MyAgent to the environment room at random position
self.agents = # Code for list of 10 agents
# Setting position in the pos field of the agents
self.room.agents.setup_pos(self.room)
def step(self):
# 7. Update all agents values on each step
# 7. Filter all agents with health <=0. Remove them from the environment (room)
# Extra. Find the closest neighbor to the dying agent (the one that will be removed) And send a goodbye message with its id.
# 8. Check if room has agents left. If no agents, stop the simulation
def end(self):
# 9. Report Agent's positions
self.report('Agents position', [agent.pos for agent in self.agents])parameters ={'size': 10}
model = MyModel(parameters)
results = myModel.run()
# 10. Get the health results and last positions

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!