Debian Codegraph @

src/dr/inference/operators/TeamOperator.java

raw symbols SCIP
1 /*
2 * TeamOperator.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.math.MathUtils;
29
30 /**
31 * Combines N oprators - each time a random subset of k out of the N are run. If operators have weight then the
32 * probability of any subset is proportional to the sum of it's members weights.
33 *
34 * Currently very basic as no Coercible operators are handled correctly. Getting coercion to work is non trivial.
35 *
36 * @author Joseph Heled
37 */
38
39 public class TeamOperator extends SimpleMCMCOperator /*implements AdaptableMCMCOperator*/ {
40
41 private final MCMCOperator[] operators;
42 //private final ArrayList<Integer> operatorToOptimizeList;
43
44 //private int currentOptimizedOperator;
45 // private final double targetProbability;
46 private final int nPick;
47 private final boolean unequalWeights;
48
49 private final MCMCOperator[] currentRound;
50 private int nToReject;
51 private int[][] binomial;
52
53 public TeamOperator(MCMCOperator[] operators, int nPick, double weight) {
54 setWeight(weight);
55
56 this.operators = operators;
57 //targetProbability = targetProb;
58 final int N = operators.length;
59 assert 0 < nPick && nPick <= N;
60 this.nPick = nPick;
61 currentRound = new MCMCOperator[N];
62
63
64 {
65 boolean b = false;
66 double w = operators[0].getWeight();
67 for( MCMCOperator o : operators ) {
68 if( o.getWeight() != w ) {
69 b = true;
70 break;
71 }
72 }
73 unequalWeights = b;
74 }
75
76 if( unequalWeights ) {
77 final int M = N+1;
78 binomial = new int[M][M];
79 for(int n = 0; n < M; ++n) {
80 // binomial[n] = new int[operators.length];
81 binomial[n][0] = 1;
82 }
83
84 for(int n = 1; n < M; ++n) {
85 for(int k = 1; k <= n; ++k) {
86 binomial[n][k] = binomial[n-1][k] + binomial[n-1][k-1];
87 }
88 }
89 } else {
90 for(int k = 0; k < N; ++k) {
91 currentRound[k] = operators[k];
92 }
93 }
94 }
95
96 // public void addOperator(SimpleMCMCOperator operation) {
97 //
98 // operatorList.add(operation);
99 // if (operation instanceof AdaptableMCMCOperator) {
100 //
101 // if (((AdaptableMCMCOperator) operation).getMode() == AdaptationMode.ADAPTATION_ON)
102 //
103 // operatorToOptimizeList.add(operatorList.size() - 1);
104 //
105 // }
106 // }
107
108 private void choose() {
109 final int n = operators.length;
110 if( nPick < n ) {
111 if( unequalWeights ) {
112 chooseUsingWeights();
113 } else {
114 // equal weights, just pick a subset of 'nPick' operators uniformly
115 for(int k = 0; k < nPick; ++k) {
116 final int which = k + MathUtils.nextInt(n - k);
117 final MCMCOperator tmp = currentRound[k];
118 currentRound[k] = currentRound[which];
119 currentRound[which] = tmp;
120 }
121 }
122 }
123 }
124
125 private void chooseUsingWeights() {
126 // sum(o_w : o in operators already selected)
127 double inSumWeights = 0.0;
128 // sum(o_w : o in remaining operators)
129 double sumWeightsRemaining = 0;
130 for( MCMCOperator o : operators ) {
131 sumWeightsRemaining += o.getWeight();
132 }
133
134 // Number of operators still to pick
135 int k = nPick;
136 // Operator under consideration
137 int j = 0;
138 while( k > 0 ) {
139 // remaining to choose from
140 final int n = operators.length - j;
141
142 if( k == n ) {
143 // speedup
144 for( ; k > 0; k--, j++ ) {
145 currentRound[k-1] = operators[j];
146 }
147 } else {
148 final int cnk = binomial[n][k];
149 final int cnk1 = binomial[n-1][k-1];
150 final int cnk2 = k >= 2 ? binomial[n-2][k-2] : 0;
151
152 final double tot = cnk1 * sumWeightsRemaining + cnk * inSumWeights;
153 final double we0 = operators[j].getWeight();
154 final double has = cnk2 * (sumWeightsRemaining-we0) + cnk1 * (we0+inSumWeights);
155
156 final double r = MathUtils.nextDouble();
157 if( r < has/tot ) {
158 currentRound[k-1] = operators[j];
159 k -= 1;
160 inSumWeights += we0;
161 }
162 j += 1;
163 sumWeightsRemaining -= we0;
164 }
165 }
166 }
167
168 public final double doOperation() {
169
170 choose();
171
172 double logP = 0;
173
174 for(int k = 0; k < nPick; ++k) {
175 MCMCOperator operation = currentRound[k];
176 logP += operation.operate();
177 }
178 nToReject = nPick;
179
180 return logP;
181 }
182
183 public void accept(double deviation) {
184 super.accept(deviation);
185 for(int k = 0; k < nPick; ++k) {
186 currentRound[k].accept(deviation);
187 }
188 }
189
190 public void reject() {
191 super.reject();
192 for(int k = 0; k < nToReject; ++k) {
193 currentRound[k].reject();
194 }
195 }
196
197 public void reset() {
198 for( MCMCOperator op : operators ) {
199 op.reset();
200 }
201 }
202
203 public String getOperatorName() {
204 StringBuffer sb = new StringBuffer("Team " + nPick + " (");
205 for( MCMCOperator operation : operators ) {
206 sb.append(operation.getOperatorName()+",");
207 }
208 return sb.substring(0, sb.length()-1) + ")";
209 }
210
211 // public double getTargetAcceptanceProbability() {
212 // return targetProbability;
213 // }
214
215 public String getPerformanceSuggestion() {
216 return "";
217 }
218
219 }