| 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.Bounds; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inferencexml.operators.UniformOperatorParser; |
| 31 |
import dr.math.MathUtils; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class UniformOperator extends SimpleMCMCOperator { |
| 41 |
|
| 42 |
public UniformOperator(Parameter parameter, double weight) { |
| 43 |
this(parameter, weight, null, null); |
| 44 |
} |
| 45 |
|
| 46 |
public UniformOperator(Parameter parameter, double weight, Double lowerBound, Double upperBound) { |
| 47 |
this.parameter = parameter; |
| 48 |
setWeight(weight); |
| 49 |
|
| 50 |
this.lowerBound = lowerBound; |
| 51 |
this.upperBound = upperBound; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
public Parameter getParameter() { |
| 58 |
return parameter; |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public final double doOperation() { |
| 65 |
|
| 66 |
final int index = MathUtils.nextInt(parameter.getDimension()); |
| 67 |
final Bounds<Double> bounds = parameter.getBounds(); |
| 68 |
final double lower = (lowerBound == null ? bounds.getLowerLimit(index) : Math.max(bounds.getLowerLimit(index), lowerBound)); |
| 69 |
final double upper = (upperBound == null ? bounds.getUpperLimit(index) : Math.min(bounds.getUpperLimit(index), upperBound)); |
| 70 |
final double newValue = (MathUtils.nextDouble() * (upper - lower)) + lower; |
| 71 |
|
| 72 |
parameter.setParameterValue(index, newValue); |
| 73 |
|
| 74 |
|
| 75 |
return 0.0; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
public final String getOperatorName() { |
| 80 |
return "uniform(" + parameter.getParameterName() + ")"; |
| 81 |
} |
| 82 |
|
| 83 |
public final void optimize(double targetProb) { |
| 84 |
|
| 85 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 86 |
} |
| 87 |
|
| 88 |
public boolean isOptimizing() { |
| 89 |
return false; |
| 90 |
} |
| 91 |
|
| 92 |
public void setOptimizing(boolean opt) { |
| 93 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 94 |
} |
| 95 |
|
| 96 |
public String getPerformanceSuggestion() { |
| 97 |
return ""; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
} |
| 107 |
|
| 108 |
public String toString() { |
| 109 |
return UniformOperatorParser.UNIFORM_OPERATOR + "(" + parameter.getParameterName() + ")"; |
| 110 |
} |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private Parameter parameter = null; |
| 115 |
private final Double lowerBound; |
| 116 |
private final Double upperBound; |
| 117 |
} |