| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 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 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 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 |
|
| 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 |
|
| 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 |
} |