| 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.evomodel.antigenic; |
| 27 |
|
| 28 |
import dr.inference.model.Parameter; |
| 29 |
import dr.inference.model.Variable; |
| 30 |
import dr.inference.operators.MCMCOperator; |
| 31 |
import dr.inference.operators.SimpleMCMCOperator; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public class ClusterSingleMoveOperator extends SimpleMCMCOperator { |
| 46 |
public final static boolean DEBUG = false; |
| 47 |
|
| 48 |
public final static String CLUSTER_SINGLE_MOVE_OPERATOR = "clusterSingleMoveOperator"; |
| 49 |
|
| 50 |
private final int N; |
| 51 |
private int K; |
| 52 |
private final Parameter allocationParameter; |
| 53 |
|
| 54 |
public ClusterSingleMoveOperator(Parameter allocationParameter, double weight) { |
| 55 |
this.allocationParameter = allocationParameter; |
| 56 |
this.N = allocationParameter.getDimension(); |
| 57 |
|
| 58 |
setWeight(weight); |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public Parameter getParameter() { |
| 66 |
return (Parameter) allocationParameter; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public Variable getVariable() { |
| 73 |
return allocationParameter; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
public final double doOperation() { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
int[] allocations = new int[allocationParameter.getDimension()]; |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
int[] occupancy = new int[N]; |
| 89 |
|
| 90 |
int K = 0; |
| 91 |
for (int i = 0; i < allocations.length; i++) { |
| 92 |
allocations[i] = (int) allocationParameter.getParameterValue(i); |
| 93 |
occupancy[allocations[i]] += 1; |
| 94 |
if (occupancy[allocations[i]] == 1) { |
| 95 |
K++; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
double hastings = 0.0; |
| 101 |
|
| 102 |
|
| 103 |
int element = MathUtils.nextInt(N); |
| 104 |
int elementAssignment = allocations[element]; |
| 105 |
int elementClusterSize = occupancy[elementAssignment]; |
| 106 |
|
| 107 |
|
| 108 |
int target = MathUtils.nextInt(N); |
| 109 |
while (element == target) { |
| 110 |
target = MathUtils.nextInt(N); |
| 111 |
} |
| 112 |
int targetAssignment = allocations[target]; |
| 113 |
int targetClusterSize = occupancy[targetAssignment]; |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
if (elementAssignment != targetAssignment) { |
| 118 |
|
| 119 |
allocations[element] = targetAssignment; |
| 120 |
allocationParameter.setParameterValue(element, targetAssignment); |
| 121 |
|
| 122 |
if (elementClusterSize > 1) { |
| 123 |
|
| 124 |
hastings = Math.log(elementClusterSize - 1) - Math.log(targetClusterSize); |
| 125 |
} |
| 126 |
|
| 127 |
if (DEBUG) { |
| 128 |
System.err.println("Move element " + element + " from cluster " + elementAssignment + " to cluster " + targetAssignment); |
| 129 |
} |
| 130 |
|
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
else { |
| 136 |
|
| 137 |
|
| 138 |
int clusterIndex = 0; |
| 139 |
int targetIndex = MathUtils.nextInt(N-K); |
| 140 |
int newCluster = 0; |
| 141 |
for (int i = 0; i < N; i++) { |
| 142 |
if (occupancy[i] == 0){ |
| 143 |
if (clusterIndex == targetIndex) { |
| 144 |
newCluster = i; |
| 145 |
} |
| 146 |
clusterIndex++; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
while (occupancy[newCluster] > 0) { |
| 151 |
newCluster++; |
| 152 |
} |
| 153 |
|
| 154 |
|
| 155 |
allocations[element] = newCluster; |
| 156 |
allocationParameter.setParameterValue(element, newCluster); |
| 157 |
|
| 158 |
if (DEBUG) { |
| 159 |
System.err.println("Move element " + element + " from cluster " + elementAssignment + " to new cluster " + newCluster); |
| 160 |
} |
| 161 |
|
| 162 |
} |
| 163 |
|
| 164 |
|
| 165 |
return hastings; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
public final String getOperatorName() { |
| 171 |
return CLUSTER_SINGLE_MOVE_OPERATOR +"(" + allocationParameter.getId() + ")"; |
| 172 |
} |
| 173 |
|
| 174 |
public final void optimize(double targetProb) { |
| 175 |
|
| 176 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 177 |
} |
| 178 |
|
| 179 |
public boolean isOptimizing() { |
| 180 |
return false; |
| 181 |
} |
| 182 |
|
| 183 |
public void setOptimizing(boolean opt) { |
| 184 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 185 |
} |
| 186 |
|
| 187 |
public double getMinimumAcceptanceLevel() { |
| 188 |
return 0.1; |
| 189 |
} |
| 190 |
|
| 191 |
public double getMaximumAcceptanceLevel() { |
| 192 |
return 0.4; |
| 193 |
} |
| 194 |
|
| 195 |
public double getMinimumGoodAcceptanceLevel() { |
| 196 |
return 0.20; |
| 197 |
} |
| 198 |
|
| 199 |
public double getMaximumGoodAcceptanceLevel() { |
| 200 |
return 0.30; |
| 201 |
} |
| 202 |
|
| 203 |
public String getPerformanceSuggestion() { |
| 204 |
if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) { |
| 205 |
return ""; |
| 206 |
} else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) { |
| 207 |
return ""; |
| 208 |
} else { |
| 209 |
return ""; |
| 210 |
} |
| 211 |
} |
| 212 |
|
| 213 |
public String toString() { |
| 214 |
return getOperatorName(); |
| 215 |
} |
| 216 |
|
| 217 |
|
| 218 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 219 |
|
| 220 |
public String getParserName() { |
| 221 |
return CLUSTER_SINGLE_MOVE_OPERATOR; |
| 222 |
} |
| 223 |
|
| 224 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 225 |
|
| 226 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 227 |
|
| 228 |
Parameter allocationParameter = (Parameter) xo.getChild(Parameter.class); |
| 229 |
|
| 230 |
return new ClusterSingleMoveOperator(allocationParameter, weight); |
| 231 |
|
| 232 |
} |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
public String getParserDescription() { |
| 239 |
return "An operator that moves single elements between clusters."; |
| 240 |
} |
| 241 |
|
| 242 |
public Class getReturnType() { |
| 243 |
return ClusterSingleMoveOperator.class; |
| 244 |
} |
| 245 |
|
| 246 |
|
| 247 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 248 |
return rules; |
| 249 |
} |
| 250 |
|
| 251 |
private final XMLSyntaxRule[] rules = { |
| 252 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 253 |
new ElementRule(Parameter.class) |
| 254 |
}; |
| 255 |
}; |
| 256 |
|
| 257 |
public int getStepCount() { |
| 258 |
return 1; |
| 259 |
} |
| 260 |
|
| 261 |
} |