| 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.operators; |
| 27 |
|
| 28 |
import dr.inference.loggers.LogColumn; |
| 29 |
import dr.inference.loggers.Loggable; |
| 30 |
import dr.inference.loggers.NumberColumn; |
| 31 |
import dr.math.MathUtils; |
| 32 |
|
| 33 |
import java.util.ArrayList; |
| 34 |
import java.util.List; |
| 35 |
import java.util.logging.Logger; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class SimpleOperatorSchedule implements OperatorSchedule, Loggable { |
| 44 |
|
| 45 |
private final List<MCMCOperator> operators = new ArrayList<MCMCOperator>(); |
| 46 |
private final List<Integer> availableOperators = new ArrayList<Integer>(); |
| 47 |
private double totalWeight = 0; |
| 48 |
private int current = 0; |
| 49 |
private boolean sequential = false; |
| 50 |
private OptimizationTransform optimizationSchedule = OptimizationTransform.DEFAULT; |
| 51 |
|
| 52 |
int operatorUseThreshold = Integer.MAX_VALUE; |
| 53 |
double operatorAcceptanceThreshold = 0.0; |
| 54 |
|
| 55 |
public SimpleOperatorSchedule() { |
| 56 |
} |
| 57 |
|
| 58 |
public SimpleOperatorSchedule(int operatorUseThreshold, double operatorAcceptanceThreshold) { |
| 59 |
this.operatorUseThreshold = operatorUseThreshold; |
| 60 |
this.operatorAcceptanceThreshold = operatorAcceptanceThreshold; |
| 61 |
} |
| 62 |
|
| 63 |
public void addOperators(List<MCMCOperator> operators) { |
| 64 |
for (MCMCOperator operator : operators) { |
| 65 |
this.operators.add(operator); |
| 66 |
this.availableOperators.add(this.operators.size() - 1); |
| 67 |
} |
| 68 |
|
| 69 |
totalWeight = calculateTotalWeight(); |
| 70 |
|
| 71 |
} |
| 72 |
|
| 73 |
public void operatorsHasBeenUpdated() { |
| 74 |
totalWeight = calculateTotalWeight(); |
| 75 |
} |
| 76 |
|
| 77 |
public void addOperator(MCMCOperator op) { |
| 78 |
operators.add(op); |
| 79 |
availableOperators.add(operators.size() - 1); |
| 80 |
totalWeight = calculateTotalWeight(); |
| 81 |
} |
| 82 |
|
| 83 |
private double getWeight(int index) { |
| 84 |
return operators.get(availableOperators.get(index)).getWeight(); |
| 85 |
} |
| 86 |
|
| 87 |
private double calculateTotalWeight() { |
| 88 |
double totalWeight = 0.0; |
| 89 |
for (int i : availableOperators) { |
| 90 |
totalWeight += operators.get(i).getWeight(); |
| 91 |
} |
| 92 |
return totalWeight; |
| 93 |
} |
| 94 |
|
| 95 |
public int getNextOperatorIndex() { |
| 96 |
|
| 97 |
checkOperatorAcceptanceRates(); |
| 98 |
|
| 99 |
if (sequential) { |
| 100 |
int index = getWeightedOperatorIndex(current); |
| 101 |
current += 1; |
| 102 |
if (current >= totalWeight) { |
| 103 |
current = 0; |
| 104 |
} |
| 105 |
return index; |
| 106 |
} |
| 107 |
|
| 108 |
final double v = MathUtils.nextDouble(); |
| 109 |
|
| 110 |
return getWeightedOperatorIndex(v * totalWeight); |
| 111 |
} |
| 112 |
|
| 113 |
public void setSequential(boolean sequential) { |
| 114 |
this.sequential = sequential; |
| 115 |
} |
| 116 |
|
| 117 |
private int getWeightedOperatorIndex(double q) { |
| 118 |
int index = 0; |
| 119 |
double weight = getWeight(index); |
| 120 |
while (weight <= q) { |
| 121 |
index += 1; |
| 122 |
weight += getWeight(index); |
| 123 |
} |
| 124 |
return index; |
| 125 |
} |
| 126 |
|
| 127 |
public MCMCOperator getOperator(int index) { |
| 128 |
return operators.get(availableOperators.get(index)); |
| 129 |
} |
| 130 |
|
| 131 |
public int getOperatorCount() { |
| 132 |
return availableOperators.size(); |
| 133 |
} |
| 134 |
|
| 135 |
private void checkOperatorAcceptanceRates() { |
| 136 |
List<Integer> toRemove = new ArrayList<Integer>(); |
| 137 |
|
| 138 |
for (int i : availableOperators) { |
| 139 |
MCMCOperator op = operators.get(i); |
| 140 |
if (!(op instanceof AdaptableMCMCOperator) && op.getCount() > operatorUseThreshold) { |
| 141 |
double acceptanceRate = ((double)op.getAcceptCount()) / op.getCount(); |
| 142 |
if (acceptanceRate < operatorAcceptanceThreshold) { |
| 143 |
toRemove.add(i); |
| 144 |
Logger.getLogger("dr.app.beast").info("Operator " + op.getOperatorName() + |
| 145 |
" turned off with an acceptance rate of " + acceptanceRate + ", after " + op.getCount() + " tries."); |
| 146 |
|
| 147 |
} |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
if (!toRemove.isEmpty()) { |
| 152 |
availableOperators.removeAll(toRemove); |
| 153 |
totalWeight = calculateTotalWeight(); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
public double getOptimizationTransform(double d) { |
| 158 |
switch( optimizationSchedule ) { |
| 159 |
case DEFAULT: |
| 160 |
case LOG: return Math.log(d); |
| 161 |
case SQRT: return Math.sqrt(d); |
| 162 |
case LINEAR: return d; |
| 163 |
|
| 164 |
default: throw new UnsupportedOperationException("Unknown enum value"); |
| 165 |
} |
| 166 |
} |
| 167 |
|
| 168 |
public void setOptimizationSchedule(OptimizationTransform optimizationSchedule) { |
| 169 |
this.optimizationSchedule = optimizationSchedule; |
| 170 |
} |
| 171 |
|
| 172 |
public long getMinimumAcceptAndRejectCount() { |
| 173 |
long minCount = Long.MAX_VALUE; |
| 174 |
for( MCMCOperator op : operators ) { |
| 175 |
if( op.getAcceptCount() < minCount || op.getRejectCount() < minCount ) { |
| 176 |
minCount = op.getCount(); |
| 177 |
} |
| 178 |
} |
| 179 |
return minCount; |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
public LogColumn[] getColumns() { |
| 190 |
List<LogColumn> columnList = new ArrayList<LogColumn>(); |
| 191 |
for (int i = 0; i < getOperatorCount(); i++) { |
| 192 |
MCMCOperator op = getOperator(i); |
| 193 |
columnList.add(new OperatorAcceptanceColumn(op.getOperatorName(), op)); |
| 194 |
if (op instanceof AdaptableMCMCOperator) { |
| 195 |
columnList.add(new OperatorSizeColumn(op.getOperatorName() + "_size", (AdaptableMCMCOperator)op)); |
| 196 |
} |
| 197 |
} |
| 198 |
LogColumn[] columns = columnList.toArray(new LogColumn[columnList.size()]); |
| 199 |
return columns; |
| 200 |
} |
| 201 |
|
| 202 |
private class OperatorAcceptanceColumn extends NumberColumn { |
| 203 |
private final MCMCOperator op; |
| 204 |
|
| 205 |
public OperatorAcceptanceColumn(String label, MCMCOperator op) { |
| 206 |
super(label); |
| 207 |
this.op = op; |
| 208 |
} |
| 209 |
|
| 210 |
public double getDoubleValue() { |
| 211 |
return MCMCOperator.Utils.getAcceptanceProbability(op); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
private class OperatorSizeColumn extends NumberColumn { |
| 216 |
private final AdaptableMCMCOperator op; |
| 217 |
|
| 218 |
public OperatorSizeColumn(String label, AdaptableMCMCOperator op) { |
| 219 |
super(label); |
| 220 |
this.op = op; |
| 221 |
} |
| 222 |
|
| 223 |
public double getDoubleValue() { |
| 224 |
return op.getRawParameter(); |
| 225 |
} |
| 226 |
} |
| 227 |
} |