| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
package dr.inference.model; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class ValuesPool extends DefaultModel { |
| 33 |
private final Variable<Double> pool; |
| 34 |
private final Variable<Double> selector; |
| 35 |
private final double defaultValue; |
| 36 |
|
| 37 |
ValuesPool(Variable<Double> pool, Variable<Double> selector, Double defaultValue) { |
| 38 |
this.pool = pool; |
| 39 |
this.selector = selector; |
| 40 |
this.defaultValue = defaultValue; |
| 41 |
|
| 42 |
assert pool.getSize() == selector.getSize(); |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
addVariable(pool); |
| 48 |
addVariable(selector); |
| 49 |
|
| 50 |
addStatistic(numberOfParams); |
| 51 |
} |
| 52 |
|
| 53 |
public Variable<Double> getPool() { |
| 54 |
return pool; |
| 55 |
} |
| 56 |
|
| 57 |
public Variable<Double> getSelector() { |
| 58 |
return selector; |
| 59 |
} |
| 60 |
|
| 61 |
public int length() { |
| 62 |
return pool.getSize(); |
| 63 |
} |
| 64 |
|
| 65 |
private int get(int index) { |
| 66 |
final double s = selector.getValue(index); |
| 67 |
return s < 0 ? -1 : (int)(s+0.5); |
| 68 |
} |
| 69 |
|
| 70 |
public Double getValue(int index) { |
| 71 |
final int s = get(index); |
| 72 |
return s < 0 ? defaultValue : pool.getValue(s); |
| 73 |
} |
| 74 |
|
| 75 |
public Bounds<Double> getBounds() { |
| 76 |
return bounds; |
| 77 |
} |
| 78 |
|
| 79 |
private final Bounds<Double> bounds = new Bounds<Double>() { |
| 80 |
public Double getUpperLimit(int dimension) { |
| 81 |
final int s = get(dimension); |
| 82 |
return s < 0 ? defaultValue : pool.getBounds().getUpperLimit(s); |
| 83 |
} |
| 84 |
|
| 85 |
public Double getLowerLimit(int dimension) { |
| 86 |
final int s = get(dimension); |
| 87 |
return s < 0 ? defaultValue : pool.getBounds().getLowerLimit(s); |
| 88 |
} |
| 89 |
|
| 90 |
public int getBoundsDimension() { |
| 91 |
return pool.getSize(); |
| 92 |
} |
| 93 |
}; |
| 94 |
|
| 95 |
@SuppressWarnings({"FieldCanBeLocal"}) |
| 96 |
private final Statistic numberOfParams = new Statistic.Abstract() { |
| 97 |
|
| 98 |
public String getStatisticName() { |
| 99 |
return "numberOfParams"; |
| 100 |
} |
| 101 |
|
| 102 |
public int getDimension() { |
| 103 |
return 1; |
| 104 |
} |
| 105 |
|
| 106 |
public double getStatisticValue(int dim) { |
| 107 |
double m = -1; |
| 108 |
for(int k = 0; k < selector.getSize(); ++k) { |
| 109 |
final Double sk = selector.getValue(k); |
| 110 |
if( sk > m ) { |
| 111 |
m = sk; |
| 112 |
} |
| 113 |
} |
| 114 |
return m+1; |
| 115 |
} |
| 116 |
}; |
| 117 |
|
| 118 |
public boolean hasChanged(int i, Object object, int index) { |
| 119 |
if( object == pool ) { |
| 120 |
if( get(i) == index ) { |
| 121 |
return true; |
| 122 |
} |
| 123 |
} else if ( object == selector ) { |
| 124 |
if( i == index ) { |
| 125 |
return true; |
| 126 |
} |
| 127 |
} |
| 128 |
return false; |
| 129 |
} |
| 130 |
} |