coopihc.base.State.State

class State(**kwargs)[source]

Bases: dict

The container class for States. State subclasses dictionnary and adds a few methods:

  • reset(dic = reset_dic), which passes reset values to the StateElements it holds and triggers their reset method

  • filter(mode=mode, filterdict=filterdict), which filters out the state to extract some information.

  • serialize(), which transforms the state into a format that can be serializable, e.g. to send as a JSON format.

Initializing a State is straightforward:

state = State()
substate = State()
substate["x1"] = discrete_array_element(init=1, low=1, high=3)
substate["x3"] = array_element(
    init=1.5 * numpy.ones((2, 2)), low=numpy.ones((2, 2)), high=2 * numpy.ones((2, 2))
)

substate2 = State()
substate2["y1"] = discrete_array_element(init=1, low=1, high=3)

state["sub1"] = substate
state["sub2"] = substate2

Methods

clear

copy

equals

equality checks that also checks for spaces (hard).

filter

Extract some part of the state information

fromkeys

Create a new dictionary with keys from iterable and values set to value.

get

Return the value for key if key is in the dictionary, else default.

items

keys

pop

If key is not found, d is returned if given, otherwise KeyError is raised

popitem

Remove and return a (key, value) pair as a 2-tuple.

reset

Initialize the state.

serialize

Makes the state serializable.

setdefault

Insert key with a value of default if key is not in the dictionary.

update

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values

clear() None.  Remove all items from D.
copy() a shallow copy of D
equals(other, mode='hard')[source]

equality checks that also checks for spaces (hard).

_example_state = example_game_state()
obs = {
    "game_info": {"turn_index": numpy.array(0), "round_index": numpy.array(0)},
    "task_state": {"position": numpy.array(2), "targets": numpy.array([0, 1])},
    "user_action": {"action": numpy.array(0)},
    "assistant_action": {"action": numpy.array(2)},
}
del _example_state["user_state"]
del _example_state["assistant_state"]
assert not _example_state.equals(obs, mode="hard")
filter(mode='array', filterdict=None)[source]

Extract some part of the state information

An example for filterdict’s structure is as follows:

filterdict = dict(
    {
        "sub1": dict({"x1": 0, "x3": slice(0, 1)}),
        "sub2": dict({"y1": 0}),
    }
)

This will filter out

  • the first component (index 0) for subsubstate x1 in substate sub1,

  • the first and second components for subsubstate x3 in substate sub1,

  • the first component for subsubstate y1 in substate sub2.

Example usage:

# Filter out spaces
f_state = state.filter(mode="space", filterdict=filterdict)

# Filter out as arrays
f_state = state.filter(mode="array", filterdict=filterdict)

# Filter out as StateElement
f_state = state.filter(mode="stateelement", filterdict=filterdict)

# Get spaces
f_state = state.filter(mode="space")

# Get arrays
f_state = state.filter(mode="array")

# Get Gym Compatible arrays
f_state = state.filter(mode="array-Gym")
Parameters
  • mode (str, optional) – “array” or “spaces” or “stateelement”, defaults to “array”. If “stateelement”, returns a dictionnary with the selected stateelements. If “spaces”, returns the same dictionnary, but with only the spaces (no array information). If “array”, returns the same dictionnary, but with only the value arrays (no space information).

  • filterdict (dictionnary, optional) – the dictionnary that indicates which components to filter out, defaults to None

Returns

The dictionnary with the filtered state

Return type

dictionnary

fromkeys(value=None, /)

Create a new dictionary with keys from iterable and values set to value.

get(key, default=None, /)

Return the value for key if key is in the dictionary, else default.

items() a set-like object providing a view on D's items
keys() a set-like object providing a view on D's keys
pop(k[, d]) v, remove specified key and return the corresponding value.

If key is not found, d is returned if given, otherwise KeyError is raised

popitem()

Remove and return a (key, value) pair as a 2-tuple.

Pairs are returned in LIFO (last-in, first-out) order. Raises KeyError if the dict is empty.

reset(dic={})[source]

Initialize the state. See StateElement

Example usage:

# Normal reset
state.reset()


# Forced reset
reset_dic = {
    "sub1": {"x1": 3},
    "sub2": {"y1": 3},
}
state.reset(dic=reset_dic)
serialize()[source]

Makes the state serializable.

state.serialize()
Returns

serializable dictionnary

Return type

dict

setdefault(key, default=None, /)

Insert key with a value of default if key is not in the dictionary.

Return the value for key if key is in the dictionary, else default.

update([E, ]**F) None.  Update D from dict/iterable E and F.

If E is present and has a .keys() method, then does: for k in E: D[k] = E[k] If E is present and lacks a .keys() method, then does: for k, v in E: D[k] = v In either case, this is followed by: for k in F: D[k] = F[k]

values() an object providing a view on D's values