RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.rltoys.envio.actions; 00002 00003 import java.util.ArrayList; 00004 import java.util.Arrays; 00005 import java.util.LinkedHashMap; 00006 import java.util.List; 00007 import java.util.Map; 00008 00009 import rlpark.plugin.rltoys.math.ranges.Range; 00010 00011 public class Actions { 00012 static private double[][] createActionsAsArrays(Range... actionValues) { 00013 if (actionValues.length == 0) { 00014 double[][] result = new double[1][]; 00015 result[0] = new double[0]; 00016 return result; 00017 } 00018 Range[] childValues = Arrays.copyOf(actionValues, actionValues.length - 1); 00019 double[][] childActions = createActionsAsArrays(childValues); 00020 double[][] result = new double[3 * childActions.length][]; 00021 int actionIndex = 0; 00022 for (double[] childAction : childActions) { 00023 double[][] newActions = createNewActions(childAction, actionValues[actionValues.length - 1]); 00024 for (double[] newAction : newActions) { 00025 result[actionIndex] = newAction; 00026 actionIndex++; 00027 } 00028 } 00029 return result; 00030 } 00031 00032 private static double[][] createNewActions(double[] childAction, Range actionValue) { 00033 double[] action = new double[childAction.length + 1]; 00034 for (int i = 0; i < childAction.length; i++) 00035 action[i] = childAction[i]; 00036 double[][] result = { action.clone(), action.clone(), action.clone() }; 00037 result[0][childAction.length] = actionValue.min(); 00038 result[1][childAction.length] = (actionValue.max() + actionValue.min()) / 2.0; 00039 result[2][childAction.length] = actionValue.max(); 00040 return result; 00041 } 00042 00043 static public Action[] createActions(Range... actionValues) { 00044 List<ActionArray> result = new ArrayList<ActionArray>(); 00045 double[][] actions = createActionsAsArrays(actionValues); 00046 for (double[] action : actions) 00047 result.add(new ActionArray(action)); 00048 Action[] resultArray = new Action[result.size()]; 00049 result.toArray(resultArray); 00050 return resultArray; 00051 } 00052 00053 static public Action[] createActions(double... actionValues) { 00054 Range[] ranges = new Range[actionValues.length]; 00055 for (int i = 0; i < ranges.length; i++) 00056 ranges[i] = new Range(-actionValues[i], actionValues[i]); 00057 return createActions(ranges); 00058 } 00059 00060 static public boolean isOneDimension(Action action) { 00061 return ((ActionArray) action).actions.length == 1; 00062 } 00063 00064 static public Map<Action, Integer> createActionIntMap(Action[] actions) { 00065 Map<Action, Integer> actionToIndex = new LinkedHashMap<Action, Integer>(); 00066 for (int i = 0; i < actions.length; i++) 00067 actionToIndex.put(actions[i], i); 00068 return actionToIndex; 00069 } 00070 }