Skip to content

active

Interface(uid, value, value_min, value_max, shape, dtype) dataclass

Interface to a feature in an active environment.

Represents a single feature of the environment, which can be either an input, an output, or the reward of the environment.

Parameters:

Name Type Description Default
uid str

Identifier of the feature inside the environment

required
value int | float | NDArray[Any] | None

The value of the feature

required
value_min SupportsFloat | NDArray[Any] | list[Any]

Simple representation of the minimum value

required
value_max SupportsFloat | NDArray[Any] | list[Any]

Simple representation of the maximum value

required
shape Sequence[int]

Tuple representing the shape of the value

required
dtype type[floating[Any]] | type[integer[Any]]

Data type of this interface, e.g., numpy.float32

required

Observation(sensors, rewards) dataclass

An observation of an active environment.

The observation contains 'sensors', which are the raw observations of featured values, and rewards, which are a rated quantification of the environment state.

Parameters:

Name Type Description Default
sensors list[Interface]

List of interface objects, i.e., raw observations

required
rewards list[Interface]

List of interface objects, i.e., rated state

required

Action(actuators) dataclass

An action in an active environment.

The action contains 'actuators', which represent setpoints in the environment. Each actuator targets exactly one input feature.

Parameters:

Name Type Description Default
actuators list[Interface]

List of interface objects, which are setpoints

required

StopLearning

Bases: Exception

Stop learning.

This exception is raised when the learning process should stop.

learn_active(environment, learner)

Learn from an active environment.

Learn from an active environment by interacting with it and learning from the observations. The learning process stops when the environment ends or when the learner requests to stop.

Parameters:

Name Type Description Default
environment ActiveEnvironment

The active environment.

required
learner ActiveLearner

The active learner.

required

Returns:

Type Description
Model

The model learned from the environment.

Source code in src/flowcean/core/strategies/active.py
 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
def learn_active(
    environment: ActiveEnvironment,
    learner: ActiveLearner,
) -> Model:
    """Learn from an active environment.

    Learn from an active environment by interacting with it and
    learning from the observations. The learning process stops when the
    environment ends or when the learner requests to stop.

    Args:
        environment: The active environment.
        learner: The active learner.

    Returns:
        The model learned from the environment.
    """
    model = None

    try:
        while True:
            observations = environment.observe()
            action = learner.propose_action(observations)
            environment.act(action)
            environment.step()
            observations = environment.observe()
            model = learner.learn_active(action, observations)
    except StopLearning:
        pass
    if model is None:
        message = "No model was learned."
        raise RuntimeError(message)
    return model