RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.robot.helpers; 00002 00003 import java.util.Arrays; 00004 00005 import rlpark.plugin.rltoys.envio.observations.Observation; 00006 import rlpark.plugin.robot.interfaces.RobotLive; 00007 import rlpark.plugin.robot.observations.ObservationVersatile; 00008 import rlpark.plugin.robot.observations.ObservationVersatileArray; 00009 import zephyr.plugin.core.api.monitoring.abstracts.DataMonitor; 00010 import zephyr.plugin.core.api.monitoring.abstracts.Monitored; 00011 00012 public class Robots { 00013 static public void addToMonitor(DataMonitor monitor, final RobotLive problem) { 00014 for (String label : problem.legend().getLabels()) { 00015 final int obsIndex = problem.legend().indexOf(label); 00016 monitor.add(label, new Monitored() { 00017 @Override 00018 public double monitoredValue() { 00019 double[] obs = Robots.toDoubles(problem.lastReceivedRawObs()); 00020 if (obs == null) 00021 return -1; 00022 return obs[obsIndex]; 00023 } 00024 }); 00025 } 00026 } 00027 00028 public static byte[] doubleArrayToByteArray(double[] current) { 00029 byte[] result = new byte[current.length << 2]; 00030 int j = 0; 00031 for (int i = 0; i < current.length; i++) { 00032 int x = (int) current[i]; 00033 byte[] b = new byte[] { (byte) (x >>> 24), (byte) (x >>> 16), (byte) (x >>> 8), (byte) x }; 00034 System.arraycopy(b, 0, result, j, 4); 00035 j += 4; 00036 } 00037 return result; 00038 } 00039 00040 public static double[] byteArrayToDoubleArray(byte[] current) { 00041 double[] result = new double[current.length / 4]; 00042 int j = 0; 00043 for (int i = 0; i < result.length; i++) { 00044 result[i] = byteArrayToInt(Arrays.copyOfRange(current, j, j + 4)); 00045 j += 4; 00046 } 00047 return result; 00048 } 00049 00050 public static final int byteArrayToInt(byte[] current) { 00051 return (current[0] << 24) + ((current[1] & 0xFF) << 16) + ((current[2] & 0xFF) << 8) + (current[3] & 0xFF); 00052 } 00053 00054 public static double[] toDoubles(ObservationVersatileArray observation) { 00055 return observation != null ? observation.doubleValues() : null; 00056 } 00057 00058 public static double[] toDoubles(Observation obs) { 00059 return toDoubles((ObservationVersatileArray) obs); 00060 } 00061 00062 public static double[] toDoubles(ObservationVersatile obs) { 00063 return obs != null ? obs.doubleValues() : null; 00064 } 00065 }