| 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 |
|
| 27 |
|
| 28 |
|
| 29 |
package dr.inference.operators; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
public abstract class SimpleOperator implements MCMCOperator { |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public SimpleOperator() { |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
public abstract String getOperatorName(); |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
public final double getWeight() { |
| 50 |
return weight; |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
public final void setWeight(double w) { |
| 57 |
if (w > 0) { |
| 58 |
weight = w; |
| 59 |
} else throw new IllegalArgumentException("Weight must be a positive real. (called with " + w + ")"); |
| 60 |
} |
| 61 |
|
| 62 |
public void accept(double deviation) { |
| 63 |
lastDeviation = deviation; |
| 64 |
|
| 65 |
if (!operateAllowed) { |
| 66 |
operateAllowed = true; |
| 67 |
accepted += 1; |
| 68 |
sumDeviation += deviation; |
| 69 |
} else throw new RuntimeException("Accept/reject methods called twice without operate called in between!"); |
| 70 |
} |
| 71 |
|
| 72 |
public void reject() { |
| 73 |
if (!operateAllowed) { |
| 74 |
operateAllowed = true; |
| 75 |
rejected += 1; |
| 76 |
} else throw new RuntimeException("Accept/reject methods called twice without operate called in between!"); |
| 77 |
} |
| 78 |
|
| 79 |
public final void reset() { |
| 80 |
operateAllowed = true; |
| 81 |
accepted = 0; |
| 82 |
rejected = 0; |
| 83 |
lastDeviation = 0.0; |
| 84 |
sumDeviation = 0.0; |
| 85 |
} |
| 86 |
|
| 87 |
public final long getCount() { |
| 88 |
return accepted + rejected; |
| 89 |
} |
| 90 |
|
| 91 |
public final long getAcceptCount() { |
| 92 |
return accepted; |
| 93 |
} |
| 94 |
|
| 95 |
public final void setAcceptCount(long accepted) { |
| 96 |
this.accepted = accepted; |
| 97 |
} |
| 98 |
|
| 99 |
public final long getRejectCount() { |
| 100 |
return rejected; |
| 101 |
} |
| 102 |
|
| 103 |
public final void setRejectCount(long rejected) { |
| 104 |
this.rejected = rejected; |
| 105 |
} |
| 106 |
|
| 107 |
public final double getMeanDeviation() { |
| 108 |
return sumDeviation / accepted; |
| 109 |
} |
| 110 |
|
| 111 |
public final double getDeviation() { |
| 112 |
return lastDeviation; |
| 113 |
} |
| 114 |
|
| 115 |
public final double getSumDeviation() { |
| 116 |
return sumDeviation; |
| 117 |
} |
| 118 |
|
| 119 |
public final void setSumDeviation(double sumDeviation) { |
| 120 |
this.sumDeviation = sumDeviation; |
| 121 |
} |
| 122 |
|
| 123 |
public final double getAcceptanceProbability() { |
| 124 |
return (double) accepted / (double) (accepted + rejected); |
| 125 |
} |
| 126 |
|
| 127 |
public double getSpan(boolean reset) { |
| 128 |
double span = 0; |
| 129 |
if( spanDeviation[1] > spanDeviation[0] && spanCount > 20 ) { |
| 130 |
span = spanDeviation[1] - spanDeviation[0]; |
| 131 |
|
| 132 |
if( reset ) { |
| 133 |
spanDeviation[0] = Double.MAX_VALUE; |
| 134 |
spanDeviation[1] = -Double.MAX_VALUE; |
| 135 |
spanCount = 0; |
| 136 |
} |
| 137 |
} |
| 138 |
return span; |
| 139 |
} |
| 140 |
|
| 141 |
public double getMeanEvaluationTime() { |
| 142 |
return (double) sumEvaluationTime / (double) (accepted + rejected); |
| 143 |
} |
| 144 |
|
| 145 |
public long getTotalEvaluationTime() { |
| 146 |
return sumEvaluationTime; |
| 147 |
} |
| 148 |
|
| 149 |
public void addEvaluationTime(long time) { |
| 150 |
sumEvaluationTime += time; |
| 151 |
} |
| 152 |
|
| 153 |
private long sumEvaluationTime = 0; |
| 154 |
|
| 155 |
private double[] spanDeviation = {Double.MAX_VALUE,-Double.MAX_VALUE}; |
| 156 |
private int spanCount = 0; |
| 157 |
|
| 158 |
private double weight = 1.0; |
| 159 |
private long accepted = 0; |
| 160 |
private long rejected = 0; |
| 161 |
private double sumDeviation = 0.0; |
| 162 |
private double lastDeviation = 0.0; |
| 163 |
protected boolean operateAllowed = true; |
| 164 |
|
| 165 |
} |