元胞自动机离散模型
元胞自动机是一个离散模型,类似于任何其他自动机,它有自己的起始状态和一组规则。
A cellular automaton is a model of a system of “cell” objects with the following characteristics :
- The cells live on a grid which can be either 1D or even multi-dimensional
- Each cell has a state. The number of state possibilities is typically finite. The simplest example has the two possibilities of 1 and 0
- Each cell has a neighbourhood and it is typically a list of adjacent cells
元胞自动机的工作原理?
元胞自动机的工作原理如下图所示:
- 通过为每个单元分配状态来选择初始状态。
- 根据一些确定每个细胞的新状态的固定规则,创建了一个新的世代:
- 单元格的当前状态。
- 其附近细胞的状态。
- 因此,通过查看所有先前的相邻状态来计算新状态。
元胞自动机的例子:
1.康威的人生游戏
2. 第 90 条:
规则 90 是一个基本元胞自动机,由一维单元格数组组成,每个单元格可以包含 0 或 1 值。每个单元格是其两个邻居的异或(XOR)。Current State 111 011 101 100 011 010 001 000 Next State 0 1 0 1 1 0 1 0
如果我们将 Next State 连接成一个二进制数并将其转换为十进制 (01011010) 2,则它变为 90,因此我们得到了 Rule 90 的名称。
3.当初始状态有一个非零单元格时,该图具有谢尔宾斯基三角形的外观。
谢尔宾斯基三角形的实现:
C++
// C++ code to implement Sierpinski Triangle
#include
using namespace std;
// Length of the string used as a "Seed"
#define LENGTH 34
// How many lines the program will generate
int n = 15;
// All the patterns possible
vector > rules = { { 0, 0, 0 }, { 0, 0, 1 },
{ 0, 1, 0 }, { 0, 1, 1 },
{ 1, 0, 0 }, { 1, 0, 1 },
{ 1, 1, 0 }, { 1, 1, 1 } };
// Utility Function to Print Rules
void printRules()
{
int i, j;
cout << "The rules of Rule 90 Cellular Automaton"
" are as follows: \n";
for (i = 0; i < 8; i++) {
cout << "\t\tRule " << i + 1 << ": ";
for (j = 0; j < 3; j++)
cout << rules[i][j] << " ";
cout << "-> sets cell to: "
<< (rules[i][0]
^ rules[i][2]) << "\n";
}
}
// Print the Current State
void outState(vector s)
{
cout << "\t\t";
for (int i = 0; i < LENGTH; i++) {
if (s[i] == 1)
cout << "\u25A0";
else if (s[i] == 0)
cout << " ";
}
cout << "\n";
}
// Function to print the triangle
void utilize()
{
// Initialize starting state
// to sierpinski triangle
vector sierpinski(LENGTH);
vector updateState(LENGTH);
sierpinski[(LENGTH) / 2 - 1] = 1;
// Print Sierpinski Triangle
// initial String
outState(sierpinski);
// Loop to generate/update the state
// and update state arrays
for (int i = 0; i < n; i++)
{
// Erase the old state
updateState.assign(LENGTH, 0);
// Create the new state
for (int j = 1; j < LENGTH - 1; j++)
{
int f1 = sierpinski[j - 1];
int f2 = sierpinski[j];
int f3 = sierpinski[j + 1];
// Create an array with the
// current pattern
vector current;
current.push_back(f1);
current.push_back(f2);
current.push_back(f3);
// XOR the current state's neighbours
// to set the cell's value
// in Update State
updateState[j] = current[0] ^ current[2];
}
// Update the current state
sierpinski.assign(updateState.begin(),
updateState.end());
// Print the new current state
outState(sierpinski);
}
}
// Driver code
int main()
{
// Print out the rules for
// rule 90 cellular Automaton
printRules();
cout << "\n\t\t\tSIERPINSKI TRIANGLE\n\n";
utilize();
return 0;
}
输出
The rules of Rule 90 Cellular Automata are as follows:
Rule 1: 0 0 0 -> sets cell to: 0
Rule 2: 0 0 1 -> sets cell to: 1
Rule 3: 0 1 0 -> sets cell to: 0
Rule 4: 0 1 1 -> sets cell to: 1
Rule 5: 1 0 0 -> sets cell to: 1
Rule 6: 1 0 1 -> sets cell to: 0
Rule 7: 1 1 0 -> sets cell to: 1
Rule 8: 1 1 1 -> sets cell to: 0
SIERPINSKI TRIANGLE
■
■ ■
■ ■
■ ■ ■ ■
■ ■
■ ■ ■ ■
■ ■ ■ ■
■ ■ ■ ■ ■ ■ ■ ■
■ ■
■ ■ ■ ■
■ ■ ■ ■
■ ■ ■ ■ ■ ■ ■ ■
■ ■ ■ ■
■ ■ ■ ■ ■ ■ ■ ■
■ ■ ■ ■ ■ ■ ■ ■
■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■ ■