dynamic normalization for pheromones
This commit is contained in:
parent
e45d33bf8d
commit
ecb9cc26e9
46
server.py
46
server.py
@ -39,14 +39,16 @@ def setup(params=None):
|
|||||||
canvas_width = 500
|
canvas_width = 500
|
||||||
canvas_height = 500
|
canvas_height = 500
|
||||||
|
|
||||||
def __init__(self, portrayal_method, grid_width, grid_height, canvas_width=500, canvas_height=500,):
|
def __init__(self, portrayal_method, grid_width, grid_height, canvas_width=500, canvas_height=500, norm_method=None):
|
||||||
super().__init__(portrayal_method, grid_width, grid_height, canvas_width, canvas_height)
|
super().__init__(portrayal_method, grid_width, grid_height, canvas_width, canvas_height)
|
||||||
|
self.norm_method = norm_method
|
||||||
|
|
||||||
def render(self, model):
|
def render(self, model):
|
||||||
grid_state = defaultdict(list)
|
grid_state = defaultdict(list)
|
||||||
|
norm = self.norm_method(model)
|
||||||
for x in range(model.grid.width):
|
for x in range(model.grid.width):
|
||||||
for y in range(model.grid.height):
|
for y in range(model.grid.height):
|
||||||
portrayal = self.portrayal_method(model, (x, y))
|
portrayal = self.portrayal_method(model, (x, y), norm)
|
||||||
if portrayal:
|
if portrayal:
|
||||||
portrayal["x"] = x
|
portrayal["x"] = x
|
||||||
portrayal["y"] = y
|
portrayal["y"] = y
|
||||||
@ -63,13 +65,13 @@ def setup(params=None):
|
|||||||
return max(int(255 - level * 255 / normalization), 0)
|
return max(int(255 - level * 255 / normalization), 0)
|
||||||
|
|
||||||
|
|
||||||
def portray_ant_density(model, pos):
|
def portray_ant_density(model, pos, norm):
|
||||||
if model.grid.is_nest(pos):
|
if model.grid.is_nest(pos):
|
||||||
col = "red"
|
col = "red"
|
||||||
elif model.grid.is_food(pos):
|
elif model.grid.is_food(pos):
|
||||||
col = "green"
|
col = "green"
|
||||||
else:
|
else:
|
||||||
col = get_color(level=len(model.grid[pos]), normalization=5)
|
col = get_color(level=len(model.grid[pos]), normalization=norm)
|
||||||
col = f"rgb({col}, {col}, {col})"
|
col = f"rgb({col}, {col}, {col})"
|
||||||
|
|
||||||
|
|
||||||
@ -83,9 +85,12 @@ def setup(params=None):
|
|||||||
"Color": col,
|
"Color": col,
|
||||||
}
|
}
|
||||||
|
|
||||||
def portray_pheromone_density(model, pos):
|
def get_max_grid_val(model, key):
|
||||||
col_a = get_color(level=model.grid.fields["A"][pos], normalization=3)
|
return np.max(model.grid.fields[key])
|
||||||
col_b = get_color(level=model.grid.fields["B"][pos], normalization=3)
|
|
||||||
|
def portray_pheromone_density(model, pos, norm):
|
||||||
|
col_a = get_color(level=model.grid.fields["A"][pos], normalization=norm)
|
||||||
|
col_b = get_color(level=model.grid.fields["B"][pos], normalization=norm)
|
||||||
return {
|
return {
|
||||||
"Shape": "hex",
|
"Shape": "hex",
|
||||||
"r": 1,
|
"r": 1,
|
||||||
@ -101,10 +106,31 @@ def setup(params=None):
|
|||||||
width = params['width']
|
width = params['width']
|
||||||
height = params['height']
|
height = params['height']
|
||||||
pixel_ratio = 10
|
pixel_ratio = 10
|
||||||
grid_ants = CanvasHexGridMultiAgents(portray_ant_density, width, height, width*pixel_ratio, height*pixel_ratio)
|
grid_ants = CanvasHexGridMultiAgents(portray_ant_density,
|
||||||
grid_pheromones = CanvasHexGridMultiAgents(portray_pheromone_density, width, height, width*pixel_ratio, height*pixel_ratio)
|
width, height, width*pixel_ratio, height*pixel_ratio,
|
||||||
|
norm_method=lambda m: 5)
|
||||||
|
|
||||||
|
def norm_ants(model):
|
||||||
|
return 5
|
||||||
|
|
||||||
|
def norm_pheromones(model):
|
||||||
|
max_a = np.max(model.grid.fields["A"])
|
||||||
|
max_b = np.max(model.grid.fields["B"])
|
||||||
|
return np.ceil(np.max([max_a, max_b, 20]) + 1e-4).astype(int)
|
||||||
|
|
||||||
|
grid_pheromones = CanvasHexGridMultiAgents(portray_pheromone_density,
|
||||||
|
width, height, width*pixel_ratio, height*pixel_ratio,
|
||||||
|
norm_method=norm_pheromones
|
||||||
|
)
|
||||||
test_text = TextElement()
|
test_text = TextElement()
|
||||||
return ModularServer(ActiveWalkerModel, [lambda m: "<h3>Ant density</h3><h5>Nest: Red, Food: Green</h5>", grid_ants, lambda m: "<h3>Pheromone Density</h3><h5>Pheromone A: Cyan, Pheromone B: Pink</h5>", grid_pheromones],
|
return ModularServer(ActiveWalkerModel,
|
||||||
|
[lambda m: "<h3>Ant density</h3><h5>Nest: Red, Food: Green</h5>",
|
||||||
|
grid_ants,
|
||||||
|
lambda m: f"<h5>Normalization Value: {norm_ants(m)}</h5>",
|
||||||
|
lambda m: "<h3>Pheromone Density</h3><h5>Pheromone A: Cyan, Pheromone B: Pink</h5>",
|
||||||
|
grid_pheromones,
|
||||||
|
lambda m: f"<h5>Normalization Value: {norm_pheromones(m)}</h5>"
|
||||||
|
],
|
||||||
"Active Random Walker Ants", params)
|
"Active Random Walker Ants", params)
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
Loading…
Reference in New Issue
Block a user