RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.rltoys.envio.actions; 00002 00003 import java.util.Arrays; 00004 00005 import rlpark.plugin.rltoys.math.vector.implementations.PVector; 00006 import rlpark.plugin.rltoys.math.vector.implementations.Vectors; 00007 import zephyr.plugin.core.api.monitoring.annotations.Monitor; 00008 00009 00010 public class ActionArray implements Action { 00011 private static final long serialVersionUID = 6468757011578627902L; 00012 @Monitor 00013 final public double[] actions; 00014 00015 public ActionArray(double... actions) { 00016 this.actions = actions == null ? null : actions.clone(); 00017 assert actions == null || Vectors.checkValues(new PVector(actions)); 00018 } 00019 00020 @Override 00021 public boolean equals(Object obj) { 00022 if (obj == null) 00023 return false; 00024 if (super.equals(obj)) 00025 return true; 00026 ActionArray other = (ActionArray) obj; 00027 if (actions == other.actions) 00028 return true; 00029 if (actions == null || other.actions == null) 00030 return false; 00031 if (actions.length != other.actions.length) 00032 return false; 00033 for (int i = 0; i < actions.length; i++) 00034 if (actions[i] != other.actions[i]) 00035 return false; 00036 return true; 00037 } 00038 00039 static public Action merge(Action... actions) { 00040 if (actions.length == 1) 00041 return actions[0]; 00042 int size = 0; 00043 double[][] actionData = new double[actions.length][]; 00044 for (int i = 0; i < actions.length; i++) { 00045 ActionArray action = (ActionArray) actions[i]; 00046 actionData[i] = action.actions; 00047 size += actionData[i].length; 00048 } 00049 double[] result = new double[size]; 00050 int index = 0; 00051 for (double[] data : actionData) { 00052 System.arraycopy(data, 0, result, index, data.length); 00053 index += data.length; 00054 } 00055 return new ActionArray(result); 00056 } 00057 00058 public Action[] decompose() { 00059 Action[] result = new Action[actions.length]; 00060 for (int i = 0; i < result.length; i++) 00061 result[i] = new ActionArray(actions[i]); 00062 return result; 00063 } 00064 00065 @Override 00066 public int hashCode() { 00067 return Arrays.hashCode(actions); 00068 } 00069 00070 @Override 00071 public String toString() { 00072 return Arrays.toString(actions); 00073 } 00074 00075 public ActionArray copy() { 00076 return new ActionArray(actions); 00077 } 00078 00079 static public double toDouble(Action a) { 00080 final ActionArray action = (ActionArray) a; 00081 assert action.actions.length == 1; 00082 return action.actions[0]; 00083 } 00084 00085 static public ActionArray getDim(Action a, int i) { 00086 return new ActionArray(((ActionArray) a).actions[i]); 00087 } 00088 00089 public static boolean checkAction(Action a) { 00090 return ((ActionArray) a).actions != null; 00091 } 00092 00093 public static Action toAction(double a) { 00094 return new ActionArray(a); 00095 } 00096 }