caches recent evals (de-bounce fn evals so other code can be written simply) keeps best eval (maximal) @author johnmount
| 14 | * |
| 15 | */ |
| 16 | public final class SFun implements ScalarFn { |
| 17 | public final VectorFn f; |
| 18 | public final double[] x0; |
| 19 | public final double[] dir; |
| 20 | public final double boxBound; |
| 21 | public VEval min = null; |
| 22 | public VEval max = null; |
| 23 | private final Map<Double,VEval> cache = new LinkedHashMap<Double,VEval>() { |
| 24 | private static final long serialVersionUID = 1L; |
| 25 | |
| 26 | @Override |
| 27 | protected boolean removeEldestEntry(final Map.Entry<Double,VEval> eldest) { |
| 28 | return size()>10; |
| 29 | } |
| 30 | }; |
| 31 | |
| 32 | /** |
| 33 | * |
| 34 | * @param f funciton |
| 35 | * @param x0 point to evaluate around |
| 36 | * @param dir direction to move |
| 37 | * @param boxBound boundingBox on coords |
| 38 | * @param fx0 if not null f(x0) |
| 39 | */ |
| 40 | public SFun(final VectorFn f, final double[] x0, final double[] dir, final double boxBound, final VEval fx0) { |
| 41 | this.f = f; |
| 42 | this.x0 = x0; |
| 43 | this.dir = dir; |
| 44 | this.boxBound = boxBound; |
| 45 | if(fx0!=null) { |
| 46 | if((!Double.isInfinite(fx0.fx))&&(!Double.isNaN(fx0.fx))) { |
| 47 | min = fx0; |
| 48 | max = fx0; |
| 49 | } |
| 50 | cache.put(0.0,fx0); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | @Override |
| 55 | public double eval(final double s) { |
| 56 | VEval fx = cache.get(s); |
| 57 | if(null==fx) { |
| 58 | final double[] newX = newX(x0,dir,s,boxBound); |
| 59 | fx = f.eval(newX,false,false); |
| 60 | if((!Double.isInfinite(fx.fx))&&(!Double.isNaN(fx.fx))) { |
| 61 | if((min==null)||(fx.fx<min.fx)) { |
| 62 | min = fx; |
| 63 | } |
| 64 | if((max==null)||(fx.fx>max.fx)) { |
| 65 | max = fx; |
| 66 | } |
| 67 | } |
| 68 | cache.put(s,fx); |
| 69 | } |
| 70 | return fx.fx; |
| 71 | } |
| 72 | |
| 73 | static double[] newX(final double[] oldX, final double[] delta, final double scale, final double boxBound) { |
nothing calls this directly
no outgoing calls
no test coverage detected