smartgrid.observation.base_observation.BaseObservation¶
- class smartgrid.observation.base_observation.BaseObservation[source]¶
Bases:
object
Base class for defining any kind of Observation (global, local, “total”).
Observations are information that agents receive about the environment. They describe the current state of the environment through various metrics, and are used to take decisions (actions).
This class, although being a dataclass, does not define any attribute. Instead, it defines several helper methods, to avoid duplicating them in the various Observations classes:
fields()
, which lists the fields that an Observation class possesses.asdict()
, which returns a dictionary representation of an Observation.space()
, which returns theSpace
in which Observations live (i.e., which values they can take).a magic method that allows to easily create NumPy arrays by using the standard
numpy.asarray()
method, such as:np.asarray(obs)
.
Classes that extend this BaseObservation can thus be used either:
in a user-friendly manner, as a dataclass, by accessing the fields by their names, getting the fields names;
or in a programmatic manner, by converting them to NumPy arrays, which are commonly used in learning algorithms, as inputs of neural networks (tensors).
Methods
__init__
()asdict
()Return the Observation as a dictionary.
fields
()Returns the names of fields that compose an Observation.
space
(world, agent)Describe the space in which Observations take their values.
- asdict() Dict[str, Any] [source]¶
Return the Observation as a dictionary.
Fields can be excluded by setting the metadata
include
custom property toFalse
, such as:my_field: Any = field(metadata={'include': False})
.- Parameters:
self – An instance of observation.
- Returns:
The observation represented as a dictionary, with the fields’ names as keys and the fields’ values as values, in the order of definition.
- classmethod fields() Tuple[str] [source]¶
Returns the names of fields that compose an Observation.
Fields can be excluded by setting the metadata
include
custom property toFalse
, such as:my_field: Any = field(metadata={'include': False})
.- Parameters:
cls – Either the class itself, or an instance of the class; this method supports both. In other words, it can be used as
Observation.fields()
, orobs = Observation(...); obs.fields()
.- Returns:
The fields’ names as a tuple, in their order of definition.
- classmethod space(world: World, agent: Agent) Space [source]¶
Describe the space in which Observations take their values.
This method is useful if an algorithm has assumptions or requirements on the observation space. For example, values can be interpolated, by knowing their original domain.
We currently use ratios in
[0, 1]
for each metric of observations. This makes it easier for learning algorithms (avoids perceiving a given dimension as more important than another because of an extended range). It also means that theworld
andagent
parameters do not influe on the space (they could beNone
).In the future, we could use the true ranges from the agent’s
AgentProfile
and let users convert these observations to[0, 1]
when necessary. This would provide more useful information, e.g., the actual battery storage in[0, capacity]
, rather than a ratio, or the actual hour in[0, 23]
rather than a value(h % 24) / 24
, which is hard to interpret for human users.- Parameters:
- Return type:
- Returns:
A gym Box, whose
low
field indicates the minimum value of each element of the observation vector. Similarly, thehigh
field indicates the maximum value of each element, such that each element i of the vector is contained betweenlow[i]
andhigh[i]
. The Box’s shape is the number of fields.