We conclude our study of functions and modules by considering a case study of developing a program to solve an interesting scientific problem. as Monte Carlo simulation to study a natural model known as percolation.
We model the system as an
n-by-
ngrid of sites. Each site is either
blockedor
open; open sites are initially
empty. A
fullsite is an open site that can be connected to an open site in the top row via a chain of neighboring (left, right, up, down) open sites. If there is a full site in the bottom row, then we say that the system
percolates.
If sites are independently set to be open with
vacancy probability p, what is the probability that the system percolates? No mathematical solution to this problem has yet been derived. Our task is to write computer programs to help study the problem.
Data representation.Our first step is to pick a representation of the data. We use one
boolean matrix isOpen[][]to represent which sites are open and another boolean matrix
isFull[][]that to represent which sites are full.
Vertical percolation.Given a boolean matrix that represents the open sites, how do we figure out whether it represents a system that percolates? For the moment, we will consider a much simpler version of the problem that we call
vertical percolation. The simplification is to restrict attention to vertical connection paths.
VerticalPercolation.java
determines the sites that are filled by some path that is connected vertically to the top using a simple calculation.
Data visualization.PercolationVisualizer.java
is a test client that generates random boolean matrices and plots them using standard drawing.
Estimating probabilities.PercolationProbability.java
estimates the probability that a random
n-by-
nsystem with site vacancy probability
ppercolates. We refer to this quantity as the
percolation probability. To estimate its value, we simply run a number of experiments.
Recursive solution for percolation.How do we test whether a system percolates in the general case when
anypath starting at the top and ending at the bottom (not just a vertical one) will do the job? Remarkably, we can solve this problem with a compact program, based on a classic recursive scheme known as
depth-first search.
Percolation.javatakes this approach. See the textbook for details.
Adaptive plot.PercolationPlot.javaplots the percolation probability as a function of the site vacancy probability
pfor an
n-by-
nsystem. It uses a recursive approach that produces a good-looking curve at relatively low cost.
The curves support the hypothesis that there is a
thresholdvalue (about 0.593): if
pis greater than the threshold, then the system almost certainly percolates; if
pis less than the threshold, then the system almost certainly does not percolate. As
nincreases, the curve approaches a step function that changes value from 0 to 1 at the threshold. This phenomenon, known as a
phase transition, is found in many physical systems.
ExercisesAnalytic solution: n2.
Famous 1949 problem of Nobel prize winning chemist, Flory. Conjecture: exponent of root mean square displacement is 3/4 in 2D and 3/5 in 3D.
Or keep a trail of length n.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4