cadCAD/documentation/historical_state_access.md

4.9 KiB

Historical State Access

The 3rd parameter of state and policy update functions (labels as sH of type List[List[dict]]) provides access to past Partial State Updates (PSU) given a negative offset number. access_block is used to access past PSUs (List[dict]) from sH.

Example: -2 denotes to second to last PSU

Exclusion List

Create a list of states to exclude from the reported PSU.

exclusion_list = [
    'nonexsistant', 'last_x', '2nd_to_last_x', '3rd_to_last_x', '4th_to_last_x'
]
Example Policy Updates
Last partial state update
from cadCAD.configuration.utils import config_sim, access_block

def last_update(_g, substep, sH, s):
    return {"last_x": access_block(
            state_history=sH,
            target_field="last_x", # Add a field to the exclusion list
            psu_block_offset=-1,
            exculsion_list=exclusion_list
        )
    }
  • Note: Although target_field adding a field to the exclusion may seem redundant, it is useful in the case of the exclusion list being empty while the target_field is assigned to a state or a policy key.
2nd to last partial state update
def second2last_update(_g, substep, sH, s):
    return {"2nd_to_last_x": access_block(sH, "2nd_to_last_x", -2, exclusion_list)}
Define State Updates
3rd to last partial state update
def third_to_last_x(_g, substep, sH, s, _input):
    return '3rd_to_last_x', access_block(sH, "3rd_to_last_x", -3, exclusion_list)
4rd to last partial state update
def fourth_to_last_x(_g, substep, sH, s, _input):
    return '4th_to_last_x', access_block(sH, "4th_to_last_x", -4, exclusion_list)
Non-exsistant partial state update
  • psu_block_offset >= 0 doesn't exsist
def nonexsistant(_g, substep, sH, s, _input):
    return 'nonexsistant', access_block(sH, "nonexsistant", 0, exclusion_list)

Example Simulation

link

Example Output

State History
+----+-------+-----------+------------+-----+
|    |   run |   substep |   timestep |   x |
|----+-------+-----------+------------+-----|
|  0 |     1 |         0 |          0 |   0 |
|  1 |     1 |         1 |          1 |   1 |
|  2 |     1 |         2 |          1 |   2 |
|  3 |     1 |         3 |          1 |   3 |
|  4 |     1 |         1 |          2 |   4 |
|  5 |     1 |         2 |          2 |   5 |
|  6 |     1 |         3 |          2 |   6 |
|  7 |     1 |         1 |          3 |   7 |
|  8 |     1 |         2 |          3 |   8 |
|  9 |     1 |         3 |          3 |   9 |
+----+-------+-----------+------------+-----+
Accessed State History:

Example: last_x

+----+-----------------------------------------------------------------------------------------------------------------------------------------------------+
|    | last_x                                                                                                                                              |
|----+-----------------------------------------------------------------------------------------------------------------------------------------------------|
|  0 | []                                                                                                                                                  |
|  1 | [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}]                                                                                                   |
|  2 | [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}]                                                                                                   |
|  3 | [{'x': 0, 'run': 1, 'substep': 0, 'timestep': 0}]                                                                                                   |
|  4 | [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}] |
|  5 | [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}] |
|  6 | [{'x': 1, 'run': 1, 'substep': 1, 'timestep': 1}, {'x': 2, 'run': 1, 'substep': 2, 'timestep': 1}, {'x': 3, 'run': 1, 'substep': 3, 'timestep': 1}] |
|  7 | [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}] |
|  8 | [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}] |
|  9 | [{'x': 4, 'run': 1, 'substep': 1, 'timestep': 2}, {'x': 5, 'run': 1, 'substep': 2, 'timestep': 2}, {'x': 6, 'run': 1, 'substep': 3, 'timestep': 2}] |
+----+-----------------------------------------------------------------------------------------------------------------------------------------------------+