Debian Codegraph @

src/dr/evomodel/antigenic/phyloclustering/misc/obsolete/ClusterWalkOperator.java

raw symbols SCIP
1 package dr.evomodel.antigenic.phyloclustering.misc.obsolete;
2
3
4 import dr.inference.model.Bounds;
5 import dr.inference.model.Parameter;
6 import dr.inference.operators.*;
7 import dr.math.MathUtils;
8
9 import java.util.ArrayList;
10 import java.util.List;
11
12
13 public class ClusterWalkOperator extends AbstractAdaptableOperator {
14
15
16
17 public enum BoundaryCondition {
18 reflecting,
19 absorbing
20 }
21
22 public ClusterWalkOperator(Parameter parameter, double windowSize, BoundaryCondition bc, double weight, AdaptationMode mode) {
23 this(parameter, null, windowSize, bc, weight, mode);
24 }
25
26 public ClusterWalkOperator(Parameter parameter, Parameter updateIndex, double windowSize, BoundaryCondition bc,
27 double weight, AdaptationMode mode) {
28 this(parameter, updateIndex, windowSize, bc, weight, mode, null, null);
29 }
30
31 public ClusterWalkOperator(Parameter parameter, Parameter updateIndex, double windowSize, BoundaryCondition bc,
32 double weight, AdaptationMode mode, Double lowerOperatorBound, Double upperOperatorBound) {
33 super(mode);
34 this.parameter = parameter;
35 this.windowSize = windowSize;
36 this.condition = bc;
37
38 setWeight(weight);
39 if (updateIndex != null) {
40 updateMap = new ArrayList<Integer>();
41 for (int i = 0; i < updateIndex.getDimension(); i++) {
42 if (updateIndex.getParameterValue(i) == 1.0)
43 updateMap.add(i);
44 }
45 }
46
47 this.lowerOperatorBound = lowerOperatorBound;
48 this.upperOperatorBound = upperOperatorBound;
49 }
50
51 /**
52 * @return the parameter this operator acts on.
53 */
54 public Parameter getParameter() {
55 return parameter;
56 }
57
58 public final double getWindowSize() {
59 return windowSize;
60 }
61
62 /**
63 * change the parameter and return the hastings ratio.
64 */
65 public final double doOperation() {
66 System.out.println("Walking cluster");
67
68 // a random dimension to perturb
69 int index;
70 if (updateMap == null) {
71 index = MathUtils.nextInt(parameter.getDimension());
72 } else {
73 index = updateMap.get(MathUtils.nextInt(updateMap.size()));
74 }
75
76 // a random point around old value within windowSize * 2
77 double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize;
78 double newValue = parameter.getParameterValue(index) + draw;
79
80 final Bounds<Double> bounds = parameter.getBounds();
81 final double lower = (lowerOperatorBound == null ? bounds.getLowerLimit(index) : Math.max(bounds.getLowerLimit(index), lowerOperatorBound));
82 final double upper = (upperOperatorBound == null ? bounds.getUpperLimit(index) : Math.min(bounds.getUpperLimit(index), upperOperatorBound));
83
84 if (condition == BoundaryCondition.reflecting) {
85 newValue = RandomWalkOperator.reflectValue(newValue, lower, upper);
86 } else if (newValue < lower || newValue > upper) {
87 // throw new OperatorFailedException("proposed value outside boundaries");
88 return Double.NEGATIVE_INFINITY;
89 }
90
91 parameter.setParameterValue(index, newValue);
92
93 return 0.0;
94 }
95
96 //MCMCOperator INTERFACE
97 public final String getOperatorName() {
98 return parameter.getParameterName();
99 }
100
101 public double getAdaptableParameter() {
102 return Math.log(windowSize);
103 }
104
105 public void setAdaptableParameter(double value) {
106 windowSize = Math.exp(value);
107 }
108
109 public double getRawParameter() {
110 return windowSize;
111 }
112
113 public String getAdaptableParameterName() {
114 return "windowSize";
115 }
116
117 public String toString() {
118 return ClusterWalkOperatorParser.CLUSTER_WALK_OPERATOR + "(" + parameter.getParameterName() + ", " + windowSize + ", " + getWeight() + ")";
119 }
120
121 //PRIVATE STUFF
122
123 private Parameter parameter = null;
124 private double windowSize = 0.01;
125 private List<Integer> updateMap = null;
126 private final BoundaryCondition condition;
127
128 private final Double lowerOperatorBound;
129 private final Double upperOperatorBound;
130 }