2021年美赛A题真菌元胞自动机Python实现
import matplotlib.pyplot as plt
import random
import numpy as np
import matplotlib.animation as animation
def save_fungi_ca_gif(): # save the gif file to the path
target_gif_path = "E:/engineering space/figure/gif format/"
target_gif_name = "aggressive_ca.gif"
target_gif_full = target_gif_path + target_gif_name
anim_1.save(target_gif_full, writer='pillow') # save the animation
print('Saved')
def calculate_random_neighbour(stay, left, up, right, down, no_reproduce): # get the relative location of the newly
total_rate = stay + left + up + right + down + no_reproduce # reproduced fungi cell
if total_rate == 0.0:
total_rate = 1
cap1 = stay / total_rate # cap 1 to 5 is the boundary of probability area
cap2 = cap1 + left / total_rate
cap3 = cap2 + up / total_rate
cap4 = cap3 + right / total_rate
cap5 = cap4 + down / total_rate
rnd = random.random()
if 0 <= rnd < cap1:
return 0
elif cap1 <= rnd < cap2: # divide the range into 6 parts
return 1
elif cap2 <= rnd < cap3: # randomly select one part
return 2
elif cap3 <= rnd < cap4:
return 3
elif cap4 <= rnd < cap5:
return 4
else:
return 5
def get_extension_parameters(i_get_extension_parameters): # get the extension parameters
extension_parameters = extension_rate_list[fungi_list[i_get_extension_parameters][3]]
return extension_parameters
def get_random_neighbour(i_get_random_neighbour): # calculate the density of neighbour mesh as well as self mesh
extension_rate_get_random_neighbour = get_extension_parameters(i_get_random_neighbour)
stay_density = my_board[fungi_list[i_get_random_neighbour][0], fungi_list[i_get_random_neighbour][1]] / one_mesh_max
stay_rate = extension_rate_get_random_neighbour - stay_density
if fungi_list[i_get_random_neighbour][1] != 0:
left_density = my_board[fungi_list[i_get_random_neighbour][0], fungi_list[i_get_random_neighbour][1] - 1] \
/ one_mesh_max
left_rate = extension_rate_get_random_neighbour * (
1 - left_density + 0.1) # further calculate the transferring rate of
# the new fungi
else:
left_density = 0.5
left_rate = 0
if fungi_list[i_get_random_neighbour][0] != 0:
up_density = my_board[fungi_list[i_get_random_neighbour][0] - 1, fungi_list[i_get_random_neighbour][1]] \
/ one_mesh_max
up_rate = extension_rate_get_random_neighbour * (1 - up_density + 0.1)
else:
up_density = 0.5
up_rate = 0
if fungi_list[i_get_random_neighbour][1] != patch_size - 1:
right_density = my_board[
fungi_list[i_get_random_neighbour][0], fungi_list[i_get_random_neighbour][1] + 1] \
/ one_mesh_max
right_rate = extension_rate_get_random_neighbour * (1 - right_density + 0.1)
else:
right_density = 0.5
right_rate = 0
if fungi_list[i_get_random_neighbour][0] != patch_size - 1:
down_density = my_board[fungi_list[i_get_random_neighbour][0] + 1, fungi_lis

本文介绍了一种使用Python实现的真菌元胞自动机模型,该模型模拟了不同种类真菌的竞争生长过程。通过设定初始条件、扩展率及繁殖死亡规则等参数,实现了真菌在二维网格上的扩散模拟,并利用matplotlib库生成动态图像。
最低0.47元/天 解锁文章
8547

被折叠的 条评论
为什么被折叠?



