Debian Codegraph @

src/dr/geo/InhomogeneousRandomWalk.java

raw symbols SCIP
1 /*
2 * InhomogeneousRandomWalk.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.geo;
27
28 import dr.app.gui.ColorFunction;
29
30 import javax.swing.*;
31 import java.awt.*;
32 import java.awt.geom.Rectangle2D;
33 import java.util.Random;
34
35 /**
36 * @author Alexei Drummond
37 */
38 public class InhomogeneousRandomWalk extends JComponent {
39
40 Lattice lattice;
41 RateMatrix rates;
42 int i, j;
43 Random random;
44 double time;
45 int[][] sample;
46 int maxSample = 0;
47
48 ColorFunction cf = new ColorFunction(
49 new Color[]{Color.white, Color.blue, Color.magenta, Color.red},
50 new float[]{0.0f, 0.10f, 0.20f, 1.0f}
51 );
52
53 public InhomogeneousRandomWalk(
54 Lattice lattice, Location loc, Random random, RateMatrix rates) {
55
56 this.lattice = lattice;
57 this.rates = rates;
58 this.i = loc.i;
59 this.j = loc.j;
60 this.random = random;
61
62 sample = new int[lattice.latticeWidth()][lattice.latticeHeight()];
63 }
64
65 public Dimension getPreferredSize() {
66 return new Dimension(lattice.latticeWidth(), lattice.latticeHeight());
67 }
68
69 public void paintComponent(Graphics g) {
70
71 lattice.paintLattice(g);
72
73 for (int i = 0; i < lattice.latticeWidth(); i++) {
74 for (int j = 0; j < lattice.latticeHeight(); j++) {
75 if (sample[i][j] > 0) {
76 if (lattice.getState(i, j) >= 0) {
77 float intensity = (float) sample[i][j] / (float) maxSample;
78 g.setColor(cf.getColor(intensity));
79 g.drawRect(i, j, 1, 1);
80 }
81 }
82 }
83 }
84 }
85
86
87 public double computeP(Location from, Location to, double t, int n) {
88
89
90 int count = 0;
91 for (int rep = 0; rep < n; rep++) {
92 simulate(from, t);
93 if (i == to.i && j == to.j) {
94 count += 1;
95 }
96 }
97 return (double) count / (double) n;
98 }
99
100
101 public void simulate(Location from, double t) {
102 this.i = from.i;
103 this.j = from.j;
104 time = 0;
105 while (time < t) {
106 step(t);
107 }
108 }
109
110 public void simulate(int steps) {
111 for (int i = 0; i < steps; i++) {
112 step(Double.MAX_VALUE);
113 }
114 }
115
116 public void step(double toTime) {
117
118 int fromState = lattice.getState(i, j);
119
120 double moveUp = 0.0;
121 double moveLeft = 0.0;
122 double moveRight = 0.0;
123 double moveDown = 0.0;
124
125 if (i > 0) {
126 moveLeft = rates.getRate(fromState, lattice.getState(i - 1, j));
127 }
128
129 if (i < lattice.latticeWidth() - 1) {
130 moveRight = rates.getRate(fromState, lattice.getState(i + 1, j));
131 }
132
133 if (j > 0) {
134 moveUp = rates.getRate(fromState, lattice.getState(i, j - 1));
135 }
136
137 if (j < lattice.latticeHeight() - 1) {
138 moveDown = rates.getRate(fromState, lattice.getState(i, j + 1));
139 }
140
141 double totalRate =
142 moveUp + moveDown + moveLeft + moveRight;
143
144
145 double V = random.nextDouble();
146 double dt = -Math.log(V) / totalRate;
147 if (time + dt > toTime) {
148 time = toTime;
149 return;
150 }
151
152 double U = random.nextDouble() * totalRate;
153
154 if (U < moveUp) {
155 // move up
156 j -= 1;
157 } else {
158 U -= moveUp;
159 if (U < moveRight) {
160 // move right
161 i += 1;
162 } else {
163 U -= moveRight;
164 if (U < moveDown) {
165 // move down
166 j += 1;
167 } else {
168 // move left
169 i -= 1;
170 }
171 }
172 }
173
174 time += dt;
175 sample[i][j] += 1;
176 if (sample[i][j] > maxSample) maxSample = sample[i][j];
177 }
178
179 public static void main(String[] args) {
180
181 String kmlFile = args[0];
182 double left = Double.parseDouble(args[1]);
183 double top = Double.parseDouble(args[2]);
184 double width = Double.parseDouble(args[3]);
185 double height = Double.parseDouble(args[4]);
186
187 Rectangle2D bounds = new Rectangle2D.Double(left, top, width, height);
188
189 Random random = new Random();
190
191
192 final double[][] rates = new double[2][2];
193 rates[0][0] = 0.0;
194 rates[0][1] = 0.0;
195 rates[1][0] = 0.0;
196 rates[1][1] = 2.0;
197
198 KMLRenderer renderer = new KMLRenderer(kmlFile, Color.white, Color.black);
199 renderer.setBounds(bounds);
200 renderer.render(1000);
201
202 RateMatrix matrix = new RateMatrix() {
203
204 public double getRate(int i, int j) {
205 return rates[i][j];
206 }
207 };
208
209 InhomogeneousRandomWalk walk = new InhomogeneousRandomWalk(renderer, new Location(0, 0), random, matrix);
210
211 for (int i = 0; i < 5; i++) {
212 Location start = Lattice.Utils.getRandomLocation(renderer, 1, random);
213 for (int j = 0; j < 1000; j++) {
214 walk.simulate(start, 4000);
215 }
216 }
217 JFrame frame = new JFrame();
218
219 frame.getContentPane().add(BorderLayout.CENTER, walk);
220 frame.pack();
221 frame.setVisible(true);
222 }
223
224
225 }