RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.rltoys.algorithms.representations.discretizer.partitions; 00002 00003 import java.util.Random; 00004 00005 import rlpark.plugin.rltoys.algorithms.representations.discretizer.Discretizer; 00006 import rlpark.plugin.rltoys.algorithms.representations.discretizer.DiscretizerFactory; 00007 import rlpark.plugin.rltoys.math.ranges.Range; 00008 00009 public abstract class AbstractPartitionFactory implements DiscretizerFactory { 00010 static public abstract class AbstractPartition implements Discretizer { 00011 private static final long serialVersionUID = 5477929434176764517L; 00012 public final int resolution; 00013 public final double intervalWidth; 00014 public final double min; 00015 public final double max; 00016 00017 public AbstractPartition(double min, double max, int resolution) { 00018 this.min = min; 00019 this.max = max; 00020 this.resolution = resolution; 00021 intervalWidth = (max - min) / resolution; 00022 } 00023 00024 @Override 00025 public String toString() { 00026 return String.format("[%f:%f]/%d", min, max, resolution); 00027 } 00028 00029 @Override 00030 public int resolution() { 00031 return resolution; 00032 } 00033 00034 @Override 00035 abstract public int discretize(double input); 00036 } 00037 00038 private static final long serialVersionUID = 3356344048646899647L; 00039 protected final Range[] ranges; 00040 private double randomShiftRatio = Double.NaN; 00041 private Random random; 00042 00043 public AbstractPartitionFactory(Range... ranges) { 00044 this.ranges = ranges; 00045 } 00046 00047 public void setRandom(Random random, double randomShiftRatio) { 00048 this.random = random; 00049 this.randomShiftRatio = randomShiftRatio; 00050 } 00051 00052 protected double computeShift(double offset, int tilingIndex, int inputIndex) { 00053 double result = tilingIndex * offset; 00054 if (random != null) 00055 return result - random.nextFloat() * offset * randomShiftRatio / 2.0; 00056 return result; 00057 } 00058 }