在 Mathematica 中使用四维表模拟元胞自动机

发布于 2024-11-19 09:34:51 字数 2159 浏览 3 评论 0原文

在书中 元胞自动机建模使用mathematica进行模拟,作者使用以下代码来模拟二维晶格中的元胞自动机,

从摩尔邻域来看,更新规则是

update[site, N, E, S, W, NE, SE, SW, NW]

其中N = North,E = East,S = South,W = West, NE= 东北,SE= 东南,SW= 东南,NW= 西北。这些论点代表了摩尔街区最近邻居的价值观。为了应用这些规则,它使用下面的代码,

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]

对于如下所示的表(书中第 144 页),

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}]

他使用以下更新规则

update[{_,0,0},_,_,_,_,_,_,_,_]  := {RND, 3,5}

我的问题是:通过使用如下所示的四维表?我还可以应用以下更新规则吗?

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}}

这是我认为如何使用我的表格来处理元胞自动机的示例。我可以做这样的事情吗?还是我错了?

编辑我的表格

通过将我的表格更改为下面的代码,我可以使用上面的更新规则吗?

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}]];

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}]];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

Oo萌小芽oO 2024-11-26 09:34:51

《使用 mathematica 进行元胞自动机模拟建模》一书已于 15 年前编写,因此建议查看 Mathematica 中的 CellularAutomaton[] 函数。

函数 CellularAutomaton[] 有点复杂,但如果您想做 2D Moore CA,您可以这样调用该函数:

CellualrAutomaton[{func[#]&,{},{1,1}},initialMatrixVal, numIterations ];

上面的代码将执行的操作是调用函数 func[],将摩尔邻域作为 initialMatrixVal 中每个节点的 fn 参数,持续“numIterations”次。

CellularAutomaton[] 函数假设周期性边界条件。

希望有帮助!

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!

你穿错了嫁妆 2024-11-26 09:34:51

“使用 mathematica 进行元胞自动机模拟建模”一书的大部分代码可以在 Wolfram 网站(library.wolfram.com?)中找到。如果您搜索作者的姓名,您将找到本书涵盖的大多数主题的示例代码。

我可能有书中大部分示例的代码,如果您有兴趣,请告诉我。

祝你好运!

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!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文