Debian Codegraph @

src/dr/inference/operators/SimpleOperatorSchedule.java

raw symbols SCIP
1 /*
2 * SimpleOperatorSchedule.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.loggers.LogColumn;
29 import dr.inference.loggers.Loggable;
30 import dr.inference.loggers.NumberColumn;
31 import dr.math.MathUtils;
32
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.logging.Logger;
36
37 /**
38 * This class implements a simple operator schedule.
39 *
40 * @author Alexei Drummond
41 * @version $Id: SimpleOperatorSchedule.java,v 1.5 2005/06/14 10:40:34 rambaut Exp $
42 */
43 public class SimpleOperatorSchedule implements OperatorSchedule, Loggable {
44
45 private final List<MCMCOperator> operators = new ArrayList<MCMCOperator>();
46 private final List<Integer> availableOperators = new ArrayList<Integer>();
47 private double totalWeight = 0;
48 private int current = 0;
49 private boolean sequential = false;
50 private OptimizationTransform optimizationSchedule = OptimizationTransform.DEFAULT;
51
52 int operatorUseThreshold = Integer.MAX_VALUE; // operator use threshold over which an operator may get turned off if ...
53 double operatorAcceptanceThreshold = 0.0; // acceptance rate threshold under which an operator gets turned off
54
55 public SimpleOperatorSchedule() {
56 }
57
58 public SimpleOperatorSchedule(int operatorUseThreshold, double operatorAcceptanceThreshold) {
59 this.operatorUseThreshold = operatorUseThreshold;
60 this.operatorAcceptanceThreshold = operatorAcceptanceThreshold;
61 }
62
63 public void addOperators(List<MCMCOperator> operators) {
64 for (MCMCOperator operator : operators) {
65 this.operators.add(operator);
66 this.availableOperators.add(this.operators.size() - 1);
67 }
68
69 totalWeight = calculateTotalWeight();
70
71 }
72
73 public void operatorsHasBeenUpdated() {
74 totalWeight = calculateTotalWeight();
75 }
76
77 public void addOperator(MCMCOperator op) {
78 operators.add(op);
79 availableOperators.add(operators.size() - 1);
80 totalWeight = calculateTotalWeight();
81 }
82
83 private double getWeight(int index) {
84 return operators.get(availableOperators.get(index)).getWeight();
85 }
86
87 private double calculateTotalWeight() {
88 double totalWeight = 0.0;
89 for (int i : availableOperators) {
90 totalWeight += operators.get(i).getWeight();
91 }
92 return totalWeight;
93 }
94
95 public int getNextOperatorIndex() {
96
97 checkOperatorAcceptanceRates();
98
99 if (sequential) {
100 int index = getWeightedOperatorIndex(current);
101 current += 1;
102 if (current >= totalWeight) {
103 current = 0;
104 }
105 return index;
106 }
107
108 final double v = MathUtils.nextDouble();
109 //System.err.println("v=" + v);
110 return getWeightedOperatorIndex(v * totalWeight);
111 }
112
113 public void setSequential(boolean sequential) {
114 this.sequential = sequential;
115 }
116
117 private int getWeightedOperatorIndex(double q) {
118 int index = 0;
119 double weight = getWeight(index);
120 while (weight <= q) {
121 index += 1;
122 weight += getWeight(index);
123 }
124 return index;
125 }
126
127 public MCMCOperator getOperator(int index) {
128 return operators.get(availableOperators.get(index));
129 }
130
131 public int getOperatorCount() {
132 return availableOperators.size();
133 }
134
135 private void checkOperatorAcceptanceRates() {
136 List<Integer> toRemove = new ArrayList<Integer>();
137
138 for (int i : availableOperators) {
139 MCMCOperator op = operators.get(i);
140 if (!(op instanceof AdaptableMCMCOperator) && op.getCount() > operatorUseThreshold) {
141 double acceptanceRate = ((double)op.getAcceptCount()) / op.getCount();
142 if (acceptanceRate < operatorAcceptanceThreshold) {
143 toRemove.add(i);
144 Logger.getLogger("dr.app.beast").info("Operator " + op.getOperatorName() +
145 " turned off with an acceptance rate of " + acceptanceRate + ", after " + op.getCount() + " tries.");
146
147 }
148 }
149 }
150
151 if (!toRemove.isEmpty()) {
152 availableOperators.removeAll(toRemove);
153 totalWeight = calculateTotalWeight();
154 }
155 }
156
157 public double getOptimizationTransform(double d) {
158 switch( optimizationSchedule ) {
159 case DEFAULT:
160 case LOG: return Math.log(d);
161 case SQRT: return Math.sqrt(d);
162 case LINEAR: return d;
163
164 default: throw new UnsupportedOperationException("Unknown enum value");
165 }
166 }
167
168 public void setOptimizationSchedule(OptimizationTransform optimizationSchedule) {
169 this.optimizationSchedule = optimizationSchedule;
170 }
171
172 public long getMinimumAcceptAndRejectCount() {
173 long minCount = Long.MAX_VALUE;
174 for( MCMCOperator op : operators ) {
175 if( op.getAcceptCount() < minCount || op.getRejectCount() < minCount ) {
176 minCount = op.getCount();
177 }
178 }
179 return minCount;
180 }
181
182 // **************************************************************
183 // Loggable IMPLEMENTATION
184 // **************************************************************
185
186 /**
187 * @return the log columns.
188 */
189 public LogColumn[] getColumns() {
190 List<LogColumn> columnList = new ArrayList<LogColumn>();
191 for (int i = 0; i < getOperatorCount(); i++) {
192 MCMCOperator op = getOperator(i);
193 columnList.add(new OperatorAcceptanceColumn(op.getOperatorName(), op));
194 if (op instanceof AdaptableMCMCOperator) {
195 columnList.add(new OperatorSizeColumn(op.getOperatorName() + "_size", (AdaptableMCMCOperator)op));
196 }
197 }
198 LogColumn[] columns = columnList.toArray(new LogColumn[columnList.size()]);
199 return columns;
200 }
201
202 private class OperatorAcceptanceColumn extends NumberColumn {
203 private final MCMCOperator op;
204
205 public OperatorAcceptanceColumn(String label, MCMCOperator op) {
206 super(label);
207 this.op = op;
208 }
209
210 public double getDoubleValue() {
211 return MCMCOperator.Utils.getAcceptanceProbability(op);
212 }
213 }
214
215 private class OperatorSizeColumn extends NumberColumn {
216 private final AdaptableMCMCOperator op;
217
218 public OperatorSizeColumn(String label, AdaptableMCMCOperator op) {
219 super(label);
220 this.op = op;
221 }
222
223 public double getDoubleValue() {
224 return op.getRawParameter();
225 }
226 }
227 }