{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic Usage\n", "\n", "This example demonstrates the basic usage and interface of a control environment in EPyT-Control." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from IPython.display import display, HTML\n", "display(HTML('\\\"Open'))" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "%pip install epyt-control --quiet" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Import a pre-defined control environment where the chlorine injection is to be controlled\n", "from my_env import SimpleChlorineInjectionEnv" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Create new instance of the control environment\n", "env = SimpleChlorineInjectionEnv()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inspect the observation space (i.e. input to the agent/controller) by accessing the [observation_space](https://gymnasium.farama.org/api/env/#gymnasium.Env.observation_space) property:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(env.observation_space) # Observations: 34 dimension real-valued vector" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Inspect the observation space (i.e. input to the agent/controller) by accessing the [action_space](https://gymnasium.farama.org/api/env/#gymnasium.Env.action_space) property:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "print(env.action_space) # Action: A single scalar between 0 and 10000" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Reset the environment by calling the [reset()](https://epyt-control.readthedocs.io/en/stable/epyt_control.envs.html#epyt_control.envs.rl_env.RlEnv.reset) function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "obs, _ = env.reset()\n", "print(obs)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Execute some random actions by calling the [step()](https://epyt-control.readthedocs.io/en/stable/epyt_control.envs.html#epyt_control.envs.rl_env.RlEnv.step) function -- note that a random action can be generated by calling the [sample()](https://gymnasium.farama.org/api/spaces/#gymnasium.spaces.Space.sample) function of the environment's [action space](https://gymnasium.farama.org/api/env/#gymnasium.Env.action_space):" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Run some iterations -- note that autorest=True\n", "for _ in range(20):\n", " # Pick a random action\n", " act = env.action_space.sample()\n", "\n", " # Apply the action, get a reward (to be maximized) and\n", " # new observations (i.e. sensor readings)\n", " obs, reward, terminated, _, _ = env.step(act)\n", " print(f\"Action: {act}, Reward: {reward}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Do not forget to close the environment by calling the [close()](https://epyt-flow.readthedocs.io/en/stable/epyt_flow.gym.html#epyt_flow.gym.scenario_control_env.ScenarioControlEnv.close) function:" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "env.close()" ] } ], "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.10.16" } }, "nbformat": 4, "nbformat_minor": 2 }