Ejemplo n.º 1
0
 def loadHierarchy(self):
     self.cs = pyogmaneo.ComputeSystem()
     self.h = pyogmaneo.Hierarchy("pupper.ohr")
Ejemplo n.º 2
0
    def __init__(self,
                 env,
                 layerSizes=2 * [Int3(4, 4, 16)],
                 layerRadius=4,
                 hiddenSize=Int3(8, 8, 16),
                 imageRadius=9,
                 imageScale=1.0,
                 obsResolution=32,
                 actionResolution=16,
                 rewardScale=1.0,
                 terminalReward=0.0,
                 infSensitivity=1.0,
                 nThreads=4,
                 loadName=None):
        self.env = env

        pyogmaneo.ComputeSystem.setNumThreads(nThreads)
        self.cs = pyogmaneo.ComputeSystem()

        self.imEnc = None
        self.imEncIndex = -1

        self.inputSizes = []
        self.inputLows = []
        self.inputHighs = []
        self.inputTypes = []
        self.imageSizes = []
        self.imgsPrev = []
        self.actionIndices = []

        self.rewardScale = rewardScale
        self.terminalReward = terminalReward

        self.infSensitivity = infSensitivity

        if type(self.env.observation_space) is gym.spaces.Discrete:
            self.inputSizes.append(Int3(1, 1, self.env.observation_space.n))
            self.inputTypes.append(pyogmaneo.inputTypeNone)
            self.inputLows.append([0.0])
            self.inputHighs.append([0.0])
        elif type(self.env.observation_space) is gym.spaces.Box:
            if len(self.env.observation_space.shape) == 1 or len(
                    self.env.observation_space.shape) == 0:
                squareSize = int(
                    np.ceil(np.sqrt(len(self.env.observation_space.low))))
                squareTotal = squareSize * squareSize
                self.inputSizes.append(
                    Int3(squareSize, squareSize, obsResolution))
                self.inputTypes.append(pyogmaneo.inputTypeNone)
                lows = list(self.env.observation_space.low)
                highs = list(self.env.observation_space.high)

                # Detect large numbers/inf
                for i in range(len(lows)):
                    if abs(lows[i]) > 100000 or abs(highs[i]) > 100000:
                        # Indicate inf by making low greater than high
                        lows[i] = 1.0
                        highs[i] = -1.0

                self.inputLows.append(lows)
                self.inputHighs.append(highs)
            elif len(self.env.observation_space.shape) == 2:
                scaledSize = (int(self.env.observation_space.shape[0] *
                                  imageScale),
                              int(self.env.observation_space.shape[1] *
                                  imageScale), 1)

                self.imageSizes.append(scaledSize)
            elif len(self.env.observation_space.shape) == 3:
                scaledSize = (int(self.env.observation_space.shape[0] *
                                  imageScale),
                              int(self.env.observation_space.shape[1] *
                                  imageScale), 3)

                self.imageSizes.append(scaledSize)
            else:
                raise Exception("Unsupported Box input: Dimensions too high " +
                                str(self.env.observation_space.shape))
        else:
            raise Exception("Unsupported input type " +
                            str(type(self.env.observation_space)))

        if len(self.imageSizes) > 0:
            vlds = []

            for i in range(len(self.imageSizes)):
                vld = pyogmaneo.ImVisibleLayerDesc(
                    Int3(self.imageSizes[i][0], self.imageSizes[i][1],
                         self.imageSizes[i][2]), imageRadius)

                vlds.append(vld)

                self.imgsPrev.append(np.zeros(self.imageSizes[i]))

            self.imEnc = pyogmaneo.ImageEncoder(self.cs, hiddenSize, vlds)

            self.imEncIndex = len(self.inputSizes)
            self.inputSizes.append(hiddenSize)
            self.inputTypes.append(pyogmaneo.inputTypeNone)
            self.inputLows.append([0.0])
            self.inputHighs.append([1.0])

        # Actions
        if type(self.env.action_space) is gym.spaces.Discrete:
            self.actionIndices.append(len(self.inputSizes))
            self.inputSizes.append(Int3(1, 1, self.env.action_space.n))
            self.inputTypes.append(pyogmaneo.inputTypeAction)
            self.inputLows.append([0.0])
            self.inputHighs.append([0.0])
        elif type(self.env.action_space) is gym.spaces.Box:
            if len(self.env.action_space.shape) < 3:
                if len(self.env.action_space.shape) == 2:
                    self.actionIndices.append(len(self.inputSizes))
                    self.inputSizes.append(
                        Int3(self.env.action_space.shape[0],
                             self.env.action_space.shape[1], actionResolution))
                    self.inputTypes.append(pyogmaneo.inputTypeAction)
                    lows = list(self.env.action_space.low)
                    highs = list(self.env.action_space.high)

                    self.inputLows.append(lows)
                    self.inputHighs.append(highs)
                else:
                    squareSize = int(
                        np.ceil(np.sqrt(len(self.env.action_space.low))))
                    squareTotal = squareSize * squareSize
                    self.actionIndices.append(len(self.inputSizes))
                    self.inputSizes.append(
                        Int3(squareSize, squareSize, actionResolution))
                    self.inputTypes.append(pyogmaneo.inputTypeAction)
                    lows = list(self.env.action_space.low)
                    highs = list(self.env.action_space.high)

                    self.inputLows.append(lows)
                    self.inputHighs.append(highs)
            else:
                raise Exception(
                    "Unsupported Box action: Dimensions too high " +
                    str(self.env.action_space.shape))
        else:
            raise Exception("Unsupported action type " +
                            str(type(self.env.action_space)))

        lds = []

        for i in range(len(layerSizes)):
            ld = pyogmaneo.LayerDesc()

            ld.hiddenSize = layerSizes[i]

            ld.ffRadius = layerRadius
            ld.pRadius = layerRadius
            ld.aRadius = layerRadius

            lds.append(ld)

        if loadName is None:
            self.h = pyogmaneo.Hierarchy(self.cs, self.inputSizes,
                                         self.inputTypes, lds)
        else:
            self.h = pyogmaneo.Hierarchy(loadName)

        self.actions = []

        for i in range(len(self.actionIndices)):
            index = self.actionIndices[i]

            size = len(self.inputLows[index])

            startAct = []

            for j in range(size):
                startAct.append(np.random.randint(0, self.inputSizes[index].z))

            self.actions.append(startAct)
Ejemplo n.º 3
0
#
#  This copy of EOgmaNeo is licensed to you under the terms described
#  in the PYEOGMANEO_LICENSE.md file included in this distribution.
# ----------------------------------------------------------------------------

# -*- coding: utf-8 -*-

import numpy as np
import pyogmaneo
import matplotlib.pyplot as plt

# Set the number of threads
pyogmaneo.ComputeSystem.setNumThreads(4)

# Create the compute system
cs = pyogmaneo.ComputeSystem()

# This defines the resolution of the input encoding - we are using a simple single column that represents a bounded scalar through a one-hot encoding. This value is the number of "bins"
inputColumnSize = 64

# The bounds of the scalar we are encoding (low, high)
bounds = (-1.0, 1.0)

# Define layer descriptors: Parameters of each layer upon creation
lds = []

for i in range(5):  # Layers with exponential memory
    ld = pyogmaneo.LayerDesc()

    # Set the hidden (encoder) layer size: width x height x columnSize
    ld.hiddenSize = pyogmaneo.Int3(4, 4, 16)