<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title>Salah&#x27;s blog</title>
    <link rel="self" type="application/atom+xml" href="https://salaheddineghamri.github.io/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://salaheddineghamri.github.io"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2024-06-01T00:00:00+00:00</updated>
    <id>https://salaheddineghamri.github.io/atom.xml</id>
    <entry xml:lang="en">
        <title>Basic Q-learning algorithm</title>
        <published>2024-06-01T00:00:00+00:00</published>
        <updated>2024-06-01T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Salah Eddine Ghamri
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://salaheddineghamri.github.io/posts/post-8571/"/>
        <id>https://salaheddineghamri.github.io/posts/post-8571/</id>
        
        <content type="html" xml:base="https://salaheddineghamri.github.io/posts/post-8571/">&lt;h1 id=&quot;introduction&quot;&gt;Introduction&lt;&#x2F;h1&gt;
&lt;p&gt;Q-Learning is a popular reinforcement learning algorithm that belongs to the
class of value-based methods. It enables an agent to learn the optimal
action-selection policy by interacting with its environment and maximizing a
cumulative reward value. In order to fully understand this algorithm we will
implement a simple version of it step by step. Q-learning can be used in
various applications while go to goal application in a grid environment can
showcase its capability to be used in mobile robotics path finding tasks
similar to djikstra and A* algorithms.&lt;&#x2F;p&gt;
&lt;p&gt;Before engaging the topic of Q-learning and its intricacies, an overview of
reinforcement learning (RL) and its fundamental terminology is warranted.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;reinforcement-learning-rl-reinforcement-learning-is-a-type-of-machine&quot;&gt;Reinforcement learning (RL) reinforcement learning is a type of machine&lt;&#x2F;h2&gt;
&lt;p&gt;learning where the main goal is to make an agent learn to take correct
decisions in its environment. The idea behind it is strengthening or promoting
certain behaviors the agent would make in systematic way, hence the word
“reinforcement” is used. It remains a process of trial and error, where
consistent reward for good (successful) actions and penalties for bad
(unsuccessful) ones gradually guide the agent towards discovering the optimal
policy for achieving its goals.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;callout note&quot;&gt;
    
    &lt;div class=&quot;icon&quot;&gt;
        &lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;20&quot; height=&quot;20&quot;&gt;&lt;path d=&quot;M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM11 7H13V9H11V7ZM11 11H13V17H11V11Z&quot; fill=&quot;currentColor&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
    &lt;&#x2F;div&gt;
    &lt;div class=&quot;content&quot;&gt;
        
        &lt;p&gt;&lt;strong&gt;A glimpse of history&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
        
        &lt;p&gt;Reinforcement learning’s history intertwines two threads:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;strong&gt;Trial-and-error learning&lt;&#x2F;strong&gt;: Rooted in animal psychology, it explores how
actions with positive outcomes are reinforced and repeated. Early AI
researchers like Marvin Minsky and Donald Michie experimented with this
concept.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;strong&gt;Optimal control&lt;&#x2F;strong&gt;: Focused on designing controllers to minimize system
behavior over time, this thread involved mathematical concepts like value
functions and Bellman’s equation. Dynamic programming emerged as a key method.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;These threads converged in the late 1980s, thanks to contributions like
temporal-difference learning and Q-learning, marking the birth of modern
reinforcement learning as we know it.&lt;sup&gt;&lt;a href=&quot;#1&quot;&gt;[1]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;

    &lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;h2 id=&quot;terminology&quot;&gt;Terminology&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;strong&gt;agent&lt;&#x2F;strong&gt;: the learner or the decision maker it takes actions and receives
rewards. In robotic context, it is the robot itself or a subsystem of the robot
that will perform a defined task.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;environment&lt;&#x2F;strong&gt;: it’s the context where the agent operates. it can be real or
virtual or any system able to provide information of its state and reacts to
the agent actions generating new states and rewards.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;action&lt;&#x2F;strong&gt;: a choice made by the agent that influences the environment altering
its state.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;action space&lt;&#x2F;strong&gt;: all possible actions the agent can take in a given state.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;state&lt;&#x2F;strong&gt;: a snapshot of the environment at a specific point in time. it has
all information agent need to make an action.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;state space&lt;&#x2F;strong&gt;: all possible states that the agent can be in from the current state.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;policy&lt;&#x2F;strong&gt;: a strategy or mapping that guides the agent’s decision-making
process. it determines which action the agent should take in each state to
maximize cumulative rewards.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;reward&lt;&#x2F;strong&gt;: a scalar feedback signal or a numerical score from the environment
that indicates how good or bad the action was in a particular state. positive
rewards encourage desired behavior, while negative rewards discourage
undesirable behavior.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;cumulative rewards&lt;&#x2F;strong&gt;: a total sum of rewards received by an agent over a
sequence of actions or time steps. It represents the overall performance of the
agent in achieving its goals.&lt;&#x2F;p&gt;
&lt;p&gt;Alright, buckle up and let’s dive into it!&lt;&#x2F;p&gt;
&lt;h1 id=&quot;q-learning&quot;&gt;Q-learning&lt;&#x2F;h1&gt;
&lt;p&gt;So what is Q-learning ?&lt;&#x2F;p&gt;
&lt;p&gt;It is an algorithm relatively simple, the idea behind it is to keep track of all accumulative
rewards received by taking any action in any given state.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;callout note&quot;&gt;
    
    &lt;div class=&quot;icon&quot;&gt;
        &lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;20&quot; height=&quot;20&quot;&gt;&lt;path d=&quot;M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM11 7H13V9H11V7ZM11 11H13V17H11V11Z&quot; fill=&quot;currentColor&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
    &lt;&#x2F;div&gt;
    &lt;div class=&quot;content&quot;&gt;
        
        &lt;p&gt;&lt;strong&gt;What does Q mean ?&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
        
        &lt;p&gt;The Q there may mean “quality” or maybe it’s just a mathematical variable name chosen by Chris Watkins.&lt;sup&gt;&lt;a href=&quot;#2&quot;&gt;[2]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;

    &lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;“Q” refers to the function “Q” that the algorithm computes to represent the rewards for an action
taken by the agent. To keep track of all possible rewards the agent
maintains a state-action table named “Q-table” contains what is called “Q-values” for each (stat, action) set (cell).
This table is updated through the episodes in the training phase to accumulate all possible rewards.
The best action to take for a given state is then the action that has the maximum Q-value among all other actions.&lt;&#x2F;p&gt;
&lt;p&gt;In nutshell, The Q-table stores a score for taking each action in each state of the agent.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;q-table&quot;&gt;Q-table&lt;&#x2F;h2&gt;
&lt;p&gt;The Q-table represents the expected cumulative reward for taking each possible action in each state.
The Q-table is initialized arbitrarily, and its values are updated as the agent interacts with its environment.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;updating-q-table-and-bellman-optimality-equation&quot;&gt;Updating Q-table and Bellman optimality equation&lt;&#x2F;h2&gt;
&lt;p&gt;The most important aspect of Q-learning is the update of the Q-values in the Q-table.
This update process will maximize the Q-value of some actions for each specific state of the agent.
This eventually realizes the learning nature of this algorithm and we will get an optimal policy
with actions that have the maximum Q-value in the table.&lt;&#x2F;p&gt;
&lt;p&gt;The update of the Q-values will be according to the following formula:&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;callout equation&quot;&gt;
    
    &lt;div class=&quot;icon&quot;&gt;
        &lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;&lt;!-- Uploaded to: SVG Repo, www.svgrepo.com, Generator: SVG Repo Mixer Tools --&gt;
&lt;svg width=&quot;20&quot; height=&quot;20&quot; viewBox=&quot;0 0 48 48&quot; xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot;&gt;&lt;defs&gt;&lt;style&gt;.a{fill:none;stroke:#000000;stroke-linecap:round;stroke-linejoin:round;}&lt;&#x2F;style&gt;&lt;&#x2F;defs&gt;&lt;path class=&quot;a&quot; d=&quot;M40.5,5.5H7.5a2,2,0,0,0-2,2v33a2,2,0,0,0,2,2h33a2,2,0,0,0,2-2V7.5A2,2,0,0,0,40.5,5.5Z&quot;&#x2F;&gt;&lt;line class=&quot;a&quot; x1=&quot;35.3586&quot; y1=&quot;11.4476&quot; x2=&quot;27.9796&quot; y2=&quot;21.2249&quot;&#x2F;&gt;&lt;line class=&quot;a&quot; x1=&quot;35.3586&quot; y1=&quot;21.2249&quot; x2=&quot;27.9796&quot; y2=&quot;11.4476&quot;&#x2F;&gt;&lt;path class=&quot;a&quot; d=&quot;M24.1428,33.4439a6.017,6.017,0,0,1-5.2845,3.1085h0a6.2353,6.2353,0,0,1-6.2169-6.217v-4.041a6.2352,6.2352,0,0,1,6.2169-6.217h0a6.2352,6.2352,0,0,1,6.217,6.217V28.47H12.6414&quot;&#x2F;&gt;&lt;&#x2F;svg&gt;

    &lt;&#x2F;div&gt;
    &lt;div class=&quot;content&quot;&gt;
        
        &lt;p&gt;&lt;strong&gt;Q-learning formula&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
        
        &lt;br&gt;
        &lt;br&gt;
        &lt;div style=&quot;text-align: center;&quot;&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;Q^{new}(s, a) = Q(s, a) + \alpha \left[ R(s, a) + \gamma \max_{a&#x27; \in A} Q&#x27;(s&#x27;, a&#x27;) - Q(s, a) \right]&lt;&#x2F;script&gt;
&lt;&#x2F;div&gt;
&lt;br&gt;
&lt;br&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;Q^{new}(s, a)&lt;&#x2F;script&gt;
: new Q value state s and action a.&lt;br&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;Q(s, a)&lt;&#x2F;script&gt;
: current Q value.&lt;br&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;\alpha&lt;&#x2F;script&gt;
: learning rate.&lt;br&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;R(s, a)&lt;&#x2F;script&gt;
: reward after taking that action a at the state s.&lt;br&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;\gamma&lt;&#x2F;script&gt;
: discount rate.&lt;br&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;\max_{a&#x27; \in A} Q&#x27;(s&#x27;, a&#x27;)&lt;&#x2F;script&gt;
: maximum expectued future reward given the new state s&#x27; and all its possible actions.&lt;br&gt;
It is performed in a way agent will receive assessment of the effect of taking an action through time.&lt;br&gt;
    &lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;This formula is the core of the Q-learning and what realizes the learning.
At glance, it is not clear immediately how this formula can establish a learning pattern.
However, when taking its recursive nature into account it will make a greater sense.&lt;&#x2F;p&gt;
&lt;p&gt;The formula establishes a relationship between the current state and all
possible future states. It is a form of a temporal difference (TD) which is
another reinforcement-learning method that enables bootstrapping from the
current estimates to update the value function.&lt;&#x2F;p&gt;
&lt;p&gt;If only our knowledge was perfect in the current state we would knew exactly
which action to take to get a maximum reward. this is not possible for the
environements where the reward comes only after performing a specific sequence
of actions through time. Such as in mobile robotics applications, namely
go-to-goal, where we receive the reward only when the robot reaches its final
goal point after a long journey.&lt;&#x2F;p&gt;
&lt;p&gt;For such environements, we lack the forseight of how current action impact our
mission goals ahead in the future. Our indicator if we had the full knowledge
would be the accumulation of immediate rewards for taking the correct decision
in each step (decision point).&lt;&#x2F;p&gt;
&lt;p&gt;The following diagram illustrates this idea. Each correct decision in the
decision points will be immediately rewarded by 1. G is representing the
accumulative future rewards at each decision point. That is a good metric, it
allows us to take the correct decision at a specific decision point taking its
implication for the future also in account. A transition (action) with higher G
is a correct action not only now but also in the future. Wherase when we lack
this knowledge of how much rewards we will receive, no indicators (metric) that
can help us, thus the return at each step is unknown G=?.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;img src=&quot;..&#x2F;images&#x2F;TD_idea.png&quot; alt=&quot;time difference explanation&quot; &#x2F;&gt;&lt;&#x2F;p&gt;
&lt;p&gt;let’s consider that we know at each step of time all immediate future rewards
we can calculate the return G, for an episode start from t to T, as follow&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;G_t = r_ {t+1} + \gamma r_{t+2} + \gamma^2 r_{t+3} + \dots + \gamma^{T-t-1} r_{T}&lt;&#x2F;script&gt;
&lt;p&gt;or&lt;&#x2F;p&gt;
&lt;p&gt;&lt;script type=&quot;math&#x2F;tex&quot;&gt;G_t = r_ {t+1} + \gamma G_{t+1}&lt;&#x2F;script&gt;
 (useful for later)&lt;&#x2F;p&gt;
&lt;p&gt;the gamma is just a discount factor giving more importance to the immediate
rewards over the late ones.
it must be chosen between 0 and 1.&lt;&#x2F;p&gt;
&lt;p&gt;We can associate each state of the agent with a function V representing how
much being in this state is rewarding.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;Now, the question is how to calculate the V for each state ?&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Since G_t is the best metric we have to make correct decisions, it is clear
that this function V can be an approximation of G_t at each S_t.&lt;&#x2F;p&gt;
&lt;p&gt;We simply start with any rough estimation then by iterating through the episodes we start
making the value V_S equal to the return &lt;script type=&quot;math&#x2F;tex&quot;&gt;G_t&lt;&#x2F;script&gt;

this can be represented mathematically using the gradient descent formulation:&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;V_{S_{t}} = V_{S_{t}} + \alpha (G_t - V_{S_{t}})&lt;&#x2F;script&gt;
&lt;p&gt;This is a form of a gradient descent where alpha (learning rate) is a correcting factor for the error
&lt;script type=&quot;math&#x2F;tex&quot;&gt;G_t - V_{S_{t}}&lt;&#x2F;script&gt;
 with each episode this error will decrease
towards zero and alpha controls how fast that will be.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;But the problem is how to know at each t the return G_t?&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
&lt;p&gt;When it comes to solving this problem, research has revealed two effective approaches&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;The obvious one is we calculate G at the end of each learning episode and use it in the next learning episode.
This is the Monte Carlo method. Where G will be averaged through the episodes giving a closer estimate to its real value.&lt;&#x2F;li&gt;
&lt;li&gt;We bootstrap and this is the TD method.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;Bootstrapping is a mouthful word which means using information from future
states to update our estimates of the current state.&lt;&#x2F;p&gt;
&lt;p&gt;Since knowing G return is not possible without completing the whole episode, in
TD we start with an estimate and bootstrap it with the future accumulative
reward when exploring the next state. This means we gain a partial
knowledge that we use to enhance our decision making each time we explore a new state.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;callout note&quot;&gt;
    
    &lt;div class=&quot;icon&quot;&gt;
        &lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;20&quot; height=&quot;20&quot;&gt;&lt;path d=&quot;M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM11 7H13V9H11V7ZM11 11H13V17H11V11Z&quot; fill=&quot;currentColor&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
    &lt;&#x2F;div&gt;
    &lt;div class=&quot;content&quot;&gt;
        
        &lt;p&gt;&lt;strong&gt;temporal-difference in action&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
        
        &lt;p&gt;Suppose you wish to predict the weather for Saturday, and you have some model
that predicts Saturday’s weather, given the weather of each day in the week. In
the standard case, you would wait until Saturday and then adjust all your
models. However, when it is, for example, Friday, you should have a pretty good
idea of what the weather would be on Saturday – and thus be able to change,
say, Saturday’s model before Saturday arrives. &lt;sup&gt;&lt;a href=&quot;#2&quot;&gt;[5]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;&lt;&#x2F;p&gt;

    &lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;So instead of making a value function V_S target G, we instead target an estimation
called the TD target:&lt;&#x2F;p&gt;
&lt;script type=&quot;math&#x2F;tex&quot;&gt;TD_{target} = R_{t+1} + \gamma V_{S_{t+1}}&lt;&#x2F;script&gt;
&lt;p&gt;it can be seen that this TD target is just the &lt;script type=&quot;math&#x2F;tex&quot;&gt;G_{t}&lt;&#x2F;script&gt;
 under the assumption
that &lt;script type=&quot;math&#x2F;tex&quot;&gt;G_t = V_{S_{t}}&lt;&#x2F;script&gt;
 and while exposing the &lt;script type=&quot;math&#x2F;tex&quot;&gt;G_{t+1}&lt;&#x2F;script&gt;
 term.&lt;&#x2F;p&gt;
&lt;p&gt;with each episode &lt;script type=&quot;math&#x2F;tex&quot;&gt;V_S&lt;&#x2F;script&gt;
 will get closer and closer to the TD target while the TD
target will get closer and closer to the real &lt;script type=&quot;math&#x2F;tex&quot;&gt;G_t&lt;&#x2F;script&gt;
. This is a simpliefied
explanation how learning is happening in reinforcement learning.&lt;&#x2F;p&gt;
&lt;p&gt;In TD, the rewards are tracked throght the value function which gives each
state an estimate of the accumulative future rewards. On the other hand,
Q-learning is using action-value function to tracking these estimates. The
magical things here is that it’s enough to change the V value function with the Q
action state value and the formula still holds. However we will need a table to
track all the actions through all the states and this is the Q-table.&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;callout note&quot;&gt;
    
    &lt;div class=&quot;icon&quot;&gt;
        &lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 24 24&quot; width=&quot;20&quot; height=&quot;20&quot;&gt;&lt;path d=&quot;M12 22C6.47715 22 2 17.5228 2 12C2 6.47715 6.47715 2 12 2C17.5228 2 22 6.47715 22 12C22 17.5228 17.5228 22 12 22ZM12 20C16.4183 20 20 16.4183 20 12C20 7.58172 16.4183 4 12 4C7.58172 4 4 7.58172 4 12C4 16.4183 7.58172 20 12 20ZM11 7H13V9H11V7ZM11 11H13V17H11V11Z&quot; fill=&quot;currentColor&quot;&gt;&lt;&#x2F;path&gt;&lt;&#x2F;svg&gt;
    &lt;&#x2F;div&gt;
    &lt;div class=&quot;content&quot;&gt;
        
        &lt;p&gt;&lt;strong&gt;Note&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
        
        &lt;p&gt;Principle of Optimality: An optimal policy has the property that whatever the
initial state and initial decision are, the remaining decisions must constitute
an optimal policy with regard to the state resulting from the first decision.
(See Bellman, 1957, Chap. III.3.&lt;&#x2F;p&gt;

    &lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;I hope the formula is more clear with this explanation. Let’s see how to apply it on
go to goal application in mobile robotics.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;exploring-vs-expertise-with-epsilon-greedy-mechanism&quot;&gt;Exploring vs Expertise with epsilon greedy mechanism&lt;&#x2F;h2&gt;
&lt;h2 id=&quot;q-learning-hyper-parameters&quot;&gt;Q-learning hyper-parameters&lt;&#x2F;h2&gt;
&lt;p&gt;The Q learning has parameters that needs to be tuned for optimal learning.
alpha: the learning rate, this parameter controls how fast we will converge to optimal solution.
gamma: the discount factor, controls how much we count immediate rewards over final rewards. high factor means we are focusing on immediate rewards.
epsilon: the epsilon-greedy rate, controls how much much agent explores new states even if they are not optimal.
num_episodes: not related to Q learning specifically but to RL in general, each epiode finished by the agent reaching the goal.&lt;&#x2F;p&gt;
&lt;h1 id=&quot;implementation-in-python&quot;&gt;Implementation in python&lt;&#x2F;h1&gt;
&lt;h2 id=&quot;simple-grid-environment&quot;&gt;Simple grid environment&lt;&#x2F;h2&gt;
&lt;p&gt;To simplify the go to goal task. we use a small grid of small number of cells.
Each cell represents the position that can the robot be in. The robot is the
agent in the reinforcement learning context. The robot position is the state of
the agent.&lt;&#x2F;p&gt;
&lt;p&gt;Obstacles can be added to the grid by using the following convention:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;0 value: the cell is free and the robot can go there.&lt;&#x2F;li&gt;
&lt;li&gt;1 value: the cell has an obstacle and the robot is forbidden from occupying this cell.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;The state space in this case is all the cells of this grid that they are not occupied by an obstacles.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Define the grid world environment&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Obstacles are designated using &amp;quot;1&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;grid&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    [&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# start and goal points&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;start&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;goal&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;3&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;action-space&quot;&gt;Action space&lt;&#x2F;h2&gt;
&lt;p&gt;For simplification reasons we consider that the robot is only able to move in 4
directions: up, down, right, left. So we can deduce the action space of 4
actions: moving up, moving down, moving right and moving left.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Define the possible actions&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;actions&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; [(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;), (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;,&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;span&gt;)]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # down  # up  # left  # right&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;q-table-1&quot;&gt;Q-table&lt;&#x2F;h2&gt;
&lt;p&gt;We use a python dict to track a Q values of all states &#x2F; action space.&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Define the Q-table&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;Q&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; i&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;    for&lt;&#x2F;span&gt;&lt;span&gt; j&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;5&lt;&#x2F;span&gt;&lt;span&gt;):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;        for&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; actions:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            Q[(i, j), a]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;training&quot;&gt;Training&lt;&#x2F;h2&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Define the hyperparameters&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;alpha&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0.1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # learning rate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;gamma&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0.9&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # discount factor&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;epsilon&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0.1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # epsilon-greedy rate&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;num_episodes&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1000&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  # number of episodes&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Q-Learning algorithm&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; episode&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; range&lt;&#x2F;span&gt;&lt;span&gt;(num_episodes):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; start&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; False&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;    while not&lt;&#x2F;span&gt;&lt;span&gt; done:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        # Choose an action using epsilon-greedy policy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        #&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; NOTE&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;: import numpy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; np.random.random()&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; epsilon:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            action&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; random.choice(actions)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;        else&lt;&#x2F;span&gt;&lt;span&gt;:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            q_max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; max&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                Q[state, a]&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                for&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; actions&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                    state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; a[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                    and&lt;&#x2F;span&gt;&lt;span&gt; state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; a[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                    and&lt;&#x2F;span&gt;&lt;span&gt; state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; a[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                    and&lt;&#x2F;span&gt;&lt;span&gt; state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; a[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                and&lt;&#x2F;span&gt;&lt;span&gt; grid[state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; a[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]][state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; a[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            action&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span&gt;((a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; actions&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; Q[state, a]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; q_max))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        # Check if the action leads to an invalid state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;            not&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;                state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                and&lt;&#x2F;span&gt;&lt;span&gt; state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                and&lt;&#x2F;span&gt;&lt;span&gt; state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;gt;=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 0&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;                and&lt;&#x2F;span&gt;&lt;span&gt; state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 5&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;            or&lt;&#x2F;span&gt;&lt;span&gt; grid[state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]][state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        ):&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;            continue&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        # Take the action and observe the reward and new state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        next_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;], state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; action[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;])&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        # lowest reward&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        reward&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; = -&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;        if&lt;&#x2F;span&gt;&lt;span&gt; next_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; goal:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;            # highest reward&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            reward&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; 100&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            done&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; True&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        # update the Q-table&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        Q[state, action]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt; alpha)&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; Q[state, action]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; alpha&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;            reward&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; gamma&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; *&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; max&lt;&#x2F;span&gt;&lt;span&gt;(Q[next_state, a]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; actions)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;        # update next state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; next_state&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;optimal-policy&quot;&gt;Optimal policy&lt;&#x2F;h2&gt;
&lt;p&gt;We can show the learned policy as follow:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Print the optimal policy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;policy&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; {}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;for&lt;&#x2F;span&gt;&lt;span&gt; state, action&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; Q:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    q_max&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; max&lt;&#x2F;span&gt;&lt;span&gt;(Q[state, a]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; actions)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    action&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt; next&lt;&#x2F;span&gt;&lt;span&gt;((a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; for&lt;&#x2F;span&gt;&lt;span&gt; a&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; in&lt;&#x2F;span&gt;&lt;span&gt; actions&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; if&lt;&#x2F;span&gt;&lt;span&gt; Q[state, a]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; ==&lt;&#x2F;span&gt;&lt;span&gt; q_max))&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    policy[state]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; action&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;&amp;quot;learned policy: &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, policy)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;path-from-optimal-policy&quot;&gt;Path from optimal policy&lt;&#x2F;h2&gt;
&lt;p&gt;in the following is how to extract the path from the learned policy&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;python&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;# Build path from learned policy&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;path&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; []&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;current_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; start&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;path.append(start)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;while&lt;&#x2F;span&gt;&lt;span&gt; current_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; !=&lt;&#x2F;span&gt;&lt;span&gt; goal:&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    current_state&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; =&lt;&#x2F;span&gt;&lt;span&gt; (&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        current_state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; policy[current_state][&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;        current_state[&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;]&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; +&lt;&#x2F;span&gt;&lt;span&gt; policy[current_state][&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;],&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    )&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;    path.append(current_state)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;print&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;&amp;quot;path to goal : &amp;quot;&lt;&#x2F;span&gt;&lt;span&gt;, path)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h1 id=&quot;conclusion&quot;&gt;Conclusion&lt;&#x2F;h1&gt;
&lt;p&gt;In the end, the robot was able to learn the task go to goal while avoiding
obstacles. Since the learned policy is the optimal the calculated path will be
the shortest.&lt;&#x2F;p&gt;
&lt;p&gt;In a grid environment, the simple QN can replace A* and Djikstra algorithms,
however they are more efficient and using less memory due that Q learning is
keeping track of all possible state action Q values in a table.&lt;&#x2F;p&gt;
&lt;p&gt;The learned policy may not be the optimal due to the update correlations and
changing the start and target may result in wrong decisions.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;limitation-of-q-learning&quot;&gt;limitation of Q learning&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;While Q learning works well for small dimension problem, it challenging to learn optimal policy in problem where the state action space is
large. This is known as the curse of dimensionality.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;li&gt;
&lt;p&gt;Q learning is not suitable for continuous action spaces. The version of “go to gaol” problem we
showed here is highly simplified version of the mobile robot environmenent. In
reality the robot action space is more continuous rather then discret. This characteristic makes Q learning
impractical for real world application where the actions space must be fine tuned as continuous space.&lt;&#x2F;p&gt;
&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;1&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;1&lt;&#x2F;sup&gt;
&lt;p&gt;history of reinforcement learning &lt;a rel=&quot;external&quot; href=&quot;http:&#x2F;&#x2F;incompleteideas.net&#x2F;book&#x2F;ebook&#x2F;node12.html&quot;&gt;link&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;2&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;2&lt;&#x2F;sup&gt;
&lt;p&gt;first appearance of Q-learning algorithm &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.cs.rhul.ac.uk&#x2F;~chrisw&#x2F;new_thesis.pdf&quot;&gt;link&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;3&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;3&lt;&#x2F;sup&gt;
&lt;p&gt;step by step demonstation of Q-learning algorithm &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;towardsdatascience.com&#x2F;reinforcement-learning-explained-visually-part-4-q-learning-step-by-step-b65efb731d3e&quot;&gt;link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;4&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;4&lt;&#x2F;sup&gt;
&lt;p&gt;Bellman optimality equation &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;www.slideshare.net&#x2F;slideshow&#x2F;ai-introduction-to-bellman-equations&#x2F;79595354&quot;&gt;link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
&lt;div class=&quot;footnote-definition&quot; id=&quot;5&quot;&gt;&lt;sup class=&quot;footnote-definition-label&quot;&gt;5&lt;&#x2F;sup&gt;
&lt;p&gt;temporal-difference wikipedia &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Temporal_difference_learning&quot;&gt;link&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;div&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Using Cmake defined variables in c++ code</title>
        <published>2023-02-11T00:00:00+00:00</published>
        <updated>2023-02-11T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Salah Eddine Ghamri
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://salaheddineghamri.github.io/posts/post-8570/"/>
        <id>https://salaheddineghamri.github.io/posts/post-8570/</id>
        
        <content type="html" xml:base="https://salaheddineghamri.github.io/posts/post-8570/">&lt;p&gt;Cmake is a wonderful tool for generating building systems for projects. One of the applications that Cmake facilitates, is configuring the project version from within a &lt;code&gt;CMakeLists.txt&lt;&#x2F;code&gt; file.&lt;&#x2F;p&gt;
&lt;p&gt;This can be easily done by defining the version components as either, cmake variables or as definitions to the compiler command line. This article will give an overview of both methods.&lt;&#x2F;p&gt;
&lt;p&gt;Semantic versioning which is one of the popular methods used to version software gives the project a version number composed of 3 components: &lt;code&gt;Major&lt;&#x2F;code&gt;, &lt;code&gt;Minor&lt;&#x2F;code&gt; and the &lt;code&gt;patch&lt;&#x2F;code&gt;. Details on the meaning of each number go out of this article’s scope but it can be checked on this &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;semver.org&#x2F;&quot;&gt;link&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;The simplest way of defining a project version is by defining a header file containing the version components as global variables. This header file can then be included to make variables available for various uses. Doing things this way leaves us with the problem of maintaining these variables at the code level instead of aggregating this task to the build system.&lt;&#x2F;p&gt;
&lt;p&gt;So how can we avoid defining the project version in multiple locations? In the code (if we need it there), in the build system or the source packaging.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;1-cmake-variables&quot;&gt;1. Cmake variables&lt;&#x2F;h2&gt;
&lt;p&gt;The idea behind the first method is to use &lt;code&gt;configure_file()&lt;&#x2F;code&gt; command to copy an input file to another location and modify its content. The configuration file can be a &lt;code&gt;.h&lt;&#x2F;code&gt; header file that contains the definition of the project version. All variables with the decoration &lt;code&gt;@cmake_variable@&lt;&#x2F;code&gt; will be replaced with their values given in the &lt;code&gt;CMakeLists.txt&lt;&#x2F;code&gt;file.&lt;&#x2F;p&gt;
&lt;p&gt;Cmake variables can be defined in the &lt;code&gt;CMakeLists.txt&lt;&#x2F;code&gt; using the &lt;code&gt;set()&lt;&#x2F;code&gt; command. For our tutorial, it is sufficient to have them of normal type. The following &lt;code&gt;CMakeLists.txt&lt;&#x2F;code&gt; file shows how to set them:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;cmake_minimum_required&lt;&#x2F;span&gt;&lt;span&gt; (VERSION &lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;3.2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;project&lt;&#x2F;span&gt;&lt;span&gt; (MyProject)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Add the variables&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(VERSION_MAJOR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(VERSION_MINOR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;set&lt;&#x2F;span&gt;&lt;span&gt;(VERSION_PATCH &lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# generate version.h replacing all @VARIABLE@&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;configure_file&lt;&#x2F;span&gt;&lt;span&gt; (version.h.in ${CMAKE_CURRENT_SOURCE_DIR}&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&#x2F;&lt;&#x2F;span&gt;&lt;span&gt;version.h @ONLY)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;add_executable&lt;&#x2F;span&gt;&lt;span&gt; (${PROJECT_NAME} main.cpp)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;The command &lt;code&gt;configure_file()&lt;&#x2F;code&gt; will copy the content of the file &lt;code&gt;version.h.in&lt;&#x2F;code&gt; and creates a new file called &lt;code&gt;version.h&lt;&#x2F;code&gt; in &lt;code&gt;${CMAKE_CURRENT_SOURCE_DIR}&lt;&#x2F;code&gt; which is the location where the source files of the project are located. Since we added the argument &lt;code&gt;@ONLY&lt;&#x2F;code&gt; to the &lt;code&gt;configure_file()&lt;&#x2F;code&gt; call. The content of &lt;code&gt;version.h&lt;&#x2F;code&gt; will be similar to &lt;code&gt;version.h.in&lt;&#x2F;code&gt; except that all references of the form &lt;code&gt;@variable@&lt;&#x2F;code&gt; will be replaced with what has been set in the &lt;code&gt;CMakeLists.txt&lt;&#x2F;code&gt;. Of course, this happens during the build (running &lt;code&gt;make&lt;&#x2F;code&gt; command).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;configure_file()&lt;&#x2F;code&gt; can be checked &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;cmake.org&#x2F;cmake&#x2F;help&#x2F;latest&#x2F;command&#x2F;configure_file.html&quot;&gt;here&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;@ONLY&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;p&gt;Restrict variable replacement to references of the form &lt;code&gt;@VAR@&lt;&#x2F;code&gt;. This is useful for configuring scripts that use &lt;code&gt;${VAR}&lt;&#x2F;code&gt; syntax.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;The content of the &lt;code&gt;version.h.in&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#ifndef&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_H_IN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_H_IN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; PROJECT_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;@PROJECT_NAME@&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_MAJOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;@VERSION_MAJOR@&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_MINOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;@VERSION_MINOR@&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_PATCH&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;@VERSION_PATCH@&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#endif&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; &#x2F;&#x2F; VERSION_H_IN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;code&gt;version.h&lt;&#x2F;code&gt; will be the following:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#ifndef&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_H_IN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_H_IN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; PROJECT_NAME&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;MyProject&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_MAJOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;0&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_MINOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;1&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_PATCH&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;0&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#endif&lt;&#x2F;span&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt; &#x2F;&#x2F; VERSION_H_IN&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;Finally, we can include &lt;code&gt;version.h&lt;&#x2F;code&gt; and use the project versions in &lt;code&gt;main.cpp&lt;&#x2F;code&gt; like this:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;lt;iostream&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;version.h&amp;quot;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  &#x2F;&#x2F; use veriables from CMakeLists.txt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;  std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;project name &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; PROJECT_NAME &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;  std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;verions major: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; VERSION_MAJOR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;  std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;version minor: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; VERSION_MINOR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;  std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;version patch: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; VERSION_PATCH &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;  return&lt;&#x2F;span&gt;&lt;span&gt; EXIT_SUCCESS;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;&lt;h2 id=&quot;2-definitions-to-the-compiler-command-line&quot;&gt;2. Definitions to the compiler command line&lt;&#x2F;h2&gt;
&lt;p&gt;The idea behind the second method is to pass the project version components as pre-processor directives to the compiler which will translate them during compilation and replace them in the target.&lt;&#x2F;p&gt;
&lt;p&gt;For such a purpose we can rely on the &lt;code&gt;#define&lt;&#x2F;code&gt; pre-processor in c++. This pre-processor will allow us to create a c++ macro. The macro is “simply” a word that will be replaced during compilation time with the value given to it.&lt;&#x2F;p&gt;
&lt;p&gt;The same &lt;code&gt;main.cpp&lt;&#x2F;code&gt; of the first method can be transformed into:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#include&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;lt;iostream&amp;gt;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_MAJOR&lt;&#x2F;span&gt;&lt;span&gt; _VERSION_MAJOR&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_MINOR&lt;&#x2F;span&gt;&lt;span&gt; _VERSION_MINOR&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;#define&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; VERSION_PATCH&lt;&#x2F;span&gt;&lt;span&gt; _VERSION_PATCH&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;auto&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; main&lt;&#x2F;span&gt;&lt;span&gt;() -&amp;gt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; int&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;{&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #6A737D;&quot;&gt;  &#x2F;&#x2F; use veriable from CMakeLists.txt&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;  std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;verions major: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; VERSION_MAJOR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;  std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;version minor: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; VERSION_MINOR &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;  std&lt;&#x2F;span&gt;&lt;span&gt;::cout &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt; &amp;quot;version patch: &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; &amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span&gt; VERSION_PATCH &lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;&amp;lt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt; std&lt;&#x2F;span&gt;&lt;span&gt;::endl;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;  return&lt;&#x2F;span&gt;&lt;span&gt; EXIT_SUCCESS;&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;}&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;During the compilation &lt;code&gt;VERSION_MAJOR&lt;&#x2F;code&gt;, for example, will take the value &lt;code&gt;_VERSION_MAJOR&lt;&#x2F;code&gt; and when executing the target, it is as if we have written &lt;code&gt;_VERSION_MAJOR&lt;&#x2F;code&gt; in the &lt;code&gt;std::cout&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;Of course, the compiler does not have any definition for &lt;code&gt;_VERSION_MAJOR&lt;&#x2F;code&gt; yet. These definitions can be passed to the compiler via the flag &lt;code&gt;-D&lt;&#x2F;code&gt; or equivalently to the &lt;code&gt;CMakeLists.txt&lt;&#x2F;code&gt; as follow:&lt;&#x2F;p&gt;
&lt;pre class=&quot;giallo&quot; style=&quot;color: #E1E4E8; background-color: #24292E;&quot;&gt;&lt;code data-lang=&quot;cpp&quot;&gt;&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;cmake_minimum_required&lt;&#x2F;span&gt;&lt;span&gt; (VERSION &lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;3.2&lt;&#x2F;span&gt;&lt;span&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;project&lt;&#x2F;span&gt;&lt;span&gt; (MyProject2)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span&gt;# Add the variables we need&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #B392F0;&quot;&gt;ADD_DEFINITIONS&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt; -&lt;&#x2F;span&gt;&lt;span&gt;D_VERSION_MAJOR&lt;&#x2F;span&gt;&lt;span style=&quot;color: #F97583;&quot;&gt;=&lt;&#x2F;span&gt;&lt;span&gt;\&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;&amp;quot;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;ADD_DEFINITIONS( -D_VERSION_MINOR=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;ADD_DEFINITIONS( -D_VERSION_PATCH=&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color: #79B8FF;&quot;&gt;\&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;&#x2F;span&gt;
&lt;span class=&quot;giallo-l&quot;&gt;&lt;span style=&quot;color: #9ECBFF;&quot;&gt;add_executable (${PROJECT_NAME} main.cpp)&lt;&#x2F;span&gt;&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;I should draw the reader’s attention that this is not the only cmake command to enable adding compilation definitions as you may read in this &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;cmake.org&#x2F;cmake&#x2F;help&#x2F;latest&#x2F;command&#x2F;add_definitions.html&quot;&gt;link&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;which-method-to-take&quot;&gt;Which method to take?&lt;&#x2F;h2&gt;
&lt;p&gt;Which method to take? does not have a big impact. Depending on the project size and the number of libraries used, it may be preferable to use the &lt;code&gt;config_file()&lt;&#x2F;code&gt; approach as this will keep the call to the compiler small. (refer to this &lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3781222&#x2F;add-definitions-vs-configure-file&quot;&gt;thread&lt;&#x2F;a&gt;)&lt;&#x2F;p&gt;
&lt;h2 id=&quot;source&quot;&gt;Source&lt;&#x2F;h2&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;cmake.org&#x2F;cmake&#x2F;help&#x2F;latest&#x2F;command&#x2F;add_definitions.html&quot;&gt;https:&#x2F;&#x2F;cmake.org&#x2F;cmake&#x2F;help&#x2F;latest&#x2F;command&#x2F;add_definitions.html&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3781222&#x2F;add-definitions-vs-configure-file&quot;&gt;https:&#x2F;&#x2F;stackoverflow.com&#x2F;questions&#x2F;3781222&#x2F;add-definitions-vs-configure-file&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a rel=&quot;external&quot; href=&quot;https:&#x2F;&#x2F;evileg.com&#x2F;en&#x2F;post&#x2F;536&#x2F;&quot;&gt;https:&#x2F;&#x2F;evileg.com&#x2F;en&#x2F;post&#x2F;536&#x2F;&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
</feed>
