RLPark 1.0.0
Reinforcement Learning Framework in Java

CrtrLogFile.java

Go to the documentation of this file.
00001 package rlpark.plugin.critterbot.crtrlog;
00002 
00003 import java.util.ArrayList;
00004 import java.util.List;
00005 
00006 import rlpark.plugin.critterbot.CritterbotProblem;
00007 import rlpark.plugin.critterbot.actions.CritterbotAction;
00008 import rlpark.plugin.critterbot.environment.CritterbotEnvironments;
00009 import rlpark.plugin.rltoys.envio.observations.Legend;
00010 import rlpark.plugin.rltoys.utils.Utils;
00011 import rlpark.plugin.robot.helpers.Robots;
00012 import rlpark.plugin.robot.interfaces.RobotLog;
00013 import rlpark.plugin.robot.observations.ObservationVersatile;
00014 import rlpark.plugin.robot.observations.ObservationVersatileArray;
00015 import zephyr.plugin.core.api.internal.logfiles.LogFile;
00016 import zephyr.plugin.core.api.monitoring.abstracts.DataMonitor;
00017 import zephyr.plugin.core.api.monitoring.abstracts.MonitorContainer;
00018 import zephyr.plugin.core.api.monitoring.annotations.LabelProvider;
00019 
00020 @SuppressWarnings("restriction")
00021 public class CrtrLogFile implements CritterbotProblem, RobotLog, MonitorContainer {
00022   public final String filepath;
00023   private final LogFile logfile;
00024   private ObservationVersatile currentObservation;
00025   private final Legend legend;
00026   private final int timeIndex;
00027 
00028   public CrtrLogFile(String filepath) {
00029     logfile = LogFile.load(filepath);
00030     this.filepath = filepath;
00031     timeIndex = findTimeIndex();
00032     legend = createLegend();
00033   }
00034 
00035   private int findTimeIndex() {
00036     String[] labels = logfile.labels();
00037     for (int i = 0; i < labels.length; i++)
00038       if (labels[i].equals("LocalTime"))
00039         return i;
00040     return -1;
00041   }
00042 
00043   private Legend createLegend() {
00044     List<String> legendLabels = new ArrayList<String>();
00045     String[] labels = logfile.labels();
00046     for (int i = 0; i < labels.length; i++) {
00047       if (i == timeIndex)
00048         continue;
00049       legendLabels.add(labels[i]);
00050     }
00051     return new Legend(legendLabels);
00052   }
00053 
00054   @LabelProvider(ids = { "values" })
00055   String observationLabel(int index) {
00056     return legend.label(index);
00057   }
00058 
00059   @Override
00060   public Legend legend() {
00061     return legend;
00062   }
00063 
00064   @Override
00065   public ObservationVersatileArray nextStep() {
00066     logfile.step();
00067     double[] obs = logfile.currentLine();
00068     long localTime = 0;
00069     if (timeIndex >= 0) {
00070       localTime = (long) obs[timeIndex];
00071       obs = removeLocalTimeValue(obs);
00072     }
00073     currentObservation = new ObservationVersatile(localTime, Robots.doubleArrayToByteArray(obs), obs);
00074     return new ObservationVersatileArray(Utils.asList(currentObservation));
00075   }
00076 
00077   private double[] removeLocalTimeValue(double[] obs) {
00078     double[] result = new double[obs.length - 1];
00079     System.arraycopy(obs, 0, result, 0, timeIndex);
00080     System.arraycopy(obs, timeIndex + 1, result, timeIndex, obs.length - timeIndex - 1);
00081     return result;
00082   }
00083 
00084   public double[] step() {
00085     return nextStep().doubleValues();
00086   }
00087 
00088   @Override
00089   public boolean hasNextStep() {
00090     return !logfile.eof();
00091   }
00092 
00093   @Override
00094   public void close() {
00095     logfile.close();
00096   }
00097 
00098   public String filepath() {
00099     return logfile.filepath;
00100   }
00101 
00102   @Override
00103   public String label() {
00104     return logfile.label();
00105   }
00106 
00107   public static CrtrLogFile load(String filepath) {
00108     return new CrtrLogFile(filepath);
00109   }
00110 
00111   @Override
00112   public CritterbotAction lastAction() {
00113     return null;
00114   }
00115 
00116   @Override
00117   public double[] lastReceivedObs() {
00118     return currentObservation != null ? currentObservation.doubleValues() : null;
00119   }
00120 
00121   @Override
00122   public int observationPacketSize() {
00123     return logfile.labels().length * 4;
00124   }
00125 
00126   @Override
00127   public void addToMonitor(DataMonitor monitor) {
00128     CritterbotEnvironments.addObservationsLogged(this, monitor);
00129   }
00130 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark