Skip to content

Commit 3de5b73

Browse files
authored
Merge pull request eugenp#1336 from eugenp/ac_refactor
ACO refactor
2 parents a419237 + 9758e2c commit 3de5b73

1 file changed

Lines changed: 10 additions & 28 deletions

File tree

‎core-java/src/main/java/com/baeldung/algorithms/ga/ant_colony/AntColonyOptimization.java‎

Lines changed: 10 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@ public class AntColonyOptimization {
1818

1919
private int maxIterations = 1000;
2020

21-
public int numberOfCities;
22-
public int numberOfAnts;
21+
private int numberOfCities;
22+
private int numberOfAnts;
2323
private double graph[][];
2424
private double trails[][];
2525
private List<Ant> ants = new ArrayList<>();
@@ -28,8 +28,8 @@ public class AntColonyOptimization {
2828

2929
private int currentIndex;
3030

31-
public int[] bestTourOrder;
32-
public double bestTourLength;
31+
private int[] bestTourOrder;
32+
private double bestTourLength;
3333

3434
public AntColonyOptimization(int noOfCities) {
3535
graph = generateRandomMatrix(noOfCities);
@@ -43,26 +43,17 @@ public AntColonyOptimization(int noOfCities) {
4343

4444
/**
4545
* Generate initial solution
46-
*
47-
* @param n
48-
* @return
4946
*/
5047
public double[][] generateRandomMatrix(int n) {
5148
double[][] randomMatrix = new double[n][n];
52-
random.setSeed(System.currentTimeMillis());
53-
IntStream.range(0, n).forEach(i -> {
54-
IntStream.range(0, n).forEach(j -> {
55-
Integer r = random.nextInt(100) + 1;
56-
randomMatrix[i][j] = Math.abs(r);
57-
});
58-
});
49+
IntStream.range(0, n)
50+
.forEach(i -> IntStream.range(0, n)
51+
.forEach(j -> randomMatrix[i][j] = Math.abs(random.nextInt(100) + 1)));
5952
return randomMatrix;
6053
}
6154

6255
/**
6356
* Perform ant optimization
64-
*
65-
* @return
6657
*/
6758
public void startAntOptimization() {
6859
IntStream.rangeClosed(1, 3).forEach(i -> {
@@ -73,8 +64,6 @@ public void startAntOptimization() {
7364

7465
/**
7566
* Use this method to run the main logic
76-
*
77-
* @return
7867
*/
7968
public int[] solve() {
8069
setupAnts();
@@ -94,7 +83,7 @@ public int[] solve() {
9483
*/
9584
private void setupAnts() {
9685
IntStream.range(0, numberOfAnts).forEach(i -> {
97-
ants.stream().forEach(ant -> {
86+
ants.forEach(ant -> {
9887
ant.clear();
9988
ant.visitCity(-1, random.nextInt(numberOfCities));
10089
});
@@ -107,23 +96,18 @@ private void setupAnts() {
10796
*/
10897
private void moveAnts() {
10998
IntStream.range(currentIndex, numberOfCities - 1).forEach(i -> {
110-
ants.stream().forEach(ant -> {
111-
ant.visitCity(currentIndex, selectNextCity(ant));
112-
});
99+
ants.forEach(ant -> ant.visitCity(currentIndex, selectNextCity(ant)));
113100
currentIndex++;
114101
});
115102
}
116103

117104
/**
118105
* Select next city for each ant
119-
*
120-
* @param ant
121-
* @return
122106
*/
123107
private int selectNextCity(Ant ant) {
124108
int t = random.nextInt(numberOfCities - currentIndex);
125109
if (random.nextDouble() < randomFactor) {
126-
IntStream.range(0, numberOfCities).filter(i -> i == t && !ant.visited(i)).findFirst();
110+
IntStream.range(0, numberOfCities).filter(i -> i == t && !ant.visited(i)).findFirst(); //TODO unused
127111
}
128112
calculateProbabilities(ant);
129113
double r = random.nextDouble();
@@ -140,8 +124,6 @@ private int selectNextCity(Ant ant) {
140124

141125
/**
142126
* Calculate the next city picks probabilites
143-
*
144-
* @param ant
145127
*/
146128
public void calculateProbabilities(Ant ant) {
147129
int i = ant.trail[currentIndex];

0 commit comments

Comments
 (0)