| 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.MatrixParameter; |
| 29 |
import dr.inference.model.Parameter; |
| 30 |
import dr.inference.model.Variable; |
| 31 |
import dr.inference.operators.MCMCOperator; |
| 32 |
import dr.inference.operators.SimpleMCMCOperator; |
| 33 |
import dr.math.MathUtils; |
| 34 |
import dr.xml.*; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public class ClusterSplitMergeOperator extends SimpleMCMCOperator { |
| 44 |
public final static boolean DEBUG = false; |
| 45 |
|
| 46 |
public final static String CLUSTER_SPLIT_MERGE_OPERATOR = "clusterSplitMergeOperator"; |
| 47 |
|
| 48 |
private final int N; |
| 49 |
private int K; |
| 50 |
private final Parameter allocationParameter; |
| 51 |
private final MatrixParameter clusterLocations; |
| 52 |
|
| 53 |
public ClusterSplitMergeOperator(Parameter allocationParameter, |
| 54 |
MatrixParameter clusterLocations, |
| 55 |
double weight) { |
| 56 |
this.allocationParameter = allocationParameter; |
| 57 |
this.clusterLocations = clusterLocations; |
| 58 |
this.N = allocationParameter.getDimension(); |
| 59 |
|
| 60 |
setWeight(weight); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public Parameter getParameter() { |
| 68 |
return (Parameter) allocationParameter; |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public Variable getVariable() { |
| 75 |
return allocationParameter; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public final double doOperation() { |
| 82 |
|
| 83 |
|
| 84 |
int[] allocations = new int[allocationParameter.getDimension()]; |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
int[] occupancy = new int[N]; |
| 89 |
|
| 90 |
int[] occupiedIndices = new int[N]; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
for (int i = 0; i < occupiedIndices.length; i++) { |
| 95 |
occupiedIndices[i] = -1; |
| 96 |
} |
| 97 |
|
| 98 |
int K = 0; |
| 99 |
for (int i = 0; i < allocations.length; i++) { |
| 100 |
allocations[i] = (int) allocationParameter.getParameterValue(i); |
| 101 |
occupancy[allocations[i]] += 1; |
| 102 |
if (occupancy[allocations[i]] == 1) { |
| 103 |
occupiedIndices[K] = allocations[i]; |
| 104 |
K++; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
int paramDim = clusterLocations.getParameter(0).getDimension(); |
| 110 |
double[] splitDraw = new double[paramDim]; |
| 111 |
double scale = 1.0; |
| 112 |
double newClusterProb = 0.5; |
| 113 |
|
| 114 |
|
| 115 |
boolean doSplit = K == 1 || (K != N && MathUtils.nextBoolean()); |
| 116 |
|
| 117 |
if (doSplit) { |
| 118 |
|
| 119 |
|
| 120 |
int cluster1; |
| 121 |
do { |
| 122 |
|
| 123 |
cluster1 = occupiedIndices[MathUtils.nextInt(K)]; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
} while (occupancy[cluster1] == 0); |
| 128 |
|
| 129 |
|
| 130 |
int cluster2 = 0; |
| 131 |
while (occupancy[cluster2] > 0) { |
| 132 |
cluster2 ++; |
| 133 |
} |
| 134 |
|
| 135 |
int oldCount = occupancy[cluster1]; |
| 136 |
do { |
| 137 |
occupancy[cluster1] = 0; |
| 138 |
occupancy[cluster2] = 0; |
| 139 |
for (int i = 0; i < allocations.length; i++) { |
| 140 |
if (allocations[i] == cluster1 || allocations[i] == cluster2) { |
| 141 |
boolean putInNewCluster = MathUtils.nextDouble() < newClusterProb; |
| 142 |
if (putInNewCluster) { |
| 143 |
allocations[i] = cluster2; |
| 144 |
occupancy[cluster2]++; |
| 145 |
} else { |
| 146 |
allocations[i] = cluster1; |
| 147 |
occupancy[cluster1]++; |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
} while (occupancy[cluster1] != 0 && occupancy[cluster2] != 0); |
| 152 |
|
| 153 |
K++; |
| 154 |
|
| 155 |
|
| 156 |
Parameter param1 = clusterLocations.getParameter(cluster1); |
| 157 |
Parameter param2 = clusterLocations.getParameter(cluster2); |
| 158 |
|
| 159 |
double[] loc = param1.getParameterValues(); |
| 160 |
for (int dim = 0; dim < param1.getDimension(); dim++) { |
| 161 |
splitDraw[dim] = MathUtils.nextGaussian(); |
| 162 |
param1.setParameterValue(dim, loc[dim] + (splitDraw[dim] * scale)); |
| 163 |
param2.setParameterValue(dim, loc[dim] - (splitDraw[dim] * scale)); |
| 164 |
} |
| 165 |
|
| 166 |
if (DEBUG) { |
| 167 |
System.err.println("Split: " + (oldCount - occupancy[cluster1]) + " items from cluster " + cluster1 + " to create cluster " + cluster2); |
| 168 |
} |
| 169 |
} else { |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
int cluster1 = occupiedIndices[MathUtils.nextInt(K)]; |
| 174 |
int cluster2; |
| 175 |
|
| 176 |
do { |
| 177 |
cluster2 = occupiedIndices[MathUtils.nextInt(K)]; |
| 178 |
|
| 179 |
|
| 180 |
} while (cluster1 == cluster2); |
| 181 |
|
| 182 |
for (int i = 0; i < allocations.length; i++) { |
| 183 |
if (allocations[i] == cluster2) { |
| 184 |
allocations[i] = cluster1; |
| 185 |
|
| 186 |
|
| 187 |
occupancy[cluster1] ++; |
| 188 |
occupancy[cluster2] --; |
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
K--; |
| 193 |
|
| 194 |
|
| 195 |
Parameter loc1 = clusterLocations.getParameter(cluster1); |
| 196 |
Parameter loc2 = clusterLocations.getParameter(cluster2); |
| 197 |
for (int dim = 0; dim < loc1.getDimension(); dim++) { |
| 198 |
double average = (loc1.getParameterValue(dim) + loc2.getParameterValue(dim)) / 2.0; |
| 199 |
splitDraw[dim] = (loc1.getParameterValue(dim) - average) / scale; |
| 200 |
loc1.setParameterValue(dim, average); |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
} |
| 206 |
if (DEBUG) { |
| 207 |
System.err.println("Merge: " + occupancy[cluster1] + "items into cluster " + cluster1 + " from " + cluster2); |
| 208 |
} |
| 209 |
|
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
for (int i = 0; i < allocations.length; i++) { |
| 214 |
int k = (int) allocationParameter.getParameterValue(i); |
| 215 |
if (allocations[i] != k) { |
| 216 |
allocationParameter.setParameterValue(i, allocations[i]); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
return 0.0; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
public final String getOperatorName() { |
| 227 |
return CLUSTER_SPLIT_MERGE_OPERATOR +"(" + allocationParameter.getId() + ")"; |
| 228 |
} |
| 229 |
|
| 230 |
public final void optimize(double targetProb) { |
| 231 |
|
| 232 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 233 |
} |
| 234 |
|
| 235 |
public boolean isOptimizing() { |
| 236 |
return false; |
| 237 |
} |
| 238 |
|
| 239 |
public void setOptimizing(boolean opt) { |
| 240 |
throw new RuntimeException("This operator cannot be optimized!"); |
| 241 |
} |
| 242 |
|
| 243 |
public double getMinimumAcceptanceLevel() { |
| 244 |
return 0.1; |
| 245 |
} |
| 246 |
|
| 247 |
public double getMaximumAcceptanceLevel() { |
| 248 |
return 0.4; |
| 249 |
} |
| 250 |
|
| 251 |
public double getMinimumGoodAcceptanceLevel() { |
| 252 |
return 0.20; |
| 253 |
} |
| 254 |
|
| 255 |
public double getMaximumGoodAcceptanceLevel() { |
| 256 |
return 0.30; |
| 257 |
} |
| 258 |
|
| 259 |
public String getPerformanceSuggestion() { |
| 260 |
if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) { |
| 261 |
return ""; |
| 262 |
} else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) { |
| 263 |
return ""; |
| 264 |
} else { |
| 265 |
return ""; |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
public String toString() { |
| 270 |
return getOperatorName(); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 275 |
public final static String CHI = "chi"; |
| 276 |
public final static String LIKELIHOOD = "likelihood"; |
| 277 |
|
| 278 |
public String getParserName() { |
| 279 |
return CLUSTER_SPLIT_MERGE_OPERATOR; |
| 280 |
} |
| 281 |
|
| 282 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 283 |
|
| 284 |
double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 285 |
|
| 286 |
Parameter allocationParameter = (Parameter) xo.getChild(Parameter.class); |
| 287 |
|
| 288 |
MatrixParameter locationsParameter = (MatrixParameter) xo.getElementFirstChild("locations"); |
| 289 |
|
| 290 |
return new ClusterSplitMergeOperator(allocationParameter, locationsParameter, weight); |
| 291 |
|
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
public String getParserDescription() { |
| 299 |
return "An operator that splits and merges clusters."; |
| 300 |
} |
| 301 |
|
| 302 |
public Class getReturnType() { |
| 303 |
return ClusterSplitMergeOperator.class; |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 308 |
return rules; |
| 309 |
} |
| 310 |
|
| 311 |
private final XMLSyntaxRule[] rules = { |
| 312 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 313 |
new ElementRule(Parameter.class), |
| 314 |
new ElementRule("locations", new XMLSyntaxRule[] { |
| 315 |
new ElementRule(MatrixParameter.class) |
| 316 |
}) |
| 317 |
}; |
| 318 |
}; |
| 319 |
|
| 320 |
public int getStepCount() { |
| 321 |
return 1; |
| 322 |
} |
| 323 |
|
| 324 |
} |