@@ -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