Class MountainCarEnv
- Namespace
- Gym.Environments.Envs.Classic
- Assembly
- Gym.Environments.dll
MountainCar-v0 and MountainCarContinuous-v0 in one class (DR-32): a car in a valley that must build momentum to reach the flag on the right hill. Discrete mode pushes left, not at all or right (actions 0, 1, 2) and pays -1 per step; continuous mode applies a force in [-1, 1] and pays 100 at the flag minus 0.1 times the squared force. Byte for byte what Gymnasium's two classes compute, errors included.
public class MountainCarEnv : Env<NDArray, NDArray>, IEnv, IDisposable, IDynamicAttributes
- Inheritance
-
MountainCarEnv
- Implements
- Inherited Members
- Extension Methods
Remarks
The continuous mode is NumPy-typed in a way that reaches the last bit: its state is float64 after a reset and
float32 after a step, the force is the action's own element or a Python float when clamped, and each operation's
precision follows NEP 50 (a float32 state with a Python float stays float32, so position >= 0.45 is
decided in float32). The state is kept as the scalars Python holds (Gym.Environments.Envs.NpScalar), so every step computes
in the precision Gymnasium's does. The discrete mode computes in float64 throughout, with the action's own integer
type: an unsigned action 0 wraps in action - 1 (a uint8 gives 255) and drives the car right, as in
Python.
It prints as Gymnasium's class of its mode (<Continuous_MountainCarEnv instance> in continuous mode),
and its errors name that class. Accepted deviations (DR-32): the constructor takes continuous, which
Gymnasium's classes do not, and MountainCarContinuous-v0's EnvSpec.kwargs carries
continuous=True. Both modes' attributes exist in both modes (Force in continuous mode,
Power in discrete mode), unused by the other. Gymnasium's low/high (discrete) and
low_state/high_state (continuous) are the observation space's bounds and are read there.
get_keys_to_action exists in discrete mode only, as in Gymnasium (GetKeysToAction()).
Rendering is Gymnasium's, pixel for pixel: the gfxdraw car, wheels and flag, and the hill, drawn with
Aalines(Surface, Color, bool, IReadOnlyList<(double X, double Y)>) (pygame-ce's own algorithm, DR-37). Human mode renders into the window from
viewer_factory or Gymnasium.HumanViewerFactory (DR-20), paced at 30 frames per second.
Not thread-safe. References: gymnasium/envs/classic_control/mountain_car.py, gymnasium/envs/classic_control/continuous_mountain_car.py.
Its info is always {}, as Gymnasium's, so it has no typed info record (DR-95).
Constructors
MountainCarEnv(MountainCarEnvOptions)
Builds the instance from its options record: the properties set, the constructor's defaults for the rest.
public MountainCarEnv(MountainCarEnvOptions options)
Parameters
optionsMountainCarEnvOptionsThe options.
Exceptions
- ArgumentNullException
Thrown when
optionsis null.
MountainCarEnv(RenderMode, double, bool, EnvViewerFactory?)
Builds the env with the render mode given as the RenderMode enum (DR-84, DR-93), as
MountainCarEnv(string?, double, bool, EnvViewerFactory?)
builds it from the mode's string: new MountainCarEnv(RenderMode.RgbArray) builds what
new MountainCarEnv(renderMode: "rgb_array") builds, the member reaching the env as Gymnasium's string
(Name(RenderMode)), which its RenderMode property then holds.
public MountainCarEnv(RenderMode renderMode, double goalVelocity = 0, bool continuous = false, EnvViewerFactory? viewerFactory = null)
Parameters
renderModeRenderModeThe render mode, which the env must list in its
metadata["render_modes"]as for the string; it behaves exactly as its Gymnasium string does in the other constructor.goalVelocitydoubleGymnasium's
goal_velocity: the velocity the car must have at the flag (0 by default).continuousboolGym.NET's
continuous(DR-32): MountainCarContinuous-v0's force-in-[-1, 1] dynamics, flag at 0.45 and reward instead of MountainCar-v0's.viewerFactoryEnvViewerFactoryGym.NET's
viewer_factory(DR-20) for human mode; null usesGymnasium.HumanViewerFactory.
Remarks
The mode is required and comes first, so new MountainCarEnv() keeps meaning the other constructor, without a
mode; leave the mode out there for no rendering. Every other parameter means what it means there.
Exceptions
- ArgumentOutOfRangeException
Thrown for a value RenderMode does not declare (a cast integer such as
(RenderMode)99).
MountainCarEnv(string?, double, bool, EnvViewerFactory?)
Creates the env in either mode.
public MountainCarEnv(string? renderMode = null, double goalVelocity = 0, bool continuous = false, EnvViewerFactory? viewerFactory = null)
Parameters
renderModestringGymnasium's
render_mode: null,"human"or"rgb_array"; not validated, as in Gymnasium (makechecks it against the metadata).goalVelocitydoubleGymnasium's
goal_velocity: the velocity the car must have at the flag (0 by default).continuousboolGym.NET's
continuous(DR-32): MountainCarContinuous-v0's force-in-[-1, 1] dynamics, flag at 0.45 and reward instead of MountainCar-v0's.viewerFactoryEnvViewerFactoryGym.NET's
viewer_factory(DR-20) for human mode; null usesGymnasium.HumanViewerFactory.
Properties
ClassMetadata
Gymnasium's class attribute metadata (both classes have the same one): {"render_modes": ["human",
"rgb_array"], "render_fps": 30}. Every env starts with a copy as its instance metadata.
public static Metadata ClassMetadata { get; }
Property Value
Remarks
A new dict on every read, so editing one env's metadata never reaches another (Python's is one shared dict).
Clock
Gymnasium's clock: paces human rendering, null until the first render.
public Clock? Clock { get; }
Property Value
Continuous
Whether this is MountainCarContinuous's dynamics, fixed at construction.
public bool Continuous { get; }
Property Value
Force
Gymnasium's force (discrete mode), 0.001: the push of actions 0 and 2.
public double Force { get; set; }
Property Value
GoalPosition
Gymnasium's goal_position: the flag, 0.5 in discrete mode and 0.45 in continuous mode.
public double GoalPosition { get; set; }
Property Value
GoalVelocity
Gymnasium's goal_velocity: the car terminates at the flag only at this velocity or faster. Compared as a
Python number, so in continuous mode against a float32 velocity it is rounded to float32 first.
public double GoalVelocity { get; set; }
Property Value
Gravity
Gymnasium's gravity (discrete mode), 0.0025. Continuous mode uses the literal 0.0025 of its source instead.
public double Gravity { get; set; }
Property Value
IsOpen
[PyName("isopen")]
public bool IsOpen { get; set; }
Property Value
MaxAction
Gymnasium's max_action (continuous mode), 1.0: the upper clamp of the force.
public double MaxAction { get; set; }
Property Value
MaxPosition
Gymnasium's max_position, 0.6: the right wall.
public double MaxPosition { get; set; }
Property Value
MaxSpeed
Gymnasium's max_speed, 0.07: the velocity clamp.
public double MaxSpeed { get; set; }
Property Value
MinAction
Gymnasium's min_action (continuous mode), -1.0: the lower clamp of the force, read every step.
public double MinAction { get; set; }
Property Value
MinPosition
Gymnasium's min_position, -1.2: the left wall. Read every step and render.
public double MinPosition { get; set; }
Property Value
Power
Gymnasium's power (continuous mode), 0.0015: the force's scale.
public double Power { get; set; }
Property Value
PythonClassName
The Python class this instance stands for: Continuous_MountainCarEnv in continuous mode (DR-32), else MountainCarEnv.
protected override string PythonClassName { get; }
Property Value
Screen
Gymnasium's screen: the surface every frame is blitted onto, null until the first render.
Close() keeps it, as Python's close does.
public Surface? Screen { get; }
Property Value
ScreenHeight
Gymnasium's screen_height, 400 pixels; read when a frame is rendered.
public int ScreenHeight { get; set; }
Property Value
ScreenWidth
Gymnasium's screen_width, 600 pixels; read when a frame is rendered.
public int ScreenWidth { get; set; }
Property Value
State
Gymnasium's state, [position, velocity]: a new array of the values on every read, or
null before the first reset (and after null was assigned). Assigning injects a
state, as env.state = np.array([...]) does: float64 values, of any length, checked by the next step or
render with Python's errors.
public double[]? State { get; set; }
Property Value
- double[]
Remarks
Python's attribute changes container between calls (a float64 array after a reset, a tuple after a discrete
step, a float32 array after a continuous step); the values read here are exactly its values, a float32 one
widened. Assigning null stands for Python's None: the next step or render raises the
TypeError Python raises for it.
Surf
public Surface? Surf { get; }
Property Value
Methods
Close()
Once something was rendered, shuts the human-mode window down and marks the env closed (IsOpen).
Like Gymnasium's, it keeps Screen, so a later rgb_array render reuses it.
public override void Close()
Remarks
Reference: gymnasium/envs/classic_control/mountain_car.py.
ConvertAction(object?)
The untyped step's action conversion, with the discrete env's own assert first, so its message shows exactly the
value given (3 (<class 'int'>) invalid). Continuous mode converts without a check, as Gymnasium's
continuous env asserts nothing.
protected override NDArray ConvertAction(object? action)
Parameters
actionobjectThe action as given to the untyped step.
Returns
- NDArray
The action as an array: the one given, or a new one the step releases.
Exceptions
- AssertionError
Thrown in discrete mode for anything
Discrete(3).containsrejects.- TypeError
Thrown in continuous mode for a value that has no array form.
GetKeysToAction()
The discrete mode's keyboard mapping for Play (Gymnasium's
get_keys_to_action()): {(): 1, (276,): 0, (275,): 2, (275, 276): 1}, nothing pressed or both
arrows coasting, one arrow pushing that way. Each call builds a new mapping whose actions (0-d int64 arrays, DR-08)
the caller owns.
public override IReadOnlyDictionary<KeyCombination, NDArray> GetKeysToAction()
Returns
- IReadOnlyDictionary<KeyCombination, NDArray>
The mapping, in Gymnasium's order.
Remarks
Two footguns are Gymnasium's own and kept: 275 and 276 are pygame 1's arrow codes, which no pygame 2 window (nor
a Gym.NET viewer) produces, so the arrows do nothing; and the empty combination () makes play
refuse the mapping (Key combinations in keys_to_action tuples cannot be empty), so play runs
MountainCar only with a mapping of the caller's. PlayableGame takes it (its relevant keys are 275 and 276).
The override of Env's absent base (DR-45); HasPythonAttribute(string) keeps hasattr false in
continuous mode, as on Gymnasium's continuous class.
Reference: gymnasium/envs/classic_control/mountain_car.py.
Exceptions
- AttributeError
Thrown in continuous mode, where Gymnasium's class has no such method:
'Continuous_MountainCarEnv' object has no attribute 'get_keys_to_action'.
HasPythonAttribute(string)
Hides get_keys_to_action in continuous mode (DR-45): Gymnasium's Continuous_MountainCarEnv has no
such method, so hasattr and has_wrapper_attr answer false there.
protected override bool HasPythonAttribute(string name)
Parameters
namestringThe attribute, in Python spelling.
Returns
Render()
Draws Gymnasium's frame with the same primitives in the same order: the hill (an anti-aliased polyline through 100 points), the car rotated to the slope with its two wheels, and the flag, in a y-up world flipped at the end.
public override RenderResult Render()
Returns
- RenderResult
rgb_array: the(400, 600, 3)uint8 frame, a new array the caller owns.humanand any other mode: nothing.
Remarks
The car's coordinates are computed in the state's precision: after a continuous step its position is float32, so its height uses NumPy's float32 sine and its sums round to float32, as in Python. Reference: gymnasium/envs/classic_control/mountain_car.py.
Exceptions
- AssertionError
Thrown without a render mode when the env has no spec (
assert self.spec is not None).- AttributeError
Thrown before the first reset, after the screen was created, as Gymnasium's render reads the missing state there.
- TypeError
- IndexError
Thrown when an assigned state is empty.
- KeyError
Thrown in human mode when
metadata["render_fps"]was removed.- ValueError
Thrown for a NaN coordinate (a NaN state), with pygame's message.
- OverflowError
Thrown, as pygame's
OverflowError, for a coordinate pygame cannot draw.- DependencyNotInstalled
Thrown on the first human render when no viewer factory is set (DR-20).
Reset(BigInteger?, IReadOnlyDictionary<string, object?>?)
Starts an episode: reseeds the generator when a seed is given, then draws the position from
uniform(low, high) (default [-0.6, -0.4], or the "low"/"high" options) with zero velocity,
both float64. Renders in human mode.
public override ResetResult<NDArray> Reset(BigInteger? seed = null, IReadOnlyDictionary<string, object?>? options = null)
Parameters
seedBigInteger?The seed of a fresh
Generator(PCG64(SeedSequence(seed))), or null to keep the generator.optionsIReadOnlyDictionary<string, object>Gymnasium's reset options:
"low"and"high", anything Python'sfloat()accepts; custom bounds can put the car outside the observation space, as in Gymnasium.
Returns
- ResetResult<NDArray>
The float32 observation
[position, 0]and an empty info.
Remarks
The seed is applied before the options are parsed, as super().reset(seed=seed) runs first: a reset that
fails on its options leaves the freshly seeded generator, undrawn. Reference:
gymnasium/envs/classic_control/mountain_car.py.
Exceptions
- Error
Thrown for a negative seed, with Gymnasium's message.
- ValueError
Thrown for an option
float()rejects, or a lower bound above the upper.- OverflowError
Thrown, as Python's
OverflowError, for NaN or infinite bounds (from the generator).- DependencyNotInstalled
Thrown in human mode by the first render when no viewer factory is set (DR-20).
Step(NDArray)
One step. Discrete: velocity += (action - 1) * force + cos(3 * position) * -gravity, clipped to
±MaxSpeed, then the position, clipped to the walls; reward -1. Continuous: the force is
action[0] clamped to [-1, 1], velocity += force * power - 0.0025 * cos(3 * position) with the
clamps of Gymnasium's source, reward 100 at the flag minus 0.1 * action[0]². A car stopped against the
left wall loses its leftward velocity. Renders in human mode.
public override StepResult<NDArray> Step(NDArray action)
Parameters
actionNDArrayDiscrete: an element of
Discrete(3)(a 0-d integer array; the untyped step also takes a .NET integer). Continuous: an array whose first element is the force; a float32 or float64 element keeps its precision through the arithmetic, as NumPy's scalar does, and an integer or boolean one computes as float64. Read, never kept.
Returns
- StepResult<NDArray>
The float32 observation, the reward, whether the car reached the flag at GoalVelocity or faster,
truncated=Falseand{}.
Remarks
The continuous observation is a copy of the new state, where Gymnasium returns its state array itself; the values are the same. References: mountain_car.py, continuous_mountain_car.py.
Exceptions
- AssertionError
Thrown in discrete mode for an action outside the action space, with Python's message, before anything else.
- AttributeError
Thrown before the first reset (
'MountainCarEnv' object has no attribute 'state', naming the mode's class).- TypeError
- ValueError
Thrown in discrete mode when an assigned state does not hold exactly two values (Python's unpacking error).
- IndexError
Thrown in continuous mode when an assigned state holds fewer than two values, or the action has no first element (a 0-d or empty array), with NumPy's messages.
- OverflowError
Thrown in continuous mode, as Python's
OverflowError, whenmath.pow(action[0], 2)overflows (math range error, a float64 force beyond about 1.3e154); the state is then unchanged, as in Python.- NotSupportedException
Thrown in continuous mode for an action Gym.NET does not model: more than one dimension, or a float16 or complex dtype.
- DependencyNotInstalled
Thrown in human mode by the first render when no viewer factory is set (DR-20).
ToString()
The env's description, as Gymnasium prints the class of this mode: <MountainCarEnv<MountainCar-v0>>,
<Continuous_MountainCarEnv instance>; wrapper descriptions embed it.
public override string ToString()
Returns
- string
The text.