用java写一个遗传算法解决最短路径问题
时间: 2024-01-06 16:03:03 浏览: 73
用遗传算法求解最短路径问题
3星 · 编辑精心推荐
好的,以下是一个使用Java实现遗传算法解决最短路径问题的基本代码:
```java
import java.util.*;
public class GeneticAlgorithm {
private int populationSize;
private double mutationRate;
private double crossoverRate;
private int elitismCount;
private int tournamentSize;
public GeneticAlgorithm(int populationSize, double mutationRate, double crossoverRate, int elitismCount, int tournamentSize) {
this.populationSize = populationSize;
this.mutationRate = mutationRate;
this.crossoverRate = crossoverRate;
this.elitismCount = elitismCount;
this.tournamentSize = tournamentSize;
}
public Population evolve(Population population, DistanceMatrix distanceMatrix) {
Population newPopulation = new Population(this.populationSize);
// Keep the best individuals from the current population
for (int i = 0; i < this.elitismCount; i++) {
newPopulation.setIndividual(i, population.getFittestIndividual());
}
// Crossover
for (int i = this.elitismCount; i < this.populationSize; i++) {
Individual parent1 = tournamentSelection(population, distanceMatrix);
Individual parent2 = tournamentSelection(population, distanceMatrix);
Individual child = crossover(parent1, parent2);
newPopulation.setIndividual(i, child);
}
// Mutation
for (int i = this.elitismCount; i < this.populationSize; i++) {
mutate(newPopulation.getIndividual(i), distanceMatrix);
}
return newPopulation;
}
private Individual tournamentSelection(Population population, DistanceMatrix distanceMatrix) {
Population tournament = new Population(this.tournamentSize);
for (int i = 0; i < this.tournamentSize; i++) {
int randomIndex = (int) (Math.random() * population.getPopulationSize());
tournament.setIndividual(i, population.getIndividual(randomIndex));
}
return tournament.getFittestIndividual();
}
private Individual crossover(Individual parent1, Individual parent2) {
Individual child = new Individual(parent1.getChromosomeSize());
int startPos = (int) (Math.random() * parent1.getChromosomeSize());
int endPos = (int) (Math.random() * parent1.getChromosomeSize());
for (int i = 0; i < child.getChromosomeSize(); i++) {
if (startPos < endPos && i > startPos && i < endPos) {
child.setGene(i, parent1.getGene(i));
} else if (startPos > endPos) {
if (!(i < startPos && i > endPos)) {
child.setGene(i, parent1.getGene(i));
}
}
}
for (int i = 0; i < parent2.getChromosomeSize(); i++) {
if (!child.containsGene(parent2.getGene(i))) {
for (int j = 0; j < child.getChromosomeSize(); j++) {
if (child.getGene(j) == null) {
child.setGene(j, parent2.getGene(i));
break;
}
}
}
}
return child;
}
private void mutate(Individual individual, DistanceMatrix distanceMatrix) {
for (int i = 0; i < individual.getChromosomeSize(); i++) {
if (Math.random() < this.mutationRate) {
int j = (int) (Math.random() * individual.getChromosomeSize());
int gene1 = individual.getGene(i);
int gene2 = individual.getGene(j);
individual.setGene(i, gene2);
individual.setGene(j, gene1);
}
}
}
}
```
其中,DistanceMatrix是一个距离矩阵类,用于存储各个节点之间的距离。Population是一个个体群体类,包含多个个体(即路径)。Individual是一个个体类,表示一条路径。Chromosome是一个染色体类,存储路径上节点的顺序。Gene是一个基因类,表示一个节点。
下面是一个简单的主函数,用于测试遗传算法的效果:
```java
public static void main(String[] args) {
int numberOfCities = 10;
DistanceMatrix distanceMatrix = new DistanceMatrix(numberOfCities);
// Generate initial population
Population population = new Population(50, true, distanceMatrix);
// Create genetic algorithm
GeneticAlgorithm geneticAlgorithm = new GeneticAlgorithm(50, 0.01, 0.95, 2, 5);
// Evolve population
for (int i = 0; i < 100; i++) {
population = geneticAlgorithm.evolve(population, distanceMatrix);
}
// Print final solution
System.out.println("Final solution: " + population.getFittestIndividual().toString());
System.out.println("Distance: " + population.getFittestIndividual().getDistance(distanceMatrix));
}
```
以上代码只是一个简单的示例,具体实现需要根据具体问题进行调整和优化。
阅读全文