This function plots a heatmap of the pheromone intensities in a given field.
- Args:
field: A list of lists of patches, where each patch is a models.Patch object.
- Returns:
None
Example:
>>> from models.patch import Patch
>>> from utils.show_heatmap import show_heatmap
>>>
>>> field = [[Patch(x=0, y=0, pheromones=[Pheromone(intensity=1.0)]), Patch(x=1, y=0, pheromones=[Pheromone(intensity=2.0)])],
>>> [Patch(x=0, y=1, pheromones=[Pheromone(intensity=3.0)]), Patch(x=1, y=1, pheromones=[Pheromone(intensity=4.0)])]]
>>>
>>> show_heatmap(field)
import matplotlib.pyplot as plt
import numpy as np
from typing import List
from models.patch import Patch
def show_heatmap(field: List[List[Patch]]) -> None:
num_rows = len(field)
num_cols = len(field[0])
heatmap_data = np.zeros((num_rows, num_cols))
for i, row in enumerate(field):
for j, patch in enumerate(row):
heatmap_data[i, j] = sum(pheromone.get_intensity for pheromone in patch.get_pheromones())
plt.clf()
plot = plt.imshow(heatmap_data, cmap='YlOrRd', interpolation='nearest')
plt.colorbar(plot, label="Patch Pheromones Intensity")
plt.grid(visible=True, which='both', linestyle='-', linewidth=1, color='black')
plt.xticks(range(len(field[0])), range(len(field[0])))
plt.yticks(range(len(field)), range(len(field)))
plt.gca().invert_yaxis()
plt.title("Pheromone Heatmap")
plt.show(block = False)
plt.pause(1)