Simulated Annnealing fixes (#907)
* @Async and Spring Security * @Async with SecurityContext propagated * Spring and @Async * Simulated Annealing algorithm * Simulated Annealing algorithm * Rebase * Rebase
This commit is contained in:
parent
3d60f5cc7a
commit
39cc3511ad
@ -45,3 +45,4 @@
|
|||||||
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
- [Getting Started with Java Properties](http://www.baeldung.com/java-properties)
|
||||||
- [Grep in Java](http://www.baeldung.com/grep-in-java)
|
- [Grep in Java](http://www.baeldung.com/grep-in-java)
|
||||||
- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
|
- [Java - Combine Multiple Collections](http://www.baeldung.com/java-combine-multiple-collections)
|
||||||
|
- [Simulated Annealing for Travelling Salesman Problem](http://www.baeldung.com/java-simulated-annealing-for-traveling-salesman)
|
||||||
|
@ -5,18 +5,18 @@ import lombok.Data;
|
|||||||
@Data
|
@Data
|
||||||
public class City {
|
public class City {
|
||||||
|
|
||||||
private int x;
|
private int x;
|
||||||
private int y;
|
private int y;
|
||||||
|
|
||||||
public City() {
|
public City() {
|
||||||
this.x = (int) (Math.random() * 500);
|
this.x = (int) (Math.random() * 500);
|
||||||
this.y = (int) (Math.random() * 500);
|
this.y = (int) (Math.random() * 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
public double distanceToCity(City city) {
|
public double distanceToCity(City city) {
|
||||||
int x = Math.abs(getX() - city.getX());
|
int x = Math.abs(getX() - city.getX());
|
||||||
int y = Math.abs(getY() - city.getY());
|
int y = Math.abs(getY() - city.getY());
|
||||||
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
|
return Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -25,6 +25,8 @@ public class SimulatedAnnealing {
|
|||||||
currentSolution.revertSwap();
|
currentSolution.revertSwap();
|
||||||
}
|
}
|
||||||
t *= coolingRate;
|
t *= coolingRate;
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
if (i % 100 == 0) {
|
if (i % 100 == 0) {
|
||||||
System.out.println("Iteration #" + i);
|
System.out.println("Iteration #" + i);
|
||||||
@ -34,7 +36,7 @@ public class SimulatedAnnealing {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9));
|
System.out.println("Optimized distance for travel: " + simulateAnnealing(10, 10000, 0.9995));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -27,7 +27,10 @@ public class Travel {
|
|||||||
int a = generateRandomIndex();
|
int a = generateRandomIndex();
|
||||||
int b = generateRandomIndex();
|
int b = generateRandomIndex();
|
||||||
previousTravel = travel;
|
previousTravel = travel;
|
||||||
travel.set(a, travel.get(b));
|
City x = travel.get(a);
|
||||||
|
City y = travel.get(b);
|
||||||
|
travel.set(a, y);
|
||||||
|
travel.set(b, x);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void revertSwap() {
|
public void revertSwap() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user