旅行商问题java 代码
时间: 2024-05-19 07:09:42 浏览: 92
旅行商问题(TSP)是一个著名的NP难问题,是求解所有城市之间的最短路径的问题。其实现方式有很多种,这里简单介绍一下基于遗传算法(GA)的实现方式。
下面是一个简单的Java代码示例,使用遗传算法求解旅行商问题:
```
import java.util.Arrays;
import java.util.Random;
public class TSPGA {
private int[][] distance; // 城市之间的距离
private int cityNum; // 城市数量
private int populationSize; // 种群规模
private int maxGenerations; // 最大进化代数
private double crossoverProbability; // 交叉概率
private double mutationProbability; // 变异概率
private int elitismCount; // 精英个数
private int[] bestTour; // 最优路径
private int bestTourLength; // 最优路径长度
public TSPGA(int[][] distance, int populationSize, int maxGenerations,
double crossoverProbability, double mutationProbability,
int elitismCount) {
this.distance = distance;
this.cityNum = distance.length;
this.populationSize = populationSize;
this.maxGenerations = maxGenerations;
this.crossoverProbability = crossoverProbability;
this.mutationProbability = mutationProbability;
this.elitismCount = elitismCount;
}
public void solve() {
Population population = new Population(populationSize, cityNum);
population.initialize();
for (int i = 0; i < maxGenerations; i++) {
population = evolvePopulation(population);
}
bestTour = population.getFittest().getTour();
bestTourLength = population.getFittest().getTourLength();
}
private Population evolvePopulation(Population population) {
Population newPopulation = new Population(populationSize, cityNum);
for (int i = 0; i < elitismCount; i++) {
newPopulation.setIndividual(i, population.getFittest());
}
for (int i = elitismCount; i < populationSize; i++) {
Individual parent1 = selection(population);
Individual parent2 = selection(population);
Individual child = crossover(parent1, parent2);
mutate(child);
newPopulation.setIndividual(i, child);
}
return newPopulation;
}
private Individual selection(Population population) {
Random rand = new Random();
Population tournament = new Population(elitismCount, cityNum);
for (int i = 0; i < elitismCount; i++) {
tournament.setIndividual(i, population.getIndividual(rand.nextInt(populationSize)));
}
return tournament.getFittest();
}
private Individual crossover(Individual parent1, Individual parent2) {
Individual child = new Individual(cityNum);
int startPos = (int) (Math.random() * parent1.getTourLength());
int endPos = (int) (Math.random() * parent1.getTourLength());
for (int i = 0; i < child.getTourLength(); i++) {
if (startPos < endPos && i > startPos && i < endPos) {
child.setCity(i, parent1.getCity(i));
} else if (startPos > endPos) {
if (!(i < startPos && i > endPos)) {
child.setCity(i, parent1.getCity(i));
}
}
}
for (int i = 0; i < parent2.getTourLength(); i++) {
if (!child.containsCity(parent2.getCity(i))) {
for (int j = 0; j < child.getTourLength(); j++) {
if (child.getCity(j) == -1) {
child.setCity(j, parent2.getCity(i));
break;
}
}
}
}
return child;
}
private void mutate(Individual individual) {
Random rand = new Random();
for (int i = 0; i < individual.getTourLength(); i++) {
if (Math.random() < mutationProbability) {
int j = rand.nextInt(individual.getTourLength());
int temp = individual.getCity(i);
individual.setCity(i, individual.getCity(j));
individual.setCity(j, temp);
}
}
}
public int[] getBestTour() {
return bestTour;
}
public int getBestTourLength() {
return bestTourLength;
}
public static void main(String[] args) {
int[][] distance = {{0, 2, 9, 10}, {1, 0, 6, 4}, {15, 7, 0, 8}, {6, 3, 12, 0}};
TSPGA tspGA = new TSPGA(distance, 100, 1000, 0.9, 0.1, 2);
tspGA.solve();
System.out.println("Best tour length: " + tspGA.getBestTourLength());
System.out.println("Best tour: " + Arrays.toString(tspGA.getBestTour()));
}
}
class Population {
private Individual[] individuals;
public Population(int populationSize, int tourSize) {
individuals = new Individual[populationSize];
for (int i = 0; i < populationSize; i++) {
individuals[i] = new Individual(tourSize);
}
}
public void initialize() {
for (int i = 0; i < individuals.length; i++) {
individuals[i].generateIndividual();
}
}
public Individual getFittest() {
Individual fittest = individuals;
for (int i = 1; i < individuals.length; i++) {
if (fittest.getFitness() <= individuals[i].getFitness()) {
fittest = individuals[i];
}
}
return fittest;
}
public void setIndividual(int index, Individual individual) {
individuals[index] = individual;
}
public Individual getIndividual(int index) {
return individuals[index];
}
}
class Individual {
private int[] tour;
private int tourLength;
public Individual(int tourSize) {
tour = new int[tourSize];
tourLength = tourSize;
}
public void generateIndividual() {
for (int i = 0; i < tourLength; i++) {
tour[i] = i;
}
shuffle();
}
private void shuffle() {
Random rand = new Random();
for (int i = tourLength - 1; i > 0; i--) {
int j = rand.nextInt(i + 1);
int temp = tour[i];
tour[i] = tour[j];
tour[j] = temp;
}
}
public int getFitness() {
int fitness = 0;
for (int i = 0; i < tourLength - 1; i++) {
fitness += distance[tour[i]][tour[i + 1]];
}
fitness += distance[tour[tourLength - 1]][tour];
return fitness;
}
public boolean containsCity(int city) {
for (int i = 0; i < tourLength; i++) {
if (tour[i] == city) {
return true;
}
}
return false;
}
public void setCity(int index, int city) {
tour[index] = city;
}
public int getCity(int index) {
return tour[index];
}
public int getTourLength() {
return tourLength;
}
public int[] getTour() {
return tour;
}
}
```
请问你还有什么其他问题吗?
阅读全文