RLPark 1.0.0
Reinforcement Learning Framework in Java
|
00001 package rlpark.plugin.rltoys.algorithms.traces; 00002 00003 import rlpark.plugin.rltoys.math.vector.RealVector; 00004 import rlpark.plugin.rltoys.math.vector.implementations.SSortedVector; 00005 00006 public class MaxLengthTraces implements Traces { 00007 private static final long serialVersionUID = 2392872021978375762L; 00008 private final Traces traces; 00009 private final int maximumLength; 00010 00011 public MaxLengthTraces(Traces traces, int maximumLength) { 00012 this.traces = traces; 00013 this.maximumLength = maximumLength; 00014 if (!(traces.newTraces(1).vect() instanceof SSortedVector)) 00015 throw new RuntimeException("MaxLengthTraces supports only traces with a SSortedVector prototype"); 00016 } 00017 00018 @Override 00019 public Traces newTraces(int size) { 00020 return new MaxLengthTraces(traces.newTraces(size), maximumLength); 00021 } 00022 00023 @Override 00024 public void update(double lambda, RealVector phi) { 00025 traces.update(lambda, phi); 00026 controlLength(); 00027 } 00028 00029 @Override 00030 public void clear() { 00031 traces.clear(); 00032 } 00033 00034 @Override 00035 public SSortedVector vect() { 00036 return (SSortedVector) traces.vect(); 00037 } 00038 00039 private void controlLength() { 00040 int superfluousElements = vect().nonZeroElements() - maximumLength; 00041 if (superfluousElements <= 0) 00042 return; 00043 vect().removeTail(superfluousElements); 00044 } 00045 }