Debian Codegraph @

src/dr/inference/operators/ValuesPoolSwapOperator.java

raw symbols SCIP
1 /*
2 * ValuesPoolSwapOperator.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.ValuesPool;
29 import dr.inference.model.Variable;
30 import dr.inferencexml.operators.ValuesPoolSwapOperatorParser;
31 import dr.math.MathUtils;
32
33 /**
34 * @author Joseph Heled
35 * Date: 8/09/2009
36 */
37 public class ValuesPoolSwapOperator extends SimpleMCMCOperator {
38
39 private final ValuesPool pool;
40
41 public ValuesPoolSwapOperator(ValuesPool pool) {
42 this.pool = pool;
43 }
44
45 public String getOperatorName() {
46 return ValuesPoolSwapOperatorParser.VALUESPOOL_OPERATOR + "(" + pool.getModelName() + ")";
47 }
48
49 public double doOperation() {
50 final Variable<Double> selector = pool.getSelector();
51 final int[] ints = SelectorOperator.intVals(selector);
52 int[] c = SelectorOperator.counts_used_m2(ints);
53
54 int n = 0;
55 for(int k = 0; k < c.length-1; ++k) {
56 if( c[k] == c[k+1] ) {
57 ++n;
58 }
59 }
60
61 if( n == 0 ) {
62 throw new RuntimeException("No moves");
63 }
64
65 int j = MathUtils.nextInt(n);
66 int p;
67 for(p = 0; p < c.length-1; ++p) {
68 if( c[p] == c[p+1] ) {
69 if( j == 0 ) {
70 break;
71 }
72 j -= 1;
73 }
74 }
75
76 int ip = -1, ip1 = -1;
77 // exchange p's and p+1's
78 int count = c[p];
79 while( count > 0 ) {
80 while( ints[++ip] != p );
81 while( ints[++ip1] != p+1 );
82 selector.setValue(ip, (double)(p+1));
83 selector.setValue(ip1, (double)(p));
84 --count;
85 }
86 final Variable<Double> vals = pool.getPool();
87 final Double vp = vals.getValue(p);
88 final Double vp1 = vals.getValue(p+1);
89 vals.setValue(p, vp1);
90 vals.setValue(p+1, vp);
91
92 return 0;
93 }
94
95 public String getPerformanceSuggestion() {
96 return null;
97 }
98
99 }