mirror of
https://github.com/Farama-Foundation/Gymnasium.git
synced 2025-08-21 06:20:15 +00:00
834 lines
64 KiB
HTML
834 lines
64 KiB
HTML
<!doctype html>
|
||
<html class="no-js" lang="en" data-content_root="../../">
|
||
<head><meta charset="utf-8"/>
|
||
<meta name="viewport" content="width=device-width,initial-scale=1"/>
|
||
<meta name="color-scheme" content="light dark">
|
||
<meta name="description" content="A standard API for reinforcement learning and a diverse set of reference environments (formerly Gym)">
|
||
<meta property="og:title" content="Gymnasium Documentation" />
|
||
<meta property="og:type" content="website" />
|
||
<meta property="og:description" content="A standard API for reinforcement learning and a diverse set of reference environments (formerly Gym)" />
|
||
<meta property="og:url" content="https://gymnasium.farama.org/introduction/basic_usage.html" /><meta property="og:image" content="https://gymnasium.farama.org/_static/img/gymnasium-github.png" /><meta name="twitter:card" content="summary_large_image"><meta name="viewport" content="width=device-width, initial-scale=1" />
|
||
<link rel="index" title="Index" href="../../genindex/" /><link rel="search" title="Search" href="../../search/" /><link rel="next" title="Training an Agent" href="../train_agent/" /><link rel="prev" title="<no title>" href="../../" />
|
||
<link rel="canonical" href="https://gymnasium.farama.org/introduction/basic_usage.html" />
|
||
|
||
<link rel="shortcut icon" href="../../_static/favicon.png"/><!-- Generated with Sphinx 7.4.7 and Furo 2023.08.19.dev1 -->
|
||
<title>Basic Usage - Gymnasium Documentation</title>
|
||
<link rel="stylesheet" type="text/css" href="../../_static/pygments.css?v=8f2a1f02" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/styles/furo.css?v=3e7f4c72" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/sg_gallery.css?v=61a4c737" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/sg_gallery-binder.css?v=f4aeca0c" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/sg_gallery-dataframe.css?v=2082cf3c" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/sg_gallery-rendered-html.css?v=1277b6f3" />
|
||
<link rel="stylesheet" type="text/css" href="../../_static/styles/furo-extensions.css?v=82c8b628" />
|
||
|
||
|
||
|
||
|
||
<style>
|
||
body {
|
||
--color-code-background: #f8f8f8;
|
||
--color-code-foreground: black;
|
||
|
||
}
|
||
@media not print {
|
||
body[data-theme="dark"] {
|
||
--color-code-background: #202020;
|
||
--color-code-foreground: #d0d0d0;
|
||
|
||
}
|
||
@media (prefers-color-scheme: dark) {
|
||
body:not([data-theme="light"]) {
|
||
--color-code-background: #202020;
|
||
--color-code-foreground: #d0d0d0;
|
||
|
||
}
|
||
}
|
||
}
|
||
</style></head>
|
||
<body>
|
||
<header class="farama-header" aria-label="Farama header">
|
||
<div class="farama-header__container">
|
||
<div class="farama-header__left--mobile">
|
||
<label class="nav-overlay-icon" for="__navigation">
|
||
<div class="visually-hidden">Toggle site navigation sidebar</div>
|
||
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
||
<defs></defs>
|
||
<line x1="0.5" y1="4" x2="23.5" y2="4"></line>
|
||
<line x1="0.232" y1="12" x2="23.5" y2="12"></line>
|
||
<line x1="0.232" y1="20" x2="23.5" y2="20"></line>
|
||
</svg>
|
||
</label>
|
||
</div>
|
||
<div class="farama-header__left farama-header__center--mobile">
|
||
<a href="../../">
|
||
<img class="farama-header__logo only-light" src="../../_static/img/gymnasium_black.svg" alt="Light Logo"/>
|
||
<img class="farama-header__logo only-dark" src="../../_static/img/gymnasium_white.svg" alt="Dark Logo"/>
|
||
<span class="farama-header__title">Gymnasium Documentation</span>
|
||
</a>
|
||
</div>
|
||
<div class="farama-header__right">
|
||
<div class="farama-header-menu">
|
||
<button class="farama-header-menu__btn" aria-label="Open Farama Menu" aria-expanded="false" aria-haspopup="true" aria-controls="farama-menu">
|
||
<img class="farama-black-logo-invert" src="../../_static/img/farama-logo-header.svg">
|
||
<svg viewBox="0 0 24 24" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
|
||
<polyline style="stroke-linecap: round; stroke-linejoin: round; fill: none; stroke-width: 2px;" points="1 7 12 18 23 7"></polyline>
|
||
</svg>
|
||
</button>
|
||
<div class="farama-header-menu-container farama-hidden" aria-hidden="true" id="farama-menu">
|
||
<div class="farama-header-menu__header">
|
||
<a href="https://farama.org">
|
||
<img class="farama-header-menu__logo farama-white-logo-invert" src="../../_static/img/farama_solid_white.svg" alt="Farama Foundation logo">
|
||
<span>Farama Foundation</span>
|
||
</a>
|
||
<div class="farama-header-menu-header__right">
|
||
<button id="farama-close-menu">
|
||
<svg viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg" fill="none" stroke="currentColor"
|
||
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="icon-close">
|
||
<line x1="3" y1="21" x2="21" y2="3"></line>
|
||
<line x1="3" y1="3" x2="21" y2="21"></line>
|
||
</svg>
|
||
</button>
|
||
</div>
|
||
</div>
|
||
<div class="farama-header-menu__body">
|
||
<!-- Response from farama.org/api/projects.json -->
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
|
||
<script>
|
||
document.body.dataset.theme = localStorage.getItem("theme") || "auto";
|
||
</script>
|
||
|
||
|
||
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
|
||
<symbol id="svg-toc" viewBox="0 0 24 24">
|
||
<title>Contents</title>
|
||
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 1024 1024">
|
||
<path d="M408 442h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8zm-8 204c0 4.4 3.6 8 8 8h480c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8H408c-4.4 0-8 3.6-8 8v56zm504-486H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zm0 632H120c-4.4 0-8 3.6-8 8v56c0 4.4 3.6 8 8 8h784c4.4 0 8-3.6 8-8v-56c0-4.4-3.6-8-8-8zM115.4 518.9L271.7 642c5.8 4.6 14.4.5 14.4-6.9V388.9c0-7.4-8.5-11.5-14.4-6.9L115.4 505.1a8.74 8.74 0 0 0 0 13.8z"/>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-menu" viewBox="0 0 24 24">
|
||
<title>Menu</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-menu">
|
||
<line x1="3" y1="12" x2="21" y2="12"></line>
|
||
<line x1="3" y1="6" x2="21" y2="6"></line>
|
||
<line x1="3" y1="18" x2="21" y2="18"></line>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-arrow-right" viewBox="0 0 24 24">
|
||
<title>Expand</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather-chevron-right">
|
||
<polyline points="9 18 15 12 9 6"></polyline>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-sun" viewBox="0 0 24 24">
|
||
<title>Light mode</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="feather-sun">
|
||
<circle cx="12" cy="12" r="5"></circle>
|
||
<line x1="12" y1="1" x2="12" y2="3"></line>
|
||
<line x1="12" y1="21" x2="12" y2="23"></line>
|
||
<line x1="4.22" y1="4.22" x2="5.64" y2="5.64"></line>
|
||
<line x1="18.36" y1="18.36" x2="19.78" y2="19.78"></line>
|
||
<line x1="1" y1="12" x2="3" y2="12"></line>
|
||
<line x1="21" y1="12" x2="23" y2="12"></line>
|
||
<line x1="4.22" y1="19.78" x2="5.64" y2="18.36"></line>
|
||
<line x1="18.36" y1="5.64" x2="19.78" y2="4.22"></line>
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-moon" viewBox="0 0 24 24">
|
||
<title>Dark mode</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-moon">
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none" />
|
||
<path d="M12 3c.132 0 .263 0 .393 0a7.5 7.5 0 0 0 7.92 12.446a9 9 0 1 1 -8.313 -12.454z" />
|
||
</svg>
|
||
</symbol>
|
||
<symbol id="svg-sun-half" viewBox="0 0 24 24">
|
||
<title>Auto light/dark mode</title>
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24" fill="none" stroke="currentColor"
|
||
stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round" class="icon-tabler-shadow">
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||
<circle cx="12" cy="12" r="9" />
|
||
<path d="M13 12h5" />
|
||
<path d="M13 15h4" />
|
||
<path d="M13 18h1" />
|
||
<path d="M13 9h4" />
|
||
<path d="M13 6h1" />
|
||
</svg>
|
||
</symbol>
|
||
</svg>
|
||
|
||
<input type="checkbox" class="sidebar-toggle" name="__navigation" id="__navigation">
|
||
<input type="checkbox" class="sidebar-toggle" name="__toc" id="__toc">
|
||
<label class="overlay sidebar-overlay" for="__navigation">
|
||
<div class="visually-hidden">Hide navigation sidebar</div>
|
||
</label>
|
||
<label class="overlay toc-overlay" for="__toc">
|
||
<div class="visually-hidden">Hide table of contents sidebar</div>
|
||
</label>
|
||
|
||
<div class="page">
|
||
<!--<header class="mobile-header">
|
||
<div class="header-left">
|
||
<label class="nav-overlay-icon" for="__navigation">
|
||
<div class="visually-hidden">Toggle site navigation sidebar</div>
|
||
<i class="icon"><svg><use href="#svg-menu"></use></svg></i>
|
||
</label>
|
||
</div>
|
||
<div class="header-center">
|
||
<a href="../../"><div class="brand">Gymnasium Documentation</div></a>
|
||
</div>
|
||
<div class="header-right">
|
||
<div class="theme-toggle-container theme-toggle-header">
|
||
<button class="theme-toggle">
|
||
<div class="visually-hidden">Toggle Light / Dark / Auto color theme</div>
|
||
<svg class="theme-icon-when-auto"><use href="#svg-sun-half"></use></svg>
|
||
<svg class="theme-icon-when-dark"><use href="#svg-moon"></use></svg>
|
||
<svg class="theme-icon-when-light"><use href="#svg-sun"></use></svg>
|
||
</button>
|
||
</div>
|
||
<label class="toc-overlay-icon toc-header-icon" for="__toc">
|
||
<div class="visually-hidden">Toggle table of contents sidebar</div>
|
||
<i class="icon"><svg><use href="#svg-toc"></use></svg></i>
|
||
</label>
|
||
</div>
|
||
</header>-->
|
||
<aside class="sidebar-drawer">
|
||
<div class="sidebar-container">
|
||
|
||
<div class="sidebar-sticky"><a class="farama-sidebar__title" href="../../">
|
||
<img class="farama-header__logo only-light" src="../../_static/img/gymnasium_black.svg" alt="Light Logo"/>
|
||
<img class="farama-header__logo only-dark" src="../../_static/img/gymnasium_white.svg" alt="Dark Logo"/>
|
||
<span class="farama-header__title">Gymnasium Documentation</span>
|
||
</a><form class="sidebar-search-container" method="get" action="../../search/" role="search">
|
||
<input class="sidebar-search" placeholder="Search" name="q" aria-label="Search">
|
||
<input type="hidden" name="check_keywords" value="yes">
|
||
<input type="hidden" name="area" value="default">
|
||
</form>
|
||
<div id="searchbox"></div><div class="sidebar-scroll"><div class="sidebar-tree">
|
||
<p class="caption" role="heading"><span class="caption-text">Introduction</span></p>
|
||
<ul class="current">
|
||
<li class="toctree-l1 current current-page"><a class="current reference internal" href="#">Basic Usage</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../train_agent/">Training an Agent</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../create_custom_env/">Create a Custom Environment</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../record_agent/">Recording Agents</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../speed_up_env/">Speeding Up Training</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../gym_compatibility/">Compatibility with Gym</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../migration_guide/">Migration Guide - v0.21 to v1.0.0</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">API</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../api/env/">Env</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../api/registry/">Make and register</a></li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../api/spaces/">Spaces</a><input class="toctree-checkbox" id="toctree-checkbox-1" name="toctree-checkbox-1" role="switch" type="checkbox"/><label for="toctree-checkbox-1"><div class="visually-hidden">Toggle navigation of Spaces</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/spaces/fundamental/">Fundamental Spaces</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/spaces/composite/">Composite Spaces</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/spaces/utils/">Spaces Utils</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../api/wrappers/">Wrappers</a><input class="toctree-checkbox" id="toctree-checkbox-2" name="toctree-checkbox-2" role="switch" type="checkbox"/><label for="toctree-checkbox-2"><div class="visually-hidden">Toggle navigation of Wrappers</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/wrappers/table/">List of Wrappers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/wrappers/misc_wrappers/">Misc Wrappers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/wrappers/action_wrappers/">Action Wrappers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/wrappers/observation_wrappers/">Observation Wrappers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/wrappers/reward_wrappers/">Reward Wrappers</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../api/vector/">Vectorize</a><input class="toctree-checkbox" id="toctree-checkbox-3" name="toctree-checkbox-3" role="switch" type="checkbox"/><label for="toctree-checkbox-3"><div class="visually-hidden">Toggle navigation of Vectorize</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/vector/wrappers/">Wrappers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/vector/async_vector_env/">AsyncVectorEnv</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/vector/sync_vector_env/">SyncVectorEnv</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../api/vector/utils/">Utility functions</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../api/utils/">Utility functions</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../api/functional/">Functional Env</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">Environments</span></p>
|
||
<ul>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../environments/classic_control/">Classic Control</a><input class="toctree-checkbox" id="toctree-checkbox-4" name="toctree-checkbox-4" role="switch" type="checkbox"/><label for="toctree-checkbox-4"><div class="visually-hidden">Toggle navigation of Classic Control</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/classic_control/acrobot/">Acrobot</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/classic_control/cart_pole/">Cart Pole</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/classic_control/mountain_car_continuous/">Mountain Car Continuous</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/classic_control/mountain_car/">Mountain Car</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/classic_control/pendulum/">Pendulum</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../environments/box2d/">Box2D</a><input class="toctree-checkbox" id="toctree-checkbox-5" name="toctree-checkbox-5" role="switch" type="checkbox"/><label for="toctree-checkbox-5"><div class="visually-hidden">Toggle navigation of Box2D</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/box2d/bipedal_walker/">Bipedal Walker</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/box2d/car_racing/">Car Racing</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/box2d/lunar_lander/">Lunar Lander</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../environments/toy_text/">Toy Text</a><input class="toctree-checkbox" id="toctree-checkbox-6" name="toctree-checkbox-6" role="switch" type="checkbox"/><label for="toctree-checkbox-6"><div class="visually-hidden">Toggle navigation of Toy Text</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/toy_text/blackjack/">Blackjack</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/toy_text/taxi/">Taxi</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/toy_text/cliff_walking/">Cliff Walking</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/toy_text/frozen_lake/">Frozen Lake</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../environments/mujoco/">MuJoCo</a><input class="toctree-checkbox" id="toctree-checkbox-7" name="toctree-checkbox-7" role="switch" type="checkbox"/><label for="toctree-checkbox-7"><div class="visually-hidden">Toggle navigation of MuJoCo</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/ant/">Ant</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/half_cheetah/">Half Cheetah</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/hopper/">Hopper</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/humanoid/">Humanoid</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/humanoid_standup/">Humanoid Standup</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/inverted_double_pendulum/">Inverted Double Pendulum</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/inverted_pendulum/">Inverted Pendulum</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/pusher/">Pusher</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/reacher/">Reacher</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/swimmer/">Swimmer</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../environments/mujoco/walker2d/">Walker2D</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../environments/atari/">Atari</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../environments/third_party_environments/">External Environments</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">Tutorials</span></p>
|
||
<ul>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../tutorials/gymnasium_basics/">Gymnasium Basics Documentation Links</a><input class="toctree-checkbox" id="toctree-checkbox-8" name="toctree-checkbox-8" role="switch" type="checkbox"/><label for="toctree-checkbox-8"><div class="visually-hidden">Toggle navigation of Gymnasium Basics Documentation Links</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/gymnasium_basics/load_quadruped_model/">Load custom quadruped robot environments</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/gymnasium_basics/handling_time_limits/">Handling Time Limits</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/gymnasium_basics/implementing_custom_wrappers/">Implementing Custom Wrappers</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/gymnasium_basics/environment_creation/">Make your own custom environment</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/gymnasium_basics/vector_envs_tutorial/">Training A2C with Vector Envs and Domain Randomization</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1 has-children"><a class="reference internal" href="../../tutorials/training_agents/">Training Agents links in the Gymnasium Documentation</a><input class="toctree-checkbox" id="toctree-checkbox-9" name="toctree-checkbox-9" role="switch" type="checkbox"/><label for="toctree-checkbox-9"><div class="visually-hidden">Toggle navigation of Training Agents links in the Gymnasium Documentation</div><i class="icon"><svg><use href="#svg-arrow-right"></use></svg></i></label><ul>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/training_agents/reinforce_invpend_gym_v26/">Training using REINFORCE for Mujoco</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/training_agents/blackjack_tutorial/">Solving Blackjack with Q-Learning</a></li>
|
||
<li class="toctree-l2"><a class="reference internal" href="../../tutorials/training_agents/FrozenLake_tuto/">Frozenlake benchmark</a></li>
|
||
</ul>
|
||
</li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../tutorials/third-party-tutorials/">Third-Party Tutorials</a></li>
|
||
</ul>
|
||
<p class="caption" role="heading"><span class="caption-text">Development</span></p>
|
||
<ul>
|
||
<li class="toctree-l1"><a class="reference external" href="https://github.com/Farama-Foundation/Gymnasium">Github</a></li>
|
||
<li class="toctree-l1"><a class="reference external" href="https://arxiv.org/abs/2407.17032">Paper</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../gymnasium_release_notes/">Gymnasium Release Notes</a></li>
|
||
<li class="toctree-l1"><a class="reference internal" href="../../gym_release_notes/">Gym Release Notes</a></li>
|
||
<li class="toctree-l1"><a class="reference external" href="https://github.com/Farama-Foundation/Gymnasium/blob/main/docs/README.md">Contribute to the Docs</a></li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
|
||
</div>
|
||
|
||
</div>
|
||
</aside>
|
||
<div class="main-container">
|
||
|
||
|
||
|
||
|
||
|
||
<div class="main">
|
||
<div class="content">
|
||
<div class="article-container">
|
||
<a href="#" class="back-to-top muted-link">
|
||
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24">
|
||
<path d="M13 20h-2V8l-5.5 5.5-1.42-1.42L12 4.16l7.92 7.92-1.42 1.42L13 8v12z"></path>
|
||
</svg>
|
||
<span>Back to top</span>
|
||
</a>
|
||
<div class="content-icon-container"><div class="edit-this-page">
|
||
<a class="muted-link" href="https://github.com/Farama-Foundation/Gymnasium/edit/main/docs/introduction/basic_usage.md" title="Edit this page">
|
||
<svg aria-hidden="true" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round">
|
||
<path stroke="none" d="M0 0h24v24H0z" fill="none"/>
|
||
<path d="M4 20h4l10.5 -10.5a1.5 1.5 0 0 0 -4 -4l-10.5 10.5v4" />
|
||
<line x1="13.5" y1="6.5" x2="17.5" y2="10.5" />
|
||
</svg>
|
||
<span class="visually-hidden">Edit this page</span>
|
||
</a>
|
||
</div><div class="theme-toggle-container theme-toggle-content">
|
||
<button class="theme-toggle" title="Toggle color theme">
|
||
<div class="visually-hidden">Toggle Light / Dark / Auto color theme</div>
|
||
<svg class="theme-icon-when-auto">
|
||
<use href="#svg-sun-half"></use>
|
||
</svg>
|
||
<svg class="theme-icon-when-dark">
|
||
<use href="#svg-moon"></use>
|
||
</svg>
|
||
<svg class="theme-icon-when-light">
|
||
<use href="#svg-sun"></use>
|
||
</svg>
|
||
</button>
|
||
</div>
|
||
<label class="toc-overlay-icon toc-content-icon" for="__toc">
|
||
<div class="visually-hidden">Toggle table of contents sidebar</div>
|
||
<i class="icon"><svg>
|
||
<use href="#svg-toc"></use>
|
||
</svg></i>
|
||
</label>
|
||
</div>
|
||
<article role="main">
|
||
|
||
<section class="tex2jax_ignore mathjax_ignore" id="basic-usage">
|
||
<h1>Basic Usage<a class="headerlink" href="#basic-usage" title="Link to this heading">¶</a></h1>
|
||
<p>Gymnasium is a project that provides an API (application programming interface) for all single agent reinforcement learning environments, with implementations of common environments: cartpole, pendulum, mountain-car, mujoco, atari, and more. This page will outline the basics of how to use Gymnasium including its four key functions: <a class="reference internal" href="../../api/registry/#gymnasium.make" title="gymnasium.make"><code class="xref py py-meth docutils literal notranslate"><span class="pre">make()</span></code></a>, <a class="reference internal" href="../../api/env/#gymnasium.Env.reset" title="gymnasium.Env.reset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Env.reset()</span></code></a>, <a class="reference internal" href="../../api/env/#gymnasium.Env.step" title="gymnasium.Env.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Env.step()</span></code></a> and <a class="reference internal" href="../../api/env/#gymnasium.Env.render" title="gymnasium.Env.render"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Env.render()</span></code></a>.</p>
|
||
<p>At the core of Gymnasium is <a class="reference internal" href="../../api/env/#gymnasium.Env" title="gymnasium.Env"><code class="xref py py-class docutils literal notranslate"><span class="pre">Env</span></code></a>, a high-level python class representing a markov decision process (MDP) from reinforcement learning theory (note: this is not a perfect reconstruction, missing several components of MDPs). The class provides users the ability generate an initial state, transition / move to new states given an action and visualize the environment. Alongside <a class="reference internal" href="../../api/env/#gymnasium.Env" title="gymnasium.Env"><code class="xref py py-class docutils literal notranslate"><span class="pre">Env</span></code></a>, <a class="reference internal" href="../../api/wrappers/#gymnasium.Wrapper" title="gymnasium.Wrapper"><code class="xref py py-class docutils literal notranslate"><span class="pre">Wrapper</span></code></a> are provided to help augment / modify the environment, in particular, the agent observations, rewards and actions taken.</p>
|
||
<section id="initializing-environments">
|
||
<h2>Initializing Environments<a class="headerlink" href="#initializing-environments" title="Link to this heading">¶</a></h2>
|
||
<p>Initializing environments is very easy in Gymnasium and can be done via the <a class="reference internal" href="../../api/registry/#gymnasium.make" title="gymnasium.make"><code class="xref py py-meth docutils literal notranslate"><span class="pre">make()</span></code></a> function:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gymnasium</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">gym</span>
|
||
<span class="n">env</span> <span class="o">=</span> <span class="n">gym</span><span class="o">.</span><span class="n">make</span><span class="p">(</span><span class="s1">'CartPole-v1'</span><span class="p">)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>This function will return an <a class="reference internal" href="../../api/env/#gymnasium.Env" title="gymnasium.Env"><code class="xref py py-class docutils literal notranslate"><span class="pre">Env</span></code></a> for users to interact with. To see all environments you can create, use <a class="reference internal" href="../../api/registry/#gymnasium.pprint_registry" title="gymnasium.pprint_registry"><code class="xref py py-meth docutils literal notranslate"><span class="pre">pprint_registry()</span></code></a>. Furthermore, <a class="reference internal" href="../../api/registry/#gymnasium.make" title="gymnasium.make"><code class="xref py py-meth docutils literal notranslate"><span class="pre">make()</span></code></a> provides a number of additional arguments for specifying keywords to the environment, adding more or less wrappers, etc. See <a class="reference internal" href="../../api/registry/#gymnasium.make" title="gymnasium.make"><code class="xref py py-meth docutils literal notranslate"><span class="pre">make()</span></code></a> for more information.</p>
|
||
</section>
|
||
<section id="interacting-with-the-environment">
|
||
<h2>Interacting with the Environment<a class="headerlink" href="#interacting-with-the-environment" title="Link to this heading">¶</a></h2>
|
||
<p>In reinforcement learning, the classic “agent-environment loop” pictured below is a simplified representation of how an agent and environment interact with each other. The agent receives an observation about the environment, the agent then selects an action, which the environment uses to determine the reward and the next observation. The cycle then repeats itself until the environment ends (terminates).</p>
|
||
<a class="only-light reference internal image-reference" href="../../_images/AE_loop.png"><img alt="../../_images/AE_loop.png" class="only-light align-center" src="../../_images/AE_loop.png" style="width: 50%;" />
|
||
</a>
|
||
<a class="only-dark reference internal image-reference" href="../../_images/AE_loop_dark.png"><img alt="../../_images/AE_loop_dark.png" class="only-dark align-center" src="../../_images/AE_loop_dark.png" style="width: 50%;" />
|
||
</a>
|
||
<p>For Gymnasium, the “agent-environment-loop” is implemented below for a single episode (until the environment ends). See the next section for a line-by-line explanation. Note that running this code requires installing swig (<code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">swig</span></code> or <a class="reference external" href="https://www.swig.org/download.html">download</a>) along with <code class="docutils literal notranslate"><span class="pre">pip</span> <span class="pre">install</span> <span class="pre">"gymnasium[box2d]"</span></code>.</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">gymnasium</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">gym</span>
|
||
|
||
<span class="n">env</span> <span class="o">=</span> <span class="n">gym</span><span class="o">.</span><span class="n">make</span><span class="p">(</span><span class="s2">"LunarLander-v3"</span><span class="p">,</span> <span class="n">render_mode</span><span class="o">=</span><span class="s2">"human"</span><span class="p">)</span>
|
||
<span class="n">observation</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
|
||
|
||
<span class="n">episode_over</span> <span class="o">=</span> <span class="kc">False</span>
|
||
<span class="k">while</span> <span class="ow">not</span> <span class="n">episode_over</span><span class="p">:</span>
|
||
<span class="n">action</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">action_space</span><span class="o">.</span><span class="n">sample</span><span class="p">()</span> <span class="c1"># agent policy that uses the observation and info</span>
|
||
<span class="n">observation</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">terminated</span><span class="p">,</span> <span class="n">truncated</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="n">env</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
|
||
|
||
<span class="n">episode_over</span> <span class="o">=</span> <span class="n">terminated</span> <span class="ow">or</span> <span class="n">truncated</span>
|
||
|
||
<span class="n">env</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>The output should look something like this:</p>
|
||
<figure class="align-center">
|
||
<a class="reference internal image-reference" href="https://user-images.githubusercontent.com/15806078/153222406-af5ce6f0-4696-4a24-a683-46ad4939170c.gif"><img alt="https://user-images.githubusercontent.com/15806078/153222406-af5ce6f0-4696-4a24-a683-46ad4939170c.gif" src="https://user-images.githubusercontent.com/15806078/153222406-af5ce6f0-4696-4a24-a683-46ad4939170c.gif" style="width: 50%;" />
|
||
</a>
|
||
</figure>
|
||
<section id="explaining-the-code">
|
||
<h3>Explaining the code<a class="headerlink" href="#explaining-the-code" title="Link to this heading">¶</a></h3>
|
||
<p>First, an environment is created using <a class="reference internal" href="../../api/registry/#gymnasium.make" title="gymnasium.make"><code class="xref py py-meth docutils literal notranslate"><span class="pre">make()</span></code></a> with an additional keyword <code class="docutils literal notranslate"><span class="pre">"render_mode"</span></code> that specifies how the environment should be visualized. See <a class="reference internal" href="../../api/env/#gymnasium.Env.render" title="gymnasium.Env.render"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Env.render()</span></code></a> for details on the default meaning of different render modes. In this example, we use the <code class="docutils literal notranslate"><span class="pre">"LunarLander"</span></code> environment where the agent controls a spaceship that needs to land safely.</p>
|
||
<p>After initializing the environment, we <a class="reference internal" href="../../api/env/#gymnasium.Env.reset" title="gymnasium.Env.reset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Env.reset()</span></code></a> the environment to get the first observation of the environment along with an additional information. For initializing the environment with a particular random seed or options (see the environment documentation for possible values) use the <code class="docutils literal notranslate"><span class="pre">seed</span></code> or <code class="docutils literal notranslate"><span class="pre">options</span></code> parameters with <code class="xref py py-meth docutils literal notranslate"><span class="pre">reset()</span></code>.</p>
|
||
<p>As we wish to continue the agent-environment loop until the environment ends, which is in an unknown number of timesteps, we define <code class="docutils literal notranslate"><span class="pre">episode_over</span></code> as a variable to know when to stop interacting with the environment along with a while loop that uses it.</p>
|
||
<p>Next, the agent performs an action in the environment, <a class="reference internal" href="../../api/env/#gymnasium.Env.step" title="gymnasium.Env.step"><code class="xref py py-meth docutils literal notranslate"><span class="pre">Env.step()</span></code></a> executes the selected action (in this case random with <code class="docutils literal notranslate"><span class="pre">env.action_space.sample()</span></code>) to update the environment. This action can be imagined as moving a robot or pressing a button on a games’ controller that causes a change within the environment. As a result, the agent receives a new observation from the updated environment along with a reward for taking the action. This reward could be for instance positive for destroying an enemy or a negative reward for moving into lava. One such action-observation exchange is referred to as a <strong>timestep</strong>.</p>
|
||
<p>However, after some timesteps, the environment may end, this is called the terminal state. For instance, the robot may have crashed, or may have succeeded in completing a task, the environment will need to stop as the agent cannot continue. In Gymnasium, if the environment has terminated, this is returned by <code class="xref py py-meth docutils literal notranslate"><span class="pre">step()</span></code> as the third variable, <code class="docutils literal notranslate"><span class="pre">terminated</span></code>. Similarly, we may also want the environment to end after a fixed number of timesteps, in this case, the environment issues a truncated signal. If either of <code class="docutils literal notranslate"><span class="pre">terminated</span></code> or <code class="docutils literal notranslate"><span class="pre">truncated</span></code> are <code class="docutils literal notranslate"><span class="pre">True</span></code> then we end the episode but in most cases users might wish to restart the environment, this can be done with <code class="docutils literal notranslate"><span class="pre">env.reset()</span></code>.</p>
|
||
</section>
|
||
</section>
|
||
<section id="action-and-observation-spaces">
|
||
<h2>Action and observation spaces<a class="headerlink" href="#action-and-observation-spaces" title="Link to this heading">¶</a></h2>
|
||
<p>Every environment specifies the format of valid actions and observations with the <a class="reference internal" href="../../api/env/#gymnasium.Env.action_space" title="gymnasium.Env.action_space"><code class="xref py py-attr docutils literal notranslate"><span class="pre">action_space</span></code></a> and <a class="reference internal" href="../../api/env/#gymnasium.Env.observation_space" title="gymnasium.Env.observation_space"><code class="xref py py-attr docutils literal notranslate"><span class="pre">observation_space</span></code></a> attributes. This is helpful for knowing both the expected input and output of the environment, as all valid actions and observations should be contained with their respective spaces. In the example above, we sampled random actions via <code class="docutils literal notranslate"><span class="pre">env.action_space.sample()</span></code> instead of using an agent policy, mapping observations to actions which users will want to make.</p>
|
||
<p>Importantly, <code class="xref py py-attr docutils literal notranslate"><span class="pre">Env.action_space</span></code> and <code class="xref py py-attr docutils literal notranslate"><span class="pre">Env.observation_space</span></code> are instances of <code class="xref py py-class docutils literal notranslate"><span class="pre">Space</span></code>, a high-level python class that provides the key functions: <code class="xref py py-meth docutils literal notranslate"><span class="pre">Space.contains()</span></code> and <code class="xref py py-meth docutils literal notranslate"><span class="pre">Space.sample()</span></code>. Gymnasium has support for a wide range of spaces that users might need:</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../../api/spaces/fundamental/#gymnasium.spaces.Box" title="gymnasium.spaces.Box"><code class="xref py py-class docutils literal notranslate"><span class="pre">Box</span></code></a>: describes bounded space with upper and lower limits of any n-dimensional shape.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/fundamental/#gymnasium.spaces.Discrete" title="gymnasium.spaces.Discrete"><code class="xref py py-class docutils literal notranslate"><span class="pre">Discrete</span></code></a>: describes a discrete space where <code class="docutils literal notranslate"><span class="pre">{0,</span> <span class="pre">1,</span> <span class="pre">...,</span> <span class="pre">n-1}</span></code> are the possible values our observation or action can take.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/fundamental/#gymnasium.spaces.MultiBinary" title="gymnasium.spaces.MultiBinary"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultiBinary</span></code></a>: describes a binary space of any n-dimensional shape.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/fundamental/#gymnasium.spaces.MultiDiscrete" title="gymnasium.spaces.MultiDiscrete"><code class="xref py py-class docutils literal notranslate"><span class="pre">MultiDiscrete</span></code></a>: consists of a series of <a class="reference internal" href="../../api/spaces/fundamental/#gymnasium.spaces.Discrete" title="gymnasium.spaces.Discrete"><code class="xref py py-class docutils literal notranslate"><span class="pre">Discrete</span></code></a> action spaces with a different number of actions in each element.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/fundamental/#gymnasium.spaces.Text" title="gymnasium.spaces.Text"><code class="xref py py-class docutils literal notranslate"><span class="pre">Text</span></code></a>: describes a string space with a minimum and maximum length.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/composite/#gymnasium.spaces.Dict" title="gymnasium.spaces.Dict"><code class="xref py py-class docutils literal notranslate"><span class="pre">Dict</span></code></a>: describes a dictionary of simpler spaces.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/composite/#gymnasium.spaces.Tuple" title="gymnasium.spaces.Tuple"><code class="xref py py-class docutils literal notranslate"><span class="pre">Tuple</span></code></a>: describes a tuple of simple spaces.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/composite/#gymnasium.spaces.Graph" title="gymnasium.spaces.Graph"><code class="xref py py-class docutils literal notranslate"><span class="pre">Graph</span></code></a>: describes a mathematical graph (network) with interlinking nodes and edges.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/spaces/composite/#gymnasium.spaces.Sequence" title="gymnasium.spaces.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a>: describes a variable length of simpler space elements.</p></li>
|
||
</ul>
|
||
<p>For example usage of spaces, see their <a class="reference external" href="../api/spaces">documentation</a> along with <a class="reference external" href="../api/spaces/utils">utility functions</a>. There are a couple of more niche spaces <a class="reference internal" href="../../api/spaces/composite/#gymnasium.spaces.Graph" title="gymnasium.spaces.Graph"><code class="xref py py-class docutils literal notranslate"><span class="pre">Graph</span></code></a>, <a class="reference internal" href="../../api/spaces/composite/#gymnasium.spaces.Sequence" title="gymnasium.spaces.Sequence"><code class="xref py py-class docutils literal notranslate"><span class="pre">Sequence</span></code></a> and <a class="reference internal" href="../../api/spaces/fundamental/#gymnasium.spaces.Text" title="gymnasium.spaces.Text"><code class="xref py py-class docutils literal notranslate"><span class="pre">Text</span></code></a>.</p>
|
||
</section>
|
||
<section id="modifying-the-environment">
|
||
<h2>Modifying the environment<a class="headerlink" href="#modifying-the-environment" title="Link to this heading">¶</a></h2>
|
||
<p>Wrappers are a convenient way to modify an existing environment without having to alter the underlying code directly. Using wrappers will allow you to avoid a lot of boilerplate code and make your environment more modular. Wrappers can also be chained to combine their effects. Most environments that are generated via <a class="reference internal" href="../../api/registry/#gymnasium.make" title="gymnasium.make"><code class="xref py py-meth docutils literal notranslate"><span class="pre">gymnasium.make()</span></code></a> will already be wrapped by default using the <a class="reference internal" href="../../api/wrappers/misc_wrappers/#gymnasium.wrappers.TimeLimit" title="gymnasium.wrappers.TimeLimit"><code class="xref py py-class docutils literal notranslate"><span class="pre">TimeLimit</span></code></a>, <a class="reference internal" href="../../api/wrappers/misc_wrappers/#gymnasium.wrappers.OrderEnforcing" title="gymnasium.wrappers.OrderEnforcing"><code class="xref py py-class docutils literal notranslate"><span class="pre">OrderEnforcing</span></code></a> and <a class="reference internal" href="../../api/wrappers/misc_wrappers/#gymnasium.wrappers.PassiveEnvChecker" title="gymnasium.wrappers.PassiveEnvChecker"><code class="xref py py-class docutils literal notranslate"><span class="pre">PassiveEnvChecker</span></code></a>.</p>
|
||
<p>In order to wrap an environment, you must first initialize a base environment. Then you can pass this environment along with (possibly optional) parameters to the wrapper’s constructor:</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="kn">import</span><span class="w"> </span><span class="nn">gymnasium</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="nn">gym</span>
|
||
<span class="gp">>>> </span><span class="kn">from</span><span class="w"> </span><span class="nn">gymnasium.wrappers</span><span class="w"> </span><span class="kn">import</span> <span class="n">FlattenObservation</span>
|
||
<span class="gp">>>> </span><span class="n">env</span> <span class="o">=</span> <span class="n">gym</span><span class="o">.</span><span class="n">make</span><span class="p">(</span><span class="s2">"CarRacing-v3"</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">env</span><span class="o">.</span><span class="n">observation_space</span><span class="o">.</span><span class="n">shape</span>
|
||
<span class="go">(96, 96, 3)</span>
|
||
<span class="gp">>>> </span><span class="n">wrapped_env</span> <span class="o">=</span> <span class="n">FlattenObservation</span><span class="p">(</span><span class="n">env</span><span class="p">)</span>
|
||
<span class="gp">>>> </span><span class="n">wrapped_env</span><span class="o">.</span><span class="n">observation_space</span><span class="o">.</span><span class="n">shape</span>
|
||
<span class="go">(27648,)</span>
|
||
</pre></div>
|
||
</div>
|
||
<p>Gymnasium already provides many commonly used wrappers for you. Some examples:</p>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../../api/wrappers/misc_wrappers/#gymnasium.wrappers.TimeLimit" title="gymnasium.wrappers.TimeLimit"><code class="xref py py-class docutils literal notranslate"><span class="pre">TimeLimit</span></code></a>: Issues a truncated signal if a maximum number of timesteps has been exceeded (or the base environment has issued a truncated signal).</p></li>
|
||
<li><p><a class="reference internal" href="../../api/wrappers/action_wrappers/#gymnasium.wrappers.ClipAction" title="gymnasium.wrappers.ClipAction"><code class="xref py py-class docutils literal notranslate"><span class="pre">ClipAction</span></code></a>: Clips any action passed to <code class="docutils literal notranslate"><span class="pre">step</span></code> such that it lies in the base environment’s action space.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/wrappers/action_wrappers/#gymnasium.wrappers.RescaleAction" title="gymnasium.wrappers.RescaleAction"><code class="xref py py-class docutils literal notranslate"><span class="pre">RescaleAction</span></code></a>: Applies an affine transformation to the action to linearly scale for a new low and high bound on the environment.</p></li>
|
||
<li><p><a class="reference internal" href="../../api/wrappers/observation_wrappers/#gymnasium.wrappers.TimeAwareObservation" title="gymnasium.wrappers.TimeAwareObservation"><code class="xref py py-class docutils literal notranslate"><span class="pre">TimeAwareObservation</span></code></a>: Add information about the index of timestep to observation. In some cases helpful to ensure that transitions are Markov.</p></li>
|
||
</ul>
|
||
<p>For a full list of implemented wrappers in Gymnasium, see <a class="reference internal" href="../../api/wrappers/"><span class="doc std std-doc">wrappers</span></a>.</p>
|
||
<p>If you have a wrapped environment, and you want to get the unwrapped environment underneath all the layers of wrappers (so that you can manually call a function or change some underlying aspect of the environment), you can use the <a class="reference internal" href="../../api/env/#gymnasium.Env.unwrapped" title="gymnasium.Env.unwrapped"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unwrapped</span></code></a> attribute. If the environment is already a base environment, the <a class="reference internal" href="../../api/env/#gymnasium.Env.unwrapped" title="gymnasium.Env.unwrapped"><code class="xref py py-attr docutils literal notranslate"><span class="pre">unwrapped</span></code></a> attribute will just return itself.</p>
|
||
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="gp">>>> </span><span class="n">wrapped_env</span>
|
||
<span class="go"><FlattenObservation<TimeLimit<OrderEnforcing<PassiveEnvChecker<CarRacing<CarRacing-v3>>>>>></span>
|
||
<span class="gp">>>> </span><span class="n">wrapped_env</span><span class="o">.</span><span class="n">unwrapped</span>
|
||
<span class="go"><gymnasium.envs.box2d.car_racing.CarRacing object at 0x7f04efcb8850></span>
|
||
</pre></div>
|
||
</div>
|
||
</section>
|
||
<section id="more-information">
|
||
<h2>More information<a class="headerlink" href="#more-information" title="Link to this heading">¶</a></h2>
|
||
<ul class="simple">
|
||
<li><p><a class="reference internal" href="../train_agent/"><span class="doc std std-doc">Training an agent</span></a></p></li>
|
||
<li><p><a class="reference internal" href="../create_custom_env/"><span class="doc std std-doc">Making a Custom Environment</span></a></p></li>
|
||
<li><p><a class="reference internal" href="../record_agent/"><span class="doc std std-doc">Recording an agent’s behaviour</span></a></p></li>
|
||
<li><p><a class="reference internal" href="../speed_up_env/"><span class="doc std std-doc">Speeding up an Environment</span></a></p></li>
|
||
<li><p><a class="reference internal" href="../gym_compatibility/"><span class="doc std std-doc">Compatibility with OpenAI Gym</span></a></p></li>
|
||
<li><p><a class="reference internal" href="../migration_guide/"><span class="doc std std-doc">Migration Guide for Gym v0.21 to v0.26 and for v1.0.0</span></a></p></li>
|
||
</ul>
|
||
</section>
|
||
</section>
|
||
|
||
</article>
|
||
</div>
|
||
<footer>
|
||
|
||
<div class="related-pages">
|
||
<a class="next-page" href="../train_agent/">
|
||
<div class="page-info">
|
||
<div class="context">
|
||
<span>Next</span>
|
||
</div>
|
||
<div class="title">Training an Agent</div>
|
||
</div>
|
||
<svg class="furo-related-icon">
|
||
<use href="#svg-arrow-right"></use>
|
||
</svg>
|
||
</a>
|
||
|
||
</div>
|
||
<div class="bottom-of-page">
|
||
<div class="left-details">
|
||
<div class="copyright">
|
||
Copyright © 2025 Farama Foundation
|
||
</div>
|
||
<!--
|
||
Made with <a href="https://www.sphinx-doc.org/">Sphinx</a> and <a class="muted-link" href="https://pradyunsg.me">@pradyunsg</a>'s
|
||
|
||
<a href="https://github.com/pradyunsg/furo">Furo</a>
|
||
-->
|
||
</div>
|
||
<div class="right-details">
|
||
<div class="icons">
|
||
<a class="muted-link" href="https://github.com/Farama-Foundation/Gymnasium/"
|
||
aria-label="On GitHub">
|
||
<svg stroke="currentColor" fill="currentColor" stroke-width="0" viewBox="0 0 16 16">
|
||
<path fill-rule="evenodd"
|
||
d="M8 0C3.58 0 0 3.58 0 8c0 3.54 2.29 6.53 5.47 7.59.4.07.55-.17.55-.38 0-.19-.01-.82-.01-1.49-2.01.37-2.53-.49-2.69-.94-.09-.23-.48-.94-.82-1.13-.28-.15-.68-.52-.01-.53.63-.01 1.08.58 1.23.82.72 1.21 1.87.87 2.33.66.07-.52.28-.87.51-1.07-1.78-.2-3.64-.89-3.64-3.95 0-.87.31-1.59.82-2.15-.08-.2-.36-1.02.08-2.12 0 0 .67-.21 2.2.82.64-.18 1.32-.27 2-.27.68 0 1.36.09 2 .27 1.53-1.04 2.2-.82 2.2-.82.44 1.1.16 1.92.08 2.12.51.56.82 1.27.82 2.15 0 3.07-1.87 3.75-3.65 3.95.29.25.54.73.54 1.48 0 1.07-.01 1.93-.01 2.2 0 .21.15.46.55.38A8.013 8.013 0 0 0 16 8c0-4.42-3.58-8-8-8z">
|
||
</path>
|
||
</svg>
|
||
</a>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
</footer>
|
||
</div>
|
||
<aside class="toc-drawer">
|
||
|
||
|
||
<div class="toc-sticky toc-scroll">
|
||
<div class="toc-title-container">
|
||
<span class="toc-title">
|
||
On this page
|
||
</span>
|
||
</div>
|
||
<div class="toc-tree-container">
|
||
<div class="toc-tree">
|
||
<ul>
|
||
<li><a class="reference internal" href="#">Basic Usage</a><ul>
|
||
<li><a class="reference internal" href="#initializing-environments">Initializing Environments</a></li>
|
||
<li><a class="reference internal" href="#interacting-with-the-environment">Interacting with the Environment</a><ul>
|
||
<li><a class="reference internal" href="#explaining-the-code">Explaining the code</a></li>
|
||
</ul>
|
||
</li>
|
||
<li><a class="reference internal" href="#action-and-observation-spaces">Action and observation spaces</a></li>
|
||
<li><a class="reference internal" href="#modifying-the-environment">Modifying the environment</a></li>
|
||
<li><a class="reference internal" href="#more-information">More information</a></li>
|
||
</ul>
|
||
</li>
|
||
</ul>
|
||
|
||
</div>
|
||
</div>
|
||
</div>
|
||
|
||
|
||
</aside>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
<script>
|
||
const toggleMenu = () => {
|
||
const menuBtn = document.querySelector(".farama-header-menu__btn");
|
||
const menuContainer = document.querySelector(".farama-header-menu-container");
|
||
if (document.querySelector(".farama-header-menu").classList.contains("active")) {
|
||
menuBtn.setAttribute("aria-expanded", "false");
|
||
menuContainer.setAttribute("aria-hidden", "true");
|
||
} else {
|
||
menuBtn.setAttribute("aria-expanded", "true");
|
||
menuContainer.setAttribute("aria-hidden", "false");
|
||
}
|
||
document.querySelector(".farama-header-menu").classList.toggle("active");
|
||
}
|
||
|
||
document.querySelector(".farama-header-menu__btn").addEventListener("click", toggleMenu);
|
||
document.getElementById("farama-close-menu").addEventListener("click", toggleMenu);
|
||
</script>
|
||
|
||
|
||
<script async src="https://www.googletagmanager.com/gtag/js?id=G-6H9C8TWXZ8"></script>
|
||
<script>
|
||
const enableGtag = () => {
|
||
window.dataLayer = window.dataLayer || [];
|
||
function gtag(){dataLayer.push(arguments);}
|
||
gtag('js', new Date());
|
||
gtag('config', 'G-6H9C8TWXZ8');
|
||
}
|
||
(() => {
|
||
if (!localStorage.getItem("acceptedCookieAlert")) {
|
||
const boxElem = document.createElement("div");
|
||
boxElem.classList.add("cookie-alert");
|
||
const containerElem = document.createElement("div");
|
||
containerElem.classList.add("cookie-alert__container");
|
||
const textElem = document.createElement("p");
|
||
textElem.innerHTML = `This page uses <a href="https://analytics.google.com/">
|
||
Google Analytics</a> to collect statistics.`;
|
||
containerElem.appendChild(textElem);
|
||
|
||
const declineBtn = Object.assign(document.createElement("button"),
|
||
{
|
||
innerText: "Deny",
|
||
className: "farama-btn cookie-alert__button",
|
||
id: "cookie-alert__decline",
|
||
}
|
||
);
|
||
declineBtn.addEventListener("click", () => {
|
||
localStorage.setItem("acceptedCookieAlert", false);
|
||
boxElem.remove();
|
||
});
|
||
|
||
const acceptBtn = Object.assign(document.createElement("button"),
|
||
{
|
||
innerText: "Allow",
|
||
className: "farama-btn cookie-alert__button",
|
||
id: "cookie-alert__accept",
|
||
}
|
||
);
|
||
acceptBtn.addEventListener("click", () => {
|
||
localStorage.setItem("acceptedCookieAlert", true);
|
||
boxElem.remove();
|
||
enableGtag();
|
||
});
|
||
|
||
containerElem.appendChild(declineBtn);
|
||
containerElem.appendChild(acceptBtn);
|
||
boxElem.appendChild(containerElem);
|
||
document.body.appendChild(boxElem);
|
||
} else if (localStorage.getItem("acceptedCookieAlert") === "true") {
|
||
enableGtag();
|
||
}
|
||
})()
|
||
</script>
|
||
|
||
<script src="../../_static/documentation_options.js?v=25d39d6f"></script>
|
||
<script src="../../_static/doctools.js?v=9a2dae69"></script>
|
||
<script src="../../_static/sphinx_highlight.js?v=dc90522c"></script>
|
||
<script src="../../_static/scripts/furo.js?v=7660844c"></script>
|
||
|
||
<script>
|
||
|
||
const createProjectsList = (projects, displayImages) => {
|
||
const ulElem = Object.assign(document.createElement('ul'),
|
||
{
|
||
className:'farama-header-menu-list',
|
||
}
|
||
)
|
||
for (let project of projects) {
|
||
const liElem = document.createElement("li");
|
||
const aElem = Object.assign(document.createElement("a"),
|
||
{
|
||
href: project.link
|
||
}
|
||
);
|
||
liElem.appendChild(aElem);
|
||
if (displayImages) {
|
||
const imgElem = Object.assign(document.createElement("img"),
|
||
{
|
||
src: project.image ? imagesBasepath + project.image : imagesBasepath + "/farama_black.svg",
|
||
alt: `${project.name} logo`,
|
||
className: "farama-black-logo-invert"
|
||
}
|
||
);
|
||
aElem.appendChild(imgElem);
|
||
}
|
||
aElem.appendChild(document.createTextNode(project.name));
|
||
ulElem.appendChild(liElem);
|
||
}
|
||
return ulElem;
|
||
}
|
||
|
||
// Create menu with Farama projects by using the API at farama.org/api/projects.json
|
||
const createCORSRequest = (method, url) => {
|
||
let xhr = new XMLHttpRequest();
|
||
xhr.responseType = 'json';
|
||
|
||
if ("withCredentials" in xhr) {
|
||
xhr.open(method, url, true);
|
||
} else if (typeof XDomainRequest != "undefined") {
|
||
// IE8 & IE9
|
||
xhr = new XDomainRequest();
|
||
xhr.open(method, url);
|
||
} else {
|
||
// CORS not supported.
|
||
xhr = null;
|
||
}
|
||
return xhr;
|
||
};
|
||
|
||
const url = 'https://farama.org/api/projects.json';
|
||
const imagesBasepath = "https://farama.org/assets/images"
|
||
const method = 'GET';
|
||
let xhr = createCORSRequest(method, url);
|
||
|
||
xhr.onload = () => {
|
||
const jsonResponse = xhr.response;
|
||
const sections = {
|
||
"Core Projects": [],
|
||
"Mature Projects": {
|
||
"Documentation": [],
|
||
"Repositories": [],
|
||
},
|
||
"Incubating Projects": {
|
||
"Documentation": [],
|
||
"Repositories": [],
|
||
},
|
||
"Foundation": [
|
||
{
|
||
name: "About",
|
||
link: "https://farama.org/about"
|
||
},
|
||
{
|
||
name: "Standards",
|
||
link: "https://farama.org/project_standards",
|
||
},
|
||
{
|
||
name: "Donate",
|
||
link: "https://farama.org/donations"
|
||
}
|
||
]
|
||
}
|
||
|
||
// Categorize projects
|
||
Object.keys(jsonResponse).forEach(key => {
|
||
projectJson = jsonResponse[key];
|
||
if (projectJson.website !== null) {
|
||
projectJson.link = projectJson.website;
|
||
} else {
|
||
projectJson.link = projectJson.github;
|
||
}
|
||
if (projectJson.type === "core") {
|
||
sections["Core Projects"].push(projectJson)
|
||
} else if (projectJson.type == "mature") {
|
||
if (projectJson.website !== null) {
|
||
sections["Mature Projects"]["Documentation"].push(projectJson)
|
||
} else {
|
||
sections["Mature Projects"]["Repositories"].push(projectJson)
|
||
}
|
||
} else {
|
||
if (projectJson.website !== null) {
|
||
sections["Incubating Projects"]["Documentation"].push(projectJson)
|
||
} else {
|
||
sections["Incubating Projects"]["Repositories"].push(projectJson)
|
||
}
|
||
}
|
||
})
|
||
|
||
const menuContainer = document.querySelector(".farama-header-menu__body");
|
||
|
||
Object.keys(sections).forEach((key, i) => {
|
||
const sectionElem = Object.assign(
|
||
document.createElement('div'), {
|
||
className:'farama-header-menu__section',
|
||
}
|
||
)
|
||
sectionElem.appendChild(Object.assign(document.createElement('span'),
|
||
{
|
||
className:'farama-header-menu__section-title' ,
|
||
innerText: key
|
||
}
|
||
))
|
||
// is not a list
|
||
if (sections[key].constructor !== Array) {
|
||
const subSections = sections[key];
|
||
const subSectionContainerElem = Object.assign(
|
||
document.createElement('div'), {
|
||
className:'farama-header-menu__subsections-container',
|
||
style: 'display: flex'
|
||
}
|
||
)
|
||
Object.keys(subSections).forEach((subKey, i) => {
|
||
const subSectionElem = Object.assign(
|
||
document.createElement('div'), {
|
||
className:'farama-header-menu__subsection',
|
||
}
|
||
)
|
||
subSectionElem.appendChild(Object.assign(document.createElement('span'),
|
||
{
|
||
className:'farama-header-menu__subsection-title' ,
|
||
innerText: subKey
|
||
}
|
||
))
|
||
const ulElem = createProjectsList(subSections[subKey], key !== 'Foundation');
|
||
subSectionElem.appendChild(ulElem);
|
||
subSectionContainerElem.appendChild(subSectionElem);
|
||
})
|
||
sectionElem.appendChild(subSectionContainerElem);
|
||
} else {
|
||
const projects = sections[key];
|
||
const ulElem = createProjectsList(projects, true);
|
||
sectionElem.appendChild(ulElem);
|
||
}
|
||
menuContainer.appendChild(sectionElem)
|
||
});
|
||
}
|
||
|
||
xhr.onerror = function() {
|
||
console.error("Unable to load projects");
|
||
};
|
||
|
||
xhr.send();
|
||
</script>
|
||
|
||
|
||
<script>
|
||
const versioningConfig = {
|
||
githubUser: 'Farama-Foundation',
|
||
githubRepo: 'Gymnasium',
|
||
};
|
||
fetch('/main/_static/versioning/versioning_menu.html').then(response => {
|
||
if (response.status === 200) {
|
||
response.text().then(text => {
|
||
const container = document.createElement("div");
|
||
container.innerHTML = text;
|
||
document.querySelector("body").appendChild(container);
|
||
// innerHtml doenst evaluate scripts, we need to add them dynamically
|
||
Array.from(container.querySelectorAll("script")).forEach(oldScript => {
|
||
const newScript = document.createElement("script");
|
||
Array.from(oldScript.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value));
|
||
newScript.appendChild(document.createTextNode(oldScript.innerHTML));
|
||
oldScript.parentNode.replaceChild(newScript, oldScript);
|
||
});
|
||
});
|
||
} else {
|
||
console.warn("Unable to load versioning menu", response);
|
||
}
|
||
});
|
||
</script>
|
||
|
||
</body>
|
||
</html> |