蚁群算法-JAVA实现

蚁群算法

基本思想

蚂蚁靠什么找出最短路径?
• 信息素:信息素是一种由蚂蚁自身释放的易挥发的
物质,能够实现蚁群内的间接通信。蚂蚁在寻找食
物时,在其经过的路径上会释放信息素,信息素可
以被其他的蚂蚁感知,并且信息素的浓度越高,对
应的路径越短
• 正反馈:蚂蚁会以较大概率选择信息素浓度较高的
路径,并释放一定量的信息素,从而使距离较短的
信息素浓度被加强形成正反馈*

蚁群算法解决TSP问题步骤以及预备知识

预备知识


在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述


在这里插入图片描述

算法步骤

①初始化城市距离矩阵,贪心算法获得第一个路径,第一次更新信息素矩阵
②为每一只蚂蚁随机选择初始城市
③计算前往下一个城市的概率,更新禁忌表
④轮盘赌选择下一个城市
⑤更新信息素矩阵
循环执行,找出解空间的最优解
部分代码展示(用到了四个java文件)

蚂蚁类

package antGroupAlgrithm;
//蚂蚁类
public class Ants {
	// 禁忌表, 记录走过的城市
	public boolean table[];
	// 路径
	public int path[];
	//每一个的概率以及累积概率用于轮盘赌
	public double pro[];
	public double accumulatePro[];
	//记录当前路径花费的距离
	public double distance;
	Ants(int number) {
		this.table = new boolean[number];
		this.path = new int[number+1];
		
		for (int i = 0; i < number; i++) {
			this.table[i] = false;
			this.path[i]=-1;
		}
		this.path[number]=-1;
		this.distance=0;
	}
	
	public boolean notOver() {
		for(int i=0;i<this.table.length;i++)
			if(this.table[i]==false)
				return true;
		return false;
	}
}

城市类

package antGroupAlgrithm;
//城市类
public class City {
	//记录城市的坐标
	public int x,y;
	//记录城市的标志
	public int ID;
	City(int lx,int ly,int sign){
		this.x=lx;
		this.y=ly;
		this.ID=sign;
	}
}

解空间类

package antGroupAlgrithm;
//解空间的每一个元素,路径和距离的一个结构体
public class DistancePath {
	public int path[];
	public double distance;
	DistancePath(int num){
		this.path=new int[num+1];
		this.distance=0;
	}
}

主算法类
变量部分

public class AntAlgrithm {

	public int cityNum;
	public City city[];
	public Ants ants[];
	// 记录已经被选择的出发点
	public boolean start[];
	// 距离矩阵与信息素矩阵
	public double distanceMatrix[][];
	public double pheromones[][];
	
	//记录全局解结果
	public DistancePath dp[][];
	
	public static final double alpha = 1;
	public static final double beta = 2;
	public static final double p = 0.5;
	public static final int antnumber = 18;
	public static final int MAX = 100000;
	public static final int times = 500;
	
	//构造函数
	AntAlgrithm(int x[], int y[]) {
		this.cityNum = x.length;
		this.city = new City[cityNum];
		this.ants = new Ants[antnumber];
		this.start = new boolean[cityNum];
		for (int i = 0; i < this.cityNum; i++) {
			this.city[i] = new City(x[i], y[i], i);
			this.start[i] = false;
		}
		// 初始化每一只蚂蚁
		for (int i = 0; i < antnumber; i++) {
			this.ants[i] = new Ants(this.cityNum);
			this.ants[i].pro = new double[this.cityNum - 1];
			this.ants[i].accumulatePro = new double[this.cityNum - 1];
		}
		//记录解空间的初始化
		this.dp=new DistancePath[times][antnumber];
		for(int i=0;i<this.dp.length;i++) {
			for(int j=0;j<this.dp[i].length;j++)
				this.dp[i][j]=new DistancePath(this.cityNum);
		}
	}
	//初始化函数
	public void Initial() {
		this.distanceMatrix = new double[this.cityNum][this.cityNum];
		this.pheromones = new double[this.cityNum][this.cityNum];

		for (int i = 0; i < this.cityNum; i++) {
			for (int j = i; j < this.cityNum; j++) {
				if (j == i)
					this.distanceMatrix[i][j] = MAX;
				else {
					this.distanceMatrix[i][j] = Math.hypot(this.city[i].x - this.city[j].x,
							this.city[i].y - this.city[j].y);
					this.distanceMatrix[j][i] = this.distanceMatrix[i][j];
				}
			}
		}
		// 利用贪心算法更新信息素矩阵
		double cn = this.greedy();
		double tao;
		tao = antnumber / cn;
		for (int i = 0; i < this.cityNum; i++) {
			for (int j = i + 1; j < this.cityNum; j++) {
				this.pheromones[i][j] = tao;
				this.pheromones[j][i] = tao;
			}
		}
	}

主函数部分

public static void main(String[] args) {
		//城市坐标
		int xCoordinate[] = { 1304, 3639, 4177, 3712, 3488, 3326, 3238, 4196, 4312, 4386, 3007, 2562, 2788, 2381, 1332,
				3715, 3918, 4061, 3780, 3676, 4029, 4263, 3429, 3507, 3394, 3439, 2935, 3140, 2545, 2778, 2370 };
		int yCoordinate[] = { 2312, 1315, 2244, 1399, 1535, 1556, 1229, 1004, 790, 570, 1970, 1756, 1491, 1676, 695,
				1678, 2179, 2370, 2212, 2578, 2838, 2931, 1908, 2367, 2643, 3201, 3240, 3550, 2357, 2826, 2975 };
		int flag = 0;
		//初始化
		AntAlgrithm ata = new AntAlgrithm(xCoordinate, yCoordinate);
		ata.Initial();
		//演变
		while (flag < times) {
			ata.tsp();
			ata.record(flag);
			ata.remake();
			flag += 1;
		}
		//选择最优解
		ata.chooseBest();
	}

需要注意的是,蚁群算法得到的解不是固定的,但是收敛于一个较小的值

代码运行截图
在这里插入图片描述
如果觉得有帮助的话就点个赞吧!

  • 12
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 19
    评论
蚁群算法是一种基于模拟蚂蚁觅食行为的优化算法,可以用于解决TSP问题。以下是Java实现蚁群算法解决TSP问题的简单步骤: 1. 初始化参数:包括蚂蚁数量、信息素挥发系数、信息素初始浓度等。 2. 随机生成蚂蚁初始路径:可以采用随机生成、贪心或其他启发式算法生成。 3. 计算路径长度:对于每只蚂蚁生成的路径,计算其路径长度。 4. 更新信息素:根据蚂蚁生成的路径长度和信息素初始浓度更新信息素。 5. 重复执行2-4步直到满足停止条件(比如达到最大迭代次数)。 以下是简单的Java代码实现: ```java public class AntColonyOptimization { // 常量 private static final int ANT_COUNT = 50; // 蚂蚁数量 private static final double ALPHA = 1; // 信息素重要程度因子 private static final double BETA = 5; // 启发函数重要程度因子 private static final double RHO = 0.5; // 信息素挥发系数 private static final int Q = 100; // 信息素增量强度常量 private static final int MAX_ITERATIONS = 1000; // 最大迭代次数 // 数据结构 private int[][] distance; // 距离矩阵 private double[][] pheromone; // 信息素矩阵 private Ant[] ants; // 蚂蚁数组 private Random random; // 随机数生成器 public AntColonyOptimization(int[][] distance) { this.distance = distance; this.pheromone = new double[distance.length][distance.length]; for (int i = 0; i < distance.length; i++) { for (int j = 0; j < distance.length; j++) { pheromone[i][j] = 1; // 初始化信息素浓度为1 } } this.ants = new Ant[ANT_COUNT]; for (int i = 0; i < ANT_COUNT; i++) { ants[i] = new Ant(distance.length); } this.random = new Random(); } public int[] solve() { int[] bestTour = null; int bestLength = Integer.MAX_VALUE; for (int i = 0; i < MAX_ITERATIONS; i++) { // 蚂蚁搜索并保存最优路径 for (Ant ant : ants) { ant.search(distance, pheromone, ALPHA, BETA, random); if (ant.getLength() < bestLength) { bestLength = ant.getLength(); bestTour = ant.getTour(); } } // 更新信息素 updatePheromone(); } return bestTour; } private void updatePheromone() { // 挥发信息素 for (int i = 0; i < pheromone.length; i++) { for (int j = 0; j < pheromone.length; j++) { pheromone[i][j] *= (1 - RHO); } } // 更新信息素 for (Ant ant : ants) { int[] tour = ant.getTour(); for (int i = 0; i < tour.length - 1; i++) { int from = tour[i]; int to = tour[i + 1]; pheromone[from][to] += Q / ant.getLength(); } } } private class Ant { private int[] tour; private boolean[] visited; private int length; public Ant(int size) { this.tour = new int[size]; this.visited = new boolean[size]; this.length = 0; } public void search(int[][] distance, double[][] pheromone, double alpha, double beta, Random random) { int start = random.nextInt(distance.length); tour[0] = start; visited[start] = true; for (int i = 1; i < tour.length; i++) { int next = selectNext(distance, pheromone, alpha, beta, random, tour[i - 1], visited); tour[i] = next; visited[next] = true; length += distance[tour[i - 1]][tour[i]]; } length += distance[tour[tour.length - 1]][tour[0]]; // 回到起点 Arrays.fill(visited, false); // 重置访问标记 } public int[] getTour() { return tour; } public int getLength() { return length; } private int selectNext(int[][] distance, double[][] pheromone, double alpha, double beta, Random random, int from, boolean[] visited) { double[] probabilities = new double[distance.length]; double sum = 0; for (int i = 0; i < probabilities.length; i++) { if (visited[i]) { probabilities[i] = 0; } else { probabilities[i] = Math.pow(pheromone[from][i], alpha) * Math.pow(1.0 / distance[from][i], beta); sum += probabilities[i]; } } for (int i = 0; i < probabilities.length; i++) { probabilities[i] /= sum; } return nextWithProbability(probabilities, random); } private int nextWithProbability(double[] probabilities, Random random) { double[] cumulativeProbabilities = new double[probabilities.length]; cumulativeProbabilities[0] = probabilities[0]; for (int i = 1; i < probabilities.length; i++) { cumulativeProbabilities[i] = cumulativeProbabilities[i - 1] + probabilities[i]; } double p = random.nextDouble(); for (int i = 0; i < cumulativeProbabilities.length; i++) { if (p <= cumulativeProbabilities[i]) { return i; } } return cumulativeProbabilities.length - 1; } } } ``` 使用方法如下: ```java int[][] distance = {{0, 2, 9, 10}, {1, 0, 6, 4}, {15, 7, 0, 8}, {6, 3, 12, 0}}; AntColonyOptimization aco = new AntColonyOptimization(distance); int[] tour = aco.solve(); System.out.println(Arrays.toString(tour)); // 输出最优路径 ``` 上述代码中,distance是距离矩阵,AntColonyOptimization是蚁群算法类,solve()方法返回最优路径,即各个城市的访问顺序。可以根据实际情况修改常量和数据结构。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值