SuperInstance/cellular-automata-agent

GitHub: SuperInstance/cellular-automata-agent

一个 Rust 元胞自动机库,通过可配置的转换规则、邻域和模式检测,在二维网格上将 agent 行为建模为基于局部规则的涌现演化。

Stars: 0 | Forks: 0

# 元胞自动机 Agent [![crates.io](https://img.shields.io/crates/v/cellular-automata-agent.svg)](https://crates.io/crates/cellular-automata-agent) [![docs.rs](https://docs.rs/cellular-automata-agent/badge.svg)](https://docs.rs/cellular-automata-agent) [![License: MIT](https://img.shields.io/badge/License-MIT-blue.svg)](LICENSE) ## 问题背景 Agent 的认知不必是连续和数值化的。一些最强大的涌现行为正是源于应用在网格上的简单局部规则——完全类似于元胞自动机。但大多数 agent 框架并不提供 CA 的基本组件,迫使你在完整的神经网络和简单的状态机之间做出选择。 ## 存在意义 Cellular Automata Agent 弥合了简单规则与涌现行为之间的鸿沟。通过在 2D 网格上利用可配置的转换规则、邻域和模式检测来对 agent 的认知进行建模,你将获得 CA 的涌现复杂性,以及 Rust 的类型安全性和可组合性。 ## 架构 ``` ┌──────────────────────────────────┐ │ Grid (2D toroidal) │ │ ┌───┬───┬───┬───┬───┬───┐ │ │ │ 0 │ 1 │ 0 │ 1 │ 0 │ 1 │ │ │ ├───┼───┼───┼───┼───┼───┤ │ │ │ 1 │ 1 │ 0 │ 0 │ 1 │ 0 │ │ CellState: Dead | Alive │ ├───┼───┼───┼───┼───┼───┤ │ │ │ 0 │ 0 │ 1 │ 1 │ 0 │ 1 │ │ Neighborhood: Moore | VonNeumann │ └───┴───┴───┴───┴───┴───┘ │ └──────────┬───────────────────────┘ │ ┌──────────▼───────────────────────┐ │ Transition Rules │ │ • Conway (B3/S23) │ │ • HighLife (B36/S23) │ │ • Seeds (B2/S) │ │ • Custom closure │ └──────────┬───────────────────────┘ │ ┌──────────▼───────────────────────┐ │ Pattern Detection │ │ Still lifes: Block, Beehive │ │ Oscillators: Blinker │ │ Spaceships: (extensible) │ └──────────────────────────────────┘ ``` ## 安装说明 ``` [dependencies] cellular-automata-agent = "0.1" ``` ## API 参考 ### `Grid` 带有细胞状态管理的 2D 环形网格: ``` use cellular_automata_agent::grid::{Grid, CellState}; let mut grid = Grid::new(10, 10); grid.set_cell(5, 5, CellState::Alive); assert!(grid.get(5, 5).is_alive()); ``` ### `TransitionFn` 与规则 可配置的转换规则: ``` use cellular_automata_agent::rule::*; let conway = conway_rule(); // Classic B3/S23 let highlife = highlife_rule(); // B36/S23 let seeds = seeds_rule(); // B2/S ``` ### `NeighborhoodType` Moore(8 邻居)和 Von Neumann(4 邻居): ``` use cellular_automata_agent::neighborhood::NeighborhoodType; let moore = NeighborhoodType::Moore; let von_neumann = NeighborhoodType::VonNeumann; ``` ### `Generation` 时间步演化: ``` use cellular_automata_agent::generation::Generation; use cellular_automata_agent::rule::conway_rule; use cellular_automata_agent::neighborhood::NeighborhoodType; let gen = Generation::new(grid); let next = gen.advance(&conway_rule(), NeighborhoodType::Moore); println!("Population: {}", next.population()); ``` ### 模式检测 ``` use cellular_automata_agent::pattern::*; let has_block = has_block(&grid, 3, 3); let has_beehive = has_beehive(&grid, 2, 2); let has_blinker = has_blinker_h(&grid, 5, 5); ``` ## 使用示例 ### 示例 1:Conway 生命游戏 ``` use cellular_automata_agent::grid::{Grid, CellState}; use cellular_automata_agent::generation::Generation; use cellular_automata_agent::rule::conway_rule; use cellular_automata_agent::neighborhood::NeighborhoodType; let mut grid = Grid::new(20, 20); // Place a glider grid.set_cell(1, 0, CellState::Alive); grid.set_cell(2, 1, CellState::Alive); grid.set_cell(0, 2, CellState::Alive); grid.set_cell(1, 2, CellState::Alive); grid.set_cell(2, 2, CellState::Alive); let mut gen = Generation::new(grid); for _ in 0..100 { gen = gen.advance(&conway_rule(), NeighborhoodType::Moore); } println!("Population after 100 steps: {}", gen.population()); ``` ### 示例 2:自定义规则 ``` use cellular_automata_agent::grid::CellState; use cellular_automata_agent::rule::TransitionFn; // Day & Night rule (B3678/S34678) let day_night: TransitionFn = Box::new(|alive, neighbors| { if alive { if matches!(neighbors, 3|4|6|7|8) { CellState::Alive } else { CellState::Dead } } else { if matches!(neighbors, 3|6|7|8) { CellState::Alive } else { CellState::Dead } } }); ``` ### 示例 3:模式检测 ``` use cellular_automata_agent::grid::{Grid, CellState}; use cellular_automata_agent::pattern::*; // Check for stable patterns if has_block(&grid, 3, 3) { println!("Found a block (still life) at (3,3)"); } ``` ## 性能 | 操作 | 复杂度 | |-----------|-----------| | 网格细胞访问 | O(1) | | 推进一代演化 | O(W × H) | | 模式检测 | 每个模式 O(n) | | 邻居计数 | O(k) k=4 或 8 | ## 许可证 基于 [MIT 许可证](LICENSE) 授权。 ## 贡献指南 1. Fork 该仓库 2. 创建功能分支 3. 编写测试 4. 推送并提交 Pull Request
标签:Rust, 元胞自动机, 可视化界面, 康威生命游戏, 智能体架构, 科学计算, 网络流量审计, 通知系统