How to create FrozenLake random maps
Tags: machinelearning, ai, gym, python
Published: 2019-06-24 14:52:48

<!-- wp:paragraph -->
<p>Besides providing our custom map using the desc parameter, it's also possible to create random maps for the Frozen Lake environment, as shown in the code below:</p>
<!-- /wp:paragraph -->

<!-- wp:more -->
<!--more-->
<!-- /wp:more -->

<!-- wp:syntaxhighlighter/code {"language":"python"} -->
<pre class="wp-block-syntaxhighlighter-code brush: python; notranslate"># frozen-lake-random-maps-ex1.py
import gym

env = gym.make("FrozenLake-v0", desc=None, map_name=None)
env.reset()
env.render()</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>By setting both the parameters <strong>desc</strong> and <strong>map_name</strong> to <strong>None</strong>, the environment will be created using a random 8x8 map. However, it's not possible to create a random map of custom size using only the <strong>make</strong> method. But we can work around this limitation by importing the <strong>generate_random_map()</strong> function from the <strong>frozen_lake</strong> module and passing its output to the <strong>desc</strong> parameter:</p>
<!-- /wp:paragraph -->

<!-- wp:syntaxhighlighter/code {"language":"python"} -->
<pre class="wp-block-syntaxhighlighter-code brush: python; notranslate"># frozen-lake-random-maps-ex2.py
import gym
from gym.envs.toy_text.frozen_lake
    import generate_random_map

random_map = generate_random_map(size=20, p=0.8)

env = gym.make("FrozenLake-v0", desc=random_map)
env.reset()
env.render()</pre>
<!-- /wp:syntaxhighlighter/code -->

<!-- wp:paragraph -->
<p>The <strong>generate_random_map</strong>() function provides us much more flexibility. The function receives two parameters: <strong>size</strong> and <strong>p</strong>. The parameter <strong>size</strong> expects an integer representing the side of the grid, resulting in a size x size map, whereas the <strong>p</strong> parameter is the probability of occurring a frozen tile, and is used to set the proportion between frozen tiles and holes.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>The FrozenLake environment provided with the Gym library has limited options of maps, but we can work around these limitations by combining the <strong>generate_random_map()</strong> function and the <strong>desc</strong> parameter. The use of random maps it's interesting to test how well our algorithm can generalize.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2>References</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Examples:<br><a href="https://github.com/rodmsmendes/reinforcementlearning4fun/tree/master/frozen-lake-random-maps" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">https://github.com/rodmsmendes/reinforcementlearning4fun/tree/master/frozen-lake-random-maps</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Introduction to the Frozen Lake game:<br><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://reinforcementlearning4.fun/2019/06/09/introduction-reinforcement-learning-frozen-lake-example/" target="_blank">https://reinforcementlearning4.fun/2019/06/09/introduction-reinforcement-learning-frozen-lake-example/</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p> Using the Frozen Lake environment:<br><a rel="noreferrer noopener" aria-label=" (opens in a new tab)" href="https://reinforcementlearning4.fun/2019/06/16/gym-tutorial-frozen-lake/" target="_blank">https://reinforcementlearning4.fun/2019/06/16/gym-tutorial-frozen-lake/</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Installing OpenAI Gym:<br><a href="https://reinforcementlearning4.fun/2019/05/24/how-to-install-openai-gym/">https://reinforcementlearning4.fun/2019/05/24/how-to-install-openai-gym/</a></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>OpenAI Gym project homepage:<br><a href="https://gym.openai.com/" target="_blank" rel="noreferrer noopener" aria-label=" (opens in a new tab)">https://gym.openai.co</a><a href="https://gym.openai.com/">m</a></p>
<!-- /wp:paragraph -->