Debian Codegraph @

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

raw symbols SCIP
1 package dr.evomodel.antigenic.phyloclustering.misc.obsolete;
2
3 import dr.evomodel.antigenic.NPAntigenicLikelihood;
4 import dr.inference.model.Likelihood;
5 import dr.inference.model.MatrixParameter;
6 import dr.inference.model.Parameter;
7 import dr.inference.operators.MCMCOperator;
8 import dr.inference.operators.SimpleMCMCOperator;
9 import dr.math.MathUtils;
10 import dr.xml.*;
11
12
13
14 public class ClusterOperator extends SimpleMCMCOperator {
15
16
17
18 public final static String CLUSTER_OPERATOR = "clusterOperator";
19 public static final String WINDOW_SIZE = "windowSize";
20
21
22 public NPAntigenicLikelihood modelLikelihood;
23 private Parameter assignments = null;
24 private Parameter links = null;
25
26 private MatrixParameter virusLocations = null;
27 private double windowSize = 1;
28
29
30
31 public ClusterOperator(Parameter assignments, MatrixParameter virusLocations, double weight, double windowSize, NPAntigenicLikelihood Likelihood, Parameter links) {
32 //System.out.println("Constructor");
33 this.links = links;
34 this.assignments = assignments;
35 this.virusLocations = virusLocations;
36 this.windowSize = windowSize;
37 this.modelLikelihood = Likelihood;
38
39 setWeight(weight);
40
41
42 //load in the virusLocations
43 System.out.println((int)assignments.getParameterValue(0));
44 Parameter vLoc = virusLocations.getParameter(0);
45
46 //double d0= vLoc.getParameterValue(0);
47 //double d1= vLoc.getParameterValue(1);
48 //System.out.println( d0 + "," + d1);
49
50
51 //System.exit(0);
52
53 }
54
55
56
57
58 //assume no boundary
59 /**
60 * change the parameter and return the hastings ratio.
61 */
62 public final double doOperation() {
63
64 double moveWholeClusterOrChangeOnePoint = MathUtils.nextDouble();
65
66 //double moveWholeClusterProb = 0.2;
67 double moveWholeClusterProb = 0.5;
68
69 int numSamples = assignments.getDimension();
70
71 // if(moveWholeClusterOrChangeOnePoint <moveWholeClusterProb){
72 // System.out.print("Group changes: ");
73 //System.out.println("Move a group of points in a cluster together");
74 int target = MathUtils.nextInt(numSamples);
75 int targetCluster = (int) assignments.getParameterValue(target);
76
77
78 //for(int i=0; i < numSamples; i++){
79 //Parameter vLoc = virusLocations.getParameter(i);
80 //double d0= vLoc.getParameterValue(0);
81 //double d1= vLoc.getParameterValue(1);
82 //System.out.println( d0 + "," + d1);
83 //}
84
85 // a random dimension to perturb
86 int ranDim= MathUtils.nextInt(virusLocations.getParameter(0).getDimension());
87 // a random point around old value within windowSize * 2
88 double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize;
89
90
91
92 for(int i=0; i < numSamples; i++){
93 if((int) assignments.getParameterValue(i)== targetCluster ){
94 //System.out.print("update "+ i +" from ");
95 Parameter vLoc = virusLocations.getParameter(i);
96 //System.out.print(vLoc.getParameterValue(index));
97 double newValue = vLoc.getParameterValue(ranDim) + draw;
98 vLoc.setParameterValue(ranDim, newValue);
99 //System.out.println(" to " + vLoc.getParameterValue(index));
100 }
101 }
102
103 // }
104
105 /*
106 //change cluster assignment!
107 else{
108 System.out.print("Cluster assignment & Group move:");
109 //System.out.println("Change a single point to another cluster");
110 //moving the point without coupling the point is useless!
111
112
113
114 //randomly pick a point to change cluster assignment
115 //move the point and couple it with change in cluster:
116
117 int index = MathUtils.nextInt(numSamples);
118 int oldGroup = (int)assignments.getParameterValue(index);
119
120 // Set index customer link to index and all connected to it to a new assignment (min value empty)
121 int minEmp = minEmpty(modelLikelihood.getLogLikelihoodsVector());
122 links.setParameterValue(index, index);
123 int[] visited = connected(index, links);
124
125 int ii = 0;
126 while (visited[ii]!=0){
127 assignments.setParameterValue(visited[ii]-1, minEmp);
128 ii++;
129 }
130
131 //Adjust likvector for group separated
132 modelLikelihood.setLogLikelihoodsVector(oldGroup,modelLikelihood.getLogLikGroup(oldGroup) );
133 modelLikelihood.setLogLikelihoodsVector(minEmp,modelLikelihood.getLogLikGroup(minEmp) );
134 int maxFull = maxFull( modelLikelihood.getLogLikelihoodsVector());
135
136
137 //pick new link
138 int k = MathUtils.nextInt(numSamples); //wonder if there is a way to make this more efficient at choosing good moves
139
140 links.setParameterValue(index, k);
141 int newGroup = (int)assignments.getParameterValue(k);
142 int countNumChanged = 0;
143 ii = 0;
144 while (visited[ii]!=0){
145 assignments.setParameterValue(visited[ii]-1, newGroup);
146 ii++;
147 countNumChanged++;
148 }
149
150 System.out.print("changed="+countNumChanged+ " ");
151
152 int targetCluster = (int) assignments.getParameterValue(k);
153
154
155 // updating conditional likelihood vector
156 modelLikelihood.setLogLikelihoodsVector(newGroup, modelLikelihood.getLogLikGroup(newGroup));
157 if (newGroup!=minEmp){
158 modelLikelihood.setLogLikelihoodsVector(minEmp, 0);
159
160 }
161
162
163
164
165 //change the location
166 // a random dimension to perturb
167 int ranDim= MathUtils.nextInt(virusLocations.getParameter(0).getDimension());
168
169 // a random point around old value within windowSize * 2
170 double draw = (2.0 * MathUtils.nextDouble() - 1.0) * windowSize;
171
172
173 int countNumLocChanged = 0;
174 for(int i=0; i < numSamples; i++){
175 if((int) assignments.getParameterValue(i)== targetCluster ){
176 //System.out.print("update "+ i +" from ");
177 Parameter vLoc = virusLocations.getParameter(i);
178 //System.out.print(vLoc.getParameterValue(index));
179 double newValue = vLoc.getParameterValue(ranDim) + draw;
180 vLoc.setParameterValue(ranDim, newValue);
181 //System.out.println(" to " + vLoc.getParameterValue(index));
182 countNumLocChanged++;
183 }
184 }
185 System.out.print("loc changed="+countNumLocChanged+ " ");
186
187
188 // sampleMeans(maxFull); // I decide not to resample the means here.
189
190 }
191 //Want to check that virusLocations WON't get updated if rejected
192
193
194 // System.out.println(target);
195 // System.out.println(targetCluster);
196 //for(int i=0; i < assignments.getDimension();)
197 */
198
199 return 0.0;
200
201 }
202
203
204
205 /*
206 * find min Empty
207 */
208
209 public int minEmpty(double[] logLikVector){
210 int isEmpty=0;
211 int i =0;
212 while (isEmpty==0){
213 if(logLikVector[i]==0){
214 isEmpty=1;}
215 else {
216 if(i==logLikVector.length-1){isEmpty=1;}
217 i++;}
218 }
219 return i;
220 }
221
222
223
224 /*
225 * find max Full
226 */
227
228
229 public int maxFull(double[] logLikVector){
230 int isEmpty=1;
231 int i =logLikVector.length-1;
232 while (isEmpty==1){
233 if(logLikVector[i]!=0){
234 isEmpty=0;}
235 else {i--;}
236 }
237 return i;
238 }
239 /*
240 * find customers connected to i
241 */
242
243 public int[] connected(int i, Parameter clusteringParameter){
244 int n = clusteringParameter.getDimension();
245 int[] visited = new int[n+1];
246 visited[0]=i+1;
247 int tv=1;
248
249 for(int j=0;j<n;j++){
250 if(visited[j]!=0){
251 int curr = visited[j]-1;
252
253 /*look forward
254 */
255
256 int forward = (int) clusteringParameter.getParameterValue(curr);
257 visited[tv] = forward+1;
258 tv++;
259 // Check to see if is isn't already on the list
260
261 for(int ii=0; ii<tv-1; ii++){
262 if(visited[ii]==forward+1){
263 tv--;
264 visited[tv]=0;
265 }
266 }
267
268
269 /*look back
270 */
271 for (int jj=0; jj<n;jj++){
272 if((int)clusteringParameter.getParameterValue(jj)==curr){
273 visited[tv]= jj+1;
274 tv++;
275
276 for(int ii=0; ii<tv-1; ii++){
277 if(visited[ii]==jj+1){
278 tv--;
279 visited[tv]=0;
280 }
281 }
282
283 }
284 }
285
286 }}
287 return visited;
288
289 }
290
291
292
293 public void accept(double deviation) {
294 super.accept(deviation);
295 // System.out.println("Accepted!");
296
297 }
298
299 public void reject(){
300 super.reject();
301 // System.out.println("Rejected");
302 }
303
304
305
306 //MCMCOperator INTERFACE
307 public final String getOperatorName() {
308 return CLUSTER_OPERATOR;
309 }
310
311 public final void optimize(double targetProb) {
312
313 throw new RuntimeException("This operator cannot be optimized!");
314 }
315
316 public boolean isOptimizing() {
317 return false;
318 }
319
320 public void setOptimizing(boolean opt) {
321 throw new RuntimeException("This operator cannot be optimized!");
322 }
323
324 public double getMinimumAcceptanceLevel() {
325 return 0.1;
326 }
327
328 public double getMaximumAcceptanceLevel() {
329 return 0.4;
330 }
331
332 public double getMinimumGoodAcceptanceLevel() {
333 return 0.20;
334 }
335
336 public double getMaximumGoodAcceptanceLevel() {
337 return 0.30;
338 }
339
340 public String getPerformanceSuggestion() {
341 if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) {
342 return "";
343 } else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) {
344 return "";
345 } else {
346 return "";
347 }
348 }
349
350
351
352
353 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
354 public final static String ASSIGNMENTS = "assignments";
355 public final static String VIRUS_LOCATIONS = "virusLocations";
356
357 public final static String LIKELIHOOD = "likelihood";
358 public final static String LINKS = "links";
359
360
361 public String getParserName() {
362 return CLUSTER_OPERATOR;
363 }
364
365 /* (non-Javadoc)
366 * @see dr.xml.AbstractXMLObjectParser#parseXMLObject(dr.xml.XMLObject)
367 */
368 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
369
370 double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT);
371 // double windowSize = xo.getDoubleAttribute(WINDOW_SIZE);
372
373
374 XMLObject cxo = xo.getChild(ASSIGNMENTS);
375 Parameter assignments = (Parameter) cxo.getChild(Parameter.class);
376
377 MatrixParameter virusLocationsParameter = null;
378 if (xo.hasChildNamed(VIRUS_LOCATIONS)) {
379 virusLocationsParameter = (MatrixParameter) xo.getElementFirstChild(VIRUS_LOCATIONS);
380 }
381
382 cxo = xo.getChild(LINKS);
383 Parameter links = (Parameter) cxo.getChild(Parameter.class);
384
385 cxo = xo.getChild(LIKELIHOOD);
386 NPAntigenicLikelihood likelihood = (NPAntigenicLikelihood)cxo.getChild(NPAntigenicLikelihood.class);
387
388 //set window size to be 1
389 return new ClusterOperator(assignments, virusLocationsParameter, weight, 1.0, likelihood, links);
390 // public ClusterOperator(Parameter assignments, MatrixParameter virusLocations, double weight, double windowSize, NPAntigenicLikelihood Likelihood, Parameter links) {
391
392
393 }
394
395 //************************************************************************
396 // AbstractXMLObjectParser implementation
397 //************************************************************************
398
399 public String getParserDescription() {
400 return "An operator that picks a new allocation of an item to a cluster under the Dirichlet process.";
401 }
402
403 public Class getReturnType() {
404 return ClusterOperator.class;
405 }
406
407
408 public XMLSyntaxRule[] getSyntaxRules() {
409 return rules;
410 }
411
412 private final XMLSyntaxRule[] rules = {
413 AttributeRule.newDoubleRule(MCMCOperator.WEIGHT),
414 // AttributeRule.newDoubleRule(WINDOW_SIZE),
415 new ElementRule(ASSIGNMENTS,
416 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
417 new ElementRule(VIRUS_LOCATIONS, MatrixParameter.class, "Parameter of locations of all virus"),
418 new ElementRule(LINKS,
419 new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
420 new ElementRule(LIKELIHOOD, new XMLSyntaxRule[] {
421 new ElementRule(Likelihood.class),}, true),
422
423 };
424
425 };
426
427
428
429
430 public int getStepCount() {
431 return 1;
432 }
433
434 }
435
436
437
438