|
| 1 | +"""An entry-recording functionality designed to be used as the memory of an agent. |
| 2 | +
|
| 3 | +This module provides the foundational class needed for storing information in the memory of an agent. |
| 4 | +Then objective is to implement a very simple and efficient system that can be used for any agent and |
| 5 | +for any kind of information (an entry). The user defines the capacity of the memory chooses the format |
| 6 | +of the entries, which allows for a greater flexibility. Key features: |
| 7 | +
|
| 8 | +- Capacity-based memory, with a FIFO system |
| 9 | +- Efficient storage and retrieval of entries |
| 10 | +- Support for different entry_types of entries |
| 11 | +- Possibility to send entries to other agents |
| 12 | +
|
| 13 | +For now, the module contains only one main component: |
| 14 | +- Memory: A class representing the memory of an agent |
| 15 | +
|
| 16 | +""" |
| 17 | + |
| 18 | +# from experimental.devs.eventlist import EventList, SimulationEvent |
| 19 | +import copy |
| 20 | +import itertools |
| 21 | +from collections import OrderedDict |
| 22 | +from typing import Any |
| 23 | + |
| 24 | +from mesa.model import Model |
| 25 | + |
| 26 | + |
| 27 | +class Memory: |
| 28 | + """The memory of an Agent : it can store any kind of information based on a unique id of the form (agent_id, entry_id) to ensure the uniqueness of the entry. |
| 29 | +
|
| 30 | + Attributes: |
| 31 | + model (Model): The used model |
| 32 | + agent_id (int): The id of the agent |
| 33 | + capacity (int): The capacity of the memory |
| 34 | +
|
| 35 | + Structure of one entry (example): |
| 36 | + "entry_id" : { |
| 37 | + "external_agent_id" : external_agent_id, # Not mandatory : represents the agent that sent the entry (if memory was received) |
| 38 | + "entry_step" : 1, |
| 39 | + "entry_type" : "position", |
| 40 | + "entry_content" : [1,2] |
| 41 | + }, |
| 42 | +
|
| 43 | + """ |
| 44 | + |
| 45 | + def __init__(self, model: Model, agent_id: int, capacity: int): |
| 46 | + """Initializes the agent memory.""" |
| 47 | + self.model = model |
| 48 | + self.capacity = capacity |
| 49 | + self.agent_id = agent_id |
| 50 | + self._ids = itertools.count() |
| 51 | + |
| 52 | + self.memory_storage = OrderedDict() |
| 53 | + |
| 54 | + def remember( |
| 55 | + self, entry_content: Any, entry_type: Any, external_agent_id=None |
| 56 | + ) -> tuple: |
| 57 | + """Store an entry in the memory.""" |
| 58 | + entry_id = (self.agent_id, next(self._ids)) |
| 59 | + |
| 60 | + # creation of a new entry in the memory |
| 61 | + if entry_id not in self.memory_storage: |
| 62 | + self.memory_storage[entry_id] = OrderedDict() |
| 63 | + |
| 64 | + self.memory_storage[entry_id]["entry_content"] = entry_content |
| 65 | + self.memory_storage[entry_id]["entry_type"] = entry_type |
| 66 | + self.memory_storage[entry_id]["entry_step"] = self.model.steps |
| 67 | + |
| 68 | + if external_agent_id is not None: |
| 69 | + self.memory_storage[entry_id]["external_agent_id"] = external_agent_id |
| 70 | + |
| 71 | + # if the memory is longer than the capacity, we remove the oldest entry |
| 72 | + if len(self.memory_storage) > self.capacity: |
| 73 | + self.memory_storage.popitem(last=False) |
| 74 | + |
| 75 | + return entry_id |
| 76 | + |
| 77 | + def recall(self, entry_id): |
| 78 | + """Recall a specific entry.""" |
| 79 | + # Verification of the existence of the entry |
| 80 | + if entry_id not in self.memory_storage: |
| 81 | + return None |
| 82 | + return self.memory_storage[entry_id] |
| 83 | + |
| 84 | + def get_by_type(self, entry_type: str) -> list: |
| 85 | + """Returns all the ids of the entries of a specific entry_type.""" |
| 86 | + entry_list = [ |
| 87 | + entry_id |
| 88 | + for entry_id, entry in self.memory_storage.items() |
| 89 | + if entry["entry_type"] == entry_type |
| 90 | + ] |
| 91 | + return entry_list |
| 92 | + |
| 93 | + def forget(self, entry_id): |
| 94 | + """Forget a specific entry.""" |
| 95 | + if entry_id in self.memory_storage: |
| 96 | + self.memory_storage.pop(entry_id) |
| 97 | + |
| 98 | + def tell_to(self, entry_id, external_agent): |
| 99 | + """Send a precise memory to another agent by making a deep copy of the entry.""" |
| 100 | + entry_copy = copy.deepcopy(self.memory_storage[entry_id]) |
| 101 | + new_entry_id = external_agent.memory.remember( |
| 102 | + entry_copy["entry_content"], |
| 103 | + entry_copy["entry_type"], |
| 104 | + external_agent_id=self.agent_id, |
| 105 | + ) |
| 106 | + |
| 107 | + return new_entry_id |
0 commit comments