C语言实现基于投票规则的细胞自动机

算法介绍

首先我们先看一下“基于投票规则的细胞自动机”的定义:

基于投票规则的细胞自动机,实际上是具有如下限定条件的一种细胞自动机:

状态:0或1;

邻居:中心的3*3邻居;

规则:计数p表示中心的3*3邻居中1的个数(包括中心本身),if P<5,中心设置为0.否则设置为1 。

混沌随机序列:

一般我们采用混合光学双稳模型迭代方程实现混沌随机序列的产生:

题目要求:

现在给定系数A为4,Xb为2.5,要求生成一个长度为16的混沌序列{xi}。并且用一下方法将该序列转换为0 1序列:xi > 2.5 则赋值为1, 否则赋值为 0 。然后把这个序列排成4 * 4的0 1矩阵,并用上述的细胞自动机对该矩阵进行两次运算,给出最终的矩阵结果。

算法实现代码如下:

/*****************************************
Author: Nero
Data:2013/12/22
Deifning coefficient A as 4, Xb as 2.5;
*****************************************/

#include<stdio.h>
#include<math.h>
using namespace std;
const int A = 4;
const float Xb = 2.5;

void Rules_Impl(float P[],float dMatrix[4][4]);

void Cellular_Automata(float dInit)
{
    printf("dInit=%g

",dInit);
    int i ,j;                                                             //variables for counting 
    float dMatrix[4][4];
    float P[4];
    float *X = new float[16];
    X[0] = dInit;                                                  //Initializing X[0]
    int count = 0;                                               //temp variable for counting index
    for (i = 0; i < 15; i++)
    {
        X[i+1] = 4 * pow(sin(X[i]- Xb),2);                  //Sequence with a length of 16;
    }
    //Print sequence 
    for (i = 0; i < 16; i++)
    {
        printf("%f	",X[i]);
    }
    printf("

");

    for (i = 0; i < 16; i++)
    {
        X[i] = X[i]>2.5?1:0;
    }
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j< 4; j++)
        {
            dMatrix[i][j] = X[count];
            ++count;
        }
    }
    //Print Matrix
    printf("The original matrix is 
");
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            printf("%g	",dMatrix[i][j]);
        }
        printf("
");
    }
    printf("
");

    //calculate the centers, Twice
    for (i = 0; i < 2; i++)
    {
        Rules_Impl(P,dMatrix);
    }
    printf("The final matrix is 
");
    for (i = 0; i < 4; i++)
    {
        for (j = 0; j < 4; j++)
        {
            printf("%g	",dMatrix[i][j]);
        }
        printf("
");
    }
    printf("
");
}

void Inter()
{
    float Init;
    printf("Input the init Num
");
    scanf_s("%g",&Init);
    Cellular_Automata(Init);                     
}

void Rules_Impl(float P[],float dMatrix[4][4])
{
    //Four calculations for the center of the 3*3 neighbors
    //Choose Matrix[1][1], Matrix[2][1], Matrix[1][2], Matrix[2][2] as centers
    int  i =0,  j = 0;

    //Matrix[1][1]
    for (i = 0 ; i <3; i++)
    {
        for (j = 0; j < 3; j++)
        {
            P[0] += dMatrix[i][j];
        }
    }
    //Matrix[2][1]
    for (i = 1; i<4;i++)
    {
        for (j=0;j <3;j++)
        {
            P[1]+=dMatrix[i][j];
        }
    }
    //Matrix[1][2]
    for (i = 0; i<3;i++)
    {
        for (j=1;j <4;j++)
        {
            P[2]+=dMatrix[i][j];
        }
    }
    //Matrix[2][2]
    for (i = 1; i<4;i++)
    {
        for (j=1;j <4;j++)
        {
            P[3]+=dMatrix[i][j];
        }
    }
    dMatrix[1][1] = P[0]<5?0:1;
    dMatrix[2][1] = P[1]<5?0:1;
    dMatrix[1][2] = P[2]<5?0:1;
    dMatrix[2][2] = P[3]<5?0:1;
}

其中Rules_Impl()函数的作用是进行细胞自动机运算。

Cellular_Automata()函数为主要功能实现函数,包括随机序列的产生,矩阵的产生以及对Rules_Impl()函数的调用。

原文地址:https://www.cnblogs.com/nerohwang/p/3486274.html