plots for linear sensitivity decay and grid pheromone decay
This commit is contained in:
parent
9cce168536
commit
11821b6138
98
main.py
98
main.py
@ -13,12 +13,23 @@ import matplotlib.pyplot as plt
|
|||||||
from mesa.space import Coordinate
|
from mesa.space import Coordinate
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
|
check_pheromone_exponential_decay()
|
||||||
|
check_ant_sensitivity_linear_decay()
|
||||||
|
|
||||||
|
def check_pheromone_exponential_decay():
|
||||||
|
"""
|
||||||
|
Check whether wanted exponential decay of pheromones on grid is done correctly
|
||||||
|
shows plot of pheromone placed on grid vs. equivalent exponential decay function
|
||||||
|
"""
|
||||||
|
|
||||||
|
from mesa.datacollection import DataCollector
|
||||||
|
|
||||||
width = 21
|
width = 21
|
||||||
height = width
|
height = width
|
||||||
num_initial_roamers = 5
|
num_initial_roamers = 0
|
||||||
num_max_agents = 100
|
num_max_agents = 100
|
||||||
nest_position : Coordinate = (width //2, height //2)
|
nest_position : Coordinate = (width //2, height //2)
|
||||||
max_steps = 100
|
max_steps = 1000
|
||||||
|
|
||||||
model = ActiveWalkerModel(width=width, height=height,
|
model = ActiveWalkerModel(width=width, height=height,
|
||||||
num_initial_roamers=num_initial_roamers,
|
num_initial_roamers=num_initial_roamers,
|
||||||
@ -26,28 +37,71 @@ def main():
|
|||||||
num_max_agents=num_max_agents,
|
num_max_agents=num_max_agents,
|
||||||
max_steps=max_steps)
|
max_steps=max_steps)
|
||||||
|
|
||||||
# just initial testing of MultiHexGrid
|
model.grid.fields["A"][5,5] = 10
|
||||||
a = model.agent_density()
|
model.datacollector = DataCollector(
|
||||||
for loc in model.grid.iter_neighborhood(nest_position):
|
model_reporters={"pheromone_a": lambda m: m.grid.fields["A"][5,5] },
|
||||||
a[loc] = 3
|
agent_reporters={}
|
||||||
for agent in model.grid.get_neighbors(pos=nest_position, include_center=True):
|
)
|
||||||
if agent.unique_id == 2:
|
model.run_model()
|
||||||
agent.look_for_chemical = "A"
|
a_test = model.datacollector.get_model_vars_dataframe()["pheromone_a"]
|
||||||
agent.prev_pos = (9,10)
|
|
||||||
a[agent.prev_pos] = 1
|
|
||||||
for pos in agent.front_neighbors:
|
|
||||||
a[pos] = 6
|
|
||||||
agent.step()
|
|
||||||
print(f"{agent._next_pos=}")
|
|
||||||
agent.advance()
|
|
||||||
print(agent.front_neighbor)
|
|
||||||
a[agent.front_neighbor] = 5
|
|
||||||
|
|
||||||
print(agent.pos, agent.unique_id, agent.look_for_chemical)
|
import matplotlib.pyplot as plt
|
||||||
neighbors = model.grid.get_neighborhood(nest_position)
|
import numpy as np
|
||||||
print(neighbors)
|
|
||||||
|
|
||||||
print(a)
|
plt.figure()
|
||||||
|
xx = np.linspace(0,1000, 10000)
|
||||||
|
yy = a_test[0]*np.exp(-model.decay_rates["A"]*xx)
|
||||||
|
plt.plot(xx, yy, label="correct exponential function")
|
||||||
|
plt.scatter(range(len(a_test)), a_test, label="modeled decay", marker='o')
|
||||||
|
plt.title("Exponential grid pheromone decay test")
|
||||||
|
plt.legend(loc='best')
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
|
def check_ant_sensitivity_linear_decay():
|
||||||
|
"""
|
||||||
|
Check whether wanted linear decay of ant sensitivity is done correctly
|
||||||
|
shows plot of ant sensitivity placed on grid vs. equivalent linear decay function
|
||||||
|
not food sources are on the grid for this run to not reset sensitivities
|
||||||
|
"""
|
||||||
|
from mesa.datacollection import DataCollector
|
||||||
|
|
||||||
|
width = 50
|
||||||
|
height = width
|
||||||
|
num_initial_roamers = 1
|
||||||
|
num_max_agents = 100
|
||||||
|
nest_position : Coordinate = (width //2, height //2)
|
||||||
|
max_steps = 1000
|
||||||
|
num_food_sources = 0
|
||||||
|
|
||||||
|
model = ActiveWalkerModel(width=width, height=height,
|
||||||
|
num_initial_roamers=num_initial_roamers,
|
||||||
|
nest_position=nest_position,
|
||||||
|
num_max_agents=num_max_agents,
|
||||||
|
num_food_sources=num_food_sources,
|
||||||
|
max_steps=max_steps)
|
||||||
|
|
||||||
|
model.datacollector = DataCollector(
|
||||||
|
model_reporters={},
|
||||||
|
agent_reporters={"sensitivity": lambda a: a.sensitivity}
|
||||||
|
)
|
||||||
|
start = model.schedule.agents[0].sensitivity_decay_rate
|
||||||
|
model.run_model()
|
||||||
|
a_test = model.datacollector.get_agent_vars_dataframe().reset_index()["sensitivity"]
|
||||||
|
|
||||||
|
import matplotlib.pyplot as plt
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
|
plt.figure()
|
||||||
|
xx = np.linspace(0,1000, 10000)
|
||||||
|
yy = a_test[0] - start*xx
|
||||||
|
plt.title("Linear Ant Sensitivity decay test")
|
||||||
|
plt.plot(xx, yy, label="correct linear function")
|
||||||
|
plt.scatter(range(len(a_test)), a_test, label="modeled decay", marker='o')
|
||||||
|
plt.legend(loc='best')
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
6
model.py
6
model.py
@ -20,6 +20,8 @@ class ActiveWalkerModel(Model):
|
|||||||
def __init__(self, width : int, height : int , num_max_agents : int,
|
def __init__(self, width : int, height : int , num_max_agents : int,
|
||||||
num_initial_roamers : int,
|
num_initial_roamers : int,
|
||||||
nest_position : Coordinate,
|
nest_position : Coordinate,
|
||||||
|
num_food_sources=5,
|
||||||
|
food_size=10,
|
||||||
max_steps:int=1000) -> None:
|
max_steps:int=1000) -> None:
|
||||||
super().__init__()
|
super().__init__()
|
||||||
fields=["A", "B", "nests", "food"]
|
fields=["A", "B", "nests", "food"]
|
||||||
@ -41,8 +43,8 @@ class ActiveWalkerModel(Model):
|
|||||||
self.schedule.add(agent)
|
self.schedule.add(agent)
|
||||||
self.grid.place_agent(agent, pos=nest_position)
|
self.grid.place_agent(agent, pos=nest_position)
|
||||||
|
|
||||||
for _ in range(5):
|
for _ in range(num_food_sources):
|
||||||
self.grid.add_food(5)
|
self.grid.add_food(food_size)
|
||||||
|
|
||||||
self.datacollector = DataCollector(
|
self.datacollector = DataCollector(
|
||||||
model_reporters={},
|
model_reporters={},
|
||||||
|
Loading…
Reference in New Issue
Block a user