Debian Codegraph @

src/dr/evomodel/operators/CategoryOperator.java

raw symbols SCIP
1 /*
2 * CategoryOperator.java
3 *
4 * Copyright (c) 2002-2015 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.evolution.alignment.Alignment;
29 import dr.math.MathUtils;
30 import dr.oldevomodel.sitemodel.CategorySampleModel;
31 import dr.inference.model.Parameter;
32 import dr.inference.operators.SimpleMCMCOperator;
33 import dr.xml.*;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36
37
38 /**
39 * An operator on categories of sites
40 *
41 * @author Roald Forsberg
42 */
43 public class CategoryOperator extends SimpleMCMCOperator {
44
45 public static final String CATEGORY_OPERATOR = "categoryOperator";
46
47 // dimension of categoryParameter should be set beforehand
48 public CategoryOperator(CategorySampleModel siteModel, int siteCount,
49 Parameter categoryParameter, double weight) {
50 this.categoryParameter = categoryParameter;
51 setWeight(weight);
52 this.siteModel = siteModel;
53 this.categoryCount = siteModel.getCategoryCount();
54 this.siteCount = siteCount;
55 }
56
57
58 /**
59 * Alter the category of one site
60 */
61 public final double doOperation() {
62
63 int randomSite = (int) (MathUtils.nextDouble() * siteCount);
64
65 int currentCategory = (int) categoryParameter.getParameterValue(randomSite);
66
67 siteModel.subtractSitesInCategoryCount(currentCategory);
68
69 int[] temp = new int[categoryCount - 1];
70
71 int count = 0;
72
73 for (int i = 0; i < categoryCount; i++) {
74 if (i != currentCategory) {
75 temp[count] = i;
76 count++;
77 }
78 }
79
80 int newCategory = temp[(int) (MathUtils.nextDouble() * temp.length)];
81
82 categoryParameter.setParameterValue(randomSite, newCategory);
83 siteModel.addSitesInCategoryCount(newCategory);
84
85 return 0.0;
86
87 }
88
89
90 // Interface MCMCOperator
91 public final String getOperatorName() {
92 return CATEGORY_OPERATOR;
93 }
94
95
96 /**
97 * Create the Operator part of this model parameter!
98 */
99 public Element createOperatorElement(Document d) {
100 throw new RuntimeException("Not implemented!");
101 }
102
103 public static XMLObjectParser PARSER = new AbstractXMLObjectParser() {
104
105 public String getParserName() {
106 return CATEGORY_OPERATOR;
107 }
108
109 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
110
111 Parameter catParam = (Parameter) xo.getChild(Parameter.class);
112 CategorySampleModel siteModel = (CategorySampleModel) xo.getChild(CategorySampleModel.class);
113 Alignment alignment = (Alignment) xo.getChild(Alignment.class);
114
115 double weight = xo.getDoubleAttribute(WEIGHT);
116
117 return new CategoryOperator(siteModel, alignment.getSiteCount(),
118 catParam, weight);
119 }
120
121 //************************************************************************
122 // AbstractXMLObjectParser implementation
123 //************************************************************************
124
125 public String getParserDescription() {
126 return "An operator on categories of sites.";
127 }
128
129 public Class getReturnType() {
130 return CategoryOperator.class;
131 }
132
133 public XMLSyntaxRule[] getSyntaxRules() {
134 return rules;
135 }
136
137 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
138 AttributeRule.newDoubleRule("weight"),
139 new ElementRule(Parameter.class),
140 new ElementRule(CategorySampleModel.class),
141 new ElementRule(Alignment.class)
142 };
143
144 };
145
146 public String toString() {
147 return getOperatorName();
148 }
149
150 public String getPerformanceSuggestion() {
151 return "";
152 }
153
154 // Private instance variables
155 private Parameter categoryParameter;
156
157 private CategorySampleModel siteModel;
158
159 private int categoryCount;
160
161 private int siteCount;
162
163 }
164