RLPark 1.0.0
Reinforcement Learning Framework in Java

Range.java

Go to the documentation of this file.
00001 
00004 package rlpark.plugin.rltoys.math.ranges;
00005 
00006 import java.io.Serializable;
00007 import java.util.Random;
00008 
00009 import zephyr.plugin.core.api.monitoring.annotations.Monitor;
00010 
00011 @Monitor
00012 public class Range implements Serializable {
00013   private static final long serialVersionUID = 3076267038250925863L;
00014   private double min;
00015   private double max;
00016 
00017   public Range() {
00018     this(Double.MAX_VALUE, -Double.MAX_VALUE);
00019   }
00020 
00021 
00022   public Range(Range range) {
00023     this(range.min, range.max);
00024   }
00025 
00026   public Range(double min, double max) {
00027     this.min = min;
00028     this.max = max;
00029   }
00030 
00031   public double bound(double value) {
00032     return Math.max(min, Math.min(max, value));
00033   }
00034 
00035   public double choose(Random random) {
00036     return random.nextFloat() * length() + min;
00037   }
00038 
00039   public boolean in(double value) {
00040     return value >= min && value <= max;
00041   }
00042 
00043   @Override
00044   public boolean equals(Object object) {
00045     if (super.equals(object))
00046       return true;
00047     Range other = (Range) object;
00048     return min == other.min && max == other.max;
00049   }
00050 
00051   @Override
00052   public int hashCode() {
00053     return (int) (min + max);
00054   }
00055 
00056   public double length() {
00057     return max - min;
00058   }
00059 
00060   public double[] sample(final int nbValue) {
00061     double[] values = new double[nbValue];
00062     double sampleSize = (max - min) / nbValue;
00063     for (int i = 0; i < values.length; i++)
00064       values[i] = i * sampleSize + min;
00065     return values;
00066   }
00067 
00068   public double center() {
00069     return min + (length() / 2);
00070   }
00071 
00072   @Override
00073   public String toString() {
00074     return String.format("[%f,%f]", min, max);
00075   }
00076 
00077   public void update(double value) {
00078     min = Math.min(value, min);
00079     max = Math.max(value, max);
00080   }
00081 
00082   public double min() {
00083     return min;
00084   }
00085 
00086   public double max() {
00087     return max;
00088   }
00089 
00090   public void reset() {
00091     min = Double.MAX_VALUE;
00092     max = -Double.MAX_VALUE;
00093   }
00094 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark