Debian Codegraph @

src/dr/inference/operators/UniformIntegerOperator.java

raw symbols SCIP
1 /*
2 * UniformIntegerOperator.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.inference.operators;
27
28 import dr.inference.model.Parameter;
29 import dr.inference.model.Variable;
30 import dr.inferencexml.operators.UniformIntegerOperatorParser;
31 import dr.math.MathUtils;
32
33 /**
34 * A generic uniform sampler/operator for use with a multi-dimensional parameter.
35 *
36 * @author Alexei Drummond
37 * @author Andrew Rambaut
38 * @version $Id: UniformOperator.java,v 1.16 2005/06/14 10:40:34 rambaut Exp $
39 */
40 public class UniformIntegerOperator extends SimpleMCMCOperator {
41 private final int howMany;
42
43 public UniformIntegerOperator(Parameter parameter, int lower, int upper, double weight, int howMany) {
44 this(parameter, weight, howMany);
45 this.lower = lower;
46 this.upper = upper;
47 }
48
49 public UniformIntegerOperator(Parameter parameter, int lower, int upper, double weight) {
50 this(parameter, lower, upper, weight, 1);
51 }
52
53 public UniformIntegerOperator(Variable parameter, double weight, int howMany) { // Bounds.Staircase
54 this.parameter = parameter;
55 this.howMany = howMany;
56 setWeight(weight);
57 }
58
59 /**
60 * @return the parameter this operator acts on.
61 */
62 public Parameter getParameter() {
63 return (Parameter) parameter;
64 }
65
66 /**
67 * @return the Variable this operator acts on.
68 */
69 public Variable getVariable() {
70 return parameter;
71 }
72
73 /**
74 * change the parameter and return the hastings ratio.
75 */
76 public final double doOperation() {
77
78 for (int n = 0; n < howMany; ++n) {
79 // do not worry about duplication, does not matter
80 int index = MathUtils.nextInt(parameter.getSize());
81
82 if (parameter instanceof Parameter) {
83 int newValue = MathUtils.nextInt(upper - lower + 1) + lower; // from 0 to n-1, n must > 0,
84 ((Parameter) parameter).setParameterValue(index, newValue);
85 } else { // Variable<Integer>, Bounds.Staircase
86
87 int upper = ((Variable<Integer>) parameter).getBounds().getUpperLimit(index);
88 int lower = ((Variable<Integer>) parameter).getBounds().getLowerLimit(index);
89 int newValue = MathUtils.nextInt(upper - lower + 1) + lower; // from 0 to n-1, n must > 0,
90 ((Variable<Integer>) parameter).setValue(index, newValue);
91
92 }
93
94 }
95
96 return 0.0;
97 }
98
99 //MCMCOperator INTERFACE
100 public final String getOperatorName() {
101 return "uniformInteger(" + parameter.getId() + ")";
102 }
103
104 public final void optimize(double targetProb) {
105
106 throw new RuntimeException("This operator cannot be optimized!");
107 }
108
109 public boolean isOptimizing() {
110 return false;
111 }
112
113 public void setOptimizing(boolean opt) {
114 throw new RuntimeException("This operator cannot be optimized!");
115 }
116
117 public double getMinimumAcceptanceLevel() {
118 return 0.1;
119 }
120
121 public double getMaximumAcceptanceLevel() {
122 return 0.4;
123 }
124
125 public double getMinimumGoodAcceptanceLevel() {
126 return 0.20;
127 }
128
129 public double getMaximumGoodAcceptanceLevel() {
130 return 0.30;
131 }
132
133 public String getPerformanceSuggestion() {
134 if (Utils.getAcceptanceProbability(this) < getMinimumAcceptanceLevel()) {
135 return "";
136 } else if (Utils.getAcceptanceProbability(this) > getMaximumAcceptanceLevel()) {
137 return "";
138 } else {
139 return "";
140 }
141 }
142
143 public String toString() {
144 return UniformIntegerOperatorParser.UNIFORM_INTEGER_OPERATOR + "(" + parameter.getId() + ")";
145 }
146
147 //PRIVATE STUFF
148
149 private Variable parameter = null;
150 private int upper;
151 private int lower;
152 }