GameMale
登陆 / 注册 搜索

USERCENTER

SEARCHSITE

搜索

查看: 2103|回复: 14
收起左侧

[技术交流] 【Python】【原创】元胞自动机与动态迁移模型

[复制链接] |关注本帖

驯化腐化龙幼崽质量效应三部曲箭术卷轴魔法石碑冒险用绷带神秘的红茶黄色就是俏皮海盗弯钩

     楼主| 白冥 发表于 2025-2-3 21:00:40 | 显示全部楼层 |阅读模式 |取消关注该作者的回复


    目录      

    • 概述
    • 安装与环境配置
    • 系列技术贴
    • 类结构与方法
    • 源代码
    • 示例1-兰顿蚂蚁
    • 示例2-草地捕食链


    概述      
            
            上一篇元胞自动机的贴子中,我们研究过森林火灾、核污染扩散 ,并扩展了康威的生命游戏的背景知识,这些都是动态扩散模型。众所周知,元胞自动机是一个离散动力学系统,这就说明了它不仅仅可以模拟此前我们研究过的动态扩散模型,本帖将给大家带来元胞自动机的另一种应用——动态迁移模型
            本帖代码均由本人写就。


    安装与系统配置      
            本代码基于 Python 3,建议使用 Python 3.8 及以上版本。
            代码中仅使用了标准库itertools, typing,无需安装额外的第三方库。


    系列技术贴      
        元胞自动机(一):元胞自动机与动态扩散模型

    类结构与方法      
            与《元胞自动机与动态扩散模型》相同


    源代码      
            与《元胞自动机与动态扩散模型》相同


    示例1-兰顿蚂蚁
            背景:在平面上的正方形格被填上白色。在其中一格正方形有一只“蚂蚁”。它的头部朝向上下左右其中一方。若蚂蚁在白格,右转90度,将该格改为黑格,向前移一步;若蚂蚁在黑格,左转90度,将该格改为白格,向前移一步。



    1. if __name__ == "__main__":
    2.     from random import choice
    3.    
    4.     ant_state = ["w_l", "w_r", "w_u", "w_d", "b_l", "b_r", "b_u", "b_d"]
    5.    
    6.     def ant_rule(cell:Cell, neighbors:List[Cell]):
    7.         state = cell.get_state()
    8.         x, y = tuple(cell.get_coordinate())
    9.         black_neighbors = [n for n in neighbors if n.get_state() == "black"]
    10.         white_neighbors = [n for n in neighbors if n.get_state() == "white"]
    11.         ant = next([n for n in neighbors if n.get_state() in ant_state], None)
    12.         ant_moving = {
    13.             "white": {
    14.                 ("w_l", [x,y-1]): Cell("b_u", x, y),
    15.                 ("w_r", [x,y+1]): Cell("b_d", x, y),
    16.                 ("w_u", [x-1,y]): Cell("b_r", x, y),
    17.                 ("w_d", [x+1,y]): Cell("b_l", x, y),
    18.                 ("b_l", [x,y+1]): Cell("b_d", x, y),
    19.                 ("b_r", [x,y-1]): Cell("b_u", x,y),
    20.                 ("b_u", [x+1,y]): Cell("b_l", x,y),
    21.                 ("b_d", [x-1,y]): Cell("b_r", x,y)
    22.             },
    23.             "black": {
    24.                 ("w_l", [x,y-1]): Cell("w_u", x, y),
    25.                 ("w_r", [x,y+1]): Cell("w_d", x, y),
    26.                 ("w_u", [x-1,y]): Cell("w_r", x, y),
    27.                 ("w_d", [x+1,y]): Cell("w_l", x, y),
    28.                 ("b_l", [x,y+1]): Cell("w_d", x, y),
    29.                 ("b_r", [x,y-1]): Cell("w_u", x,y),
    30.                 ("b_u", [x+1,y]): Cell("w_l", x,y),
    31.                 ("b_d", [x-1,y]): Cell("w_r", x,y)
    32.             }
    33.         }
    34.         alter_color = {
    35.             "w_l": Cell("black", x, y),
    36.             "w_r": Cell("black", x, y),
    37.             "w_u": Cell("black", x, y),
    38.             "w_d": Cell("black", x, y),
    39.             "b_l": Cell("white", x, y),
    40.             "b_r": Cell("white", x, y),
    41.             "b_u": Cell("white", x, y),
    42.             "b_d": Cell("white", x, y)
    43.         }
    44.         if state in ant_moving and ant:
    45.             ant_state = ant.get_state()
    46.             ant_coord = ant.get_coordinate()
    47.             key = (ant_state, ant_coord)
    48.             if key in ant_moving[state]:
    49.                 return ant_moving[state][key]
    50.         elif state in ant_state:
    51.             return alter_color[state]
    52.         return cell
    53.     def main():
    54.         L = Space()
    55.         size = (120,120)
    56.         status = ["black", "white", "w_l", "w_r", "w_u", "w_d", "b_l", "b_r", "b_u", "b_d"]
    57.         d = 2
    58.         S = Status(default = "white", *status)
    59.         N = Neighborhood(*size)
    60.         f = Rule(S, ant_rule)
    61.         ca = CA(L, d, S, N,f)
    62.         cells = []
    63.         for i in range(120):
    64.             for j in range(120):
    65.                 cells.append(Cell("white", i, j))
    66.         cell = choice(cells)
    67.         towards = ["w_l", "w_r", "w_u", "w_d"]
    68.         state = choice(towards)
    69.         ant=Cell(state, *cell.get_coordinate())
    70.         cells.remove(cell)
    71.         cells.append(ant)
    72.         ca.evolution(cells, time = 100)
    73.    
    74.     main()
    复制代码

    示例2-草地捕食链
            背景:某片草地(长200,宽100)存在着羊群和狼群,羊以当地的牧草为食,狼则以羊为食。模拟草地上的形势。


            全局数据结构:


    1. sheep_state = ["s_l_str", "s_r_str", "s_u_str", "s_d_str", "s_l_lt", "s_r_lt", "s_u_lt", "s_d_lt", "s_l_rt", "s_r_rt", "s_u_rt", "s_d_rt", "s_l_sta", "s_r_sta", "s_u_sta", "s_d_sta"]
    2. sheep_stays = ["s_l_sta", "s_r_sta", "s_u_sta", "s_d_sta"]
    3. wolf_state = ["w_l_str", "w_r_str", "w_u_str", "w_d_str", "w_l_lt", "w_r_lt", "w_u_lt", "w_d_lt", "w_l_rt", "w_r_rt", "w_u_rt", "w_d_rt", "w_l_sta", "w_r_sta", "w_u_sta", "w_d_sta"]
    4. wolf_stays = ["w_l_sta", "w_r_sta", "w_u_sta", "w_d_sta"]
    复制代码

            导入库:
    1. from random import random
    2. from random import choice
    复制代码

            代码主体:

    1. if __name__ == "__main__":
    2.    
    3.     def rule(cell:Cell, neighbors:List[Cell]):
    4.         state = cell.get_state()
    5.         x,y = tuple(cell.get_coordinate())
    6.         empty_neighbors = [n for n in neighbors if n.get_state() == "empty"]
    7.         grass_neighbors = [n for n in neighbors if n.get_state() == "grass"]
    8.         sheep_neighbors = [n for n in neighbors if n.get_state() in sheep_state]
    9.         wolf_neighbors = [n for n in neighbors if n.get_state() in wolf_state]
    10.         w_moving = {
    11.             ("w_l_str", [x+1,y]): Cell("w_l_sta", x,y),
    12.             ("w_r_str", [x-1,y]): Cell("w_r_sta", x,y),
    13.             ("w_u_str", [x,y-1]): Cell("w_u_sta", x,y),
    14.             ("w_d_str", [x,y+1]): Cell("w_d_sta", x,y),
    15.             ("w_l_lt", [x+1,y+1]): Cell("w_d_sta", x,y),
    16.             ("w_r_lt", [x-1,y-1]): Cell("w_u_sta", x,y),
    17.             ("w_u_lt", [x+1,y-1]): Cell("w_l_sta", x,y),
    18.             ("w_d_lt", [x-1,y+1]): Cell("w_r_sta", x,y),
    19.             ("w_l_rt", [x+1,y-1]): Cell("w_u_sta", x,y),
    20.             ("w_r_rt", [x-1,y+1]): Cell("w_d_sta", x,y),
    21.             ("w_u_rt", [x-1,y-1]): Cell("w_r_sta", x,y),
    22.             ("w_d_rt", [x+1,y+1]): Cell("w_l_sta", x,y)}
    23.         w_foraging = {
    24.             ("w_l_sta", [x-1,y]): Cell("w_l_str", x,y),
    25.             ("w_l_sta", [x-1,y-1]): Cell("w_l_lt", x,y),
    26.             ("w_l_sta", [x-1,y+1]): Cell("w_l_rt",x,y),
    27.             ("w_r_sta", [x+1,y]): Cell("w_r_str", x,y),
    28.             ("w_r_sta", [x+1,y-1]): Cell("w_r_rt", x,y),
    29.             ("w_r_sta", [x+1,y+1]): Cell("w_r_lt",x,y),
    30.             ("w_u_sta", [x, y+1]): Cell("w_u_str", x,y),
    31.             ("w_u_sta", [x-1, y+1]): Cell("w_u_lt", x,y),
    32.             ("w_u_sta", [x+1, y+1]): Cell("w_u_rt",x,y),
    33.             ("w_d_sta", [x,y-1]): Cell("w_d_str", x,y),
    34.             ("w_d_sta", [x-1,y-1]): Cell("w_d_rt", x,y),
    35.             ("w_d_sta", [x+1,y+1]): Cell("w_d_lt",x,y)}
    36.         s_moving = {
    37.             ("s_l_str", [x+1,y]): Cell("s_l_sta", x,y),
    38.             ("s_r_str", [x-1,y]): Cell("s_r_sta", x,y),
    39.             ("s_u_str", [x,y-1]): Cell("s_u_sta", x,y),
    40.             ("s_d_str", [x,y+1]): Cell("s_d_sta", x,y),
    41.             ("s_l_lt", [x+1,y+1]): Cell("s_d_sta", x,y),
    42.             ("s_r_lt", [x-1,y-1]): Cell("s_u_sta", x,y),
    43.             ("s_u_lt", [x+1,y-1]): Cell("s_l_sta", x,y),
    44.             ("s_d_lt", [x-1,y+1]): Cell("s_r_sta", x,y),
    45.             ("s_l_rt", [x+1,y-1]): Cell("s_u_sta", x,y),
    46.             ("s_r_rt", [x-1,y+1]): Cell("s_d_sta", x,y),
    47.             ("s_u_rt", [x-1,y-1]): Cell("s_r_sta", x,y),
    48.             ("s_d_rt", [x+1,y+1]): Cell("s_l_sta", x,y)}
    49.         s_foraging = {
    50.             ("s_l_sta", [x-1,y]): Cell("s_l_str", x,y),
    51.             ("s_l_sta", [x-1,y-1]): Cell("s_l_lt", x,y),
    52.             ("s_l_sta", [x-1,y+1]): Cell("s_l_rt",x,y),
    53.             ("s_r_sta", [x+1,y]): Cell("s_r_str", x,y),
    54.             ("s_r_sta", [x+1,y-1]): Cell("s_r_rt", x,y),
    55.             ("s_r_sta", [x+1,y+1]): Cell("s_r_lt",x,y),
    56.             ("s_u_sta", [x, y+1]): Cell("s_u_str", x,y),
    57.             ("s_u_sta", [x-1, y+1]): Cell("s_u_lt", x,y),
    58.             ("s_u_sta", [x+1, y+1]): Cell("s_u_rt",x,y),
    59.             ("s_d_sta", [x,y-1]): Cell("s_d_str", x,y),
    60.             ("s_d_sta", [x-1,y-1]): Cell("s_d_rt", x,y),
    61.             ("s_d_sta", [x+1,y+1]): Cell("s_d_lt",x,y)}
    62.         if state == "empty":
    63.             for n in wolf_neighbors:
    64.                 n_state = n.get_state()
    65.                 n_coord = n.get_coordinate()
    66.                 if n_state in wolf_state:
    67.                     if (n_state, n_coord) in w_moving:
    68.                         return w_moving[(n_state, n_coord)]
    69.             for n in sheep_neighbors:
    70.                 n_state = n.get_state()
    71.                 n_coord = n.get_coordinate()
    72.                 if n_state in sheep_state:
    73.                     if (n_state, n_coord) in s_moving:
    74.                         return s_moving[(n_state, n_coord)]
    75.             if len(grass_neighbors):
    76.                 if random()<0.1:
    77.                     return Cell("grass", x, y)
    78.             return cell
    79.         elif state == "grass":
    80.             for n in sheep_neighbors:
    81.                 n_state = n.get_state()
    82.                 n_coord = n.get_coordinate()
    83.                 if n_state in sheep_state:
    84.                     if (n_state, n_coord) in s_moving:
    85.                         return s_moving[(n_state, n_coord)]
    86.             return cell
    87.         elif state in sheep_state:
    88.             if state in sheep_stays:
    89.                 for n in grass_neighbors:
    90.                     n_coord = n.get_coordinate()
    91.                     if (state,n_coord) in s_foraging:
    92.                         return s_foraging[(state,n_coord)]
    93.             elif state not in sheep_stays:
    94.                 return Cell("empty", x,y)
    95.             return cell
    96.         elif state in wolf_state:
    97.             if state in wolf_stays:
    98.                 for n in sheep_neighbors:
    99.                     n_coord = n.get_coordinate()
    100.                     if (state,n_coord) in w_foraging:
    101.                         return w_foraging[(state,n_coord)]
    102.             elif state not in wolf_stays:
    103.                 return Cell("empty", x,y)
    104.             return cell
    105.     def main():
    106.         L = Space()
    107.         size = (200,100)
    108.         status = ["empty", "grass", "s_l_str", "s_r_str", "s_u_str", "s_d_str", "s_l_lt", "s_r_lt", "s_u_lt", "s_d_lt", "s_l_rt", "s_r_rt", "s_u_rt", "s_d_rt", "s_l_sta", "s_r_sta", "s_u_sta", "s_d_sta", "w_l_str", "w_r_str", "w_u_str", "w_d_str", "w_l_lt", "w_r_lt", "w_u_lt", "w_d_lt", "w_l_rt", "w_r_rt", "w_u_rt", "w_d_rt", "w_l_sta", "w_r_sta", "w_u_sta", "w_d_sta"]
    109.         d = 2
    110.         S = Status(default="grass", *status)
    111.         N = Neighborhood(*size)
    112.         f = Rule(S, rule)
    113.         ca = CA(L, d, S, N,f)
    114.         cells=[]
    115.         for i in range(200):
    116.             for j in range(100):
    117.                 r =random()
    118.                 if r<0.6:
    119.                     cells.append(Cell("grass",i,j))
    120.                 elif r<0.7:
    121.                     cell = Cell(choice(sheep_state),i,j)
    122.                     cells.append(cell)
    123.                 elif r<0.73:
    124.                     cell = Cell(choice(wolf_state),i,j)
    125.                     cells.append(cell)
    126.                 else:
    127.                     cells.append(Cell("empty",i,j))
    128.         ca.evolution(cells, time=100)
    129.    
    130.     main()
    复制代码








    本帖子中包含更多资源

    您需要 登录 才可以下载或查看,没有账号?立即注册

    x

    评分

    参与人数 4血液 +5 追随 +3 堕落 +3 收起 理由
    艾维叶叶 + 1 + 1 赞一个!
    coldwolf + 1
    一条咸鱼干 + 1
    Morphyus + 5 + 1 + 1

    查看全部评分

    回复

    使用道具 举报

    守护者三角头钢铁勇士弯刀堕落之舞风雪之家魔法迸发轻语火玛瑙裸体克里斯

      回复

      使用道具 举报

      火玛瑙鎏彩万幢月亮提灯达拉然秋之歌男色时代美恐:新的开始苏醒的格罗姆织好的布料

        回复

        使用道具 举报

        超能留声机【新手友好】昆進生金蛋的鹅揄人者冠冕灵魂之椅涅槃龙衔金戒『草莓乳酪』辉光心相元石童年的蛋

          回复

          使用道具 举报

          超能留声机

            回复

            使用道具 举报

            猫咪合唱团(夏日)果体76圣甲虫秘典小镇的站台新神的赐福【新春限定】果体 隆『召唤好运的角笛』裸体克里斯一国之主凯登‧阿兰科

              惹 感觉可以用来做一些小小的游戏厚
              互动之类的
              回复

              使用道具 举报

              桂花米糕夏日柯基自定义男从Homunculus星光彩虹小粉驼『狄文卡德的残羽』苏醒的格罗姆鎏彩万幢璀璨闪蝶

                白大的知识小课堂又开课了,但还是一点都看不懂呢
                回复

                使用道具 举报

                冰海钓竿史莱姆牧场吸血魔蝠萨菲罗斯苏醒的格罗姆圣甲虫秘典可爱黑猫诺克提斯·路西斯·伽拉姆风暴磁场之鳄業火死鬥

                  没很深入接触Python,有些看不懂,我粘到vs 里面运行看看是什么。
                  回复

                  使用道具 举报

                  『领甜甜圈』艾吉奥岛田半藏诺克提斯·路西斯·伽拉姆萨菲罗斯岛田源氏BIG BOSS康纳/ConnorDoc普隆普特·阿金塔姆

                    这个是那个叫......兰顿蚂蚁来着的东西的原理?
                    回复

                    使用道具 举报

                    没有梦想的咸鱼月下之誓『大元素使』涅槃无光余烬葛莱分多猫咪合唱团(夏日)

                      感觉楼主的讲座知识开始变得深刻了啊,看这个蚂蚁感觉挺酷的
                      回复

                      使用道具 举报

                      阿努比斯信徒

                        学习区楼主等我以后有师弟了一定让他们来copy楼主的代码,少走十年弯路
                        回复

                        使用道具 举报

                        闪耀的赫尔墨斯之杖神奇宝贝大师球璀璨闪蝶自定义男从Homunculus恶魔城石鬼面驯化黑龙幼崽

                          我又来支持(氵)了,感觉距离上一篇发布并没有多久诶,拓展了新模型还是很有水平的
                          回复

                          使用道具 举报

                          炉石与家猫咪合唱团(夏日)盈满心相元石法师I· 学识之章『召唤好运的角笛』雾港捞月鎏彩万幢男巫之歌女巫之路虚空之海的鲸

                            回复

                            使用道具 举报

                            绿野之合凛霜魔典枯荣明灭自定义男从Homunculus风物长宜近地夜航鎏彩万幢雾港捞月

                              回复

                              使用道具 举报

                              您需要登录后才可以回帖 登录 | 立即注册

                              本版积分规则

                              关闭

                              站长公告上一条 /1 下一条

                              文字版|手机版|小黑屋|GameMale

                              GMT+8, 2026-8-7 09:03 , Processed in 0.174251 second(s), 112 queries , Redis On.

                              Copyright © 2013-2026 GameMale

                              All Rights Reserved.

                              快速回复 返回列表