Skip to content

Commit 7f45d8f

Browse files
maibinpivovarit
authored andcommitted
Simulated Annealing algorithm (eugenp#900)
* @async and Spring Security * @async with SecurityContext propagated * Spring and @async * Simulated Annealing algorithm * Simulated Annealing algorithm * Rebase * Rebase
1 parent 2fe2e2a commit 7f45d8f

4 files changed

Lines changed: 129 additions & 0 deletions

File tree

‎core-java/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
<artifactId>log4j-over-slf4j</artifactId>
8686
<version>${org.slf4j.version}</version>
8787
</dependency>
88+
<dependency>
89+
<groupId>org.projectlombok</groupId>
90+
<artifactId>lombok</artifactId>
91+
<version>1.16.12</version>
92+
<scope>provided</scope>
93+
</dependency>
8894

8995
<!-- test scoped -->
9096

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package com.baeldung.algorithms;
2+
3+
import lombok.Data;
4+
5+
@Data
6+
public class City {
7+
8+
private int x;
9+
private int y;
10+
11+
public City() {
12+
this.x = (int) (Math.random() * 500);
13+
this.y = (int) (Math.random() * 500);
14+
}
15+
16+
public double distanceToCity(City city) {
17+
int x = Math.abs(getX() - city.getX());
18+
int y = Math.abs(getY() - city.getY());
19+
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
20+
}
21+
22+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.baeldung.algorithms;
2+
3+
public class SimulatedAnnealing {
4+
5+
private static Travel travel = new Travel(10);
6+
7+
public static double simulateAnnealing(double startingTemperature, int numberOfIterations, double coolingRate) {
8+
System.out.println("Starting SA with temperature: " + startingTemperature + ", # of iterations: "
9+
+ numberOfIterations + " and colling rate: " + coolingRate);
10+
double t = startingTemperature;
11+
travel.generateInitialTravel();
12+
double bestDistance = travel.getDistance();
13+
System.out.println("Initial distance of travel: " + bestDistance);
14+
Travel bestSolution = travel;
15+
Travel currentSolution = bestSolution;
16+
17+
for (int i = 0; i < numberOfIterations; i++) {
18+
if (t > 0.1) {
19+
currentSolution.swapCities();
20+
double currentDistance = currentSolution.getDistance();
21+
if (currentDistance == 0)
22+
continue;
23+
if (currentDistance < bestDistance) {
24+
bestDistance = currentDistance;
25+
} else if (Math.exp((currentDistance - bestDistance) / t) < Math.random()) {
26+
currentSolution.revertSwap();
27+
}
28+
t *= coolingRate;
29+
}
30+
if (i % 100 == 0) {
31+
System.out.println("Iteration #" + i);
32+
}
33+
}
34+
return bestDistance;
35+
}
36+
37+
public static void main(String[] args) {
38+
System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9));
39+
}
40+
41+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.baeldung.algorithms;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collections;
5+
6+
import lombok.Data;
7+
8+
@Data
9+
public class Travel {
10+
11+
private ArrayList<City> travel = new ArrayList<>();
12+
private ArrayList<City> previousTravel = new ArrayList<>();
13+
14+
public Travel(int numberOfCities) {
15+
for (int i = 0; i < numberOfCities; i++) {
16+
travel.add(new City());
17+
}
18+
}
19+
20+
public void generateInitialTravel() {
21+
if (travel.isEmpty())
22+
new Travel(10);
23+
Collections.shuffle(travel);
24+
}
25+
26+
public void swapCities() {
27+
int a = generateRandomIndex();
28+
int b = generateRandomIndex();
29+
previousTravel = travel;
30+
travel.set(a, travel.get(b));
31+
}
32+
33+
public void revertSwap() {
34+
travel = previousTravel;
35+
}
36+
37+
private int generateRandomIndex() {
38+
return (int) (Math.random() * travel.size());
39+
}
40+
41+
public City getCity(int index) {
42+
return travel.get(index);
43+
}
44+
45+
public int getDistance() {
46+
int distance = 0;
47+
for (int index = 0; index < travel.size(); index++) {
48+
City starting = getCity(index);
49+
City destination;
50+
if (index + 1 < travel.size()) {
51+
destination = getCity(index + 1);
52+
} else {
53+
destination = getCity(0);
54+
}
55+
distance += starting.distanceToCity(destination);
56+
}
57+
return distance;
58+
}
59+
60+
}

0 commit comments

Comments
 (0)