mdp
This module contains the Markov Decision Process, value iteration, Q learning and policy gradient.
GridMDP
Bases: MDP
A Markov decision process on a grid.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
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 | |
__init__(grid, initial_state, terminal_states, transition_probabilities_per_action, restrict_actions_to_available_states=False)
A Markov decision process on a grid.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
List[List[Union[float, None]]]
|
List of lists, containing the rewards of the grid states or None. |
required |
initial_state
|
GridState
|
Initial state in the grid. |
required |
terminal_states
|
Set[GridState]
|
Set of terminal states in the grid. |
required |
transition_probabilities_per_action
|
Dict[GridState, List[Tuple[float, GridState]]]
|
Dictionary of transition probabilities per action, mapping from action to list of tuples (probability, next state). |
required |
restrict_actions_to_available_states
|
Optional[bool]
|
Whether to restrict actions to those that result in valid next states. |
False
|
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
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 | |
MDP
A Markov decision process.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
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 | |
__init__(states, actions, initial_state, terminal_states, transition_probabilities, reward)
A Markov decision process.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
Set[Any]
|
Set of states. |
required |
actions
|
Set[Any]
|
Set of actions. |
required |
initial_state
|
Any
|
Initial state. |
required |
terminal_states
|
Set[Any]
|
Set of terminal states. |
required |
transition_probabilities
|
Dict[Tuple[Any, Any], List[Tuple[float, Any]]]
|
Dictionary of transition probabilities, mapping from tuple (state, action) to list of tuples (probability, next state). |
required |
reward
|
Dict[Any, float]
|
Dictionary of rewards per state, mapping from state to reward. |
required |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
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 | |
execute_action(state, action)
Executes the action in the current state and returns the new state, obtained reward and terminal flag.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
160 161 162 163 164 165 | |
get_actions(state)
Get the set of actions available in a certain state, returns [None] for terminal states.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
125 126 127 128 129 | |
get_reward(state)
Get the reward for a specific state.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
131 132 133 | |
get_states()
Get the set of states.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
121 122 123 | |
get_transitions_with_probabilities(state, action)
Get the list of transitions with their probability, returns [(0.0, state)] for terminal states.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
139 140 141 142 143 144 145 | |
is_terminal(state)
Return whether a state is a terminal state.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
135 136 137 | |
sample_next_state(state, action)
Randomly sample the next state given the current state and taken action.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
147 148 149 150 151 152 153 154 155 156 157 158 | |
PolicyGradientBuffer
dataclass
Buffer for the policy gradient method.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 | |
mean_episode_length()
Mean episode length.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
526 527 528 | |
mean_episode_return()
Mean episode return.
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
522 523 524 | |
best_action_from_q_table(*, state, available_actions, q_table)
Derive the best action from a Q table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Any
|
The state in which to take an action. |
required |
available_actions
|
Set[Any]
|
Set of available actions. |
required |
q_table
|
QTable
|
The Q table, mapping from state-action pair to value estimate. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
The best action according to the Q table. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 | |
derive_deterministic_policy(mdp, policy)
Compute the best policy for an MDP given the stochastic policy.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mdp
|
MDP
|
The underlying MDP. |
required |
policy
|
CategoricalPolicy
|
The stochastic policy. |
required |
Returns:
| Type | Description |
|---|---|
Dict[Any, Any]
|
Deterministic policy, i.e. mapping from state to action. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 | |
derive_policy(mdp, utility_of_states)
Compute the best policy for an MDP given the utility of the states.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mdp
|
MDP
|
The underlying MDP. |
required |
utility_of_states
|
StateValueTable
|
The dictionary containing the utility (estimate) of all states. |
required |
Returns:
| Type | Description |
|---|---|
Dict[Any, Any]
|
Policy, i.e. mapping from state to action. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
expected_utility_of_action(mdp, state, action, utility_of_states)
Compute the expected utility of taking an action in a state.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mdp
|
MDP
|
The underlying MDP. |
required |
state
|
Any
|
The start state. |
required |
action
|
Any
|
The action to be taken. |
required |
utility_of_states
|
StateValueTable
|
The dictionary containing the utility (estimate) of all states. |
required |
Returns:
| Type | Description |
|---|---|
float
|
Expected utility |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 | |
greedy_value_estimate_for_state(*, q_table, state)
Compute the greedy (best possible) value estimate for a state from the Q table.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Any
|
The state for which to estimate the value, when being greedy. |
required |
q_table
|
QTable
|
The Q table, mapping from state-action pair to value estimate. |
required |
Returns:
| Type | Description |
|---|---|
float
|
The value based on the greedy estimate. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
412 413 414 415 416 417 418 419 420 421 422 423 424 425 | |
policy_gradient(*, mdp, policy, lr=0.01, iterations=50, batch_size=5000, seed=None, return_history=False, use_random_init_state=False, verbose=True)
Train a paramterized policy using vanilla policy gradient.
Adapted from: https://github.com/openai/spinningup/blob/master/spinup/examples/pytorch/pg_math/1_simple_pg.py
The MIT License (MIT)
Copyright (c) 2018 OpenAI (http://openai.com)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mdp
|
MDP
|
The underlying MDP. |
required |
policy
|
CategoricalPolicy
|
The stochastic policy to be trained. |
required |
lr
|
float
|
Learning rate. |
0.01
|
iterations
|
int
|
Number of iterations. |
50
|
batch_size
|
int
|
Number of samples generated for each policy update. |
5000
|
seed
|
Optional[int]
|
Random seed for reproducibility (default: None). |
None
|
return_history
|
bool
|
Whether to return the whole history of value estimates instead of just the final estimate. |
False
|
use_random_init_state
|
bool
|
bool, if the agent should be initialized randomly. |
False
|
verbose
|
bool
|
bool, if traing progress should be printed. |
True
|
Returns:
| Type | Description |
|---|---|
Union[List[CategoricalPolicy], CategoricalPolicy]
|
The final policy, if return_history is false. The |
Union[List[CategoricalPolicy], CategoricalPolicy]
|
history of policies as list, if return_history is true. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
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 | |
q_learning(*, mdp, alpha, epsilon, iterations, seed=None, return_history=False)
Derive a value estimate for state-action pairs by means of Q learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mdp
|
MDP
|
The underlying MDP. |
required |
alpha
|
float
|
Learning rate. |
required |
epsilon
|
float
|
Exploration-exploitation threshold. A random action is taken with probability epsilon, the best action otherwise. |
required |
iterations
|
int
|
Number of iterations. |
required |
seed
|
Optional[int]
|
Random seed for reproducibility (default: None). |
None
|
return_history
|
Optional[bool]
|
Whether to return the whole history of value estimates instead of just the final estimate. |
False
|
Returns:
| Type | Description |
|---|---|
Union[QTable, List[QTable]]
|
The final value estimate, if return_history is false. The |
Union[QTable, List[QTable]]
|
history of value estimates as list, if return_history is true. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
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 | |
random_action(available_actions)
Derive a random action from the set of available actions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
available_actions
|
Set[Any]
|
Set of available actions. |
required |
Returns:
| Type | Description |
|---|---|
Any
|
A random action. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
397 398 399 400 401 402 403 404 405 406 407 408 409 | |
value_iteration(mdp, epsilon, max_iterations, return_history=False)
Derive a utility estimate by means of value iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mdp
|
MDP
|
The underlying MDP. |
required |
epsilon
|
float
|
Termination criterion: if maximum difference in utility update is below epsilon, the iteration is terminated. |
required |
max_iterations
|
int
|
Maximum number of iterations, if exceeded, RuntimeError is raised. |
required |
return_history
|
Optional[bool]
|
Whether to return the whole history of utilities instead of just the final estimate. |
False
|
Returns:
| Type | Description |
|---|---|
Union[StateValueTable, List[StateValueTable]]
|
The final utility estimate, if return_history is false. The |
Union[StateValueTable, List[StateValueTable]]
|
history of utility estimates as list, if return_history is true. |
Source code in src/behavior_generation_lecture_python/mdp/mdp.py
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 | |