⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 antcolony.java

📁 java实现的基本蚁群算法
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package ant;
/*
 * @(#)Antcolony.java 1.0 03/05/22
 *
 * You can modify the template of this file in the
 * directory ..\JCreator\Templates\Template_2\Project_Name.java
 *
 * You can also create your own project template by making a new
 * folder in the directory ..\JCreator\Template\. Use the other
 * templates as examples.
 *
 */

import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.util.Vector;

class AntCanvas extends Canvas
{
    //画布,一切画图操作均由该类完成
	//Image image;
	Color obs_color;//障碍物颜色
	Color origin_color;//我的颜色
	Color back_color;//背景色
        Color end_color;//食物点的颜色
	//boolean first;
	boolean reset;

/*	public AntCanvas(Image img)	{

		super();
 		image = img;
		obs_color = Color.white;
		setBackground(Color.black);
		setForeground(Color.white);
		first = true;
		reset = false;

	}*/

	public AntCanvas()	{

		super();
 		//image = null;
 		back_color=Antcolony.BACK_COLOR;
		setBackground(back_color);
		setForeground(Color.white);
		obs_color = Antcolony.OBS_COLOR;
		origin_color=Antcolony.ORIGIN_COLOR;
                end_color=Antcolony.End_COLOR;
		//first = true;
		reset = true;

	}

	public void Clear() {
        //清空画布
		reset = true;
		repaint();
	}


	public void paint(Graphics g) {
		int i;
                //重画的时候仅仅画障碍物
		g.setColor(Color.black);
		g.fillRect(0,0,size().width,size().height);
		g.setColor(obs_color);
		for(i=0;i<Antcolony.obsCount;i++){
			g.fillRect(Antcolony.obsP[i].x,Antcolony.obsP[i].y,1,1);
		}

	}
	public void process(){
        //处理动画的过程
		Graphics g=this.getGraphics();
                g.setColor(end_color);
 		for(int j=0;j<Antcolony.EndPts;j++){
                  //画所有的食物点
                  g.fillRect(Antcolony.EndPt[j].x,Antcolony.EndPt[j].y,2,2);
                }
       for(int i=0;i<Antcolony.antCount;i++){
                  //每只蚂蚁开始决策,并画蚂蚁
			Antcolony.ants[i].Process();
			Antcolony.ants[i].Draw(g);
		}
		//double r=Math.random();
		//if(r<1){
                for(int i=0;i<Antcolony.phe.size();i++){
                        Pheromone v=(Pheromone)(Antcolony.phe.elementAt(i));
                        //Antcolony的drawPhe变量标志是否画信息素
                        switch(Antcolony.drawPhe){
                        case (1):
                            v.Draw(g);
                            break;
                        case (2):
                            if(v.kind==1)v.Draw(g);
                            break;
                        case (3):
                            if(v.kind==0)v.Draw(g);
                            break;
                        }
                        v.delimit(g);
                }
		//}
          	g.setColor(origin_color);
                for(int i=0;i<Antcolony.OriginPts;i++){
                  //画所有的窝
                  g.fillRect(Antcolony.OriginPt[i].x,Antcolony.OriginPt[i].y,2,2);
                }

	}
	Graphics GetGra() {

		return this.getGraphics();
	}
}
public class Antcolony extends Applet  implements Runnable  {
	boolean isStandalone = false;//系统的参数,是否独立运行,不用管
	Thread runner;//创建一个线程,让动画平滑的运行
	boolean running;//是否让动画运行
        boolean reset=false;//是否按下了重置按钮
	static Color OBS_COLOR=Color.red;//障碍物的颜色
	static Color ORIGIN_COLOR=Color.yellow;//窝的颜色
	static Color BACK_COLOR=Color.black;//背景色
	static Color ANT_COLOR=Color.white;//蚂蚁的颜色
        static Color End_COLOR=Color.cyan;//食物点的颜色
	AntCanvas canvas=new AntCanvas();//画图用的画布
	int obs_grid[][];//障碍物网格数组,这是个width*height的矩阵,数组中存储的是障碍物数组(obsP[])的指标,这样做可以加快索引的速度
        static Point obsP[];//障碍物数组,存储的是点的信息,指标是障碍物的总数
	static int obsCount;//障碍物的数量,最大为width*height
	static Point EndPt[];//食物点数组,值为食物点坐标。
        static int EndPts=1;//食物点的个数,初始的时候为1,最大数为100
        static int Pheromone_grid[][][];//信息素网格数组,2*width*height的三维矩阵,第一维是信息素种类(窝的信息素为0,食物的为1),它存储的是信息素的种类和值
        static Vector phe;//信息素向量(相当于一个数组),当环境更新信息素的时候,只需要查找这个向量就可以了,不用搜索整个width*height这么多的Pheromone_grid数组点
        static int Max_Pheromone=500000;//最大信息素数值,应该根据地图的复杂程度来定,越复杂越大!
	static Point OriginPt[];//窝点信息
        static int OriginPts=1;//窝的个数,最大为100
	static int width=300,height=300;//环境的长和宽
	static int antCount;//蚂蚁的数量
        static int Delimiter=5;//信息素消散的速率,为整数,越大则消散的越快
        static int FoodR=10;//食物和窝产生梯度的信息素的半径
	static ant ants[];//蚂蚁数组
        static int drawPhe=2;//画信息素的模式,0为不画,1为画所有的信息素,2为画食物的信息素,3为画窝的信息素
	int delay=10;//每次运行的间隔速率,越小程序运行越快(这个参数基本没用,因为当蚂蚁多了以后,处理过程很耗时间)

        //下面是一些控件信息
        Button btnStart=new Button("开始");
        Button btnReset=new Button("重来");
        Button btnMap=new Button("编辑地图");
        Button btnConfig=new Button("设置");
        Choice choPDraw=new Choice();
	public void init() {
                //初始化函数,先画各种控件
		setLayout(new BorderLayout());
                Panel pan=new Panel();
                add("South",pan);
                this.add("Center",canvas);
                pan.add(btnStart);
                pan.add(btnReset);
                pan.add(btnConfig);
                pan.add(btnMap);
                pan.add(choPDraw);
                choPDraw.addItem("不画信息素");
                choPDraw.addItem("画所有信息素");
                choPDraw.addItem("画食物信息素");
                choPDraw.addItem("画窝的信息素");
                choPDraw.select(2);

                //初始化各个数组
		obs_grid=new int [width][height];
                phe=new Vector();
                Pheromone_grid=new int [2][width][height];
                for(int i=0;i<width;i++){
			for(int j=0;j<height;j++){
				obs_grid[i][j]=-1;
                                for(int k=0;k<2;k++){
                                  Pheromone_grid[k][i][j]=0;
                                }
			}
		}

 		antCount=50;//蚂蚁个数缺省为50
                //初始化蚂蚁,这些属性都是蚂蚁的最原始的属性
		ants=new ant[antCount];
      		for(int i=0;i<antCount;i++){
		        ants[i]= new ant(new Point(0,0),3,i,this,ANT_COLOR,0.001,50);
		}

                //下面装载缺省的地图,包括障碍物、食物点、窝点的位置,都放到数组grid[][]中然后交给init_map函数统一处理
                int grid[][]=new int[width][height];

                //下面从地图库中加在地图
                Maps maps=new Maps();
                maps.LoadMap(grid,0);
                //初始化地图
                reinit_map(grid);

                //初始化所有的蚂蚁
		reinit();
	}
        public void reinit_map(int grid[][]){
                //将数组grid[][]中存储的信息转换到当前的环境数据结构中
                //相当于把一个位图信息width*height像素转化成窝、食物、障碍物

                //先停止程序的运行
                running=false;
                btnStart.setLabel("开始");


                obsCount=0;
                EndPts=0;
                OriginPts=0;
                obsP=new Point[width*height];
                OriginPt=new Point[100];
                EndPt=new Point[100];

                //清空obs_grid和Pheromone两个数组中的值
                for(int i=0;i<width;i++){
						for(int j=0;j<height;j++){
							obs_grid[i][j]=-1;
							for(int k=0;k<2;k++){
							  Pheromone_grid[k][i][j]=0;
							}
						}
				}

                //从grid数组中读取信息
                for(int i=0;i<width;i++){

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -