ForecastEnv

ForecastEnv: rolling-origin forecasting as a real gymnasium environment.

The action is the forecast: a DataFrame indexed by the current origin’s target times, with a "point" column or quantile-level float columns. The observation is a Observation — a TimeView frozen at the origin’s asof plus the target index to forecast. The model never touches the dataset; the observation is its whole world.

Rewards are delayed to settlement, as in reality: submitting a day-ahead forecast tells you nothing — the score arrives once the actuals do. At each step the env settles every previously submitted origin whose targets have become knowable (per the target field’s availability rule) and pays their scores as the reward (negated when the objective is lower-is-better, so more reward is always better). info["settled"] carries the full SettlementRecord s for analyzers.

class emflow.envs.forecast.Observation(feed: DataFeed, origin: Origin)[source]

Bases: TimeView

What a model sees at one forecast origin: point-in-time data access (via TimeView) plus the target index it must forecast. column names the target-field column this origin is scoped to (multi-zone problems); None means the env’s default.

class emflow.envs.forecast.SettlementRecord(origin: Origin, prediction: DataFrame, actuals: Series, score: float)[source]

Bases: object

One origin scored against its (now available) actuals.

origin: Origin
prediction: DataFrame
actuals: Series
score: float
class emflow.envs.forecast.ForecastEnv(feed: DataFeed, origins: Sequence[Origin], target_field: str, objective: Objective, *, target_column=None, train_end=None, quantiles: Sequence[float] | None = None)[source]

Bases: Env

Rolling-origin forecast evaluation over a fixed list of origins.

Parameters

feed:

The point-in-time data gate.

origins:

The origins to step through (from problem.origins(split)).

target_field:

Name of the actual field being forecast.

objective:

Scores each settled origin (prediction vs actuals).

target_column:

Column of the target field to score (default: its only column).

train_end:

Clock time of the training view served in reset()’s info["train"].

quantiles:

If set, actions must provide exactly these quantile columns; if None, actions must provide a "point" column.

metadata: dict[str, Any] = {'render_modes': []}
reset(*, seed=None, options=None)[source]

Resets the environment to an initial internal state, returning an initial observation and info.

This method generates a new starting state often with some randomness to ensure that the agent explores the state space and learns a generalised policy about the environment. This randomness can be controlled with the seed parameter otherwise if the environment already has a random number generator and reset() is called with seed=None, the RNG is not reset.

Therefore, reset() should (in the typical use case) be called with a seed right after initialization and then never again.

For Custom environments, the first line of reset() should be super().reset(seed=seed) which implements the seeding correctly.

Changed in version v0.25: The return_info parameter was removed and now info is expected to be returned.

Args:
seed (optional int): The seed that is used to initialize the environment’s PRNG (np_random) and

the read-only attribute np_random_seed. If the environment does not already have a PRNG and seed=None (the default option) is passed, a seed will be chosen from some source of entropy (e.g. timestamp or /dev/urandom). However, if the environment already has a PRNG and seed=None is passed, the PRNG will not be reset and the env’s np_random_seed will not be altered. If you pass an integer, the PRNG will be reset even if it already exists. Usually, you want to pass an integer right after the environment has been initialized and then never again. Please refer to the minimal example above to see this paradigm in action.

options (optional dict): Additional information to specify how the environment is reset (optional,

depending on the specific environment)

Returns:
observation (ObsType): Observation of the initial state. This will be an element of observation_space

(typically a numpy array) and is analogous to the observation returned by step().

info (dictionary): This dictionary contains auxiliary information complementing observation. It should be analogous to

the info returned by step().

step(action)[source]

Run one timestep of the environment’s dynamics using the agent actions.

When the end of an episode is reached (terminated or truncated), it is necessary to call reset() to reset this environment’s state for the next episode.

Changed in version 0.26: The Step API was changed removing done in favor of terminated and truncated to make it clearer to users when the environment had terminated or truncated which is critical for reinforcement learning bootstrapping algorithms.

Args:

action (ActType): an action provided by the agent to update the environment state.

Returns:
observation (ObsType): An element of the environment’s observation_space as the next observation due to the agent actions.

An example is a numpy array containing the positions and velocities of the pole in CartPole.

reward (SupportsFloat): The reward as a result of taking the action. terminated (bool): Whether the agent reaches the terminal state (as defined under the MDP of the task)

which can be positive or negative. An example is reaching the goal state or moving into the lava from the Sutton and Barto Gridworld. If true, the user needs to call reset().

truncated (bool): Whether the truncation condition outside the scope of the MDP is satisfied.

Typically, this is a timelimit, but could also be used to indicate an agent physically going out of bounds. Can be used to end the episode prematurely before a terminal state is reached. If true, the user needs to call reset().

info (dict): Contains auxiliary diagnostic information (helpful for debugging, learning, and logging).

This might, for instance, contain: metrics that describe the agent’s performance state, variables that are hidden from observations, or individual reward terms that are combined to produce the total reward. In OpenAI Gym <v26, it contains “TimeLimit.truncated” to distinguish truncation and termination, however this is deprecated in favour of returning terminated and truncated variables.

done (bool): (Deprecated) A boolean value for if the episode has ended, in which case further step() calls will

return undefined results. This was removed in OpenAI Gym v26 in favor of terminated and truncated attributes. A done signal may be emitted for different reasons: Maybe the task underlying the environment was solved successfully, a certain timelimit was exceeded, or the physics simulation has entered an invalid state.