Debian Codegraph @

src/dr/geo/operators/UniformGeoSpatialOperator.java

raw symbols SCIP
1 /*
2 * UniformGeoSpatialOperator.java
3 *
4 * Copyright (c) 2002-2017 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.geo.operators;
27
28 import dr.geo.AbstractPolygon2D;
29 import dr.geo.Polygon2D;
30 import dr.inference.model.Parameter;
31 import dr.inference.operators.SimpleMCMCOperator;
32 import dr.math.MathUtils;
33
34 import java.awt.geom.Point2D;
35 import java.util.List;
36
37 /**
38 * @author Marc A. Suchard
39 */
40 public class UniformGeoSpatialOperator extends SimpleMCMCOperator {
41
42 public UniformGeoSpatialOperator(Parameter parameter, double weight, List<AbstractPolygon2D> polygonList) {
43 this.parameter = parameter;
44 this.polygonList = polygonList;
45 setWeight(weight);
46 }
47
48 /**
49 * change the parameter and return the hastings ratio.
50 */
51 public final double doOperation() {
52
53 double[] mass = null;
54 int whichRegion = 0;
55 if (polygonList.size() > 1) {
56 mass = new double[polygonList.size()];
57 for (int i = 0; i < polygonList.size(); ++i) {
58 mass[i] = polygonList.get(i).calculateArea();
59 }
60 whichRegion = MathUtils.randomChoicePDF(mass);
61 }
62
63 totalOps++;
64 if (whichRegion == 0) {
65 currentSum++; // For debugging prior probability of first region
66 }
67
68 AbstractPolygon2D polygon = polygonList.get(whichRegion);
69
70 double[][] minMax = polygon.getXYMinMax();
71
72 int attempts = 0;
73 Point2D pt;
74 do {
75 pt = new Point2D.Double(
76 (MathUtils.nextDouble() * (minMax[0][1] - minMax[0][0])) + minMax[0][0],
77 (MathUtils.nextDouble() * (minMax[1][1] - minMax[1][0])) + minMax[1][0]);
78 attempts++;
79
80 } while (!polygon.containsPoint2D(pt));
81
82 if (DEBUG) {
83 System.err.println("region: " + whichRegion + " attempts: " + attempts + " " + mass[0] + " " + mass[1] + " " + (
84 (double) currentSum / (double) totalOps
85 ));
86 }
87
88 parameter.setParameterValue(0, pt.getX());
89 parameter.setParameterValue(1, pt.getY());
90
91 return 0.0;
92 }
93
94 //MCMCOperator INTERFACE
95 public final String getOperatorName() {
96 return "uniformGeoSpatial(" + parameter.getParameterName() + ")";
97 }
98
99 public String getPerformanceSuggestion() {
100 return "";
101 }
102
103 public String toString() {
104 return UniformGeoSpatialOperatorParser.UNIFORM_OPERATOR + "(" + parameter.getParameterName() + ")";
105 }
106
107 //PRIVATE STUFF
108 private final Parameter parameter;
109 private final List<AbstractPolygon2D> polygonList;
110
111 private long totalOps = 0;
112 private long currentSum = 0;
113
114 private static final boolean DEBUG = false;
115 }