{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "## Welcome to StableRLS\n", "This first tutorial shows how to:\n", "1. import the package\n", "2. create a FMU from your simulink model\n", "3. create and simulate the environment\n", "\n", "The basic config file has three sections: General, FMU and Reinforcement Learning. By default the parameters of all sections will be available within the environment class. The documentation provides more information about all available config options. To get started we need to set:\n", "- FMU_path : location the FMU is stored. If the FMU is created this is also used as target location.\n", "- stop_time : when the time is reached the simulation/ episode is terminated.\n", "- dt : we run a fixed step simulation and this is the timestep.\n", "\n", "---\n", "\n", "```\n", "[General]\n", "\n", "[FMU]\n", "FMU_path = 00-Simulink_Linux.fmu\n", "stop_time = 1\n", "dt = 0.5\n", "\n", "[Reinforcement Learning]\n", "```" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "# this contains the environment class\n", "import stablerls.gymFMU as gymFMU\n", "# this will read our config file\n", "import stablerls.configreader as cfg_reader\n", "\n", "import numpy as np\n", "import logging\n", "# normally we dont recommend the Info logging but here its used for demonstation\n", "logging.basicConfig(level=logging.INFO)" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "For simplicity we already included the compiled FMU models for Linux and Windows. However, if you own Matlab you can compile the *.slx models on your own. If you want to compile the model you can keep the default FMU_path in the config file. Otherwise please change it to 00-Simulink_Windows.fmu or 00-Simulink_Linux.fmu depending on your operating system." ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Warning: The data dictionary 'BusSystem.sldd' was not found.\n", "Warning: The data dictionary 'BusSystem.sldd' was not found.\n", "Setting System Target to FMU Co-Simulation for model 'SimulinkExample00'.\n", "Setting Hardware Implementation > Device Type to 'MATLAB Host' for model 'SimulinkExample00'.\n", "### 'GenerateComments' is disabled for Co-Simulation Standalone FMU Export.\n", "\n", "Build Summary\n", "\n", "Top model targets built:\n", "\n", "Model Action Rebuild Reason \n", "===================================================================================================\n", "SimulinkExample00 Code generated and compiled. Code generation information file does not exist. \n", "\n", "1 of 1 models built (0 models already up to date)\n", "Build duration: 0h 0m 6.3136s\n", "### Model was successfully exported to co-simulation standalone FMU: '/home/cao2851/git/stablerls/examples/SimulinkExample00.fmu'.\n" ] } ], "source": [ "# First of all we have to read the config file\n", "config = cfg_reader.configreader('00-config.cfg')\n", "\n", "# if we want to we can compile the simulink model. \n", "# Matlab and Matlab Engine for python is required!\n", "if True:\n", " import stablerls.createFMU as createFMU\n", " createFMU.createFMU(config,'SimulinkExample00.slx')" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The FMU is available now and the default options of the StableRLS gymnasium environment are sufficient to run the first simulation." ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stderr", "output_type": "stream", "text": [ "INFO:stablerls.fmutools:Using: 00-Simulink_Linux.fmu\n", "INFO:stablerls.fmutools:Unzipped in /tmp/tmphq_9jw1t\n", "INFO:stablerls.fmutools:Found inputs - access them by the corresponding number:\n", "INFO:stablerls.fmutools: 0: Control.Sum2\n", "INFO:stablerls.fmutools: 1: Control.Sum1\n", "INFO:stablerls.fmutools: 2: Control.Terminated.Signal1\n", "INFO:stablerls.fmutools: 3: Control.Terminated.Signal2\n", "INFO:stablerls.fmutools:Found outputs - access them by the corresponding number:\n", "INFO:stablerls.fmutools: 0: Measurement.SumOut\n", "INFO:stablerls.gymFMU:Simulation done\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Action: [1 2 3 4]\n", "Observation: [3.]\n", "Reward: 1\n", "\n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "INFO:stablerls.fmutools:Close fmu and try deleting unzipdir\n" ] }, { "name": "stdout", "output_type": "stream", "text": [ "Action: [1 2 3 4]\n", "Observation: [3.]\n", "Reward: 1\n", "\n" ] } ], "source": [ "# create instance of the model\n", "env = gymFMU.StableRLS(config)\n", "\n", "# default reset call bevor the simulation starts\n", "obs = env.reset()\n", "\n", "# we wont change the action \n", "action = np.array([1,2,3,4])\n", "\n", "terminated = False\n", "truncated = False\n", "while not (terminated or truncated):\n", " observation, reward, terminated, truncated, info = env.step(action)\n", " print(f'Action: {action}\\nObservation: {observation}\\nReward: {reward}\\n')\n", " \n", "env.close()" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "The actions and outputs of each simulation/ episode are stored within the class. (Reset when we call the reset function)" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0., 0., 0., 0.],\n", " [1., 2., 3., 4.],\n", " [1., 2., 3., 4.]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "env.inputs" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[0.],\n", " [3.],\n", " [3.]])" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "env.outputs" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "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.9.16" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }