GameMale
登陆 / 注册 搜索

USERCENTER

SEARCHSITE

搜索

查看: 1079|回复: 13
收起左侧

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

[复制链接] |关注本帖

邪恶的面具海盗弯钩神秘的红茶冒险用绷带魔法石碑箭术卷轴质量效应三部曲

     楼主| 白冥 发表于 2025-2-3 21:00:40 | 显示全部楼层 |阅读模式 <
    本帖最后由 白冥 于 2025-2-4 00:56 编辑

    目录      

    • 概述
    • 安装与环境配置
    • 系列技术贴
    • 类结构与方法
    • 源代码
    • 示例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

    查看全部评分

    回复

    使用道具 举报

    Doc神奇宝贝大师球森林羊男一只普通的鳄鱼雪王的心脏破旧打火机落雪勇者与龙的传说-封面闪耀种子

      zhnlwwdzz 发表于 2025-3-2 13:49:29 | 显示全部楼层 <
      回复

      举报

      万众瞩目神奇宝贝大师球神奇宝贝图鉴『搓粉团珠』雪王的心脏肉垫手套『落樱缤纷』里昂‧S‧甘乃迪小小安全帽丹雀衔五穗,人间始丰登

        zhuovboyan 发表于 2025-2-9 04:25:01 | 显示全部楼层 <
        回复

        举报

        虎克船长位于左侧的随从已派遣远征

          Sam30 发表于 2025-2-7 22:01:18 | 显示全部楼层 <
          回复

          举报

          永远的克叔裸体克里斯猫咪点唱机【夏日限定】夏日的泰凯斯保卫领土【新春限定】果体 隆破碎的圣水瓶業火死鬥传奇亚瑟‧摩根

            大河内太 发表于 2025-2-6 04:33:30 | 显示全部楼层 <
            惹 感觉可以用来做一些小小的游戏厚
            互动之类的
            回复

            举报

            自定义男从Homunculus璀璨闪蝶裸体克里斯吃饱金币的Doge永亘环青鸾林中松鼠史莱姆蛋

              Yang羊 发表于 2025-2-4 12:56:13 | 显示全部楼层 <
              白大的知识小课堂又开课了,但还是一点都看不懂呢
              回复

              举报

              召唤古代战士沙漠神灯石肤术敖蜃星黑暗交易水泡术炼金之心我的冶金打火机安德森‧戴维斯位于左侧的随从已派遣远征

                圣卫幻梦 发表于 2025-2-4 11:37:39 | 显示全部楼层 <
                没很深入接触Python,有些看不懂,我粘到vs 里面运行看看是什么。
                回复

                举报

                召唤古代战士炼金之心敖蜃星邪恶圣杯百相千面-戏Futūrum(未来)岛田半藏诺克提斯·路西斯·伽拉姆铁汉柔情岛田源氏

                  2302594 发表于 2025-2-4 06:45:57 | 显示全部楼层 <
                  这个是那个叫......兰顿蚂蚁来着的东西的原理?
                  回复

                  举报

                  艾吉奥位于左侧的随从已派遣远征

                    万俟 发表于 2025-2-4 06:20:07 | 显示全部楼层 <
                    感觉楼主的讲座知识开始变得深刻了啊,看这个蚂蚁感觉挺酷的
                    回复

                    举报

                    【新手友好】昆進索贝克信徒位于左侧的随从已派遣远征

                      野生阿努厨 发表于 2025-2-3 22:55:05 | 显示全部楼层 <
                      学习区楼主等我以后有师弟了一定让他们来copy楼主的代码,少走十年弯路
                      回复

                      举报

                      永亘环闪耀的赫尔墨斯之杖神奇宝贝大师球璀璨闪蝶自定义男从Homunculus石鬼面海盗弯钩

                        coldwolf 发表于 2025-2-3 22:35:53 | 显示全部楼层 <
                        我又来支持(氵)了,感觉距离上一篇发布并没有多久诶,拓展了新模型还是很有水平的
                        回复

                        举报

                        John Reese裸体克里斯位于左侧的随从已派遣远征自定义男从Homunculus虚空之海的鲸永浴爱河黄粱一梦【新春限定】果体 隆新神的赐福永远的克叔

                          娱乐法师火布偶 发表于 2025-2-3 21:41:30 | 显示全部楼层 <
                          回复

                          举报

                          裸体克里斯位于左侧的随从已派遣远征【新春限定】果体 隆永亘环内森·德雷克璀璨金币安德森‧戴维斯绿茵宝钻未散的宴席脏器再生手术

                            Morphyus 发表于 2025-2-3 21:35:22 | 显示全部楼层 <
                            回复

                            举报

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

                            本版积分规则

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

                            GMT+8, 2025-5-2 06:07 , Processed in 0.112412 second(s), 103 queries , Redis On.

                            Copyright © 2013-2025 GameMale

                            All Rights Reserved.

                            快速回复 返回列表