RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.critterbot.environment; 00002 00003 import rlpark.plugin.critterbot.internal.CritterbotConnection; 00004 import rlpark.plugin.critterbot.internal.CritterbotSound; 00005 import rlpark.plugin.rltoys.envio.observations.Legend; 00006 import rlpark.plugin.robot.internal.sync.ObservationSynchronizer; 00007 import rlpark.plugin.robot.observations.ObservationReceiver; 00008 import rlpark.plugin.robot.observations.ObservationVersatile; 00009 00010 public class CritterbotRobot extends CritterbotEnvironment { 00011 public enum SoundMode { 00012 None, FFT 00013 } 00014 00015 static public final SoundMode defaultSoundMode = SoundMode.None; 00016 00017 static final Integer DiscoControlPort = 2330; 00018 static final String WalterIP = "10.0.1.10"; 00019 static final String GremlinIP = "10.0.1.20"; 00020 private final Legend legend; 00021 private final double[] lastSoundData; 00022 private final ObservationSynchronizer soundSync; 00023 00024 public CritterbotRobot() { 00025 this(true, defaultSoundMode); 00026 } 00027 00028 public CritterbotRobot(boolean onCritterbotNetwork) { 00029 this(onCritterbotNetwork, defaultSoundMode); 00030 } 00031 00032 public CritterbotRobot(SoundMode soundMode) { 00033 this(true, soundMode); 00034 } 00035 00036 public CritterbotRobot(boolean onCritterbotNetwork, SoundMode soundMode) { 00037 this(new CritterbotConnection(onCritterbotNetwork ? WalterIP : "localhost", DiscoControlPort), soundMode); 00038 } 00039 00040 public CritterbotRobot(ObservationReceiver receiver, SoundMode soundMode) { 00041 super(receiver); 00042 CritterbotSound soundConnection = soundMode == SoundMode.None ? null : new CritterbotSound(WalterIP, 7001); 00043 legend = soundConnection == null ? super.legend() : buildLegend(soundConnection); 00044 lastSoundData = soundConnection == null ? null : new double[soundConnection.legend().nbLabels()]; 00045 soundSync = soundConnection == null ? null : new ObservationSynchronizer(soundConnection, false); 00046 } 00047 00048 private Legend buildLegend(CritterbotSound soundConnection) { 00049 Legend robotLegend = super.legend(); 00050 Legend soundLegend = soundConnection.legend(); 00051 String[] labels = new String[robotLegend.nbLabels() + soundLegend.nbLabels()]; 00052 int index = 0; 00053 for (String label : robotLegend.getLabels()) { 00054 labels[index] = label; 00055 index++; 00056 } 00057 for (String label : soundLegend.getLabels()) { 00058 labels[index] = label; 00059 index++; 00060 } 00061 return new Legend(labels); 00062 } 00063 00064 @Override 00065 public Legend legend() { 00066 return legend; 00067 } 00068 00069 @Override 00070 public double[] waitNewObs() { 00071 double[] robotObs = super.waitNewObs(); 00072 if (robotObs == null) 00073 return null; 00074 if (lastSoundData == null) 00075 return robotObs; 00076 ObservationVersatile soundUpdatedObs = soundSync.newObsNow().last(); 00077 if (soundUpdatedObs != null) 00078 System.arraycopy(soundUpdatedObs.doubleValues(), 0, lastSoundData, 0, lastSoundData.length); 00079 double[] obs = new double[robotObs.length + lastSoundData.length]; 00080 System.arraycopy(robotObs, 0, obs, 0, robotObs.length); 00081 System.arraycopy(lastSoundData, 0, obs, robotObs.length, lastSoundData.length); 00082 return obs; 00083 } 00084 }