Skip to content

Commit 5c08a1d

Browse files
author
linyiqun
committed
ACO蚁群算法bug修复
ACO蚁群算法bug修复
1 parent 8991861 commit 5c08a1d

File tree

1 file changed

+16
-6
lines changed

1 file changed

+16
-6
lines changed

Others/DataMining_ACO/ACOTool.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,10 @@ private void refreshPheromone(int t) {
270270

271271
addPheromone = 0;
272272
for (Ant ant : totalAnts) {
273-
// 每只蚂蚁传播的信息素为控制因子除以距离总成本
274-
addPheromone += Q / ant.calSumDistance();
273+
if(ant.pathContained(i, j)){
274+
// 每只蚂蚁传播的信息素为控制因子除以距离总成本
275+
addPheromone += Q / ant.calSumDistance();
276+
}
275277
}
276278

277279
// 将上次的结果值加上递增的量,并存入图中
@@ -284,17 +286,22 @@ private void refreshPheromone(int t) {
284286

285287
}
286288

287-
public void antStartSearching() {
289+
/**
290+
* 蚁群算法迭代次数
291+
* @param loopCount
292+
* 具体遍历次数
293+
*/
294+
public void antStartSearching(int loopCount) {
288295
// 蚁群寻找的总次数
289-
int loopCount = 0;
296+
int count = 0;
290297
// 选中的下一个城市
291298
String selectedCity = "";
292299

293300
pheromoneTimeMap = new HashMap<String, Double>();
294301
totalAnts = new ArrayList<>();
295302
random = new Random();
296303

297-
while (loopCount < 10) {
304+
while (count < loopCount) {
298305
initAnts();
299306

300307
while (true) {
@@ -312,13 +319,16 @@ public void antStartSearching() {
312319
// 周期时间叠加
313320
currentTime++;
314321
refreshPheromone(currentTime);
322+
count++;
315323
}
316324

317325
// 根据距离成本,选出所花距离最短的一个路径
318326
Collections.sort(totalAnts);
319327
bestPath = totalAnts.get(0).currentPath;
328+
System.out.println(MessageFormat.format("经过{0}次循环遍历,最终得出的最佳路径:", count));
329+
System.out.print("entrance");
320330
for (String cityName : bestPath) {
321-
System.out.println(MessageFormat.format("-->{0}", cityName));
331+
System.out.print(MessageFormat.format("-->{0}", cityName));
322332
}
323333
}
324334

0 commit comments

Comments
 (0)