<reponame>eayvali/DeepRL
# -*- coding: utf-8 -*-
"""
Created on Wed Dec 18 13:01:18 2019
Discretization
@author: elif.ayvali
"""
import sys
import gym
import pandas as pd
import numpy as np
import matplotlib.collections as mc
import matplotlib.pyplot as plt

def create_uniform_grid(low, high, bins=(10, 10)):
    """Define a uniformly-spaced grid that can be used to discretize a space.
    
    Parameters
    ----------
    low : array_like
        Lower bounds for each dimension of the continuous space.
    high : array_like
        Upper bounds for each dimension of the continuous space.
    bins : tuple
        Number of bins along each corresponding dimension.
    
    Returns
    -------
    grid : list of array_like
        A list of arrays containing split points for each dimension.
    """
#     grid=[]
#     low_bins=np.linspace(low[0], high[0], num=bins[0]+1)[1:-1]
#     high_bins=np.linspace(low[1], high[1], num=bins[1]+1)[1:-1]
#     grid.append(low_bins)
#     grid.append(high_bins)
      #Achieve the same in single line:
    grid = [np.linspace(low[dim], high[dim], bins[dim] + 1)[1:-1] for dim in range(len(bins))]
    print(grid)
    return grid

def discretize(sample, grid):
    """Discretize a sample as per given grid.
    
    Parameters
    ----------
    sample : array_like
        A single sample from the (original) continuous space.
    grid : list of array_like
        A list of arrays containing split points for each dimension.
    
    Returns
    -------
    discretized_sample : array_like
        A sequence of integers with the same number of dimensions as sample.
    """
    return list(int(np.digitize(s, g)) for s, g in zip(sample, grid))  # apply along each dimension
    
def visualize_samples(samples, discretized_samples, grid, low=None, high=None):
    """Visualize original and discretized samples on a given 2-dimensional grid."""

    fig, ax = plt.subplots(figsize=(10, 10))
    
    # Show grid
    ax.xaxis.set_major_locator(plt.FixedLocator(grid[0]))
    ax.yaxis.set_major_locator(plt.FixedLocator(grid[1]))
    ax.grid(True)
    
    # If bounds (low, high) are specified, use them to set axis limits
    if low is not None and high is not None:
        ax.set_xlim(low[0], high[0])
        ax.set_ylim(low[1], high[1])
    else:
        # Otherwise use first, last grid locations as low, high (for further mapping discretized samples)
        low = [splits[0] for splits in grid]
        high = [splits[-1] for splits in grid]

    # Map each discretized sample (which is really an index) to the center of corresponding grid cell
    grid_extended = np.hstack((np.array([low]).T, grid, np.array([high]).T))  # add low and high ends
    grid_centers = (grid_extended[:, 1:] + grid_extended[:, :-1]) / 2  # compute center of each grid cell
    locs = np.stack(grid_centers[i, discretized_samples[:, i]] for i in range(len(grid))).T  # map discretized samples

    ax.plot(samples[:, 0], samples[:, 1], 'o')  # plot original samples
    ax.plot(locs[:, 0], locs[:, 1], 's')  # plot discretized samples in mapped locations
    ax.add_collection(mc.LineCollection(list(zip(samples, locs)), colors='orange'))  # add a line connecting each original-discretized sample
    ax.legend(['original', 'discretized'])

    

low = [-1.0, -5.0]
high = [1.0, 5.0]
grid = create_uniform_grid(low, high)  
samples = np.array(
    [[-1.0 , -5.0],
     [-0.81, -4.1],
     [-0.8 , -4.0],
     [-0.5 ,  0.0],
     [ 0.2 , -1.9],
     [ 0.8 ,  4.0],
     [ 0.81,  4.1],
     [ 1.0 ,  5.0]])
discretized_samples = np.array([discretize(sample, grid) for sample in samples])
print("\nSamples:", repr(samples), sep="\n")
print("\nDiscretized samples:", repr(discretized_samples), sep="\n")
visualize_samples(samples, discretized_samples, grid, low, high)


# Create an environment and set random seed
env = gym.make('MountainCar-v0')
env.seed(505);

# Create a grid to discretize the state space of the RL problem
state_grid = create_uniform_grid(env.observation_space.low, env.observation_space.high, bins=(10, 10))

# Obtain some samples from the space, discretize them, and then visualize them
state_samples = np.array([env.observation_space.sample() for i in range(10)])
discretized_state_samples = np.array([discretize(sample, state_grid) for sample in state_samples])
visualize_samples(state_samples, discretized_state_samples, state_grid,
                  env.observation_space.low, env.observation_space.high)
plt.xlabel('position'); plt.ylabel('velocity');  # axis labels for MountainCar-v0 state space


# -*- coding: utf-8 -*-
"""
Created on Wed Dec 18 13:01:18 2019
Discretization
@author: elif.ayvali
"""
import sys
import gym
import pandas as pd
import numpy as np
import matplotlib.collections as mc
import matplotlib.pyplot as plt

def create_uniform_grid(low, high, bins=(10, 10)):
    """Define a uniformly-spaced grid that can be used to discretize a space.
    
    Parameters
    ----------
    low : array_like
        Lower bounds for each dimension of the continuous space.
    high : array_like
        Upper bounds for each dimension of the continuous space.
    bins : tuple
        Number of bins along each corresponding dimension.
    
    Returns
    -------
    grid : list of array_like
        A list of arrays containing split points for each dimension.
    """
#     grid=[]
#     low_bins=np.linspace(low[0], high[0], num=bins[0]+1)[1:-1]
#     high_bins=np.linspace(low[1], high[1], num=bins[1]+1)[1:-1]
#     grid.append(low_bins)
#     grid.append(high_bins)
      #Achieve the same in single line:
    grid = [np.linspace(low[dim], high[dim], bins[dim] + 1)[1:-1] for dim in range(len(bins))]
    print(grid)
    return grid

def discretize(sample, grid):
    """Discretize a sample as per given grid.
    
    Parameters
    ----------
    sample : array_like
        A single sample from the (original) continuous space.
    grid : list of array_like
        A list of arrays containing split points for each dimension.
    
    Returns
    -------
    discretized_sample : array_like
        A sequence of integers with the same number of dimensions as sample.
    """
    return list(int(np.digitize(s, g)) for s, g in zip(sample, grid))  # apply along each dimension
    
def visualize_samples(samples, discretized_samples, grid, low=None, high=None):
    """Visualize original and discretized samples on a given 2-dimensional grid."""

    fig, ax = plt.subplots(figsize=(10, 10))
    
    # Show grid
    ax.xaxis.set_major_locator(plt.FixedLocator(grid[0]))
    ax.yaxis.set_major_locator(plt.FixedLocator(grid[1]))
    ax.grid(True)
    
    # If bounds (low, high) are specified, use them to set axis limits
    if low is not None and high is not None:
        ax.set_xlim(low[0], high[0])
        ax.set_ylim(low[1], high[1])
    else:
        # Otherwise use first, last grid locations as low, high (for further mapping discretized samples)
        low = [splits[0] for splits in grid]
        high = [splits[-1] for splits in grid]

    # Map each discretized sample (which is really an index) to the center of corresponding grid cell
    grid_extended = np.hstack((np.array([low]).T, grid, np.array([high]).T))  # add low and high ends
    grid_centers = (grid_extended[:, 1:] + grid_extended[:, :-1]) / 2  # compute center of each grid cell
    locs = np.stack(grid_centers[i, discretized_samples[:, i]] for i in range(len(grid))).T  # map discretized samples

    ax.plot(samples[:, 0], samples[:, 1], 'o')  # plot original samples
    ax.plot(locs[:, 0], locs[:, 1], 's')  # plot discretized samples in mapped locations
    ax.add_collection(mc.LineCollection(list(zip(samples, locs)), colors='orange'))  # add a line connecting each original-discretized sample
    ax.legend(['original', 'discretized'])

    

low = [-1.0, -5.0]
high = [1.0, 5.0]
grid = create_uniform_grid(low, high)  
samples = np.array(
    [[-1.0 , -5.0],
     [-0.81, -4.1],
     [-0.8 , -4.0],
     [-0.5 ,  0.0],
     [ 0.2 , -1.9],
     [ 0.8 ,  4.0],
     [ 0.81,  4.1],
     [ 1.0 ,  5.0]])
discretized_samples = np.array([discretize(sample, grid) for sample in samples])
print("\nSamples:", repr(samples), sep="\n")
print("\nDiscretized samples:", repr(discretized_samples), sep="\n")
visualize_samples(samples, discretized_samples, grid, low, high)


# Create an environment and set random seed
env = gym.make('MountainCar-v0')
env.seed(505);

# Create a grid to discretize the state space of the RL problem
state_grid = create_uniform_grid(env.observation_space.low, env.observation_space.high, bins=(10, 10))

# Obtain some samples from the space, discretize them, and then visualize them
state_samples = np.array([env.observation_space.sample() for i in range(10)])
discretized_state_samples = np.array([discretize(sample, state_grid) for sample in state_samples])
visualize_samples(state_samples, discretized_state_samples, state_grid,
                  env.observation_space.low, env.observation_space.high)
plt.xlabel('position'); plt.ylabel('velocity');  # axis labels for MountainCar-v0 state space
# -*- coding: utf-8 -*- Created on Wed Dec 18 13:01:18 2019 Discretization @author: elif.ayvali Define a uniformly-spaced grid that can be used to discretize a space.          Parameters     ----------     low : array_like         Lower bounds for each dimension of the continuous space.     high : array_like         Upper bounds for each dimension of the continuous space.     bins : tuple         Number of bins along each corresponding dimension.          Returns     -------     grid : list of array_like         A list of arrays containing split points for each dimension. #     grid=[] #     low_bins=np.linspace(low[0], high[0], num=bins[0]+1)[1:-1] #     high_bins=np.linspace(low[1], high[1], num=bins[1]+1)[1:-1] #     grid.append(low_bins) #     grid.append(high_bins) #Achieve the same in single line: Discretize a sample as per given grid.          Parameters     ----------     sample : array_like         A single sample from the (original) continuous space.     grid : list of array_like         A list of arrays containing split points for each dimension.          Returns     -------     discretized_sample : array_like         A sequence of integers with the same number of dimensions as sample. # apply along each dimension Visualize original and discretized samples on a given 2-dimensional grid. # Show grid # If bounds (low, high) are specified, use them to set axis limits # Otherwise use first, last grid locations as low, high (for further mapping discretized samples) # Map each discretized sample (which is really an index) to the center of corresponding grid cell # add low and high ends # compute center of each grid cell # map discretized samples # plot original samples # plot discretized samples in mapped locations # add a line connecting each original-discretized sample # Create an environment and set random seed # Create a grid to discretize the state space of the RL problem # Obtain some samples from the space, discretize them, and then visualize them # axis labels for MountainCar-v0 state space