Debian Codegraph @

src/dr/evomodel/operators/RandomWalkOnMapOperator.java

raw symbols SCIP
1 /*
2 * RandomWalkOnMapOperator.java
3 *
4 * Copyright (c) 2002-2015 Alexei Drummond, Andrew Rambaut and Marc Suchard
5 *
6 * This file is part of BEAST.
7 * See the NOTICE file distributed with this work for additional
8 * information regarding copyright ownership and licensing.
9 *
10 * BEAST is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU Lesser General Public License as
12 * published by the Free Software Foundation; either version 2
13 * of the License, or (at your option) any later version.
14 *
15 * BEAST is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with BEAST; if not, write to the
22 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
23 * Boston, MA 02110-1301 USA
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 //import org.apiacoa.games.terrain.CostNodeTileTerrain;
35
36
37 /**
38 * @author Marc Suchard
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 // this.terrain = mapModel.getTerrain();
57 //
58 // if (this.terrain != null) {
59 // maxX = terrain.getColumns();
60 // maxY = terrain.getRows();
61 // }
62 this.windowSize = windowSize;
63 setWeight(weight);
64 this.numberPoints = parameter.getDimension() / 2;
65
66 }
67
68
69 /**
70 * @return the parameter this operator acts on.
71 */
72 public Parameter getParameter() {
73 return parameter;
74 }
75
76 public final double getWindowSize() {
77 return windowSize;
78 }
79
80 /**
81 * change the parameter and return the hastings ratio.
82 */
83 public final double doOperation() {
84
85 // a random dimension to perturb
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) { // eight uniform choices
94 deltaX = MathUtils.nextInt(3) - 1;
95 deltaY = MathUtils.nextInt(3) - 1;
96 }
97
98 // a random point around old value within window [-1,1] todo expand windowsize
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 // throw new OperatorFailedException("proposed value outside boundaries");
106 return Double.NEGATIVE_INFINITY;
107 }
108 }
109
110 // if (terrain != null) {
111 // if (newX < 0 || newY < 0 || newX >= maxX || newY >= maxY || model.isBlocked(newX, newY)) {
112 // throw new OperatorFailedException("proposed value outside boundaries");
113 // }
114 // }
115
116 parameter.setParameterValue(index, newX);
117 parameter.setParameterValue(index + 1, newY);
118
119 return 0.0;
120 }
121
122 //MCMCOperator INTERFACE
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 // AbstractXMLObjectParser implementation
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 //PRIVATE STUFF
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