package trafficlight;
/**
*
* @author Bob
*/
import java.util.*;
public class Car extends Thread
{
static double aberranceRate=0;//变异率
static int population=0;//种群人口个数
static int bestNum=0;//择优个数
static int generation=0;//进化过程中有多少代
static boolean block[][]=new boolean[20][20];//环境
static LinkedList<Car> nation;//所有机器人集合
static LinkedList<Car> bests=new LinkedList<Car>();//最优的机器人集合
static int currentAlive=0;//当前存活的机器人个数
static int currentGeneration=0;//当前是第几代
static boolean evolveFinish=false;//进化是否完成
boolean s[]=new boolean[4];//四种信号:停止、直行、左转、右转 //规定 true 代表灯在亮 false代表灯没亮
float rate;//每一个进程的正确率
int righttimes;
//设置参数
static void setTotal(double rate,int pop,int Num,int g)
{
aberranceRate=rate;
population=pop;
bestNum=Num;
generation=g;
evolveFinish=false;
currentGeneration=0;
}
//**************************
BinaryTree logicTree;//机器人的行动逻辑树
//**************************
static void evolve()
{
currentGeneration+=1;
currentAlive=population;
nation=new LinkedList<Car>();
if(currentGeneration==1)
{
for(int i=0;i<population;i++)
{
Car r=new Car();
nation.add(r);
r.start();
}
}
else
{
for(int i=0;i<population;i++)
{
Car r=new Car(bests.get((int)(Math.random()*bestNum)),
bests.get((int)(Math.random()*bestNum)));
nation.add(r);
r.start();
}
}
}
//返回最优的一个机器人
public static Car getBestOne()
{
while(evolveFinish!=true)
{
try
{
Thread.sleep(100);
}
catch(Exception ex)
{}
}
return bests.getFirst();
}
//线程活动
public void run()
{
int times=(int)(Math.random()*1000)+300;
int i;
int direct;
righttimes=0;
for(i=0;i<times;i++)
{
signalmodle();
direct=direction();
//go(direct);
if(rightWay(direct))
{
righttimes++;
}
}
rate=(float)righttimes/(float)times;
//System.out.printf("right %d all %d rate %f \n",righttimes,times,rate);
filter(this);
die();
}
//优胜劣汰
public synchronized static void filter(Car r)
{
if (bests.size() < bestNum)
{
bests.add(r);
}
else
{
Car temp;
for (int i = 0; i < bests.size(); i++)
{
temp=bests.get(i);
if (r.rate > temp.rate ||(r.rate==temp.rate&&r.righttimes>temp.righttimes))
{
bests.add(i,r);
bests.removeLast();
return;
}
}
}
}
//判断行为的准确性
public boolean rightWay(int direct)
{
boolean judge;
judge=false;
if(s[0]==true)
{
switch(direct)
{
case 0:judge=true;break;
case 1:if(s[1]==true)
judge=true;
else
judge=false;
break;
case 2:if(s[2]==true)
judge=true;
else
judge=false;
break;
case 3:if(s[3]==true)
judge=true;
else
judge=false;
break;
}
}
else
{
switch(direct)
{
case 0:judge=false;break;
case 1:if(s[1]==true)
judge=true;
else
judge=false;
break;
case 2:if(s[2]==true)
judge=true;
else
judge=false;
break;
case 3:if(s[3]==true)
judge=true;
else
judge=false;
break;
}
}
return judge;
}
//机器人死亡
public synchronized static void die()
{
currentAlive-=1;
if(currentAlive==0&¤tGeneration<generation)
{
evolve();
}
else if(currentAlive==0&¤tGeneration==generation)
{
evolveFinish=true;
Car.show();
System.out.printf("\nFinished2.\n");
}
}
public static void show()
{
System.out.printf("\nMark\n");
Car r=Car.getBestOne();
int times;
int righttimes;
times=(int)(Math.random()*10)+10;
System.out.printf("Correct rate is : %f\n",r.rate);
righttimes=0;
for(int i=0;i<times;i++)
{
int direct;
r.signalmodle();
r.signalprint();
direct=r.direction();
switch(direct)
{
case 0:
System.out.printf("Stop\n");
break;
case 1:
System.out.printf("Forward\n");
break;
case 2:
System.out.printf("Leftturn\n");
break;
case 3:
System.out.printf("Rightturn\n");
break;
}
if(r.rightWay(direct))
{
righttimes++;
}
}
System.out.printf("Test signal's times is : %d and the correct times is :%d\n",times,righttimes);
System.out.printf("Correct rate is : %f\n",(float)righttimes/(float)times);
}
public void signalmodle()
{
//以下部分是先模拟信号的情况,以后可定需要写一个随机信号函数或者类,来输出信号,这里只是为了方便做测试而已
for(int i=0;i<4;i++)
{
int j=(int)(Math.random() *10);
if(j%2==0)
{
s[i]=false;
}
else
{
s[i]=true;
}
}
}
public void signalprint()
{
System.out.printf("Signal is : ");
if(s[0])
{
System.out.printf("Stop ");
}
if(s[1])
{
System.out.printf("Forward ");
}
if(s[2])
{
System.out.printf("Leftturn ");
}
if(s[3])
{
System.out.printf("Rightturn ");
}
System.out.printf("\n");
}
//构造函数1
Car()
{
logicTree=BinaryTree.randomTree(elements.get());
//logicTree.print();
}
//构造函数2
Car(Car father,Car mother)
{
logicTree=BinaryTree.giveBirthTo(father.logicTree, mother.logicTree);
if(aberranceRate!=0)
{
if((int)((int)(1/aberranceRate)*Math.random())==0)//是否变异
{
logicTree=BinaryTree.aberrance(logicTree);
}
}
}
//在信号的指导下进行运动
private void go(int direct)
{
System.out.printf("\n");
switch(direct)
{
case 0:
System.out.printf("ddddStop\n");
break;
case 1:
System.out.printf("Forward\n");
break;
case 2:
System.out.printf("Leftturn\n");
break;
case 3:
System.out.printf("Rightturn\n");
break;
}
}
public int direction()
{
int direct;
boolean b=false;
direct=0;
TreeNode node=logicTree.root;
//System.out.printf("\nThis is direction judge progress\n");
while(b==false)
{
if(node.value=="stop")
{
b=(node.Right!=null)?getboolean(node.Right):false;
direct=0;
}
else if(node.value=="forward")
{
b=(node.Right!=null)?getboolean(node.Right):false;
direct=1;
}
else if(node.value=="leftturn")
{
b=(node.Right!=null)