The Java Model code __java of cellular automata

Source: Internet
Author: User


public class Car {


public static void Main (string[] args) {

Int[] speed1=new int[6];//statistic results
int count=0;
int probability=15;//appears the possibility of a car
int num=100;//Initialization Vehicle
int n=600;//Total Distance
int nn=360;//Watch Time
Int[] Appear=new int[1];
Int[] circulation=new int[n];//statistic flow
Int[][] Che=new Int[2][n];
Chushihua (Che,n);
Initspeed (che,n,num,appear);
Repair (che,n);
Traffic change
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 ("Number of vehicles with different speeds");
for (int i=0;i<6;i++) {count=count+speed1[i]; System.out.print (speed1[i]+ "");}
System.out.println ("The ratio of the high speed car appears:" + (SPEED1[4]+SPEED1[5))/(float) count+ "The ratio of the car appears:" + (SPEED1[2]+SPEED1[3))/(float) count+
"The rate at which a low-speed vehicle appears:" + (Speed1[0]+speed1[1])/(float) count);
Float hh= (speed1[4]+speed1[5])/(float) count;
System.out.println ("Lane passes the number of vehicles");
for (int i=0;i<n;i++) {System.out.print (circulation[i]+ "");}
SYSTEM.OUT.PRINTLN ("Average traffic density");
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 Average traffic density:" +CC);
System.out.println ("\ n Total number of vehicles:" +appear[0]*6);
System.out.println ("\ n security:" +APPEAR[0]*CC*HH/6);
}
public static void Chushihua (int[][] che,int N) {//initialization array


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 {//output matrix
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) {//initialization speed
for (int i=0;i<num;i++) {
int speed= (int) (Math.random () *6+3);//initial speed
int j= (int) (Math.random () *2);//initial speed
int k= (int) (Math.random () * (n-5) +3)//initial speed
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) {//repair speed
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) {//forward
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) {//Increase vehicle
int s= (int) (Math.random () *100);//initial speed
for (int i=10;i>0;i--) {
if (s>probability) {
s= (int) (Math.random () *100);
int k= (int) (Math.random () *5);//initial speed
int speed= (int) (Math.random () *6+3);//initial speed
if (che[0][k]==0) {
Che[0][k]=speed;
a[0]++;
}
}
}
Repair (che,n);
}
public static void Over (int[][] che,int N) {//overtaking situation
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 (int[][] che,int N) {//Go back to the original road after overtaking
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) {//Increase speed
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]--;
}
}
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.