mdp
Model
MDP Model class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
states
|
int or list[str] or list[list[str]]
|
A list of state labels or an amount of states to be used. Also allows to provide a matrix of states to define a grid model. |
required |
actions
|
int or list
|
A list of action labels or an amount of actions to be used. |
required |
transitions
|
array - like or function
|
The transitions between states, an array can be provided and has to be |S| x |A| x |S| or a function can be provided. If a function is provided, it has be able to deal with np.array arguments. If none is provided, it will be randomly generated. |
None
|
reachable_states
|
array - like
|
A list of states that can be reached from each state and actions. It must be a matrix of size |S| x |A| x |R| where |R| is the max amount of states reachable from any given state and action pair. It is optional but useful for speedup purposes. |
None
|
rewards
|
array - like or function
|
The reward matrix, has to be |S| x |A| x |S|. A function can also be provided here but it has to be able to deal with np.array arguments. If provided, it will be use in combination with the transition matrix to fill to expected rewards. |
None
|
rewards_are_probabilistic
|
bool
|
Whether the rewards provided are probabilistic or pure rewards. If probabilist 0 or 1 will be the reward with a certain probability. |
False
|
state_grid
|
array - like
|
If provided, the model will be converted to a grid model. |
None
|
start_probabilities
|
list
|
The distribution of chances to start in each state. If not provided, there will be an uniform chance for each state. |
None
|
end_states
|
list
|
Entering either state in the list during a simulation will end the simulation. |
[]
|
end_actions
|
list
|
Playing action of the list during a simulation will end the simulation. |
[]
|
print_debug
|
bool
|
Whether to print debug logs about the creation progress of the MDP Model. |
False
|
seed
|
int
|
For reproducible randomness. |
12131415
|
Attributes:
Name | Type | Description |
---|---|---|
states |
ndarray
|
A 1D array of states indices. Used to loop over states. |
state_labels |
list[str]
|
A list of state labels. (To be mainly used for plotting) |
state_count |
int
|
How many states are in the Model. |
state_grid |
ndarray
|
The state indices organized as a 2D grid. (Used for plotting purposes) |
actions |
ndarry
|
A 1D array of action indices. Used to loop over actions. |
action_labels |
list[str]
|
A list of action labels. (To be mainly used for plotting) |
action_count |
int
|
How many action are in the Model. |
transition_table |
ndarray
|
A 3D matrix of the transition probabilities. Can be None in the case a transition function is provided instead. Note: When possible, use reachable states and reachable probabilities instead. |
transition_function |
function
|
A callable function taking 3 arguments: s, a, s_p; and returning a float between 0.0 and 1.0. Can be None in the case a transition table is provided instead. Note: When possible, use reachable states and reachable probabilities instead. |
reachable_states |
ndarray
|
A 3D array of the shape S x A x R, where R is max amount to states that can be reached from any state-action pair. |
reachable_probabilities |
ndarray
|
A 3D array of the same shape as reachable_states, the array represent the probability of reaching the state pointed by the reachable_states matrix. |
reachable_state_count |
int
|
The maximum of states that can be reached from any state-action combination. |
immediate_reward_table |
ndarray
|
A 3D matrix of shape S x A x S of the reward that will received when taking action a, in state s and landing in state s_p. Can be None in the case an immediate rewards function is provided instead. |
immediate_reward_function |
function
|
A callable function taking 3 argments: s, a, s_p and returning the immediate reward the agent will receive. Can be None in the case an immediate rewards function is provided instead. |
expected_reward_table |
ndarray
|
A 2D array of shape S x A. It represents the rewards that is expected to be received when taking action a from state s. It is made by taking the weighted average of immediate rewards and the transitions. |
start_probabilities |
ndarray
|
A 1D array of length |S| containing the probility distribution of the agent starting in each state. |
rewards_are_probabilisitic |
bool
|
Whether the immediate rewards are probabilitic, ie: returning a 0 or 1 based on the reward that is considered to be a probability. |
end_states |
list[int]
|
A list of states that, when reached, terminate a simulation. |
end_actions |
list[int]
|
A list of actions that, when taken, terminate a simulation. |
is_on_gpu |
bool
|
Whether the numpy array of the model are stored on the gpu or not. |
gpu_model |
Model
|
An equivalent model with the np.ndarray objects on GPU. (If already on GPU, returns self) |
cpu_model |
Model
|
An equivalent model with the np.ndarray objects on CPU. (If already on CPU, returns self) |
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/agents/model_based_util/mdp.py
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 |
|
cpu_model
property
The same model but on the CPU instead of the GPU. If already on the CPU, the current model object is returned.
gpu_model
property
The same model but on the GPU instead of the CPU. If already on the GPU, the current model object is returned.
get_coords(items)
Function to get the coordinate (on the state_grid) for the provided state index or indices.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
items
|
int or list[int]
|
The states ids or id get convert to a 2D coordinate. |
required |
Returns:
Name | Type | Description |
---|---|---|
item_coords |
list[int] or list[list[int]]
|
The 2D positions of the provided item ids. |
Source code in olfactory_navigation/agents/model_based_util/mdp.py
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 |
|
load_from_file(file)
classmethod
Function to load a MDP model from a pickle file. The json structure must contain the same items as in the constructor of this class.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file
|
str
|
The file and path of the model to be loaded. |
required |
Returns:
Name | Type | Description |
---|---|---|
loaded_model |
Model
|
An instance of the loaded model. |
Source code in olfactory_navigation/agents/model_based_util/mdp.py
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 |
|
reward(s, a, s_p)
Returns the rewards of playing action a when in state s and landing in state s_p. If the rewards are probabilistic, it will return 0 or 1.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
int
|
The current state |
required |
a
|
int
|
The action taking in state s |
required |
s_p
|
int
|
The state landing in after taking action a in state s |
required |
Returns:
Name | Type | Description |
---|---|---|
reward |
int or float
|
The reward received. |
Source code in olfactory_navigation/agents/model_based_util/mdp.py
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 |
|
save(file_name, path='./Models')
Function to save the current model in a pickle file. By default, the model will be saved in 'Models' directory in the current working directory but this can be changed using the 'path' parameter.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
file_name
|
str
|
The name of the json file the model will be saved in. |
required |
path
|
str
|
The path at which the model will be saved. |
'./Models'
|
Source code in olfactory_navigation/agents/model_based_util/mdp.py
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 |
|
transition(s, a)
Returns a random posterior state knowing we take action a in state t and weighted on the transition probabilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
s
|
int
|
The current state |
required |
a
|
int
|
The action to take |
required |
Returns:
Name | Type | Description |
---|---|---|
s_p |
int
|
The posterior state |
Source code in olfactory_navigation/agents/model_based_util/mdp.py
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 |
|
log(content)
Function to print a log line with a timestamp.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
content
|
str
|
The content to be printed as a log. |
required |
Source code in olfactory_navigation/agents/model_based_util/mdp.py
30 31 32 33 34 35 36 37 38 39 |
|