Debian Codegraph @

src/dr/evomodel/operators/MicrosatelliteBitFlipOperator.java

raw symbols SCIP
1 /*
2 * MsatBitFlipOperator.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.inference.model.Parameter;
29 import dr.inference.operators.MCMCOperator;
30 import dr.inference.operators.SimpleMCMCOperator;
31 import dr.xml.*;
32 import dr.math.MathUtils;
33
34 /**
35 * @author Chieh-Hsi Wu
36 *
37 * This operator performs bitflip operation on the bit vector representing the model.
38 *
39 */
40 public class MicrosatelliteBitFlipOperator extends SimpleMCMCOperator {
41 private Parameter parameter;
42 private Parameter dependencies;
43 private int[] variableIndices;
44 public static final int PRESENT = 1;
45 public static final int ABSENT = 0;
46 public static final int NO_DEPENDENCY = -1;
47 public static final String MODEL_CHOOSE = "modelChoose";
48 public static final String DEPENDENCIES = "dependencies";
49 public static final String VARIABLE_INDICES = "variableIndices";
50
51
52 public MicrosatelliteBitFlipOperator(Parameter parameter, Parameter dependencies, double weight, int[] variableIndices){
53 this.parameter = parameter;
54 this.dependencies = dependencies;
55 this.variableIndices = variableIndices;
56 if(parameter.getDimension() != dependencies.getDimension())
57 throw new RuntimeException("Dimenension of the parameter ("+parameter.getDimension()+
58 ") does not equal to the dimension of the dependencies parameter("+dependencies.getDimension()+").");
59 setWeight(weight);
60 }
61
62 public String getOperatorName(){
63 return "msatModelSwitch(" + parameter.getParameterName() + ")";
64 }
65
66 public double doOperation() {
67
68 double logq = 0.0;
69 double[] bitVec = new double[parameter.getDimension()];
70 for(int i = 0; i < bitVec.length; i++){
71 bitVec[i] = parameter.getParameterValue(i);
72 }
73 //int index = (int)MathUtils.nextDouble()*parameter.getDimension();
74 int index = variableIndices[MathUtils.nextInt(variableIndices.length)];
75 //System.out.println(index);
76 int oldVal = (int)parameter.getParameterValue(index);
77 int newVal = -1;
78 if(oldVal == ABSENT){
79 newVal = PRESENT;
80 }else if(oldVal == PRESENT){
81 newVal = ABSENT;
82 }else{
83 throw new RuntimeException("The parameter can only take values 0 or 1.");
84 }
85 bitVec[index] = newVal;
86 for(int i = 0; i < bitVec.length; i++){
87 int dependentInd = (int)dependencies.getParameterValue(i);
88 if(dependentInd > NO_DEPENDENCY){
89 if(bitVec[dependentInd] == ABSENT && bitVec[i]==PRESENT){
90 //throw new OperatorFailedException("");
91 return Double.NEGATIVE_INFINITY;
92 }
93 }
94
95 }
96 parameter.setParameterValue(index, newVal);
97
98 return logq;
99 }
100
101 public final String getPerformanceSuggestion() {
102 return "no suggestions available";
103 }
104
105 public static dr.xml.XMLObjectParser PARSER = new AbstractXMLObjectParser() {
106
107 public String getParserName() {
108 return "msatModelSwitchOperator";
109 }
110
111 public Object parseXMLObject(XMLObject xo) throws XMLParseException {
112
113 double weight = xo.getDoubleAttribute(WEIGHT);
114 Parameter modelChoose = (Parameter) xo.getElementFirstChild(MODEL_CHOOSE);
115 Parameter dependencies = (Parameter)xo.getElementFirstChild(DEPENDENCIES);
116 int[] variableIndices;
117 if(xo.hasChildNamed(VARIABLE_INDICES)){
118
119 double[] temp = ((Parameter)xo.getElementFirstChild(VARIABLE_INDICES)).getParameterValues();
120 variableIndices = new int[temp.length];
121 for(int i = 0; i < temp.length;i++){
122 variableIndices[i] = (int)temp[i];
123 }
124
125 }else{
126 variableIndices = new int[]{0, 1, 2, 3, 4, 5};
127 }
128
129 return new MicrosatelliteBitFlipOperator(modelChoose, dependencies, weight, variableIndices);
130 }
131
132 //************************************************************************
133 // AbstractXMLObjectParser implementation
134 //************************************************************************
135
136 public String getParserDescription() {
137 return "This element returns a microsatellite averaging operator on a given parameter.";
138 }
139
140 public Class getReturnType() {
141 return MCMCOperator.class;
142 }
143
144 public XMLSyntaxRule[] getSyntaxRules() {
145 return rules;
146 }
147
148 private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{
149 AttributeRule.newDoubleRule(WEIGHT),
150 new ElementRule(MODEL_CHOOSE, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
151 new ElementRule(DEPENDENCIES, new XMLSyntaxRule[]{new ElementRule(Parameter.class)}),
152 new ElementRule(VARIABLE_INDICES, new XMLSyntaxRule[]{new ElementRule(Parameter.class)},true)
153
154 };
155
156 };
157
158 }