| 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.MatrixParameterInterface; |
| 29 |
import dr.inference.multidimensionalscaling.mm.MMAlgorithm; |
| 30 |
import dr.inference.multidimensionalscaling.mm.MultiDimensionalScalingMM; |
| 31 |
import dr.xml.*; |
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
public class ModeFindOperator extends AbstractAdaptableOperator { |
| 38 |
|
| 39 |
public static final String OPERATOR = "modeFindOperator"; |
| 40 |
public static final String MAX_TIMES = "maxTimes"; |
| 41 |
public static final String SCALE_FACTOR = "sd"; |
| 42 |
|
| 43 |
private double scaleFactor; |
| 44 |
|
| 45 |
private final int maxTimes; |
| 46 |
private final int maxSteps; |
| 47 |
private int executeTimes = 0; |
| 48 |
private final MultiDimensionalScalingMM mm; |
| 49 |
|
| 50 |
public ModeFindOperator(MultiDimensionalScalingMM mm, int maxTimes, double weight, |
| 51 |
AdaptationMode mode, double scaleFactor) { |
| 52 |
this(mm, maxTimes, weight, 1000, mode, scaleFactor); |
| 53 |
} |
| 54 |
|
| 55 |
public ModeFindOperator(MultiDimensionalScalingMM mm, int maxTimes, double weight, |
| 56 |
int maxModeSteps, AdaptationMode mode, double scaleFactor) { |
| 57 |
super(mode); |
| 58 |
setWeight(weight); |
| 59 |
|
| 60 |
this.maxTimes = maxTimes; |
| 61 |
this.mm = mm; |
| 62 |
this.maxSteps = maxModeSteps; |
| 63 |
this.scaleFactor = scaleFactor; |
| 64 |
} |
| 65 |
|
| 66 |
public double doOperation() { |
| 67 |
|
| 68 |
if (executeTimes < maxTimes) { |
| 69 |
|
| 70 |
MatrixParameterInterface parameter = mm.getLikelihood().getMatrixParameter(); |
| 71 |
|
| 72 |
|
| 73 |
boolean sample = mode == AdaptationMode.ADAPTATION_ON || mode == AdaptationMode.DEFAULT; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
mm.run(maxSteps); |
| 83 |
|
| 84 |
++executeTimes; |
| 85 |
|
| 86 |
double logHR = Double.POSITIVE_INFINITY; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
if (sample) { |
| 91 |
System.err.println("Adaptive sampling: " + scaleFactor); |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
logHR = 0.0; |
| 104 |
} |
| 105 |
|
| 106 |
return logHR; |
| 107 |
|
| 108 |
} else { |
| 109 |
throw new RuntimeException("Finished max times"); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
public final String getOperatorName() { |
| 115 |
return OPERATOR; |
| 116 |
} |
| 117 |
|
| 118 |
@Override |
| 119 |
public double getAdaptableParameter() { |
| 120 |
return Math.log(scaleFactor); |
| 121 |
} |
| 122 |
|
| 123 |
@Override |
| 124 |
public void setAdaptableParameter(double value) { |
| 125 |
scaleFactor = Math.exp(value); |
| 126 |
} |
| 127 |
|
| 128 |
@Override |
| 129 |
public double getRawParameter() { |
| 130 |
return scaleFactor; |
| 131 |
} |
| 132 |
|
| 133 |
public String getAdaptableParameterName() { |
| 134 |
return "scaleFactor"; |
| 135 |
} |
| 136 |
|
| 137 |
public static XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 138 |
|
| 139 |
public String getParserName() { |
| 140 |
return OPERATOR; |
| 141 |
} |
| 142 |
|
| 143 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 144 |
|
| 145 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 146 |
double scaleFactor = xo.getAttribute(SCALE_FACTOR, 1.0); |
| 147 |
|
| 148 |
System.err.println(mode); |
| 149 |
|
| 150 |
|
| 151 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 152 |
|
| 153 |
int maxTimes = xo.getAttribute(MAX_TIMES, Integer.MAX_VALUE); |
| 154 |
|
| 155 |
MultiDimensionalScalingMM mm = (MultiDimensionalScalingMM) xo.getChild(MMAlgorithm.class); |
| 156 |
|
| 157 |
return new ModeFindOperator(mm, maxTimes, weight, mode, scaleFactor); |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
public String getParserDescription() { |
| 165 |
return "This element returns a mode finder that always accepts."; |
| 166 |
} |
| 167 |
|
| 168 |
public Class getReturnType() { |
| 169 |
return ModeFindOperator.class; |
| 170 |
} |
| 171 |
|
| 172 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 173 |
return rules; |
| 174 |
} |
| 175 |
|
| 176 |
private final XMLSyntaxRule[] rules = { |
| 177 |
AttributeRule.newIntegerRule(MAX_TIMES, true), |
| 178 |
AttributeRule.newDoubleRule(WEIGHT), |
| 179 |
AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true), |
| 180 |
AttributeRule.newDoubleRule(SCALE_FACTOR, true), |
| 181 |
new ElementRule(MultiDimensionalScalingMM.class), |
| 182 |
}; |
| 183 |
|
| 184 |
}; |
| 185 |
} |