| 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.inferencexml.operators; |
| 27 |
|
| 28 |
import dr.inference.model.Parameter; |
| 29 |
import dr.inference.operators.*; |
| 30 |
import dr.xml.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class UpDownOperatorParser extends AbstractXMLObjectParser { |
| 35 |
|
| 36 |
public static final String UP_DOWN_OPERATOR = "upDownOperator"; |
| 37 |
public static final String UP = "up"; |
| 38 |
public static final String DOWN = "down"; |
| 39 |
|
| 40 |
public static final String SCALE_FACTOR = ScaleOperatorParser.SCALE_FACTOR; |
| 41 |
|
| 42 |
public String getParserName() { |
| 43 |
return UP_DOWN_OPERATOR; |
| 44 |
} |
| 45 |
|
| 46 |
private Scalable[] getArgs(final XMLObject list) throws XMLParseException { |
| 47 |
Scalable[] args = new Scalable[list.getChildCount()]; |
| 48 |
for (int k = 0; k < list.getChildCount(); ++k) { |
| 49 |
final Object child = list.getChild(k); |
| 50 |
if (child instanceof Parameter) { |
| 51 |
args[k] = new Scalable.Default((Parameter) child); |
| 52 |
} else if (child instanceof Scalable) { |
| 53 |
args[k] = (Scalable) child; |
| 54 |
} else { |
| 55 |
XMLObject xo = (XMLObject) child; |
| 56 |
if (xo.hasAttribute("count")) { |
| 57 |
final int count = xo.getIntegerAttribute("count"); |
| 58 |
|
| 59 |
final Scalable s = (Scalable) xo.getChild(Scalable.class); |
| 60 |
args[k] = new Scalable() { |
| 61 |
|
| 62 |
public int scale(double factor, int nDims, boolean testBounds) { |
| 63 |
return s.scale(factor, count, testBounds); |
| 64 |
} |
| 65 |
|
| 66 |
@Override |
| 67 |
public boolean testBounds() { |
| 68 |
return s.testBounds(); |
| 69 |
} |
| 70 |
|
| 71 |
public String getName() { |
| 72 |
return s.getName() + "(" + count + ")"; |
| 73 |
} |
| 74 |
}; |
| 75 |
} else if (xo.hasAttribute("df")) { |
| 76 |
final int df = xo.getIntegerAttribute("df"); |
| 77 |
|
| 78 |
final Scalable s = (Scalable) xo.getChild(Scalable.class); |
| 79 |
args[k] = new Scalable() { |
| 80 |
|
| 81 |
public int scale(double factor, int nDims, boolean testBounds) { |
| 82 |
s.scale(factor, -1, testBounds); |
| 83 |
return df; |
| 84 |
} |
| 85 |
|
| 86 |
@Override |
| 87 |
public boolean testBounds() { |
| 88 |
return s.testBounds(); |
| 89 |
} |
| 90 |
|
| 91 |
public String getName() { |
| 92 |
return s.getName() + "[df=" + df + "]"; |
| 93 |
} |
| 94 |
}; |
| 95 |
} |
| 96 |
} |
| 97 |
|
| 98 |
} |
| 99 |
return args; |
| 100 |
} |
| 101 |
|
| 102 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 103 |
|
| 104 |
final double scaleFactor = xo.getDoubleAttribute(SCALE_FACTOR); |
| 105 |
|
| 106 |
final double weight = xo.getDoubleAttribute(MCMCOperator.WEIGHT); |
| 107 |
|
| 108 |
final AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 109 |
|
| 110 |
final Scalable[] upArgs = getArgs(xo.getChild(UP)); |
| 111 |
final Scalable[] dnArgs = getArgs(xo.getChild(DOWN)); |
| 112 |
|
| 113 |
return new UpDownOperator(upArgs, dnArgs, scaleFactor, weight, mode); |
| 114 |
} |
| 115 |
|
| 116 |
public String getParserDescription() { |
| 117 |
return "This element represents an operator that scales two parameters in different directions. " + |
| 118 |
"Each operation involves selecting a scale uniformly at random between scaleFactor and 1/scaleFactor. " + |
| 119 |
"The up parameter is multipled by this scale and the down parameter is divided by this scale."; |
| 120 |
} |
| 121 |
|
| 122 |
public Class getReturnType() { |
| 123 |
return UpDownOperator.class; |
| 124 |
} |
| 125 |
|
| 126 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 127 |
return rules; |
| 128 |
} |
| 129 |
|
| 130 |
private final XMLSyntaxRule[] ee = { |
| 131 |
new ElementRule(Scalable.class, true), |
| 132 |
new ElementRule(Parameter.class, true), |
| 133 |
new ElementRule("scale", new XMLSyntaxRule[]{ |
| 134 |
AttributeRule.newIntegerRule("count", true), |
| 135 |
AttributeRule.newIntegerRule("df", true), |
| 136 |
new ElementRule(Scalable.class), |
| 137 |
}, true), |
| 138 |
}; |
| 139 |
|
| 140 |
private final XMLSyntaxRule[] rules = { |
| 141 |
AttributeRule.newDoubleRule(SCALE_FACTOR), |
| 142 |
AttributeRule.newDoubleRule(MCMCOperator.WEIGHT), |
| 143 |
AttributeRule.newBooleanRule(AdaptableMCMCOperator.AUTO_OPTIMIZE, true), |
| 144 |
|
| 145 |
|
| 146 |
new ElementRule(UP, ee, 1, Integer.MAX_VALUE), |
| 147 |
new ElementRule(DOWN, ee, 1, Integer.MAX_VALUE), |
| 148 |
}; |
| 149 |
|
| 150 |
} |