Debian Codegraph @

src/dr/evomodel/operators/PatternWeightIncrementOperator.java

raw symbols SCIP
1 /*
2 * PatternWeightIncrementOperator.java
3 *
4 * Copyright (c) 2002-2016 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
24 */
25
26 package dr.evomodel.operators;
27
28 import dr.evomodel.treelikelihood.BeagleTreeLikelihood;
29 import dr.inference.operators.GibbsOperator;
30 import dr.inference.operators.SimpleMCMCOperator;
31 import jebl.math.Random;
32
33 /**
34 * @author Andrew Rambaut
35 */
36 // Cleaning out untouched stuff. Can be resurrected if needed
37 @Deprecated
38 public class PatternWeightIncrementOperator extends SimpleMCMCOperator implements GibbsOperator {
39
40 public static final String PATTERN_WEIGHT_INCREMENT_OPERATOR = "patternWeightIncrementOperator";
41
42 public PatternWeightIncrementOperator(BeagleTreeLikelihood treeLikelihood, double weight) {
43 this.treeLikelihood = treeLikelihood;
44 setWeight(weight);
45
46 finalPatternWeights = treeLikelihood.getPatternWeights();
47
48 double[] weights = new double[finalPatternWeights.length];
49 treeLikelihood.setPatternWeights(finalPatternWeights);
50 // treeLikelihood.setPatternWeights(weights);
51 }
52
53 public double doOperation() {
54
55 if (allPatternsAdded) {
56 return 0.0;
57 }
58
59 double[] weights = treeLikelihood.getPatternWeights();
60 double[] w = new double[weights.length];
61
62 double sum = 0.0;
63 for (int i = 0; i < weights.length; i++) {
64 w[i] = finalPatternWeights[i] - weights[i];
65 sum += w[i];
66 }
67
68 // System.out.println("PatternWeightIncrementOperator - Sites remaining: " + sum);
69 if (sum < 1.0) {
70 allPatternsAdded = true;
71 System.out.println("PatternWeightIncrementOperator - All sites added");
72 return 0.0;
73 }
74
75 for (int i = 0; i < weights.length; i++) {
76 w[i] /= sum;
77 if (i > 0) {
78 w[i] += w[i - 1];
79 }
80 }
81
82 int r = Random.randomChoice(w);
83
84 weights[r] ++;
85
86 if (weights[r] > finalPatternWeights[r]) {
87 throw new RuntimeException("Pattern weight exceeding final weight");
88 }
89
90 treeLikelihood.setPatternWeights(weights);
91
92 treeLikelihood.makeDirty();
93
94 return 0;
95 }
96
97 public void reject() {
98 super.reject();
99 }
100
101 public String getOperatorName() {
102 return PATTERN_WEIGHT_INCREMENT_OPERATOR;
103 }
104
105 private final BeagleTreeLikelihood treeLikelihood;
106 private final double[] finalPatternWeights;
107
108 private boolean allPatternsAdded = false;
109
110 }