Skip to main content

modeling - Implementing the Biham–Middleton–Levine traffic model as CellularAutomaton



In an attempt to understand how to make rules for CellularAutomaton[], I set out to try to implement the Biham–Middleton–Levine traffic model.


It is a 2D, k=3 model with a 3x3 neighborhood, so the basic rule = {n, 3, {1,1}} has 19 638 states to map. Obviously not (practically) possible to encode into n (or?).


RulePlot[] is very useful for visualising (simple) rules, but it cannot(?) show more complicated ones. E.g. I tried a 5-neighbor totalistic rule for a k=2 model with binary encoding weights {{0,2,0},{4,1,8},{0,16,0}} to distinguish cell states, but I can't get it to work. The limitations of RulePlot[] are not very clear - where should I look?


I could not find any examples of explicit replacement rules, i.e. {lhs->rhs} - patterns would be useful - (but how to set the dimensions in this case?), so I ended up implementing it using a general function:


(* '1' moves right, '2' moves down, neighborhood is [[1;;3,1;;3]] *)
bml = {Switch[#[[2, 2]],
0, If[#[[2, 1]] == 1, 1, If[#[[1, 2]] == 2, 2, 0]], (* move in if one is coming, and Red comes first *)
1, If[#[[2, 3]] == 0, If[#[[1, 2]] == 2, 2, 0], 1], (* move a Red, if there is room, and let a Blue in if one is ready *)
2, If[#[[3, 2]] == 0 && #[[3, 1]] != 1, 0,
If[#[[3, 2]] == 1 && #[[3, 3]] == 0, 0, 2]] (* move a Blue if room, and a Red isn't coming or a Red is going *)

] &, {}, {1,1}};

Running the model is then (borrowing from the implementation of Conway):


fill = 0.36;  (* filling factor, something between 0.2 and 0.5 is interesting *)
board = Map[If[# < fill/2, 1, If[# < fill, 2, 0]] & , RandomReal[1, {100, 100}] , {2}];
Tally[Flatten[board]] (* show initial count *)
Dynamic[ArrayPlot[board = Last[CellularAutomaton[bml, board, {{0, 1}}]], ColorRules -> {2 -> Blue, 1 -> Red, 0 -> White}, ImageSize -> Large]]

It runs reasonably, but (of course) nowhere near as e.g. Jason Davies' WebGL implementation, so I'd be interested in seeing what optimisations could be done on the model above. And any alternative Mathematica implementations?


Are there other ways to implement conservative ('mass'-preserving) models? I'm thinking diffusion, brownian motion, etc. Is there another way of keeping track of the state changes, to give this illusion of 'movement'?




Answer



The following code simulates the Biham-Middleton-Levin traffic model for 1000 iterations using explicit rules. According to Wikipedia the blue and red cars take turns to move, which means each step is simpler than what you implemented, however.



  1. Simulating cellular automata with better performance has been dealt with here and here. (I've contributed to both.)

  2. Slow as simulating the system may be, visualizing it is even slower! It could be visualized frame by frame with Dynamic, but the frame rate would drop quickly with larger grids. We cannot get the same performance as WebGL, as you noted.

  3. I don't see any tangible benefit in using RulePlot, if it could be made to work, for this scenario.


Code (Export may be replaced by ListAnimate):


fill = 0.36;
board = RandomChoice[{1 - fill, fill/2, fill/2} -> {0, 1, 2}, {100, 100}];


red = {
{{_, _, _}, {1, 0, _}, {_, _, _}} -> 1,
{{_, _, _}, {_, 1, 0}, {_, _, _}} -> 0,
{{_, _, _}, {_, x_, _}, {_, _, _}} :> x
};
blue = {
{{_, _, _}, {_, 2, _}, {_, 0, _}} -> 0,
{{_, 2, _}, {_, 0, _}, {_, _, _}} -> 2,
{{_, _, _}, {_, x_, _}, {_, _, _}} :> x

};

redStep = CellularAutomaton[red];
blueStep = CellularAutomaton[blue];

evolve[board_] := blueStep[redStep[board]]

frames = NestList[evolve, board, 1000];

imageFrames = MapIndexed[Column[{

ArrayPlot[#, ColorRules -> {2 -> Blue, 1 -> Red, 0 -> White}, ImageSize -> 400],
StringTemplate["Iteration ``"]@First[#2]
}] &, frames];

Export["~/Desktop/traffic.gif", imageFrames]


Just to show you how I might have written you implementation using explicit rules, the two first rules in the Switch statement might've been written like this:


moveInRedComesFirst = {
{{_, _, _}, {1, 0, _}, {_, _, _}} -> 1,

{{_, 2, _}, {_, 0, _}, {_, _, _}} -> 2,
{{_, _, _}, {_, 0, _}, {_, _, _}} -> 0
};
moveRedLetBlueIn = {
{{_, _, _}, {2, 1, 0}, {_, _, _}} -> 2,
{{_, _, _}, {_, 1, 0}, {_, _, _}} -> 0,
{{_, _, _}, {_, 1, _}, {_, _, _}} -> 1
};

The order of the rules encode the If statements in a natural way; if the first rule doesn't match, it will proceed to the second one, and so on. The complete ruleset would be combination of these two and the third rule:



Join[moveInRedComesFirst, moveRedLetBlueIn, moveBlueIfRoomNoRedComingOrGoing]

Comments

Popular posts from this blog

plotting - Filling between two spheres in SphericalPlot3D

Manipulate[ SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, Mesh -> None, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], {n, 0, 1}] I cant' seem to be able to make a filling between two spheres. I've already tried the obvious Filling -> {1 -> {2}} but Mathematica doesn't seem to like that option. Is there any easy way around this or ... Answer There is no built-in filling in SphericalPlot3D . One option is to use ParametricPlot3D to draw the surfaces between the two shells: Manipulate[ Show[SphericalPlot3D[{1, 2 - n}, {θ, 0, Pi}, {ϕ, 0, 1.5 Pi}, PlotPoints -> 15, PlotRange -> {-2.2, 2.2}], ParametricPlot3D[{ r {Sin[t] Cos[1.5 Pi], Sin[t] Sin[1.5 Pi], Cos[t]}, r {Sin[t] Cos[0 Pi], Sin[t] Sin[0 Pi], Cos[t]}}, {r, 1, 2 - n}, {t, 0, Pi}, PlotStyle -> Yellow, Mesh -> {2, 15}]], {n, 0, 1}]

plotting - Plot 4D data with color as 4th dimension

I have a list of 4D data (x position, y position, amplitude, wavelength). I want to plot x, y, and amplitude on a 3D plot and have the color of the points correspond to the wavelength. I have seen many examples using functions to define color but my wavelength cannot be expressed by an analytic function. Is there a simple way to do this? Answer Here a another possible way to visualize 4D data: data = Flatten[Table[{x, y, x^2 + y^2, Sin[x - y]}, {x, -Pi, Pi,Pi/10}, {y,-Pi,Pi, Pi/10}], 1]; You can use the function Point along with VertexColors . Now the points are places using the first three elements and the color is determined by the fourth. In this case I used Hue, but you can use whatever you prefer. Graphics3D[ Point[data[[All, 1 ;; 3]], VertexColors -> Hue /@ data[[All, 4]]], Axes -> True, BoxRatios -> {1, 1, 1/GoldenRatio}]

plotting - Mathematica: 3D plot based on combined 2D graphs

I have several sigmoidal fits to 3 different datasets, with mean fit predictions plus the 95% confidence limits (not symmetrical around the mean) and the actual data. I would now like to show these different 2D plots projected in 3D as in but then using proper perspective. In the link here they give some solutions to combine the plots using isometric perspective, but I would like to use proper 3 point perspective. Any thoughts? Also any way to show the mean points per time point for each series plus or minus the standard error on the mean would be cool too, either using points+vertical bars, or using spheres plus tubes. Below are some test data and the fit function I am using. Note that I am working on a logit(proportion) scale and that the final vertical scale is Log10(percentage). (* some test data *) data = Table[Null, {i, 4}]; data[[1]] = {{1, -5.8}, {2, -5.4}, {3, -0.8}, {4, -0.2}, {5, 4.6}, {1, -6.4}, {2, -5.6}, {3, -0.7}, {4, 0.04}, {5, 1.0}, {1, -6.8}, {2, -4.7}, {3, -1.