agent
Agent
A generic agent class.
It is meant to define the general structure for an agent meant to evolve in a environment of olfactory cues. To define such agent, a set of methods need to be implemented. This methods can be seperated into 3 categories:
- Training methods
- Simulation methods
- General methods
The training methods are meant to train the agent before testing their performance in a simulation. A single method is needed for this:
- train()
The simulation methods are meant for the agent to make choices and receiving observations during a simulation. The following methods are required for this:
- initialize_state(): This method is meant for the state of the agent(s) to be initialized before the simulation loop starts. The state of the agent can be an internal clock, a belief or something else arbitrary.
- choose_action(): Here the agent(s) is asked to choose an action to play based on its internal state.
- update_state(): Then, after the agent(s) has taken an action, the observation it makes along with whether he reached the source or not is returned to him using this method. This allows the agent to update its internal state.
- kill(): Finally, the method asks for a set of agents to be terminated. The basic case happens when the agent reaches the source but it can also be asked to terminate if it has reached the end of the simulation without success.
The general methods are methods to perform general actions with the agent. These methods are:
- save(): To save the agent to long term storage.
- load(): To load the agent from long term storage.
- modify_environment(): To provide an equivalent agent with a different environment linked to it. If the agent has previously been trained, the trained components needs to be adapted to this new environment.
- to_gpu(): To create an alternative version of the agent whether the array instances are stored on the GPU memory instead of the CPU memory.
- to_cpu(): To create an alternative version of the agent whether the array instances are stored on the CPU memory instead of the GPU memory.
For a user to implement an agent, the main methods to define are the Simulation methods! The training method is, as stated, optional, as some agent definitions do not require it. And the General methods all have some default behavior and are therefore only needed to be overwritten in specific cases.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
environment |
Environment
|
The olfactory environment the agent is meant to evolve in. |
required |
threshold |
float or list[float]
|
The olfactory threshold. If an odor cue above this threshold is detected, the agent detects it, else it does not. If a list of threshold is provided, he agent should be able to detect |thresholds|+1 levels of odor. |
3e-6
|
actions |
dict or ndarray
|
The set of action available to the agent. It should match the type of environment (ie: if the environment has layers, it should contain a layer component to the action vector, and similarly for a third dimension). Else, a dict of strings and action vectors where the strings represent the action labels. If none is provided, by default, all unit movement vectors are included and shuch for all layers (if the environment has layers.) |
None
|
name |
str
|
A custom name for the agent. If it is not provided it will be named like " |
None
|
seed |
int
|
For reproducible randomness. |
12131415
|
Attributes:
Name | Type | Description |
---|---|---|
environment |
Environment
|
|
threshold |
float or list[float]
|
|
name |
str
|
|
action_set |
ndarray
|
The actions allowed of the agent. Formulated as movement vectors as [(layer,) (dz,) dy, dx]. |
action_labels |
list[str]
|
The labels associated to the action vectors present in the action set. |
saved_at |
str
|
If the agent has been saved, the path at which it is saved is recorded in this variable. |
on_gpu |
bool
|
Whether the arrays are on the GPU memory or not. For this, the support for Cupy needs to be enabled and the agent needs to have been moved to the GPU using the to_gpu() function. |
class_name |
str
|
The name of the class of the agent. |
seed |
int
|
The seed used for the random operations (to allow for reproducability). |
rnd_state |
RandomState
|
The random state variable used to generate random values. |
Source code in olfactory_navigation/agent.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
class_name
property
The name of the class of the agent.
choose_action()
Function to allow for the agent(s) to choose an action to take based on its current state.
It should return a 2D array of shape n by 2 (or 3, or 4 depending of whether the environment has layers and/or a 3rd dimension), where n is how many agents are to choose an action. It should be n 2D vectors of (the layer and) the change in the (z,) y, and x positions.
Returns:
Name | Type | Description |
---|---|---|
movement_vector |
ndarray
|
An array of n vectors in 2D space of the movement(s) the agent(s) will take. |
Source code in olfactory_navigation/agent.py
229 230 231 232 233 234 235 236 237 238 239 240 241 |
|
initialize_state(n=1)
Function to initialize the internal state of the agent(s) for the simulation process. The internal state can be concepts such as the "memory" or "belief" of the agent. The n parameter corresponds to how many "instances" need to instanciated. This is meant so that we work with a "group" of agents instead of individual instances.
This is done with the purpose that the state of the group of agents be stored in (Numpy) arrays to allow vectorization instead of sequential loops.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n |
int
|
How many agents to initialize. |
1
|
Source code in olfactory_navigation/agent.py
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
|
kill(simulations_to_kill)
Function to kill any agents that either reached the source or failed by not reaching the source before the horizon or failing to update its own state. The agents where the simulations_to_kill paramater is True have to removed from the list of agents. It is necessary because their reference will also be removed from the simulation loop. Therefore, if they are not removed, the array sizes will not match anymore.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
simulations_to_kill |
ndarray
|
An array of size n containing boolean values of whether or not agent's simulations are terminated and therefore should be removed. |
required |
Source code in olfactory_navigation/agent.py
268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
|
load(folder)
classmethod
Function to load a trained agent from long term storage. By default, as for the save function, it will load the agent from the folder assuming it is a pickle file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder |
str
|
The folder in which the agent was saved. |
required |
Returns:
Name | Type | Description |
---|---|---|
loaded_agent |
Agent
|
The agent loaded from the folder. |
Source code in olfactory_navigation/agent.py
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 |
|
modify_environment(new_environment)
Function to modify the environment of the agent.
Note: By default, a new agent is created with the same threshold and name but with a this new environment! If there are any trained elements to the agent, they are to be modified in this method to be adapted to this new environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
new_environment |
Environment
|
The new environment to replace the agent in an equivalent agent. |
required |
Returns:
Name | Type | Description |
---|---|---|
modified_agent |
Agent
|
A new Agent whose environment has been replaced. |
Source code in olfactory_navigation/agent.py
368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
save(folder=None, force=False, save_environment=False)
Function to save a trained agent to long term storage. By default, the agent is saved in its entirety using pickle.
However, it is strongly advised to overwrite this method to only save save the necessary components of the agents in order to be able to load it and reproduce its behavior. For instance, if the agent is saved after the simulation is run, the state would also be saved within the pickle which is not wanted.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder |
str
|
The folder in which the agent's data should be saved. |
None
|
force |
bool
|
If the agent is already saved at the folder provided, the saving should fail. If the already saved agent should be overwritten, this parameter should be toggled to True. |
False
|
save_environment |
bool
|
Whether to save the agent's linked environment alongside the agent itself. |
False
|
Source code in olfactory_navigation/agent.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 |
|
to_cpu()
Function to send the numpy arrays of the agent to the gpu. It returns a new instance of the Agent class with the arrays on the gpu.
Returns:
Name | Type | Description |
---|---|---|
cpu_agent |
Agent
|
A new environment instance where the arrays are on the cpu memory. |
Source code in olfactory_navigation/agent.py
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 |
|
to_gpu()
Function to send the numpy arrays of the agent to the gpu. It returns a new instance of the Agent class with the arrays on the gpu.
Returns:
Name | Type | Description |
---|---|---|
gpu_agent |
Agent
|
A new environment instance where the arrays are on the gpu memory. |
Source code in olfactory_navigation/agent.py
393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
train()
Optional function to train the agent in the olfactory environment it is in. This function is optional as some agents have some fixed behavior and therefore dont require training.
Source code in olfactory_navigation/agent.py
201 202 203 204 205 206 |
|
update_state(observation, source_reached)
Function to update the internal state(s) of the agent(s) based on the action(s) taken and the observation(s) received. The observations are then compared with the threshold to decide whether something was sensed or not.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
observation |
ndarray
|
A 1D array of odor cues (float values) retrieved from the environment. |
required |
source_reached |
array
|
A 1D array of boolean values signifying whether each agent reached or not the source. |
required |
Returns:
Name | Type | Description |
---|---|---|
update_successfull |
(ndarray, optional)
|
If nothing is returned, it means all the agent's state updates have been successfull. Else, a boolean np.ndarray of size n can be returned confirming for each agent whether the update has been successful or not. |
Source code in olfactory_navigation/agent.py
244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 |
|