用遗传算法解决TSP问题java
时间: 2023-12-16 12:04:23 浏览: 120
好的,你想了解用遗传算法解决TSP问题的Java实现。TSP问题是指在给定的n个城市之间寻找一条最短的路径,使得每个城市都恰好被经过一次。而遗传算法是一种模拟自然界进化过程的算法,可以用来解决TSP问题。
下面是一个简单的Java实现:
首先,我们需要定义一个表示城市的类City:
```java
public class City {
private int x;
private int y;
public City(int x, int y) {
this.x = x;
this.y = y;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public double distanceTo(City city) {
int dx = getX() - city.getX();
int dy = getY() - city.getY();
return Math.sqrt(dx * dx + dy * dy);
}
@Override
public String toString() {
return "(" + getX() + ", " + getY() + ")";
}
}
```
然后,我们定义一个表示路径的类Route:
```java
public class Route {
private List<City> cities;
public Route(List<City> cities) {
this.cities = cities;
}
public List<City> getCities() {
return cities;
}
public double getTotalDistance() {
double distance = 0;
for (int i = 0; i < cities.size() - 1; i++) {
distance += cities.get(i).distanceTo(cities.get(i + 1));
}
distance += cities.get(cities.size() - 1).distanceTo(cities.get(0));
return distance;
}
@Override
public String toString() {
return cities.toString() + " -> " + getTotalDistance();
}
}
```
接下来,我们实现一个生成随机路径的方法:
```java
public static Route generateRandomRoute(List<City> cities) {
List<City> randomCities = new ArrayList<>(cities);
Collections.shuffle(randomCities);
return new Route(randomCities);
}
```
然后,我们需要实现一个计算适应度的方法,即计算每个路径的适应度:
```java
public static double calculateFitness(Route route) {
return 1.0 / route.getTotalDistance();
}
```
接下来,我们实现遗传算法的主要步骤:
1. 初始化种群
```java
List<Route> population = new ArrayList<>();
for (int i = 0; i < populationSize; i++) {
population.add(generateRandomRoute(cities));
}
```
2. 选择
```java
List<Route> matingPool = new ArrayList<>();
for (int i = 0; i < populationSize; i++) {
Route a = population.get(random.nextInt(populationSize));
Route b = population.get(random.nextInt(populationSize));
Route selected = calculateFitness(a) > calculateFitness(b) ? a : b;
matingPool.add(selected);
}
```
3. 交叉
```java
List<Route> offspring = new ArrayList<>();
for (int i = 0; i < populationSize; i++) {
Route parentA = matingPool.get(random.nextInt(populationSize));
Route parentB = matingPool.get(random.nextInt(populationSize));
int crossoverPoint1 = random.nextInt(cities.size());
int crossoverPoint2 = random.nextInt(cities.size());
int start = Math.min(crossoverPoint1, crossoverPoint2);
int end = Math.max(crossoverPoint1, crossoverPoint2);
List<City> childCities = new ArrayList<>();
for (int j = start; j <= end; j++) {
childCities.add(parentA.getCities().get(j));
}
for (int j = 0; j < cities.size(); j++) {
if (!childCities.contains(parentB.getCities().get(j))) {
childCities.add(parentB.getCities().get(j));
}
}
offspring.add(new Route(childCities));
}
```
4. 变异
```java
for (int i = 0; i < populationSize; i++) {
if (random.nextDouble() < mutationRate) {
Route route = offspring.get(i);
int mutationPoint1 = random.nextInt(cities.size());
int mutationPoint2 = random.nextInt(cities.size());
Collections.swap(route.getCities(), mutationPoint1, mutationPoint2);
}
}
```
5. 替换
```java
for (int i = 0; i < populationSize; i++) {
Route parent = population.get(i);
Route child = offspring.get(i);
if (calculateFitness(child) > calculateFitness(parent)) {
population.set(i, child);
}
}
```
最后,我们可以在main方法中调用遗传算法求解TSP问题:
```java
public static void main(String[] args) {
List<City> cities = new ArrayList<>();
// 添加城市
cities.add(new City(60, 200));
cities.add(new City(180, 200));
cities.add(new City(80, 180));
cities.add(new City(140, 180));
cities.add(new City(20, 160));
cities.add(new City(100, 160));
cities.add(new City(200, 160));
cities.add(new City(140, 140));
cities.add(new City(40, 120));
cities.add(new City(100, 120));
cities.add(new City(180, 100));
cities.add(new City(60, 80));
cities.add(new City(120, 80));
cities.add(new City(180, 60));
cities.add(new City(20, 40));
cities.add(new City(100, 40));
cities.add(new City(200, 40));
cities.add(new City(20, 20));
cities.add(new City(60, 20));
cities.add(new City(160, 20));
int populationSize = 100;
double mutationRate = 0.01;
List<Route> population = new ArrayList<>();
for (int i = 0; i < populationSize; i++) {
population.add(generateRandomRoute(cities));
}
int generations = 1000;
for (int i = 0; i < generations; i++) {
List<Route> matingPool = new ArrayList<>();
for (int j = 0; j < populationSize; j++) {
Route a = population.get(random.nextInt(populationSize));
Route b = population.get(random.nextInt(populationSize));
Route selected = calculateFitness(a) > calculateFitness(b) ? a : b;
matingPool.add(selected);
}
List<Route> offspring = new ArrayList<>();
for (int j = 0; j < populationSize; j++) {
Route parentA = matingPool.get(random.nextInt(populationSize));
Route parentB = matingPool.get(random.nextInt(populationSize));
int crossoverPoint1 = random.nextInt(cities.size());
int crossoverPoint2 = random.nextInt(cities.size());
int start = Math.min(crossoverPoint1, crossoverPoint2);
int end = Math.max(crossoverPoint1, crossoverPoint2);
List<City> childCities = new ArrayList<>();
for (int k = start; k <= end; k++) {
childCities.add(parentA.getCities().get(k));
}
for (int k = 0; k < cities.size(); k++) {
if (!childCities.contains(parentB.getCities().get(k))) {
childCities.add(parentB.getCities().get(k));
}
}
offspring.add(new Route(childCities));
}
for (int j = 0; j < populationSize; j++) {
if (random.nextDouble() < mutationRate) {
Route route = offspring.get(j);
int mutationPoint1 = random.nextInt(cities.size());
int mutationPoint2 = random.nextInt(cities.size());
Collections.swap(route.getCities(), mutationPoint1, mutationPoint2);
}
}
for (int j = 0; j < populationSize; j++) {
Route parent = population.get(j);
Route child = offspring.get(j);
if (calculateFitness(child) > calculateFitness(parent)) {
population.set(j, child);
}
}
}
Route bestRoute = Collections.max(population, Comparator.comparingDouble(GA::calculateFitness));
System.out.println(bestRoute);
}
```
这样,你就可以用遗传算法解决TSP问题了。
阅读全文