How to do it...

Let's develop the Windy Gridworld environment:

  1. Import the necessary modules, NumPy, and the discrete class, from Gym:
>>> import numpy as np>>> import sys>>> from gym.envs.toy_text import discrete
  1. Define four actions:
>>> UP = 0>>> RIGHT = 1>>> DOWN = 2>>> LEFT = 3
  1. Let's start by defining the __init__ method in the WindyGridworldEnv class:
>>> class WindyGridworldEnv(discrete.DiscreteEnv): ...     def __init__(self): ...         self.shape = (7, 10) ...         nS = self.shape[0] * self.shape[1] ...         nA = 4 ...         # Wind locations ...         winds = np.zeros(self.shape) ...         winds[:,[3,4,5,8]] = 1 ...         winds[:,[6,7]] = 2 ...         self.goal = (3, 7) ...         # Calculate transition probabilities and rewards ...         P = {} ...         for s in range(nS): ... position = np.unravel_index(s, ...

Get PyTorch 1.x Reinforcement Learning Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.