| 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.model.Parameter; |
| 29 |
import dr.inference.model.Variable; |
| 30 |
import dr.inferencexml.operators.UniformIntegerOperatorParser; |
| 31 |
import dr.math.MathUtils; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class UniformIntegerOperator extends SimpleMCMCOperator { |
| 41 |
private final int howMany; |
| 42 |
|
| 43 |
public UniformIntegerOperator(Parameter parameter, int lower, int upper, double weight, int howMany) { |
| 44 |
this(parameter, weight, howMany); |
| 45 |
this.lower = lower; |
| 46 |
this.upper = upper; |
| 47 |
} |
| 48 |
|
| 49 |
public UniformIntegerOperator(Parameter parameter, int lower, int upper, double weight) { |
| 50 |
this(parameter, lower, upper, weight, 1); |
| 51 |
} |
| 52 |
|
| 53 |
public UniformIntegerOperator(Variable parameter, double weight, int howMany) { |
| 54 |
this.parameter = parameter; |
| 55 |
this.howMany = howMany; |
| 56 |
setWeight(weight); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public Parameter getParameter() { |
| 63 |
return (Parameter) parameter; |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
public Variable getVariable() { |
| 70 |
return parameter; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public final double doOperation() { |
| 77 |
|
| 78 |
for (int n = 0; n < howMany; ++n) { |
| 79 |
|
| 80 |
int index = MathUtils.nextInt(parameter.getSize()); |
| 81 |
|
| 82 |
if (parameter instanceof Parameter) { |
| 83 |
int newValue = MathUtils.nextInt(upper - lower + 1) + lower; |
| 84 |
((Parameter) parameter).setParameterValue(index, newValue); |
| 85 |
} else { |
| 86 |
|
| 87 |
int upper = ((Variable<Integer>) parameter).getBounds().getUpperLimit(index); |
| 88 |
int lower = ((Variable<Integer>) parameter).getBounds().getLowerLimit(index); |
| 89 |
int newValue = MathUtils.nextInt(upper - lower + 1) + lower; |
| 90 |
((Variable<Integer>) parameter).setValue(index, newValue); |
| 91 |
|
| 92 |
} |
| 93 |
|
| 94 |
} |
| 95 |
|
| 96 |
return 0.0; |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
public final String getOperatorName() { |
| 101 |
return "uniformInteger(" + parameter.getId() + ")"; |
| 102 |
} |
| 103 |
|
| 104 |
public final void optimize(double targetProb) { |
| 105 |
|
| 106 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 107 |
} |
| 108 |
|
| 109 |
public boolean isOptimizing() { |
| 110 |
return false; |
| 111 |
} |
| 112 |
|
| 113 |
public void setOptimizing(boolean opt) { |
| 114 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 115 |
} |
| 116 |
|
| 117 |
public double getMinimumAcceptanceLevel() { |
| 118 |
return 0.1; |
| 119 |
} |
| 120 |
|
| 121 |
public double getMaximumAcceptanceLevel() { |
| 122 |
return 0.4; |
| 123 |
} |
| 124 |
|
| 125 |
public double getMinimumGoodAcceptanceLevel() { |
| 126 |
return 0.20; |
| 127 |
} |
| 128 |
|
| 129 |
public double getMaximumGoodAcceptanceLevel() { |
| 130 |
return 0.30; |
| 131 |
} |
| 132 |
|
| 133 |
public String getPerformanceSuggestion() { |
| 134 |
if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) { |
| 135 |
return ""; |
| 136 |
} else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) { |
| 137 |
return ""; |
| 138 |
} else { |
| 139 |
return ""; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
public String toString() { |
| 144 |
return UniformIntegerOperatorParser.UNIFORM_INTEGER_OPERATOR + "(" + parameter.getId() + ")"; |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
private Variable parameter = null; |
| 150 |
private int upper; |
| 151 |
private int lower; |
| 152 |
} |