Debian Codegraph @

src/dr/inference/operators/MaskMoveOperator.java

raw symbols SCIP
1 /*
2 * MaskMoveOperator.java
3 *
4 * Copyright (c) 2002-2016 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.List;
32
33 /**
34 * A generic operator that flips masks.
35 *
36 * @author Marc A. Suchard
37 * @version $Id$
38 */
39 public class MaskMoveOperator extends SimpleMCMCOperator {
40
41 public MaskMoveOperator(List<Parameter> masks, Parameter cutPoint, int[] selectBefore, int[] selectAfter, double weight) {
42 this.masks = masks;
43 this.cutPoint = cutPoint;
44 this.selectBefore = selectBefore;
45 this.selectAfter = selectAfter;
46
47 setWeight(weight);
48
49 if (!checkMaskValues(masks, cutPoint, selectBefore, selectAfter)) {
50 throw new IllegalArgumentException("Bad initialization state");
51 }
52 }
53
54 public static boolean checkMaskValues(List<Parameter> masks, Parameter cutPoint, int[] selectBefore, int[] selectAfter) {
55
56 int cut = (int) (cutPoint.getParameterValue(0) + 0.5);
57 for (int i = 0; i < masks.size(); ++i) {
58 Parameter mask = masks.get(i);
59
60 boolean before = (i < cut);
61
62 final int[] ones;
63 final int[] zeros;
64
65 if (before) {
66 ones = selectBefore;
67 zeros = selectAfter;
68 } else {
69 ones = selectAfter;
70 zeros = selectBefore;
71 }
72
73 for (int idx : ones) {
74 if (mask.getParameterValue(idx) != 1) return false;
75 }
76
77 for (int idx: zeros) {
78 if (mask.getParameterValue(idx) != 0) return false;
79 }
80 }
81 return true;
82 }
83
84 private String printMask() {
85 StringBuilder sb = new StringBuilder();
86 int cut = (int) (cutPoint.getParameterValue(0) + 0.5);
87 sb.append("Cut: " + cut + "\n");
88 for (int i = 0; i < masks.size(); ++i) {
89 sb.append((i + 1) + " " + masks.get(i).getParameterValue(selectBefore[0]) + " " + masks.get(i).getParameterValue(selectAfter[0]) + "\n");
90 }
91 return sb.toString();
92 }
93
94 public final double doOperation() {
95 double logq = 0.0;
96
97 StringBuilder sb = null;
98
99 int currentCutPoint = (int) (cutPoint.getParameterValue(0) + 0.5);
100
101 if (DEBUG) {
102 sb = new StringBuilder();
103 sb.append("Starting state\n");
104 sb.append(printMask());
105 }
106
107 final boolean moveUp;
108 if (currentCutPoint == 0) {
109 moveUp = true;
110 logq -= Math.log(2);
111 } else if (currentCutPoint == masks.size()) {
112 moveUp = false;
113 logq -= Math.log(2);
114 } else if (MathUtils.nextDouble() < 0.5) {
115 moveUp = false;
116 } else {
117 moveUp = true;
118 }
119
120 if (DEBUG) {
121 sb.append("moveUp = " + moveUp + "\n");
122 }
123
124 Parameter change = (moveUp) ? masks.get(currentCutPoint) : masks.get(currentCutPoint - 1);
125
126 final int[] ones;
127 final int[] zeros;
128 if (moveUp) {
129 ones = selectBefore;
130 zeros = selectAfter;
131
132 } else {
133
134
135 ones = selectAfter;
136 zeros = selectBefore;
137 }
138
139 for (int i : zeros) {
140 change.setParameterValueQuietly(i, 0);
141 }
142
143 for (int i : ones) {
144 change.setParameterValueQuietly(i, 1);
145 }
146
147 change.fireParameterChangedEvent();
148
149 cutPoint.setParameterValue(0, currentCutPoint + ((moveUp) ? 1 : -1));
150
151 if (DEBUG) {
152 if (!checkMaskValues(masks, cutPoint, selectBefore, selectAfter)) {
153 sb.append(printMask());
154 System.err.println(sb.toString());
155 System.exit(-1);
156 }
157 }
158
159 return logq;
160 }
161
162 // Interface MCMCOperator
163 public final String getOperatorName() {
164 return "maskMove(" + cutPoint.getParameterName() + ")";
165 }
166
167 public final String getPerformanceSuggestion() {
168 return "no performance suggestion";
169 }
170
171 public String toString() {
172 return getOperatorName();
173 }
174
175 // Private instance variables
176 final private List<Parameter> masks;
177 final private Parameter cutPoint;
178 final private int[] selectBefore;
179 final private int[] selectAfter;
180
181 final static private boolean DEBUG = false;
182 }