元胞自动机的Java模型代码



public class Car {


public static void main(String[] args) {

int[] speed1=new int[6];//统计结果
int count=0;
int probability=15;//出现车的可能
int num=100;//初始化车辆
int n=600;//总路程
int nn=360;//监视时间
int[] appear=new int[1];
int[] circulation=new int[n];//统计流量
int[][] che=new int[2][n];
chuShiHua(che,n);
initSpeed(che,n,num,appear);
repair(che,n);
//车流变化
for(int j=0;j<nn;j++){

for(int m=1000;m>0;m--)
for (int i = 1000; i >0; i--) ;
output(che,n,j+1,speed1,circulation);
move(che, n);
if(j%2==0)
add(che, n,probability,appear);
if(j%3==0)
over(che, n);
if(j%4==0||j%6==0)
back(che, n);
if(j%5==0)
if(probability>55)
addspeed(che, n);
else if(((int) (Math.random()*100))>55)addspeed(che, n);
if(j%5==0)
if(probability<75)
addspeed(che, n);
else if(((int) (Math.random()*100))>65)addspeed(che, n);
}
System.out.println("出现不同速度的车的数目");
for(int i=0;i<6;i++){count=count+speed1[i];System.out.print(speed1[i]+"  ");}
System.out.println("高速车出现的比率:"+(speed1[4]+speed1[5])/(float)count+"中车出现的比率:"+(speed1[2]+speed1[3])/(float)count+
"低速车出现的比率:"+(speed1[0]+speed1[1])/(float)count);
float hh=(speed1[4]+speed1[5])/(float)count;
System.out.println("车道经过车辆数");
for(int i=0;i<n;i++){System.out.print(circulation[i]+"  ");}
System.out.println("车流平均密度");
count=0;
for(int i=0;i<n;i++){count=count+circulation[i];System.out.print(i+"="+circulation[i]/(float)nn+"**");}
float cc=count/(float)n/nn;
System.out.println("\n平均车流密度:"+cc);
System.out.println("\n总车数:"+appear[0]*6);
System.out.println("\n安全性:"+appear[0]*cc*hh/6);
}
public static void chuShiHua(int[][] che,int n) {//初始化数组


for(int j=0;j<2;j++)
for(int k=0;k<n;k++){
che[j][k]=0;
}
}
public static void output(int[][] che,int n,int a,int[] speed1,int[] cir) {//输出矩阵
int[] speed=new int[6];

for(int i=0;i<2;i++){
for (int j = 0; j < n; j++){
if(che[i][j]!=0){cir[j]++;speed[che[i][j]-3]++;speed1[che[i][j]-3]++;}
//System.out.print(che[i][j]+" ");
}
//System.out.println();
}//System.out.print(a+"*************");
//for(int i=0;i<6;i++){System.out.print(i+3+"="+speed[i]+"**");}
//System.out.println("\n********************************************\n");

}
public static void initSpeed(int[][] che,int n,int num,int[] a) {//初始化车速
for(int i=0;i<num;i++){
int speed=(int) (Math.random()*6+3);//初始速度
int j=(int) (Math.random()*2);//初始速度
int k=(int) (Math.random()*(n-5)+3);//初始速度
che[j][k]=speed;
if(speed!=0)a[0]++;
for (int k2 =k-3 ; k2 < k+3; k2++) {
if(k2==k)continue;
else {
che[j][k2]=0;
}
}
}
}
public static void repair(int[][] che,int n) {//修复车速
int i,j,count=0,k,f=1,c = 0,c1;
for(i=0;i<2;i++)
for (j= 0; j< n-5; j++) {
c=che[i][j];
k=j;
for(count=0;count<c;count++){
if(k<n&&che[i][k]!=0&&(c>che[i][k]))
{che[i][j]=che[i][k];count=c;}
else k++;
}
}
}
public static void move(int[][] che,int n) {//向前移动
int i,j,k;
for(i=0;i<2;i++)
for ( j = n-4; j<n; j++) {
che[i][j]=0;
}
for(i=0;i<2;i++)
for ( j = n-5; j >=0; j--) {
k=che[i][j];
if(k>2&&k<5){che[i][2+j]=che[i][j];che[i][j]=0;}
if(k>4&&k<7){che[i][3+j]=che[i][j];che[i][j]=0;}
if(k>6&&k<9){che[i][4+j]=che[i][j];che[i][j]=0;}
}
check(che, n);
}
public static void add(int[][] che,int n,int probability,int[] a) {//增加车辆
int s=(int) (Math.random()*100);//初始速度
for(int i=10;i>0;i--){
if(s>probability){
s=(int) (Math.random()*100);
int k=(int) (Math.random()*5);//初始速度
int speed=(int) (Math.random()*6+3);//初始速度
if(che[0][k]==0){
che[0][k]=speed;
a[0]++;
}
}
}
repair(che,n);
}
public static void over(int[][] che,int n) {//超车情况
boolean f=true;
for(int i=5;i<n;i++){
for(int j=i-5;j<5;j++)
if(che[1][j]>0)f=false;
for(int j=i;j<5;j++)
if(che[1][j]>0)f=false;

if(f=true&&che[0][i]<9&&che[0][i]>5)
{che[1][i]=che[0][i]++;che[0][i]=0;}

}
}
public static void back(int[][] che,int n) {//超车后回去原来的道
boolean f=true;
for(int i=5;i<n-5;i++){
if(che[1][i]!=0&&((int) (Math.random()*n))>35)
{
for(int k=i-5;k<i+5;k++)
if(che[0][i]!=0)f=false;
if(f)che[0][i]=che[1][i];che[1][i]=0;
f=true;
}
}
repair(che, n);
}
public static void addspeed(int [][] che,int n) {//增加速度
for(int i=0;i<2;i++)
for (int j = 0; j < n; j++) {
if(che[i][j]!=0&&che[i][j]<8)
{
if(che[i][j]<5)
if(((int) (Math.random()*100))>15)
if(((int) (Math.random()*100))>45)
che[i][j]=che[i][j]+2;
else che[i][j]=che[i][j]+1;
if(che[i][j]>4)
if(((int) (Math.random()*100))>65)che[i][j]++;
}
}
if((Math.random()*100)>25)
over(che, n);
repair(che, n);
}
public static void subspeed(int [][] che,int n){
for(int i=0;i<2;i++)
for (int j = 0; j < n; j++) {
if(che[i][j]!=0&&che[i][j]>3)
{
if(che[i][j]>6)
if(((int) (Math.random()*100))>15)
if(((int) (Math.random()*100))>45)
che[i][j]=che[i][j]-2;
else che[i][j]=che[i][j]-1;
if(che[i][j]<7)
if(((int) (Math.random()*100))>65)che[i][j]--;
}
}
if((Math.random()*100)>25)
over(che, n);
move(che, n);
}
public static void check(int[][] che,int n) {
for (int j = 0; j <n; j++) {
if(che[0][j]==8)
if(((int) (Math.random()*100))>45)
che[0][j]=che[0][j]-2;
else che[0][j]--;
}
}
}
  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
元胞自动机(Cellular Automata)是一种计算模型,可以用来模拟大量简单单元的行为,从而简化复杂的物理、化学等现象。MATLAB是一种科学计算软件,可以用来编写元胞自动机模型代码。 编写元胞自动机模型需要了解元胞自动机模型的结构和规则。元胞自动机模型由网格状的单元组成,每个单元都有一个状态,状态可以是二进制、整数、浮点数等。元胞自动机模型的演化通过每个单元根据一定的规则进行状态转换,从而影响周围的单元的状态。 在MATLAB中,可以采用矩阵来表示元胞自动机模型的状态,利用循环语句实现每个单元的状态转换。一个简单的元胞自动机模型的MATLAB代码示例如下: % 定义元胞自动机模型的初始状态 state = zeros(50, 50); % 状态矩阵,大小为50x50 state(25, 25) = 1; % 中心点的状态为1,表示为黑色 % 循环演化元胞自动机模型 for i = 1:100 % 迭代100次 % 复制状态矩阵,保持状态不变 new_state = state; for x = 2:49 % 对除边界以外的每个单元 for y = 2:49 % 根据规则更新单元状态 if state(x, y) == 1 && (state(x-1, y) == 1 || state(x+1, y) == 1 || state(x, y-1) == 1 || state(x, y+1) == 1) % 周围有一个或多个黑色单元,则当前单元的状态为黑色 new_state(x, y) = 1; else % 周围没有黑色单元,则当前单元的状态为白色 new_state(x, y) = 0; end end end % 更新状态矩阵 state = new_state; % 绘制当前状态矩阵 image(state*255); colormap(gray(2)); axis equal; pause(0.1); end 上述代码实现了一个简单的"Game of Life"元胞自动机模型,运行后可以观察到模型随着时间的推移发生的演化。当然,元胞自动机的规则和思想非常丰富多样,开发者们可以灵活运用MATLAB的语法和工具,编写自己模型代码

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值