| 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.operators; |
| 27 |
|
| 28 |
import dr.evomodel.continuous.MapDiffusionModel; |
| 29 |
import dr.evomodel.continuous.TopographicalMap; |
| 30 |
import dr.inference.model.Parameter; |
| 31 |
import dr.inference.operators.*; |
| 32 |
import dr.math.MathUtils; |
| 33 |
import dr.xml.*; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
public class RandomWalkOnMapOperator extends AbstractAdaptableOperator { |
| 41 |
|
| 42 |
public static final String OPERATOR_NAME = "randomWalkOnMapOperator"; |
| 43 |
public static final String WINDOW_SIZE = "windowSize"; |
| 44 |
public static final String UPDATE_INDEX = "updateIndex"; |
| 45 |
public static final String GRID_X_DIMENSION = "xGridDimension"; |
| 46 |
public static final String GRID_Y_DIMENSION = "yGridDimension"; |
| 47 |
|
| 48 |
public RandomWalkOnMapOperator(Parameter parameter, |
| 49 |
MapDiffusionModel mapModel, |
| 50 |
double windowSize, |
| 51 |
double weight, AdaptationMode mode) { |
| 52 |
super(mode); |
| 53 |
this.parameter = parameter; |
| 54 |
this.model = mapModel; |
| 55 |
this.map = mapModel.getMap(); |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
this.windowSize = windowSize; |
| 63 |
setWeight(weight); |
| 64 |
this.numberPoints = parameter.getDimension() / 2; |
| 65 |
|
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public Parameter getParameter() { |
| 73 |
return parameter; |
| 74 |
} |
| 75 |
|
| 76 |
public final double getWindowSize() { |
| 77 |
return windowSize; |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public final double doOperation() { |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
int index; |
| 88 |
index = MathUtils.nextInt(numberPoints) * 2; |
| 89 |
|
| 90 |
int deltaX = 0; |
| 91 |
int deltaY = 0; |
| 92 |
|
| 93 |
while (deltaX == 0 && deltaY == 0) { |
| 94 |
deltaX = MathUtils.nextInt(3) - 1; |
| 95 |
deltaY = MathUtils.nextInt(3) - 1; |
| 96 |
} |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
int newX = (int) parameter.getParameterValue(index) + deltaX; |
| 101 |
int newY = (int) parameter.getParameterValue(index + 1) + deltaY; |
| 102 |
|
| 103 |
if (map != null) { |
| 104 |
if (!map.isValidPoint(newX, newY)) { |
| 105 |
|
| 106 |
return Double.NEGATIVE_INFINITY; |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
parameter.setParameterValue(index, newX); |
| 117 |
parameter.setParameterValue(index + 1, newY); |
| 118 |
|
| 119 |
return 0.0; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
public final String getOperatorName() { |
| 124 |
return parameter.getParameterName(); |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
public double getAdaptableParameter() { |
| 129 |
return Math.log(windowSize); |
| 130 |
} |
| 131 |
|
| 132 |
public void setAdaptableParameter(double value) { |
| 133 |
windowSize = Math.exp(value); |
| 134 |
} |
| 135 |
|
| 136 |
public double getRawParameter() { |
| 137 |
return windowSize; |
| 138 |
} |
| 139 |
|
| 140 |
public String getAdaptableParameterName() { |
| 141 |
return "windowSize"; |
| 142 |
} |
| 143 |
|
| 144 |
public static dr.xml.XMLObjectParser PARSER = new AbstractXMLObjectParser() { |
| 145 |
|
| 146 |
|
| 147 |
public String getParserName() { |
| 148 |
return OPERATOR_NAME; |
| 149 |
} |
| 150 |
|
| 151 |
public Object parseXMLObject(XMLObject xo) throws XMLParseException { |
| 152 |
|
| 153 |
AdaptationMode mode = AdaptationMode.parseMode(xo); |
| 154 |
|
| 155 |
double weight = xo.getDoubleAttribute(WEIGHT); |
| 156 |
double windowSize = xo.getDoubleAttribute(WINDOW_SIZE); |
| 157 |
Parameter parameter = (Parameter) xo.getChild(Parameter.class); |
| 158 |
|
| 159 |
MapDiffusionModel mapModel = (MapDiffusionModel) xo.getChild(MapDiffusionModel.class); |
| 160 |
|
| 161 |
|
| 162 |
return new RandomWalkOnMapOperator(parameter, mapModel, windowSize, weight, mode); |
| 163 |
} |
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
public String getParserDescription() { |
| 170 |
return "This element returns a random walk operator on a given map."; |
| 171 |
} |
| 172 |
|
| 173 |
public Class getReturnType() { |
| 174 |
return MCMCOperator.class; |
| 175 |
} |
| 176 |
|
| 177 |
public XMLSyntaxRule[] getSyntaxRules() { |
| 178 |
return rules; |
| 179 |
} |
| 180 |
|
| 181 |
private XMLSyntaxRule[] rules = new XMLSyntaxRule[]{ |
| 182 |
AttributeRule.newDoubleRule(WINDOW_SIZE), |
| 183 |
AttributeRule.newDoubleRule(WEIGHT), |
| 184 |
AttributeRule.newBooleanRule(AUTO_OPTIMIZE, true), |
| 185 |
new ElementRule(MapDiffusionModel.class), |
| 186 |
new ElementRule(Parameter.class) |
| 187 |
}; |
| 188 |
|
| 189 |
}; |
| 190 |
|
| 191 |
public String toString() { |
| 192 |
return OPERATOR_NAME + "(" + parameter.getParameterName() + ")"; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
private Parameter parameter = null; |
| 198 |
private double windowSize = 0.01; |
| 199 |
private TopographicalMap map; |
| 200 |
private MapDiffusionModel model; |
| 201 |
private int numberPoints; |
| 202 |
|
| 203 |
private int maxX; |
| 204 |
private int maxY; |
| 205 |
|
| 206 |
} |
| 207 |
|