State

States are the higher level containers used in CoopIHC, that can contain either StateElements or other States. They derive from Python’s built-in dictionnary, and most of the dictionnary syntax and methods apply.

  • Defining a State is straightforward

     1
     2state = State()
     3
     4substate = State()
     5substate["x1"] = discrete_array_element(init=1, low=1, high=3)
     6substate["x3"] = array_element(
     7    init=1.5 * numpy.ones((2, 2)), low=numpy.ones((2, 2)), high=2 * numpy.ones((2, 2))
     8)
     9
    10substate2 = State()
    11substate2["y1"] = discrete_array_element(init=1, low=1, high=3)
    12
    13state["sub1"] = substate
    14state["sub2"] = substate2
    15
    
  • States can be initialized to a random values (or forced, like StateElements). To use the forced reset mechanism you have to provide a reset dictionnary, whose structure is identical to the state, see below. Substates which are not in the dictionnary are reset by random sampling.

    1reset_dic = {
    2    "sub1": {"x1": 3},
    3    "sub2": {"y1": 3},
    4}
    5state.reset(dic=reset_dic)
    6assert state["sub1"]["x1"] == 3
    7assert state["sub2"]["y1"] == 3
    
  • States can also be filtered by providing a dictionnary of items that you would like to retain, where each value is an index or slice that indicates which component of the StateElement you would like to filter out.

 1filterdict = dict(
 2    {
 3        "sub1": dict({"x1": ..., "x3": slice(0, 1)}),
 4        "sub2": dict({"y1": ...}),
 5    }
 6)
 7
 8f_state = state.filter(mode="array", filterdict=filterdict)
 9f_state = state.filter(mode="stateelement", filterdict=filterdict)
10f_state = state.filter(mode="space")
11f_state = state.filter(mode="array")
12
  • States can also be serialized to a a dictionnary

1state.serialize()
  • States can also be accessed with the dot notation:

state["sub1"] == state.sub1