Simulate Cellular Automata with a four Dimensional Table in Mathematica
In the book Modelling Cellular automata Simulations with mathematica , the author is using the following code to simulate cellular automata in a two dimensional lattice,
From the Moore Neighbourhood the update rule is
update[site, N, E, S, W, NE, SE, SW, NW]
where N =North, E= East , S=South, W=West, NE= North East, SE=South East, SW= South East, NW=North West. Those arguments represent the values of the nearest neigbours in the Moor Neighbourhood. To apply those rules it uses the code below,
Moore[func_, lat_]:= MapThread[func,Map[RotateRight[lat,#]&,
{{0,0},{1,0},{0,-1},{-1,0},{0,1},
{1,-1},{-1,-1},{-1,1},{1,1}}],2]
For a table like the following (page 144 from the book)
pasture= Table[Floor[Random[]+(preyDensity+predDensity)]*
Floor[1+Random[]+predDensity/(preyDensity+predDensity)],{n},{n}]/.
2:>{RND,Random[Integer, {1,3}],Random[Integer,{1,5}]}
RND:= Random[Integer, {1,4}]
he is using the following update rule
update[{_,0,0},_,_,_,_,_,_,_,_] := {RND, 3,5}
My question is: By using a four dimensional table like the following? Can I also apply the following update rule?
InitialMatrix[x_, y_, age_, disease_] :=
ReplacePart[
Table[3, {x}, {y}, {age}, {disease}], {{_, _, 1, _} ->
0, {_, _, 2, 1} ->
Floor[dogpopulation*0.2/cellsno], {_, _, 2, 3} ->
Floor[dogpopulation*0.05/cellsno], {_, _, 3, 1} ->
Floor[dogpopulation*0.58/cellsno], {_, _, 3, 3} ->
Floor[dogpopulation*0.15/cellsno]}] /.
3 :> If[RandomReal[] > 0.2, 0, RandomInteger[{1, 2}]];
update[{{x_,0,0},{y_,z_,w_},{a_,b_,c_}},_,_,_,_,_,_,_,_] :=
{{x-1,0,0},{y+z,0,w},{a,b,c}}
This is an example of how I think I can use my table to work with cellular automata. Can I do something like that? Or I am wrong?
Edit my table
By changing my table into the following code below can I use the update rule above?
MyMatrix[x_, y_, age_, disease_] :=
Table[0, {开发者_高级运维x}, {y}] /.
0 :> ReplacePart[
Table[3, {age}, {disease}], {{1, _} -> 0, {2, 1} ->
Floor[dogpopulation*0.2/cellsno], {2, 3} ->
Floor[dogpopulation*0.05/cellsno], {3, 1} ->
Floor[dogpopulation*0.58/cellsno], {3, 3} ->
Floor[dogpopulation*0.15/cellsno]}] /.
3 :> If[RandomReal[] > 0.2, 0, RandomInteger[{1, 2}]];
The book Modelling Cellular automata Simulations with mathematica was written more than 15 years ago, so would recommend looking at the CellularAutomaton[] function in Mathematica.
The function CellularAutomaton[] is a little complex, but if you want to do 2D Moore CA this is how you call this function:
CellualrAutomaton[{func[#]&,{},{1,1}},initialMatrixVal, numIterations];
What the above code will do is call function func[], with the Moore neighborhood as fn param for each and every node in initialMatrixVal for "numIterations" times.
The CellularAutomaton[] function assumes Periodic boundary conditions.
Hope that helps!
Most of the code for the book "Modelling Cellular automata Simulations with mathematica" can be found in the Wolfram web site (library.wolfram.com ?). If you search for the author's name you will find example code for most of the topics covered in the book.
I probably have the code for most of the examples in the book, if you are interested let me know.
Good luck!
精彩评论