Debian Codegraph @

src/dr/inference/operators/BitMoveOperator.java

raw symbols SCIP
1 /*
2 * BitMoveOperator.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.math.MathUtils;
30
31 import java.util.ArrayList;
32 import java.util.List;
33
34 /**
35 * A generic operator that moves k 1 bits to k zero locations.
36 *
37 * @author Alexei Drummond
38 * @version $Id$
39 */
40 public class BitMoveOperator extends SimpleMCMCOperator {
41
42 public BitMoveOperator(Parameter bitsParameter, Parameter valuesParameter, int numBitsToMove, double weight) {
43 this.bitsParameter = bitsParameter;
44 this.valuesParameter = valuesParameter;
45
46 if (valuesParameter != null && bitsParameter.getDimension() != valuesParameter.getDimension()) {
47 throw new IllegalArgumentException("bits parameter must be same length as values parameter");
48 }
49
50 this.numBitsToMove = numBitsToMove;
51 setWeight(weight);
52 }
53
54 /**
55 * Pick a random k ones in the vector and move them to a random k zero positions.
56 */
57 public final double doOperation() {
58
59 final int dim = bitsParameter.getDimension();
60 List<Integer> ones = new ArrayList<Integer>();
61 List<Integer> zeros = new ArrayList<Integer>();
62
63 for (int i = 0; i < dim; i++) {
64 if (bitsParameter.getParameterValue(i) == 1.0) {
65 ones.add(i);
66 } else {
67 zeros.add(i);
68 }
69 }
70
71 if (ones.size() >= numBitsToMove && zeros.size() >= numBitsToMove) {
72
73 for (int i = 0; i < numBitsToMove; i++) {
74
75 int myOne = ones.remove(MathUtils.nextInt(ones.size()));
76 int myZero = zeros.remove(MathUtils.nextInt(zeros.size()));
77
78 bitsParameter.setParameterValue(myOne, 0.0);
79 bitsParameter.setParameterValue(myZero, 1.0);
80
81 if (valuesParameter != null) {
82 double value1 = valuesParameter.getParameterValue(myOne);
83 double value2 = valuesParameter.getParameterValue(myZero);
84 valuesParameter.setParameterValue(myOne, value2);
85 valuesParameter.setParameterValue(myZero, value1);
86 }
87
88 }
89 } else {
90 // might be better to return negative infinity and reject the move - there is likely
91 // to be a bit flip operator on which will sort the problem out.
92 //throw new RuntimeException("Not enough bits to move!");
93
94 return Double.NEGATIVE_INFINITY;
95 }
96
97 return 0.0;
98 }
99
100 // Interface MCMCOperator
101 public final String getOperatorName() {
102 StringBuilder builder = new StringBuilder();
103 builder.append("bitMove(");
104 builder.append(bitsParameter.getParameterName());
105
106 if (valuesParameter != null) {
107 builder.append(", ").append(valuesParameter.getParameterName());
108 }
109 builder.append(", ").append(numBitsToMove).append(")");
110
111 return builder.toString();
112 }
113
114 public final String getPerformanceSuggestion() {
115 return "no performance suggestion";
116 }
117
118 public String toString() {
119 return getOperatorName();
120 }
121
122 // Private instance variables
123
124 private Parameter bitsParameter = null;
125 private Parameter valuesParameter = null;
126 private int numBitsToMove = 1;
127 }