| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 8991861 commit 5c08a1d
1 file changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -270,8 +270,10 @@ private void refreshPheromone(int t) { | |||
| 270 | 270 | ||
| 271 | 271 | addPheromone = 0; | |
| 272 | 272 | for (Ant ant : totalAnts) { | |
| 273 | - // 每只蚂蚁传播的信息素为控制因子除以距离总成本 | ||
| 274 | - addPheromone += Q / ant.calSumDistance(); | ||
| 273 | + if(ant.pathContained(i, j)){ | ||
| 274 | + // 每只蚂蚁传播的信息素为控制因子除以距离总成本 | ||
| 275 | + addPheromone += Q / ant.calSumDistance(); | ||
| 276 | + } | ||
| 275 | 277 | } | |
| 276 | 278 | ||
| 277 | 279 | // 将上次的结果值加上递增的量,并存入图中 | |
@@ -284,17 +286,22 @@ private void refreshPheromone(int t) { | |||
| 284 | 286 | ||
| 285 | 287 | } | |
| 286 | 288 | ||
| 287 | - public void antStartSearching() { | ||
| 289 | + /** | ||
| 290 | + * 蚁群算法迭代次数 | ||
| 291 | + * @param loopCount | ||
| 292 | + * 具体遍历次数 | ||
| 293 | + */ | ||
| 294 | + public void antStartSearching(int loopCount) { | ||
| 288 | 295 | // 蚁群寻找的总次数 | |
| 289 | - int loopCount = 0; | ||
| 296 | + int count = 0; | ||
| 290 | 297 | // 选中的下一个城市 | |
| 291 | 298 | String selectedCity = ""; | |
| 292 | 299 | ||
| 293 | 300 | pheromoneTimeMap = new HashMap<String, Double>(); | |
| 294 | 301 | totalAnts = new ArrayList<>(); | |
| 295 | 302 | random = new Random(); | |
| 296 | 303 | ||
| 297 | - while (loopCount < 10) { | ||
| 304 | + while (count < loopCount) { | ||
| 298 | 305 | initAnts(); | |
| 299 | 306 | ||
| 300 | 307 | while (true) { | |
@@ -312,13 +319,16 @@ public void antStartSearching() { | |||
| 312 | 319 | // 周期时间叠加 | |
| 313 | 320 | currentTime++; | |
| 314 | 321 | refreshPheromone(currentTime); | |
| 322 | + count++; | ||
| 315 | 323 | } | |
| 316 | 324 | ||
| 317 | 325 | // 根据距离成本,选出所花距离最短的一个路径 | |
| 318 | 326 | Collections.sort(totalAnts); | |
| 319 | 327 | bestPath = totalAnts.get(0).currentPath; | |
| 328 | + System.out.println(MessageFormat.format("经过{0}次循环遍历,最终得出的最佳路径:", count)); | ||
| 329 | + System.out.print("entrance"); | ||
| 320 | 330 | for (String cityName : bestPath) { | |
| 321 | - System.out.println(MessageFormat.format("-->{0}", cityName)); | ||
| 331 | + System.out.print(MessageFormat.format("-->{0}", cityName)); | ||
| 322 | 332 | } | |
| 323 | 333 | } | |
| 324 | 334 | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments