Source code for pyrltr.analyzer.DataContainer

from pymlp.datahandling.DataHandler import DataHandler
import numpy as np


[docs]class DataContainer: """ Contains all the data, logged while running the simulations. Attributes: rewards -- the received rewards lengths -- the lengths of each episode startPositions -- the start positions of each epoch goalPositios -- the goal positions of each epoch actions -- the actions taken during a simulation undos -- the count of undos for every step """ def __init__(self, folder, prefix): """ Constructor. """ self.rewards = list() self.lengths = list() self.startPositions = list() self.goalPositions = list() self.finalPositions = list() self.actions = list() self.undos = list() self.prefix = prefix self.datahandler = DataHandler(folder) self.dataFile = "{0}_{1}"
[docs] def prepareNewEpoch(self): """ Prepares a new epoch. Adds all necessary lists so that the next epoch can be logged properly. """ self.lengths.append(list()) self.rewards.append(list()) self.startPositions.append(list()) self.goalPositions.append(list()) self.actions.append(list()) self.undos.append(list()) self.finalPositions.append(list())
[docs] def prepareNewEpisode(self): """ Prepares a new episode. Adds all necessary lists so that the next epoch can be logged properly. """ self.actions[-1].append(list()) self.undos[-1].append(list())
[docs] def addAction(self, action): """ Adds an action to the current episode. """ self.actions[-1][-1].append(action)
[docs] def addUndo(self, undos): """ Adds the number of undos for the last action. """ self.undos[-1][-1].append(undos)
[docs] def addReward(self, reward): """ Adds the sum of reward for the last episode. """ self.rewards[-1].append(reward)
[docs] def addLength(self, length): """ Adds the length of the last episode to the current epoch. """ self.lengths[-1].append(length)
[docs] def addStartPosition(self, startPosition): """ Adds the start position to the current epoch. """ self.startPositions[-1].append(startPosition)
[docs] def addGoalPosition(self, goalPosition): """ Adds the goal position to the current epoch. """ self.goalPositions[-1].append(goalPosition)
[docs] def addFinalPosition(self, position): self.finalPositions[-1].append(position)
[docs] def writeData(self, index): """ Writes the data onto the harddrive. """ self._writeDataFile(self.rewards, "rewards", index) self._writeDataFile(self.lengths, "steps", index) # TODO 2015-10-14: Find a way to deal with non-rectangular arrays # self._writeDataFile(self.undos, "undos", index) # self._writeDataFile(self.actions, "actions", index) self._writeDataFile(self.startPositions, "startPositions", index) self._writeDataFile(self.goalPositions, "goalPositions", index) self._writeDataFile(self.finalPositions, "finalPositions", index)
def _writeDataFile(self, data, filename, index): """ Writes the actual data into the file folder/prefix_filename_index.dat.gz using the pymlp.datahandling.DataHandler. """ dataFile = self.dataFile.format(self.prefix, filename) self.datahandler.saveData(dataFile, np.array(data), index)