Skip to content

Commit

Permalink
Merge pull request eugenp#1336 from eugenp/ac_refactor
Browse files Browse the repository at this point in the history
ACO refactor
  • Loading branch information
maibin authored Mar 8, 2017
2 parents a419237 + 9758e2c commit 3de5b73
Showing 1 changed file with 10 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ public class AntColonyOptimization {

private int maxIterations = 1000;

public int numberOfCities;
public int numberOfAnts;
private int numberOfCities;
private int numberOfAnts;
private double graph[][];
private double trails[][];
private List<Ant> ants = new ArrayList<>();
Expand All @@ -28,8 +28,8 @@ public class AntColonyOptimization {

private int currentIndex;

public int[] bestTourOrder;
public double bestTourLength;
private int[] bestTourOrder;
private double bestTourLength;

public AntColonyOptimization(int noOfCities) {
graph = generateRandomMatrix(noOfCities);
Expand All @@ -43,26 +43,17 @@ public AntColonyOptimization(int noOfCities) {

/**
* Generate initial solution
*
* @param n
* @return
*/
public double[][] generateRandomMatrix(int n) {
double[][] randomMatrix = new double[n][n];
random.setSeed(System.currentTimeMillis());
IntStream.range(0, n).forEach(i -> {
IntStream.range(0, n).forEach(j -> {
Integer r = random.nextInt(100) + 1;
randomMatrix[i][j] = Math.abs(r);
});
});
IntStream.range(0, n)
.forEach(i -> IntStream.range(0, n)
.forEach(j -> randomMatrix[i][j] = Math.abs(random.nextInt(100) + 1)));
return randomMatrix;
}

/**
* Perform ant optimization
*
* @return
*/
public void startAntOptimization() {
IntStream.rangeClosed(1, 3).forEach(i -> {
Expand All @@ -73,8 +64,6 @@ public void startAntOptimization() {

/**
* Use this method to run the main logic
*
* @return
*/
public int[] solve() {
setupAnts();
Expand All @@ -94,7 +83,7 @@ public int[] solve() {
*/
private void setupAnts() {
IntStream.range(0, numberOfAnts).forEach(i -> {
ants.stream().forEach(ant -> {
ants.forEach(ant -> {
ant.clear();
ant.visitCity(-1, random.nextInt(numberOfCities));
});
Expand All @@ -107,23 +96,18 @@ private void setupAnts() {
*/
private void moveAnts() {
IntStream.range(currentIndex, numberOfCities - 1).forEach(i -> {
ants.stream().forEach(ant -> {
ant.visitCity(currentIndex, selectNextCity(ant));
});
ants.forEach(ant -> ant.visitCity(currentIndex, selectNextCity(ant)));
currentIndex++;
});
}

/**
* Select next city for each ant
*
* @param ant
* @return
*/
private int selectNextCity(Ant ant) {
int t = random.nextInt(numberOfCities - currentIndex);
if (random.nextDouble() < randomFactor) {
IntStream.range(0, numberOfCities).filter(i -> i == t && !ant.visited(i)).findFirst();
IntStream.range(0, numberOfCities).filter(i -> i == t && !ant.visited(i)).findFirst(); //TODO unused
}
calculateProbabilities(ant);
double r = random.nextDouble();
Expand All @@ -140,8 +124,6 @@ private int selectNextCity(Ant ant) {

/**
* Calculate the next city picks probabilites
*
* @param ant
*/
public void calculateProbabilities(Ant ant) {
int i = ant.trail[currentIndex];
Expand Down

0 comments on commit 3de5b73

Please sign in to comment.