{"componentChunkName":"component---src-templates-default-template-tsx","path":"/guides/rl-user-guide/","result":{"data":{"asciidoc":{"id":"f552a231-9b35-5f12-b14f-0863d3bb6782","html":"<div id=\"toc\" class=\"toc\">\n<div id=\"toctitle\">Table of Contents</div>\n<ul class=\"sectlevel1\">\n<li><a href=\"#_introduction\">Introduction</a></li>\n<li><a href=\"#_concepts_and_notations\">Concepts and Notations</a></li>\n<li><a href=\"#_examples\">Examples</a></li>\n<li><a href=\"#_workflow\">Workflow</a></li>\n<li><a href=\"#_single_state_issue\">Single state issue.</a></li>\n</ul>\n</div>\n<div class=\"sect1\">\n<h2 id=\"_introduction\">Introduction</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>The AI part of HIRO is based on <a href=\"https://en.wikipedia.org/wiki/Reinforcement_learning\">Reinforcement Learning algorithms</a>.</p>\n</div>\n<div class=\"paragraph\">\n<p>Reinforcement Learning is an aspect of Machine learning where an <em>agent</em> learns to behave in an <em>environment</em>, by performing certain <em>actions</em> and observing the <em>rewards</em>/results which it get from those actions.</p>\n</div>\n<div class=\"paragraph\">\n<p>The algorithm (agent) evaluates a current situation (<em>state</em>), takes an <em>action</em>, and receives feedback (<em>reward</em>) from the environment after each act. Positive feedback is a reward (in its colloquial meaning), and negative feedback is punishment for making a mistake. Reinforcement Learning algorithms learn how to act best through many attempts and failures.</p>\n</div>\n<div class=\"paragraph\">\n<p>The current implementation of Reinforcement Learning in HIRO uses so-called <em>Monte Carlo methods</em>.</p>\n</div>\n<div class=\"paragraph\">\n<p>To make Reinforcement Learning working successfully, we need to follow some basic rules when creating issues and writing KIs.</p>\n</div>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"_concepts_and_notations\">Concepts and Notations</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>First, lets introduce some RL-specific concepts and notations:</p>\n</div>\n<div class=\"imageblock\">\n<div class=\"content\">\n<img src=\"/7.0/images/rl-ug/reinforcement_loop.png\" alt=\"Reinforcement Learning Loop\">\n</div>\n</div>\n<div class=\"olist arabic\">\n<ol class=\"arabic\">\n<li>\n<p>An <em>Episode</em> in HIRO is called an Issue/Task.</p>\n</li>\n<li>\n<p>HIRO is an <em>Agent</em>.</p>\n</li>\n<li>\n<p>To resolve an issue, HIRO is interacting with an <em>Environment</em>.</p>\n</li>\n<li>\n<p>A <em>Time step</em> consists of choosing and executing a single KI.</p>\n</li>\n<li>\n<p>Therefore at each time step HIRO is taking some <em>Action</em> (executing some KI).</p>\n</li>\n<li>\n<p>At each time step HIRO is observing an environment. This particular observation at a particular time step is called a <em>State</em>.</p>\n</li>\n<li>\n<p>Once a KI is executed, HIRO can obtain some <em>Reward</em> (numerical value). It is not necessary that a reward is received at every time step.</p>\n</li>\n<li>\n<p>Once the reward is obtained, HIRO can (but not necessarily) observe a New State. When the issue is resolved in this step, this will be the <em>Final state</em>.</p>\n</li>\n<li>\n<p>HIRO takes steps until the end of the episode/issue is reached.</p>\n</li>\n<li>\n<p>The main goal is to maximize the <em>total reward</em> and to make sure we take the most promising steps (execute the best possible KIs).</p>\n</li>\n</ol>\n</div>\n<div class=\"paragraph\">\n<p>A short description of this process could look like this:</p>\n</div>\n<div class=\"literalblock\">\n<div class=\"content\">\n<pre>`state =&gt; action =&gt; reward =&gt; nextstate =&gt; action =&gt; reward =&gt; nextstate =&gt; … =&gt; action =&gt; reward =&gt; finalstate`</pre>\n</div>\n</div>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"_examples\">Examples</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>An example to illustrate this is the famous <a href=\"https://en.wikipedia.org/wiki/Tower_of_Hanoi\">Tower of Hanoi</a>.</p>\n</div>\n<div class=\"paragraph\">\n<p>Each disk can use one particular rod. That will be the <em>State</em>.\nWe can represent the state as a string:</p>\n</div>\n<div class=\"imageblock\">\n<div class=\"content\">\n<img src=\"/7.0/images/rl-ug/hanoi_start.png\" alt=\"Hanoi Towers start configuration\">\n</div>\n</div>\n<div class=\"paragraph\">\n<p>All three disks are currently using the first rod. This state can be represented as <code>HanoiPos_1_1_1</code></p>\n</div>\n<div class=\"imageblock\">\n<div class=\"content\">\n<img src=\"/7.0/images/rl-ug/hanoi_mid.png\" alt=\"Hanoi Towers mid configuration\">\n</div>\n</div>\n<div class=\"paragraph\">\n<p>Two disks are using the first rod and the third disk is using the second rod. This state can be represented as <code>HanoiPos_1_1_2</code></p>\n</div>\n<div class=\"paragraph\">\n<p>To train the system to finish the task as fast as possible, we assign a reward of \"-1\" for each time step.</p>\n</div>\n<div class=\"paragraph\">\n<p>The <em>Reward</em> is crucial for our system to work correctly. We can obtain the reward directly from the environment or from the KI. In the last case we should increment the <code>ogit/RL/totalReward</code> variable by this local reward.</p>\n</div>\n<div class=\"paragraph\">\n<p><code>ogit/RL/totalReward = ogit/RL/totalReward + local_reward</code></p>\n</div>\n<div class=\"paragraph\">\n<p>To penalize some action we can use a negative reward:</p>\n</div>\n<div class=\"paragraph\">\n<p><code>ogit/RL/totalReward = ogit/RL/totalReward + (-local_reward)</code></p>\n</div>\n<div class=\"paragraph\">\n<p>The main idea is to encourage good actions/KIs and penalize bad actions/KIs, when it is possible. This approach is critical for HIRO and its Learning Service to learn optimal behavior. If you don_t provide any reward during the whole Issue/Episode, the system will not be able to learn anything useful from this episode.</p>\n</div>\n<div class=\"paragraph\">\n<p>Another example is the MAZE environment:</p>\n</div>\n<div class=\"ulist\">\n<ul>\n<li>\n<p>The <em>Episode</em> starts at some initial point and ends when we move to the exit.</p>\n</li>\n<li>\n<p>The <em>Environment</em> is the whole gridworld/maze, where we can navigate our agent from one state/position to another.</p>\n</li>\n<li>\n<p>The <em>State</em> can be represented as the current coordinates (X, Y) or just as a number 1,2,3..,N. We always store the current state internally.</p>\n</li>\n<li>\n<p>The <em>Reward</em> is a numerical value, which is given to our agent via KIs. In case of maze, after each time step HIRO gives the reward \"-1\" to our agent. By maximizing the total reward we are able to find the shortest path to the exit (e.g. our final reward is -100, which means it’s 100 steps, and that’s better than the reward -200, which is 200 steps). We store all the rewards obtained for each time step.</p>\n</li>\n</ul>\n</div>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"_workflow\">Workflow</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>There are two main variables in the Reinforcement Learning workflow:</p>\n</div>\n<div class=\"ulist\">\n<ul>\n<li>\n<p><code>ogit/RL/totalReward</code></p>\n</li>\n<li>\n<p><code>ogit/RL/state</code></p>\n</li>\n</ul>\n</div>\n<div class=\"paragraph\">\n<p>Both are controlled via KIs.</p>\n</div>\n<div class=\"paragraph\">\n<p>a) Use <code>ogit/RL/totalReward</code> when you need to encourage or penalize a particular KI.</p>\n</div>\n<div class=\"paragraph\">\n<p>If you need to treat your KI as an agent_s step, you should explicitly add some reward inside of this KI.\ne.g. in the KI definition, inside of the <code>DO</code> section:</p>\n</div>\n<div class=\"paragraph\">\n<p><code>ogit/RL/totalReward = ogit/RL/totalReward - 1</code></p>\n</div>\n<div class=\"paragraph\">\n<p>This will add a reward of <code>-1</code> to this action/KI, it therefore penalizes this KI.</p>\n</div>\n<div class=\"paragraph\">\n<p>For example, in case of Maze or Tower of Hanoi, just use <code>ogit/RL/totalReward = ogit/RL/totalReward - 1</code> for each KI so that you penalize extra steps taken and make HIRO to learn the shortest path.</p>\n</div>\n<div class=\"paragraph\">\n<p>b) Use <code>ogit/RL/state</code> when you need to set or change your current state.</p>\n</div>\n<div class=\"paragraph\">\n<p>Defining a meaningful state can be tricky. You need to think about what combination of environment parameters can be used as a state. For example, in a Maze environment a state can be just a X and Y coordinates (e.g. a string “22_15” which represents x=22 and y=15).\nIn Tower of Hanoi this will be <code>HanoiPos_1_1_1</code>, where each \"1\" represents a disk using the first rod.</p>\n</div>\n<div class=\"paragraph\">\n<p>Example of a KI using the Reinforcement Learning system:</p>\n</div>\n<div class=\"listingblock\">\n<div class=\"content\">\n<pre class=\"highlight\"><code class=\"language-elixir\" data-lang=\"elixir\">ki\n  id: \"HN3_R1\"\n  version: \"0@rs\"\n  description: \"Move disc 3 to position 1.\"\non\n  ogit/_id\nwhen\n  MakeMove == \"Hanoi 6.2\"\n  DiskPos[1] as DP1\n  DiskPos[2] as DP2\n  DiskPos[3] != 1\n  StepCounter &lt; 1000\n  ogit/RL/totalReward\n  ogit/RL/state\ndo\n  DiskPos[3] = 1\n  ogit/RL/state = \"HanoiPos_${DP1}_${DP2}_1\"\n  ogit/RL/totalReward = ogit/RL/totalReward - 1\n  StepCounter = StepCounter + 1</code></pre>\n</div>\n</div>\n</div>\n</div>\n<div class=\"sect1\">\n<h2 id=\"_single_state_issue\">Single state issue.</h2>\n<div class=\"sectionbody\">\n<div class=\"paragraph\">\n<p>Sometimes there is only one state, depending on the issue. In this case the Learning Service will optimize for the number of KIs which were executed during this issue. It means that the Learning Service will try to choose those KIs which will help us to finish the issue faster, with a lower number of timesteps. Another approach is to choose only KIs which produce higher rewards.</p>\n</div>\n<div class=\"paragraph\">\n<p>A good example of a single state issue is a <a href=\"https://en.wikipedia.org/wiki/Multi-armed_bandit\">Multi-armed bandit problem</a>.\nThe state is always the same, but the actions are different (one action for each bandit). The goal is to choose the best actions and get the highest total reward from these actions.</p>\n</div>\n</div>\n</div>","document":{"main":"Learning Service Guide","title":"Learning Service Guide","subtitle":""},"fields":{"toc":true,"location":["guides","rl-user-guide"]}},"sidebarYaml":{"id":"6d066bdd-c982-5a69-b909-a31e6fc044e0","showIndex":true}},"pageContext":{"id":"f552a231-9b35-5f12-b14f-0863d3bb6782","parent":"guides"}},"staticQueryHashes":["1010459453","1010459453","2356112386","2356112386","2603905930","2603905930","3026652197","3026652197","3167850324","3167850324","63159454","63159454"]}