RLPark 1.0.0
Reinforcement Learning Framework in Java

BVector.java

Go to the documentation of this file.
00001 package rlpark.plugin.rltoys.math.vector.implementations;
00002 
00003 import java.util.Arrays;
00004 
00005 import rlpark.plugin.rltoys.math.vector.BinaryVector;
00006 import rlpark.plugin.rltoys.math.vector.MutableVector;
00007 import rlpark.plugin.rltoys.math.vector.RealVector;
00008 import zephyr.plugin.core.api.monitoring.annotations.Monitor;
00009 
00010 
00011 public class BVector extends AbstractVector implements BinaryVector {
00012   private static final long serialVersionUID = 5688026326299722364L;
00013   public int[] activeIndexes;
00014   private final int[] indexesPosition;
00015   @Monitor
00016   int nbActive = 0;
00017 
00018   public BVector(int size) {
00019     super(size);
00020     activeIndexes = new int[10];
00021     indexesPosition = new int[size];
00022     Arrays.fill(indexesPosition, -1);
00023   }
00024 
00025   public BVector(BVector other) {
00026     this(other.getDimension());
00027     set(other);
00028   }
00029 
00030   @Override
00031   public void addSelfTo(double[] data) {
00032     for (int position = 0; position < nbActive; position++)
00033       data[activeIndexes[position]] += 1;
00034   }
00035 
00036   @Override
00037   public double dotProduct(double[] data) {
00038     double result = 0.0;
00039     for (int position = 0; position < nbActive; position++)
00040       result += data[activeIndexes[position]];
00041     return result;
00042   }
00043 
00044   @Override
00045   public void subtractSelfTo(double[] data) {
00046     for (int position = 0; position < nbActive; position++)
00047       data[activeIndexes[position]] -= 1;
00048   }
00049 
00050   @Override
00051   public SVector copyAsMutable() {
00052     return new SVector(this, 1);
00053   }
00054 
00055   @Override
00056   public BVector copy() {
00057     return new BVector(this);
00058   }
00059 
00060   @Override
00061   public double dotProduct(RealVector other) {
00062     double result = 0.0;
00063     for (int position = 0; position < nbActive; position++)
00064       result += other.getEntry(activeIndexes[position]);
00065     return result;
00066   }
00067 
00068   @Override
00069   public double getEntry(int index) {
00070     return indexesPosition[index] != -1 ? 1 : 0;
00071   }
00072 
00073   @Override
00074   public MutableVector mapMultiply(double d) {
00075     return copyAsMutable().mapMultiplyToSelf(d);
00076   }
00077 
00078   @Override
00079   public MutableVector newInstance(int size) {
00080     return new SVector(size);
00081   }
00082 
00083   @Override
00084   public BVector clear() {
00085     for (int i = 0; i < nbActive; i++)
00086       indexesPosition[activeIndexes[i]] = -1;
00087     nbActive = 0;
00088     return this;
00089   }
00090 
00091   protected void appendEntry(int index) {
00092     allocate(nbActive + 1);
00093     activeIndexes[nbActive] = index;
00094     indexesPosition[index] = nbActive;
00095     nbActive++;
00096   }
00097 
00098   protected void allocate(int sizeRequired) {
00099     if (activeIndexes.length >= sizeRequired)
00100       return;
00101     int newCapacity = (sizeRequired * 3) / 2 + 1;
00102     activeIndexes = Arrays.copyOf(activeIndexes, newCapacity);
00103   }
00104 
00105   protected void removeEntry(int position, int index) {
00106     assert position >= 0;
00107     swapEntry(nbActive - 1, position);
00108     indexesPosition[activeIndexes[nbActive - 1]] = -1;
00109     nbActive--;
00110   }
00111 
00112   private void swapEntry(int positionA, int positionB) {
00113     final int indexA = activeIndexes[positionA];
00114     final int indexB = activeIndexes[positionB];
00115     indexesPosition[indexA] = positionB;
00116     indexesPosition[indexB] = positionA;
00117     activeIndexes[positionA] = indexB;
00118     activeIndexes[positionB] = indexA;
00119   }
00120 
00121   public void removeEntry(int index) {
00122     int position = indexesPosition[index];
00123     if (position >= 0)
00124       removeEntry(position, index);
00125   }
00126 
00127   @Override
00128   public void setOn(int index) {
00129     assert index >= 0 && index < getDimension();
00130     int position = indexesPosition[index];
00131     if (position == -1)
00132       appendEntry(index);
00133   }
00134 
00135   @Override
00136   public String toString() {
00137     return Arrays.toString(getActiveIndexes());
00138   }
00139 
00140   @Override
00141   public int nonZeroElements() {
00142     return nbActive;
00143   }
00144 
00145   @Override
00146   final public int[] getActiveIndexes() {
00147     if (activeIndexes.length > nbActive)
00148       activeIndexes = Arrays.copyOf(activeIndexes, nbActive);
00149     return activeIndexes;
00150   }
00151 
00152   public void mergeSubVector(int start, BinaryVector other) {
00153     allocate(nbActive + other.nonZeroElements());
00154     for (int otherIndex : other.getActiveIndexes())
00155       setOn(start + otherIndex);
00156   }
00157 
00158   public void set(BinaryVector source) {
00159     clear();
00160     mergeSubVector(0, source);
00161   }
00162 
00163   public static BVector toBinary(double[] ds) {
00164     int[] is = new int[ds.length];
00165     for (int i = 0; i < is.length; i++)
00166       is[i] = (int) ds[i];
00167     return toBinary(is);
00168   }
00169 
00170   public static BVector toBinary(byte[] is) {
00171     BVector bobs = new BVector(is.length * Byte.SIZE);
00172     for (int i = 0; i < is.length; i++)
00173       for (int bi = 0; bi < Byte.SIZE; bi++) {
00174         int mask = 1 << bi;
00175         if ((is[i] & mask) != 0)
00176           bobs.setOn(i * Byte.SIZE + bi);
00177       }
00178     return bobs;
00179   }
00180 
00181   public static BVector toBinary(int[] is) {
00182     BVector bobs = new BVector(is.length * Integer.SIZE);
00183     for (int i = 0; i < is.length; i++)
00184       for (int bi = 0; bi < Integer.SIZE; bi++) {
00185         int mask = 1 << bi;
00186         if ((is[i] & mask) != 0)
00187           bobs.setOn(i * Integer.SIZE + bi);
00188       }
00189     return bobs;
00190   }
00191 
00192   public static BVector toBVector(int size, int[] orderedIndexes) {
00193     BVector result = new BVector(size);
00194     for (int i : orderedIndexes)
00195       result.setOn(i);
00196     return result;
00197   }
00198 
00199   @Override
00200   public double[] accessData() {
00201     double[] data = new double[size];
00202     for (int i : getActiveIndexes())
00203       data[i] = 1.0;
00204     return data;
00205   }
00206 
00207   @Override
00208   public int[] nonZeroIndexes() {
00209     return activeIndexes;
00210   }
00211 
00212   @Override
00213   public double sum() {
00214     return nonZeroElements();
00215   }
00216 }
 All Classes Namespaces Files Functions Variables Enumerations
Zephyr
RLPark