| 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.evomodel.arg.operators; |
| 27 |
|
| 28 |
import dr.evomodel.arg.ARGModel; |
| 29 |
import dr.inference.model.CompoundParameter; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.operators.SimpleMCMCOperator; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
import java.util.ArrayList; |
| 36 |
import java.util.logging.Logger; |
| 37 |
|
| 38 |
public class ARGPartitioningOperator extends SimpleMCMCOperator { |
| 39 |
|
| 40 |
private final CompoundParameter partitioningParameters; |
| 41 |
private final ARGModel arg; |
| 42 |
|
| 43 |
public final static String OPERATOR_NAME = "argPartitionOperator"; |
| 44 |
public static final String TOSS_SIZE = "tossSize"; |
| 45 |
public static final String TOSS_ALL = "tossAll"; |
| 46 |
|
| 47 |
private final boolean tossAll; |
| 48 |
private final boolean isRecombination; |
| 49 |
private final int tossSize; |
| 50 |
|
| 51 |
public ARGPartitioningOperator(ARGModel arg, int tossSize, int weight, boolean tossAll) { |
| 52 |
super.setWeight(weight); |
| 53 |
|
| 54 |
this.arg = arg; |
| 55 |
this.partitioningParameters = arg.getPartitioningParameters(); |
| 56 |
this.tossSize = tossSize; |
| 57 |
this.isRecombination = arg.isRecombinationPartitionType(); |
| 58 |
|
| 59 |
this.tossAll = tossAll; |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
public Parameter getParameter() { |
| 66 |
return partitioningParameters; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
public final double doOperation() { |
| 71 |
double logq = 0; |
| 72 |
|
| 73 |
final int len = partitioningParameters.getParameterCount(); |
| 74 |
|
| 75 |
if (len == 0) { |
| 76 |
return 0; |
| 77 |
} |
| 78 |
|
| 79 |
boolean[] updatePartition = new boolean[arg.getNumberOfPartitions()]; |
| 80 |
|
| 81 |
if(tossAll){ |
| 82 |
for(int i = 0 ; i < len; i++){ |
| 83 |
logq += doFlip(i,updatePartition); |
| 84 |
} |
| 85 |
}else{ |
| 86 |
logq = doFlip(MathUtils.nextInt(len),updatePartition); |
| 87 |
} |
| 88 |
|
| 89 |
arg.fireModelChanged(new PartitionChangedEvent(partitioningParameters, updatePartition)); |
| 90 |
return logq; |
| 91 |
} |
| 92 |
|
| 93 |
private double doFlip(int i, boolean[] updatePartition) { |
| 94 |
if (isRecombination) { |
| 95 |
return doRecombination(partitioningParameters.getParameter(i),updatePartition); |
| 96 |
} |
| 97 |
|
| 98 |
return doReassortment(partitioningParameters.getParameter(i),updatePartition); |
| 99 |
|
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
private double doRecombination(Parameter partition, boolean[] updatePartition) { |
| 104 |
|
| 105 |
assert checkValidRecombinationPartition(partition); |
| 106 |
|
| 107 |
int currentBreakLocation = 0; |
| 108 |
for (int i = 0, n = arg.getNumberOfPartitions(); i < n; i++) { |
| 109 |
if (partition.getParameterValue(i) == 1) { |
| 110 |
currentBreakLocation = i; |
| 111 |
break; |
| 112 |
} |
| 113 |
} |
| 114 |
|
| 115 |
assert currentBreakLocation > 0; |
| 116 |
|
| 117 |
if (MathUtils.nextBoolean()) { |
| 118 |
|
| 119 |
partition.setParameterValueQuietly(currentBreakLocation, 0.0); |
| 120 |
updatePartition[currentBreakLocation] = true; |
| 121 |
} else { |
| 122 |
partition.setParameterValueQuietly(currentBreakLocation - 1, 1.0); |
| 123 |
updatePartition[currentBreakLocation-1] = true; |
| 124 |
} |
| 125 |
|
| 126 |
if (!checkValidRecombinationPartition(partition)) { |
| 127 |
return Double.NEGATIVE_INFINITY; |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
return 0; |
| 132 |
} |
| 133 |
|
| 134 |
public static boolean checkValidRecombinationPartition(Parameter partition) { |
| 135 |
int l = partition.getDimension(); |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
return (partition.getParameterValue(0) == 0 && partition.getParameterValue(l - 1) == 1); |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
private double doReassortment(Parameter partition, boolean[] updatePartition) { |
| 145 |
|
| 146 |
assert checkValidReassortmentPartition(partition); |
| 147 |
|
| 148 |
ArrayList<Integer> list = new ArrayList<Integer>(tossSize); |
| 149 |
|
| 150 |
while (list.size() < tossSize) { |
| 151 |
int a = MathUtils.nextInt(arg.getNumberOfPartitions() - 1) + 1; |
| 152 |
if (!list.contains(a)) { |
| 153 |
list.add(a); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
for (int a : list) { |
| 159 |
if (partition.getParameterValue(a) == 0) { |
| 160 |
partition.setParameterValueQuietly(a, 1); |
| 161 |
} else { |
| 162 |
partition.setParameterValueQuietly(a, 0); |
| 163 |
} |
| 164 |
updatePartition[a] = true; |
| 165 |
} |
| 166 |
|
| 167 |
|
| 168 |
if (!checkValidReassortmentPartition(partition)) { |
| 169 |
return Double.NEGATIVE_INFINITY; |
| 170 |
} |
| 171 |
|
| 172 |
return 0; |
| 173 |
} |
| 174 |
|
| 175 |
public static boolean checkValidReassortmentPartition(Parameter partition) { |
| 176 |
if (partition.getParameterValue(0) != 0) |
| 177 |
return false; |
| 178 |
|
| 179 |
double[] a = partition.getParameterValues(); |
| 180 |
|
| 181 |
double sum = 0; |
| 182 |
|
| 183 |
for (double b : a) |
| 184 |
sum += b; |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
return !(sum == 0 || sum == a.length); |
| 191 |
|
| 192 |
} |
| 193 |
|
| 194 |
@Override |
| 195 |
public String getOperatorName() { |
| 196 |
return OPERATOR_NAME; |
| 197 |
} |
| 198 |
|
| 199 |
public String getPerformanceSuggestion() { |
| 200 |
return null; |
| 201 |
} |
| 202 |
|
| 203 |
public class PartitionChangedEvent { |
| 204 |
Parameter partitioning; |
| 205 |
boolean[] updatePartition; |
| 206 |
|
| 207 |
public PartitionChangedEvent(Parameter partitioning, boolean[] updatePartition) { |
| 208 |
this.partitioning = partitioning; |
| 209 |
this.updatePartition = updatePartition; |
| 210 |
} |
| 211 |
|
| 212 |
public Parameter getParameter() { |
| 213 |
return partitioning; |
| 214 |
} |
| 215 |
|
| 216 |
public boolean[] getUpdatedPartitions() { |
| 217 |
return updatePartition; |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
public static dr.xml.XMLObjectParser PARSER = new dr.xml.AbstractXMLObjectParser() { |
| 222 |
|
| 223 |
public String getParserName() { |
| 224 |
return OPERATOR_NAME; |
| 225 |
} |
| 226 |
|
| 227 |
public String[] getParserNames() { |
| 228 |
return new String[]{ |
| 229 |
OPERATOR_NAME, |
| 230 |
"tossPartitioningOperator", |
| 231 |
}; |
| 232 |
} |
| 233 |
|
| 234 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 235 |
|
| 236 |
int weight = xo.getIntegerAttribute(WEIGHT); |
| 237 |
|
| 238 |
ARGModel arg = (ARGModel) xo.getChild(ARGModel.class); |
| 239 |
|
| 240 |
int tossSize = 1; |
| 241 |
if (xo.hasAttribute(TOSS_SIZE)) { |
| 242 |
tossSize = xo.getIntegerAttribute(TOSS_SIZE); |
| 243 |
|
| 244 |
if (tossSize <= 0 || tossSize >= arg.getNumberOfPartitions()) { |
| 245 |
throw new XMLParseException("Toss size is incorrect"); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
boolean tossAll = false; |
| 250 |
if(xo.hasAttribute(TOSS_ALL)){ |
| 251 |
tossAll = xo.getBooleanAttribute(TOSS_ALL); |
| 252 |
} |
| 253 |
|
| 254 |
Logger.getLogger("dr.evomodel").info("Creating ARGPartitionOperator: " + TOSS_SIZE + "=" + tossSize + |
| 255 |
" " + TOSS_ALL + "=" + tossAll); |
| 256 |
|
| 257 |
|
| 258 |
return new ARGPartitioningOperator(arg, tossSize, weight, tossAll); |
| 259 |
} |
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
public String getParserDescription() { |
| 266 |
return "An operator that picks a new partitioning uniformly at random."; |
| 267 |
} |
| 268 |
|
| 269 |
public Class getReturnType() { |
| 270 |
return ARGPartitioningOperator.class; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 275 |
return rules; |
| 276 |
} |
| 277 |
|
| 278 |
private final XMLSyntaxRule[] rules = { |
| 279 |
AttributeRule.newIntegerRule(WEIGHT), |
| 280 |
AttributeRule.newIntegerRule(TOSS_SIZE,true), |
| 281 |
AttributeRule.newBooleanRule(TOSS_ALL,true), |
| 282 |
new ElementRule(ARGModel.class) |
| 283 |
}; |
| 284 |
}; |
| 285 |
|
| 286 |
public String toString() { |
| 287 |
return "tossPartitioningOperator(" + partitioningParameters.getParameterName() + ")"; |
| 288 |
} |
| 289 |
} |