11package com .baeldung .algorithms .ga .ant_colony ;
22
3+ import java .util .ArrayList ;
34import java .util .Arrays ;
5+ import java .util .List ;
46import java .util .Random ;
7+ import java .util .stream .IntStream ;
58
69public class AntColonyOptimization {
710
@@ -19,7 +22,7 @@ public class AntColonyOptimization {
1922 public int numberOfAnts ;
2023 private double graph [][];
2124 private double trails [][];
22- private Ant ants [] ;
25+ private List < Ant > ants = new ArrayList <>() ;
2326 private Random random = new Random ();
2427 private double probabilities [];
2528
@@ -35,101 +38,92 @@ public AntColonyOptimization(int noOfCities) {
3538
3639 trails = new double [numberOfCities ][numberOfCities ];
3740 probabilities = new double [numberOfCities ];
38- ants = new Ant [numberOfAnts ];
39- for (int j = 0 ; j < numberOfAnts ; j ++) {
40- ants [j ] = new Ant (numberOfCities );
41- }
41+ IntStream .range (0 , numberOfAnts ).forEach (i -> ants .add (new Ant (numberOfCities )));
4242 }
4343
4444 /**
4545 * Generate initial solution
46+ *
4647 * @param n
4748 * @return
4849 */
4950 public double [][] generateRandomMatrix (int n ) {
5051 double [][] randomMatrix = new double [n ][n ];
5152 random .setSeed (System .currentTimeMillis ());
52- for ( int i = 0 ; i < n ; i ++) {
53- for ( int j = 0 ; j < n ; j ++) {
53+ IntStream . range ( 0 , n ). forEach ( i -> {
54+ IntStream . range ( 0 , n ). forEach ( j -> {
5455 Integer r = random .nextInt (100 ) + 1 ;
5556 randomMatrix [i ][j ] = Math .abs (r );
56- }
57- }
57+ });
58+ });
5859 return randomMatrix ;
5960 }
60-
61+
6162 /**
6263 * Perform ant optimization
64+ *
6365 * @return
6466 */
65- public int [] startAntOptimization () {
66- int [] finalResult = null ;
67- for (int i = 1 ; i <= 3 ; i ++) {
67+ public void startAntOptimization () {
68+ IntStream .rangeClosed (1 , 3 ).forEach (i -> {
6869 System .out .println ("Attempt #" + i );
69- finalResult = solve ();
70- }
71- return finalResult ;
70+ solve ();
71+ });
7272 }
73-
73+
7474 /**
7575 * Use this method to run the main logic
76+ *
7677 * @return
7778 */
78- private int [] solve () {
79+ public int [] solve () {
7980 setupAnts ();
8081 clearTrails ();
81- int iteration = 0 ;
82- while (iteration < maxIterations ) {
82+ IntStream .range (0 , maxIterations ).forEach (i -> {
8383 moveAnts ();
8484 updateTrails ();
8585 updateBest ();
86- iteration ++;
87- }
86+ });
8887 System .out .println ("Best tour length: " + (bestTourLength - numberOfCities ));
8988 System .out .println ("Best tour order: " + Arrays .toString (bestTourOrder ));
9089 return bestTourOrder .clone ();
9190 }
92-
91+
9392 /**
9493 * Prepare ants for the simulation
9594 */
9695 private void setupAnts () {
97- currentIndex = -1 ;
98- for (int i = 0 ; i < numberOfAnts ; i ++) {
99- ants [i ].clear ();
100- ants [i ].visitCity (currentIndex , random .nextInt (numberOfCities ));
101- }
102- currentIndex ++;
96+ IntStream .range (0 , numberOfAnts ).forEach (i -> {
97+ ants .stream ().forEach (ant -> {
98+ ant .clear ();
99+ ant .visitCity (-1 , random .nextInt (numberOfCities ));
100+ });
101+ });
102+ currentIndex = 0 ;
103103 }
104-
104+
105105 /**
106106 * At each iteration, move ants
107107 */
108108 private void moveAnts () {
109- while (currentIndex < numberOfCities - 1 ) {
110- for (Ant a : ants )
111- a .visitCity (currentIndex , selectNextCity (a ));
109+ IntStream .range (currentIndex , numberOfCities - 1 ).forEach (i -> {
110+ ants .stream ().forEach (ant -> {
111+ ant .visitCity (currentIndex , selectNextCity (ant ));
112+ });
112113 currentIndex ++;
113- }
114+ });
114115 }
115-
116+
116117 /**
117118 * Select next city for each ant
119+ *
118120 * @param ant
119121 * @return
120122 */
121123 private int selectNextCity (Ant ant ) {
124+ int t = random .nextInt (numberOfCities - currentIndex );
122125 if (random .nextDouble () < randomFactor ) {
123- int t = random .nextInt (numberOfCities - currentIndex );
124- int j = -1 ;
125- for (int i = 0 ; i < numberOfCities ; i ++) {
126- if (!ant .visited (i )) {
127- j ++;
128- }
129- if (j == t ) {
130- return i ;
131- }
132- }
126+ IntStream .range (0 , numberOfCities ).filter (i -> i == t && !ant .visited (i )).findFirst ();
133127 }
134128 calculateProbabilities (ant );
135129 double r = random .nextDouble ();
@@ -146,9 +140,10 @@ private int selectNextCity(Ant ant) {
146140
147141 /**
148142 * Calculate the next city picks probabilites
143+ *
149144 * @param ant
150145 */
151- private void calculateProbabilities (Ant ant ) {
146+ public void calculateProbabilities (Ant ant ) {
152147 int i = ant .trail [currentIndex ];
153148 double pheromone = 0.0 ;
154149 for (int l = 0 ; l < numberOfCities ; l ++) {
@@ -189,8 +184,8 @@ private void updateTrails() {
189184 */
190185 private void updateBest () {
191186 if (bestTourOrder == null ) {
192- bestTourOrder = ants [ 0 ] .trail ;
193- bestTourLength = ants [ 0 ] .trailLength (graph );
187+ bestTourOrder = ants . get ( 0 ) .trail ;
188+ bestTourLength = ants . get ( 0 ) .trailLength (graph );
194189 }
195190 for (Ant a : ants ) {
196191 if (a .trailLength (graph ) < bestTourLength ) {
@@ -204,9 +199,9 @@ private void updateBest() {
204199 * Clear trails after simulation
205200 */
206201 private void clearTrails () {
207- for ( int i = 0 ; i < numberOfCities ; i ++)
208- for ( int j = 0 ; j < numberOfCities ; j ++)
209- trails [ i ][ j ] = c ;
202+ IntStream . range ( 0 , numberOfCities ). forEach ( i -> {
203+ IntStream . range ( 0 , numberOfCities ). forEach ( j -> trails [ i ][ j ] = c );
204+ }) ;
210205 }
211206
212207}
0 commit comments