| 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.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 |
|
| 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 |
|
| 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++; |
| 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 |
|
| 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 |
|
| 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 |
} |