olfactory_navigation
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 |
thresholds
|
float or list[float] or dict[str, float] or dict[str, list[float]]
|
The olfactory thresholds. If an odor cue above this threshold is detected, the agent detects it, else it does not. If a list of thresholds is provided, he agent should be able to detect |thresholds|+1 levels of odor. A dictionary of (list of) thresholds can also be provided when the environment is layered. In such case, the number of layers provided must match the environment's layers and their labels must match. The thresholds provided will be converted to an array where the levels start with -inf and end with +inf. |
3e-6
|
space_aware
|
bool
|
Whether the agent is aware of it's own position in space. This is to be used in scenarios where, for example, the agent is an enclosed container and the source is the variable. Note: The observation array will have a different shape when returned to the update_state function! |
False
|
spacial_subdivisions
|
ndarray
|
How many spacial compartments the agent has to internally represent the space it lives in. By default, it will be as many as there are grid points in the environment. |
None
|
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
|
|
thresholds |
ndarray
|
An array of the thresholds of detection, starting with -inf and ending with +inf. In the case of a 2D array of thresholds, the rows of thresholds apply to the different layers of the environment. |
space_aware |
bool
|
|
spacial_subdivisions |
ndarray
|
|
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. |
cpu_version |
Agent
|
An instance of the agent on the CPU. If it already is, it returns itself. |
gpu_version |
Agent
|
An instance of the agent on the CPU. If it already is, it returns itself. |
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 |
|
class_name
property
The name of the class of the agent.
cpu_version
property
A version of the Agent on the CPU. If the agent is already on the CPU it returns itself, otherwise the to_cpu function is called to generate a new one.
gpu_version
property
A version of the Agent on the GPU. If the agent is already on the GPU it returns itself, otherwise the to_gpu function is called to generate a new one.
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
329 330 331 332 333 334 335 336 337 338 339 340 341 |
|
discretize_observations(observation, action, source_reached)
Function to convert a set of observations to discrete observation ids. It uses the olfaction thresholds of the agent to discretize the odor concentrations.
In the case where the agent is also aware of it's own position in space, in which case the observation matrix is of size n by 1 + environment.dimensions, the agent first converts the points on the grid to position ids and then multiply the id by olfactory observation id.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
observation
|
ndarray
|
The observations the agent receives, in the case the agent is space_aware, the position point is also included. |
required |
action
|
ndarray
|
The action the agent did. This parameter is used in the particular case where the environment has layers and the odor thresholds are layer dependent. |
required |
source_reached
|
ndarray
|
A 1D array of boolean values signifying whether each agent reached or not the source. |
required |
Returns:
Name | Type | Description |
---|---|---|
discrete_observations |
ndarray
|
An integer array of discrete observations |
Source code in olfactory_navigation/agent.py
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 |
|
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
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
|
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
438 439 440 441 442 443 444 445 446 447 448 449 450 451 |
|
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
509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 |
|
modify_environment(new_environment)
Function to modify the environment of the agent.
Note: By default, a new agent is created with the same thresholds 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
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 |
|
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
457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 |
|
to_cpu()
Function to send the numpy arrays of the agent to the cpu. It returns a new instance of the Agent class with the arrays on the cpu.
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
606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 |
|
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
564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
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
301 302 303 304 305 306 |
|
update_state(action, 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 thresholds to decide whether something was sensed or not or to what level.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action
|
ndarray
|
A 2D array of n movement vectors. If the environment is layered, the 1st component should be the layer. |
required |
observation
|
ndarray
|
A n by 1 (or 1 + environment.dimensions if space_aware) 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
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 |
|
Environment
Class to represent an olfactory environment.
It is defined based on an olfactory data set provided as either a numpy file or an array directly with shape time, y, x. From this environment, the various parameters are applied in the following order:
- The source position is set
- The margins are added and the shape (total size) of the environment are set.
- The data file's x and y components are squished and streched the to fit the inter-marginal shape of the environment.
- The source's position is also moved to stay at the same position within the data.
- The multiplier is finally applied to modify the data file's x and y components a final time by growing or shrinking the margins to account for the multiplier. (The multiplication applies with the source position as a center point)
Note: to modify the shape of the data file's x and y components the OpenCV library's resize function is used. And the interpolation method is controlled by the interpolation_method parameter.
Then, the starting probability map is built. Either an array can be provided directly or preset option can be chosen:
- 'data_zone': The agent can start at any point in the data_zone (after all the modification parameters have been applied)
- 'odor_present': The agent can start at any point where an odor cue above the odor_present_threshold can be found at any timestep during the simulation
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_file
|
str or ndarray
|
The dataset containing the olfactory data. It can be provided as a path to a file containing said array. |
required |
data_source_position
|
list or ndarray
|
The center point of the source provided as a list or a 1D array with the components being x,y. This position is computed in the olfactory data zone (so excluding the margins). |
required |
source_radius
|
float
|
The radius from the center point of the source in which we consider the agent has reached the source. |
1.0
|
layers
|
bool or list[int] or list[str]
|
Whether or not the data provided contains layers or not. If a list of strings is provided, it will be either used to name the layers found (if numpy data), or it is used to querry the datasets of the h5 file. |
False
|
shape
|
list or ndarray
|
A 2-element array or list of how many units should be kept in the final array (including the margins). As it should include the margins, the shape should be strictly larger than the sum of the margins in each direction. By default, the shape of the olfactory data will be maintained. |
None
|
margins
|
int or list or ndarray
|
How many units have to be added to the data as margins. (Before the multiplier is applied) If a unique element is provided, the margin will be this same value on each side. If a list or array of 2 elements is provided, the first number will be vertical margins (y-axis), while the other will be on the x-axis (horizontal). |
0
|
multiplier
|
list or ndarray
|
A 2-element array or list of how much the odor field should be streched in each direction. If a value larger than 1 is provided, the margins will be reduced to accomodate for the larger size of the olfactory data size. And inversly, less than 1 will increase the margins. By default, the multipliers will be set to 1.0. |
[1.0,1.0]
|
interpolation_method
|
Nearest or Linear or Cubic
|
The interpolation method to be used in the case the data needs to be reshaped to fit the shape, margins and multiplier parameters. By default, it uses Bi-linear interpolation. The interpolation is performed using the OpenCV library. |
'Linear'
|
preprocess_data
|
bool
|
Applicable only for data_file being a path to a h5 file. Whether to reshape of the data at the creation of the environment. Reshaping the data ahead of time will require more processing at the creation and more memory overall. While if this is disabled, when gathering observations, more time will be required but less memory will need to be used at once. |
False
|
boundary_condition
|
stop or wrap or wrap_vertical or wrap_horizontal or clip
|
How the agent should behave at the boundary. Stop means for the agent to stop at the boundary, if the agent tries to move north while being on the top edge, it will stay in the same state. Wrap means for the borders to be like portals, when entering on one side, it reappears on the other side. Wrap can be specified to be only vertically or horizontally |
'stop'
|
start_zone
|
odor_present or data_zone or ndarray
|
Either an array or a string representing how the starting probabilities should be constructed. - odor_present: The start probabilities will be uniform where odor cues can be found above 0 (or a given odor_present_threshold) - data_zone: Uniform over the data zone, so without the margins. Note that the points within the source radius will be excluded from this probability grid. |
'data_zone'
|
odor_present_threshold
|
float
|
An olfactory threshold, under which the odor is considered too low to be noticed. It is used only to build the starting zone if the 'odor_present' option is selected. |
None
|
name
|
str
|
A custom name to be given to the agent.
If it is not provided, by default it will have the format:
|
None
|
seed
|
int
|
For reproducible randomness. |
12131415
|
Attributes:
Name | Type | Description |
---|---|---|
data |
ndarray
|
An array containing the olfactory data after the modification parameters have been applied. |
data_file_path |
str
|
If the data is loaded from a path, the path will be recorded here. |
data_source_position |
ndarray
|
The position of the source in the original data file (after modifications have been applied). |
layers |
ndarray
|
A numbered list of the IDs of the layers. |
layer_labels |
list[str]
|
A list of how the layers are named. |
has_layers |
bool
|
Whether or not the environment is made up of layers. |
margins |
ndarray
|
An array of the margins vertically and horizontally (after multiplier is applied). |
timestamps |
int
|
The amount of timeslices available in the environment. |
data_shape |
tuple[int]
|
The shape of the data's odor field (after modifications have been applied). |
dimensions |
int
|
The amount of dimensions of the physical space of the olfactory environment. |
shape |
tuple[int]
|
The shape of the environment. It is a tuple of the size in each axis of the environment. |
data_bounds |
ndarray
|
The bounds between which the original olfactory data stands in the coordinate system of the environment (after modifications have been applied). |
source_position |
ndarray
|
The position of the source in the padded grid (after modifications have been applied). |
source_radius |
float
|
The radius of the source. |
interpolation_method |
str
|
The interpolation used to modify the shape of the original data. |
data_processed |
bool
|
Whether the data was processed (ie the shape is at it should be) or not. |
boundary_condition |
str
|
How the agent should behave when reaching the boundary. |
start_probabilities |
ndarray
|
A probability map of where the agent is likely to start within the environment. Note: Zero within the source radius. |
start_type |
str
|
The type of the start probability map building. For instance: 'data_zone', 'odor_present', or 'custom' (if an array is provided). |
odor_present_threshold |
float
|
The threshold used to uild the start probabilities if the option 'odor_present' is used. |
name |
str
|
The name set to the agent as defined in the parameters. |
saved_at |
str
|
If the environment is saved, the path at which it is saved will be recorded here. |
on_gpu |
bool
|
Whether the environment's arrays are on the gpu's memory or not. |
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. |
cpu_version |
Environment
|
An instance of the environment on the CPU. If it already is, it returns itself. |
gpu_version |
Environment
|
An instance of the environment on the CPU. If it already is, it returns itself. |
Source code in olfactory_navigation/environment.py
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
|
_data_is_numpy
property
Wheter the data is a numpy array or not.
cpu_version
property
A version of the Environment on the CPU. If the environment is already on the CPU it returns itself, otherwise the to_cpu function is called to generate a new one.
data
property
The whole dataset with the right shape. If not preprocessed to modify its shape the data will be processed when querrying this object.
gpu_version
property
A version of the Environment on the GPU. If the environment is already on the GPU it returns itself, otherwise the to_gpu function is called to generate a new one.
distance_to_source(point, metric='manhattan')
Function to compute the distance(s) between given points and the source point.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
point
|
ndarray
|
A single or an Nx2 array containing N points. |
required |
metric
|
manhattan
|
The metric to use to compute the distance. |
'manhattan'
|
Returns:
Name | Type | Description |
---|---|---|
dist |
float or ndarray
|
A single distance or a list of distance in a 1D distance array. |
Source code in olfactory_navigation/environment.py
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 |
|
get_observation(pos, time=0, layer=0)
Function to get an observation at a given position on the grid at a given time. A set of observations can also be requested, either at a single position for multiple timestamps or with the same amoung of positions as timestamps provided.
Note: The position will not be checked against boundary conditions; if a position is out-of-bounds it will simply return 0.0!
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos
|
ndarray
|
The position or list of positions to get observations at. |
required |
time
|
int or ndarray
|
A timestamp or list of timestamps to get the observations at. |
0
|
layer
|
int or ndarray
|
A layer or list of timestamps to get the observations at. Note: If the environment doesnt have layers, this parameter will be ignored. |
0
|
Returns:
Name | Type | Description |
---|---|---|
observation |
float or ndarray
|
A single observation or list of observations. |
Source code in olfactory_navigation/environment.py
562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 |
|
load(folder)
classmethod
Function to load an environment from a given folder.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder
|
str
|
The folder of the Environment. |
required |
Returns:
Name | Type | Description |
---|---|---|
loaded_env |
Environment
|
The loaded environment. |
Source code in olfactory_navigation/environment.py
916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 |
|
modify(data_source_position=None, source_radius=None, shape=None, margins=None, multiplier=None, interpolation_method=None, boundary_condition=None)
Returns a copy of the environment with one or more parameters modified.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_source_position
|
list | ndarray | None
|
A new position for the source relative to the data file. |
None
|
source_radius
|
float | None
|
A new source radius. |
None
|
shape
|
list | ndarray | None
|
A new shape of environment. |
None
|
margins
|
int | list | ndarray | None
|
A new set of margins. |
None
|
multiplier
|
list | ndarray | None
|
A new multiplier to be applied to the data file (this will in turn increase or reduce the margins). |
None
|
interpolation_method
|
str | None
|
A new interpolation method to be used. |
None
|
boundary_condition
|
str | None
|
New boundary conditions for how the agent should behave at the edges. |
None
|
Returns:
Type | Description |
---|---|
modified_environment
|
A copy of the environment where the modified parameters have been applied. |
Source code in olfactory_navigation/environment.py
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 |
|
modify_scale(scale_factor)
Function to modify the size of the environment by a scale factor. Everything will be scaled this factor. This includes: shape, margins, source radius, and data shape.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
scale_factor
|
float
|
By how much to modify the size of the current environment. |
required |
Returns:
Name | Type | Description |
---|---|---|
modified_environment |
Environment
|
The environment with the scale factor applied. |
Source code in olfactory_navigation/environment.py
1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 |
|
move(pos, movement)
Applies a movement vector to a position point and returns a new position point while respecting the boundary conditions.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos
|
ndarray
|
The start position of the movement. |
required |
movement
|
ndarray
|
A 2D movement vector. |
required |
Returns:
Name | Type | Description |
---|---|---|
new_pos |
ndarray
|
The new position after applying the movement. |
Source code in olfactory_navigation/environment.py
716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 |
|
plot(frame=0, layer=0, ax=None)
Simple function to plot the environment with a single frame of odor cues. The starting zone is also market down with a blue contour. The source of the odor is marked by a red circle.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
frame
|
int
|
The frame of odor cues to print. |
0
|
layer
|
int
|
The layer of the odor cues to print. (Ignored if the environment is not layered.) |
0
|
ax
|
Axes
|
An ax on which the environment can be plot. |
None
|
Source code in olfactory_navigation/environment.py
486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 |
|
random_start_points(n=1)
Function to generate n starting positions following the starting probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
n
|
int
|
How many random starting positions to generate |
1
|
Returns:
Name | Type | Description |
---|---|---|
random_states_2d |
ndarray
|
The n random 2d points in a n x 2 array. |
Source code in olfactory_navigation/environment.py
691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 |
|
save(folder=None, save_arrays=False, force=False)
Function to save the environment to the memory.
By default it saved in a new folder at the current path in a new folder with the name 'Env-
The numpy arrays of the environment (grid and start_probabilities) can be saved or not. If not, when the environment is loaded it needs to be reconstructed from the original data file. The arrays are saved to .npy files along with the METADATA file.
If an environment of the same name is already saved, the saving will be interupted. It can however be forced with the force parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
folder
|
str
|
The folder to which to save the environment data. If it is not provided, it will be created in the current folder. |
None
|
save_arrays
|
bool
|
Whether or not to save the numpy arrays to memory. (The arrays can be heavy) |
False
|
force
|
bool
|
In case an environment of the same name is already saved, it will be overwritten. |
False
|
Source code in olfactory_navigation/environment.py
814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 |
|
source_reached(pos)
Checks whether a given position is within the source radius.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pos
|
ndarray
|
The position to check whether in the radius of the source. |
required |
Returns:
Name | Type | Description |
---|---|---|
is_at_source |
bool
|
Whether or not the position is within the radius of the source. |
Source code in olfactory_navigation/environment.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 |
|
to_cpu()
Function to send the numpy arrays of the environment to the cpu memory. It returns a new instance of the Environment with the arrays as numpy arrays.
Returns:
Name | Type | Description |
---|---|---|
cpu_environment |
Environment
|
A new environment instance where the arrays are on the cpu memory. |
Source code in olfactory_navigation/environment.py
1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 |
|
to_gpu()
Function to send the numpy arrays of the environment to the gpu memory. It returns a new instance of the Environment with the arrays as cupy arrays.
Returns:
Name | Type | Description |
---|---|---|
gpu_environment |
Environment
|
A new environment instance where the arrays are on the gpu memory. |
Source code in olfactory_navigation/environment.py
1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 |
|
SimulationHistory
Class to record the steps that happened during a simulation with the following information being saved:
- the positions the agents pass by
- the actions the agents take
- the observations the agents receive ('observations')
- the time in the simulation process
Parameters:
Name | Type | Description | Default |
---|---|---|---|
start_points
|
ndarray
|
The initial points of the agents in the simulation. |
required |
environment
|
Environment
|
The environment on which the simulation is run (can be different from the one associated with the agent). |
required |
agent
|
Agent
|
The agent used in the simulation. |
required |
time_shift
|
ndarray
|
An array of time shifts in the simulation data. |
required |
horizon
|
int
|
The horizon of the simulation. i.e. how many steps can be taken by the agent during the simulation before he is considered lost. |
required |
reward_discount
|
float
|
A discount to be applied to the rewards received by the agent. (eg: reward of 1 received at time n would be: 1 * reward_discount^n) |
0.99
|
Attributes:
Name | Type | Description |
---|---|---|
start_points |
ndarray
|
|
environment |
Environment
|
|
agent |
Agent
|
|
time_shift |
ndarray
|
|
horizon |
int
|
|
reward_discount |
float
|
|
environment_dimensions |
int
|
The amount of dimensions of the environment. |
environment_shape |
tuple[int]
|
The shape of the environment. |
environment_source_position |
ndarray
|
The position of the odor source in the environment. |
environment_source_radius |
float
|
The radius of the odor source in the environment. |
environment_layer_labels |
list[str] or None
|
A list of the layer labels if the environment has layers. |
agent_thresholds |
ndarray
|
An array of the olfaction thresholds of the agent. |
n |
int
|
The amount of simulations. |
start_time |
datetime
|
The datetime the simulations start. |
actions |
list[ndarray]
|
A list of numpy arrays. At each step of the simulation, an array of shape n by 2 is appended to this list representing the n actions as dy,dx vectors. |
positions |
list[ndarray]
|
A list of numpy arrays. At each step of the simulation, an array of shape n by 2 is appended to this list representing the n positions as y,x vectors. |
observations |
list[ndarray]
|
A list of numpy arrays. At each step of the simulation, an array of shape n is appended to this list representing the n observations received by the agents. |
reached_source |
ndarray
|
A numpy array of booleans saying whether the simulations reached the source or not. |
done_at_step |
ndarray
|
A numpy array containing n elements that records when a given simulation reaches the source (-1 is not reached). |
Source code in olfactory_navigation/simulation.py
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 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 |
|
done_count
property
Returns how many simulations are terminated (whether they reached the source or not).
general_analysis_df
property
A Pandas DataFrame analyzing the results of the simulations. Summarizing the performance of all the simulations with the following metrics:
- converged: Whether or not the simulation reached the source
- reached_horizon: Whether the failed simulation reached to horizon
- steps_taken: The amount of steps the agent took to reach the source, (horizon if the simulation did not reach the source)
- discounted_rewards: The discounted reward received by the agent over the course of the simulation
- extra_steps: The amount of extra steps compared to the optimal trajectory
- t_min_over_t: Normalized version of the extra steps measure, where it tends to 1 the least amount of time the agent took to reach the source compared to an optimal trajectory.
For the measures (converged, steps_taken, discounted_rewards, extra_steps, t_min_over_t), the average and standard deviations are computed in rows at the top.
runs_analysis_df
property
A Pandas DataFrame analyzing the results of the simulations. It aggregates the simulations in single rows, recording:
: The starting positions at the given axis - optimal_steps_count: The minimal amount of steps to reach the source
- converged: Whether or not the simulation reached the source
- reached_horizon: Whether the failed simulation reached to horizon
- steps_taken: The amount of steps the agent took to reach the source, (horizon if the simulation did not reach the source)
- discounted_rewards: The discounted reward received by the agent over the course of the simulation
- extra_steps: The amount of extra steps compared to the optimal trajectory
- t_min_over_t: Normalized version of the extra steps measure, where it tends to 1 the least amount of time the agent took to reach the source compared to an optimal trajectory.
simulation_dfs
property
A list of the pandas DataFrame where each dataframe is a single simulation history. Each row is a different time instant of simulation process with each column being:
- time (of the simulation data)
- [position] (z,) y, x OR x0, x1, ... xn
- (layer)
- [movement] (dz,) dy, dx OR dx0, dx1, ... dxn
- o (pure, not thresholded)
- reached_source (boolean)
simulations_at_horizon
property
Returns a boolean array of which simulations reached the horizon.
success_count
property
Returns how many simulations reached the source.
summary
property
A string summarizing the performances of all the simulations. The metrics used are averages of:
- Step count
- Extra steps
- Discounted rewards
- Tmin / T
Along with the respective the standard deviations and equally for only for the successful simulations.
add_step(actions, next_positions, observations, reached_source, interupt)
Function to add a step in the simulation history.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
actions
|
ndarray
|
The actions that were taken by the agents. |
required |
next_positions
|
ndarray
|
The positions that were reached by the agents after having taken actions. |
required |
observations
|
ndarray
|
The observations the agents receive after having taken actions. |
required |
reached_source
|
ndarray
|
A boolean array of whether each agent has reached the source or not. |
required |
interupt
|
ndarray
|
A boolean array of whether each agent has to be terminated even if it hasnt reached the source yet. |
required |
Source code in olfactory_navigation/simulation.py
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 |
|
compute_distance_to_source()
Function to compute the optimal distance to the source of each starting point according to the optimal_distance_metric attribute.
Returns:
Name | Type | Description |
---|---|---|
distance |
ndarray
|
The optimal distances to the source point. |
Source code in olfactory_navigation/simulation.py
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 |
|
load_from_file(file, environment=False, agent=False)
classmethod
Function to load the simulation history from a file. This can be useful to use the plot functions on the simulations saved in succh file.
The environment and agent can provided as a backup in the case they cannot be loaded from the file.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
A file (with the path) of the simulation histories csv. (the analysis file cannot be used for this) |
required |
environment
|
bool or Environment
|
If set to True, it will try to load the environment that was used for the simulation (if the save path is available). Or, an environment instance to be linked with the simulation history object. |
False
|
agent
|
bool or Agent
|
If set to True, it will try to load the agent that was used for the simulation (if the save path is available). An agent instance to be linked with the simulation history object. |
False
|
Returns:
Name | Type | Description |
---|---|---|
hist |
SimulationHistory
|
The loaded instance of a simulation history object. |
Source code in olfactory_navigation/simulation.py
582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 |
|
plot(sim_id=0, ax=None)
Function to plot a the trajectory of a given simulation. An ax can be use to plot it on.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sim_id
|
int
|
The id of the simulation to plot. |
0
|
ax
|
Axes
|
The ax on which to plot the path. (If not provided, a new axis will be created) |
None
|
Source code in olfactory_navigation/simulation.py
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 |
|
plot_runtimes(ax=None)
Function to plot the runtimes over the iterations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ax
|
Axes
|
The ax on which to plot the path. (If not provided, a new axis will be created) |
None
|
Source code in olfactory_navigation/simulation.py
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 |
|
plot_successes(ax=None)
Function to plot a 2D map of whether a given starting point was successfull or not (and whether it died early).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
ax
|
Axes
|
The ax on which to plot the path. (If not provided, a new axis will be created) |
None
|
Source code in olfactory_navigation/simulation.py
869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 |
|
save(file=None, folder=None, save_analysis=True, save_components=False)
Function to save the simulation history to a csv file in a given folder. Additionally, an analysis of the runs can be saved if the save_analysis is enabled. The environment and agent used can be saved in the saved folder by enabling the 'save_component' parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
The name of the file the simulation histories will be saved to.
If it is not provided, it will be by default "Simulations- |
None
|
folder
|
str
|
Folder to save the simulation histories to. If the folder name is not provided the current folder will be used. |
None
|
save_analysis
|
bool
|
Whether to save an additional csv file with an analysis of the runs of the simulation. It will contain the amount of steps taken, the amount of extra steps compared to optimality, the discounted rewards and the ratio between optimal trajectory and the steps taken. The means and standard deviations of all the runs are also computed. The file will have the same name as the simulation history file with an additional '-analysis' tag at the end. |
True
|
save_components
|
bool
|
Whether or not to save the environment and agent along with the simulation histories in the given folder. |
False
|
Source code in olfactory_navigation/simulation.py
474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 |
|
run_test(agent, n=None, start_points=None, environment=None, time_shift=0, time_loop=True, horizon=1000, initialization_values={}, reward_discount=0.99, print_progress=True, print_stats=True, print_warning=True, use_gpu=False, batches=-1)
Function to run n simulations for a given agent in its environment (or a given modified environment). The simulations start either from random start points or provided trough the start_points parameter. The simulation can have shifted initial times (in the olfactory simulation).
The simulation will run for at most 'horizon' steps, after which the simulations will be considered failed.
Some statistics can be printed at end of the simulation with the 'print_stats' parameter. It will print some performance statisitcs about the simulations such as the average discounter reward. The reward discount can be set by the 'reward_discount' parameter.
To speedup the simulations, it can be run on the gpu by toggling the 'use_gpu' parameter. This will have the consequence to send the various arrays to the gpu memory. This will only work if the agent has the support for to work with cupy arrays.
This method returns a SimulationHistory object that saves all the positions the agent went through, the actions the agent took, and the observation the agent received. It also provides the possibility the save the results to a csv file and plot the various trajectories.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
agent
|
Agent
|
The agent to be tested |
required |
n
|
int
|
How many simulation to run in parallel. n is optional but it needs to match with what is provided in start_points. |
None
|
start_points
|
ndarray
|
The starting points of the simulation in 2d space. If not provided, n random points will be generated based on the start probabilities of the environment. Else, the amount of start_points need to match to n, if it is provided. |
None
|
environment
|
Environment
|
The environment to run the simulations in. By default, the environment linked to the agent will used. This parameter is intended if the environment needs to be modified compared to environment the agent was trained on. |
None
|
time_shift
|
int or ndarray
|
The time at which to start the olfactory simulation array. It can be either a single value, or n values. |
0
|
time_loop
|
bool
|
Whether to loop the time if reaching the end. (starts back at 0) |
True
|
horizon
|
int
|
The amount of steps to run the simulation for before killing the remaining simulations. |
1000
|
initialization_values
|
dict
|
In the case the agent is to be initialized with custom values, the paramaters to be passed on the initialize_state function can be set here. |
{}
|
reward_discount
|
float
|
How much a given reward is discounted based on how long it took to get it. It is purely used to compute the Average Discount Reward (ADR) after the simulation. |
0.99
|
print_progress
|
bool
|
Whether to show a progress bar of what step the simulations are at. |
True
|
print_stats
|
bool
|
Whether to print the stats at the end of the run. |
True
|
print_warning
|
bool
|
Whether to print warnings when they occur or not. |
True
|
use_gpu
|
bool
|
Whether to run the simulations on the GPU or not. |
False
|
batches
|
int
|
In how many batches the simulations should be run. This is useful in the case there are too many simulations and the memory can fill up. The value of batches=-1 will make it that different batches amount are tried in increasing order if a MemoryError is encountered. |
-1
|
Returns:
Name | Type | Description |
---|---|---|
hist |
SimulationHistory
|
A SimulationHistory object that tracked all the positions, actions and observations. |
Source code in olfactory_navigation/simulation.py
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 |
|