RLPark 1.0.0
Reinforcement Learning Framework in Java

History.java

Go to the documentation of this file.
00001 package rlpark.plugin.rltoys.math;
00002 
00003 import java.util.Arrays;
00004 
00005 public class History {
00006   final public int length;
00007   private final float[] history;
00008   private int shift = 0;
00009   private double sum = 0.0;
00010 
00011   public History(int length) {
00012     assert length > 0;
00013     history = allocate(length);
00014     Arrays.fill(history, 0.0f);
00015     this.length = length;
00016   }
00017 
00018   public void reset() {
00019     Arrays.fill(history, 0.0f);
00020     sum = 0.0;
00021     shift = 0;
00022   }
00023 
00024   protected float[] allocate(int length) {
00025     return new float[length];
00026   }
00027 
00028   public void append(double value) {
00029     int index = index(shift);
00030     updateSum(history[index], value);
00031     history[index] = (float) value;
00032     shift += 1;
00033   }
00034 
00035   private void updateSum(double oldValue, double value) {
00036     sum = sum - oldValue + value;
00037   }
00038 
00039   protected int index(int index) {
00040     return (index + 2 * length) % length;
00041   }
00042 
00043   @Override
00044   public String toString() {
00045     return Arrays.toString(history);
00046   }
00047 
00048   public double sum() {
00049     return sum;
00050   }
00051 
00052   synchronized public float[] toArray(float[] result) {
00053     for (int i = result.length; i > 0; i--) {
00054       int index = index(shift - i);
00055       if (index < history.length)
00056         result[result.length - i] = history[index];
00057       else
00058         result[result.length - i] = 0;
00059     }
00060     return result;
00061   }
00062 
00063   public float[] toArray() {
00064     return toArray(new float[length]);
00065   }
00066 
00067   public void fill(double value) {
00068     for (int i = 0; i < length; i++)
00069       append(value);
00070   }
00071 
00072   public double newest() {
00073     return get(length - 1);
00074   }
00075 
00076   public double oldest() {
00077     return get(0);
00078   }
00079 
00080   public double get(int x) {
00081     return history[index(shift + x)];
00082   }
00083 
00084   public double capacity() {
00085     return history.length;
00086   }
00087 
00088   public int nbAdded() {
00089     return shift;
00090   }
00091 
00092   public float[] data() {
00093     return history;
00094   }
00095 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark