您的位置:首页 > 房产 > 家装 > 哪家做网站做的好_网页设计公司宣传_品牌广告投放_seo技术学院

哪家做网站做的好_网页设计公司宣传_品牌广告投放_seo技术学院

2025/2/28 12:08:32 来源:https://blog.csdn.net/2301_76444133/article/details/145912820  浏览:    关键词:哪家做网站做的好_网页设计公司宣传_品牌广告投放_seo技术学院
哪家做网站做的好_网页设计公司宣传_品牌广告投放_seo技术学院

通过二维卷积快速计算每个细胞的Moore型邻居存活数(8邻域)

import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
from scipy.signal import convolve2d
import time
import logginglogging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')class CellularAutomaton:def __init__(self, size=100):self.size = sizeself.grid = np.zeros((size, size), dtype=int)self.kernel = np.array([[1, 1, 1], [1, 0, 1], [1, 1, 1]])# 初始化结构self._init_glider_gun(10, 10)self._init_pulsar(50, 50)self._init_glider(5, 5)self._init_block(80, 80)# 可视化设置self.fig, self.ax = plt.subplots(figsize=(10, 10))self.img = self.ax.imshow(self.grid, cmap='viridis', interpolation='nearest')self.last_update = time.time() + 1e-6  # 初始化时间戳def _init_glider_gun(self, x, y):"""标准高氏滑翔机枪"""gun = [(5, 1), (5, 2), (6, 1), (6, 2),(3, 13), (4, 13), (5, 13), (6, 13), (7, 13), (8, 13),(3, 14), (4, 14), (5, 14), (6, 14), (7, 14), (8, 14),(5, 17), (6, 17), (7, 17)]for dx, dy in gun:self.grid[(x + dx) % self.size, (y + dy) % self.size] = 1def _init_pulsar(self, x, y):"""标准脉冲星"""arms = [(2, 4), (2, 5), (2, 6), (7, 4), (7, 5), (7, 6), (9, 4), (9, 5), (9, 6),(4, 2), (5, 2), (6, 2), (4, 7), (5, 7), (6, 7), (4, 9), (5, 9), (6, 9)]for dx, dy in arms:self.grid[(x + dx) % self.size, (y + dy) % self.size] = 1def _init_glider(self, x, y):self.grid[x % self.size, (y + 1) % self.size] = 1self.grid[(x + 1) % self.size, (y + 2) % self.size] = 1self.grid[(x + 2) % self.size, y % self.size:(y + 3) % self.size] = 1def _init_block(self, x, y):self.grid[x % self.size:(x + 2) % self.size, y % self.size:(y + 2) % self.size] = 1def _update_grid(self):padded = np.pad(self.grid, 1, mode='wrap')neighbor_count = convolve2d(padded, self.kernel, mode='valid')birth = (self.grid == 0) & (neighbor_count == 3)survive = (self.grid == 1) & ((neighbor_count == 2) | (neighbor_count == 3))return np.where(birth | survive, 1, 0)def update(self, frame):try:self.grid = self._update_grid()self.img.set_data(self.grid)# 安全计算FPSnow = time.time()time_diff = max(now - self.last_update, 1e-6)fps = 1 / time_diffself.last_update = nowif frame % 10 == 0:logging.info(f"Frame {frame}: Cells {np.sum(self.grid)} | FPS {fps:.1f}")return self.img,except Exception as e:logging.error(f"Update error: {str(e)}")raisedef run(self):try:ani = FuncAnimation(self.fig, self.update,frames=1000,interval=50,blit=True,cache_frame_data=False)plt.show()except Exception as e:logging.error(f"Runtime error: {str(e)}")finally:logging.info("程序正常退出")if __name__ == "__main__":sim = CellularAutomaton(size=100)sim.run()

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com