使用java实现遗传算法
时间: 2023-10-29 12:08:00 浏览: 73
以下是一个简单的遗传算法实现,它用Java编写:
```
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class GeneticAlgorithm {
private Random randomGenerator;
private int populationSize;
private int geneLength;
private ArrayList<String> population;
public GeneticAlgorithm(int populationSize, int geneLength) {
this.randomGenerator = new Random();
this.populationSize = populationSize;
this.geneLength = geneLength;
this.population = new ArrayList<String>();
// create initial population
for (int i = 0; i < populationSize; i++) {
String individual = generateRandomIndividual();
population.add(individual);
}
}
public String evolve(int generations) {
for (int i = 0; i < generations; i++) {
ArrayList<String> newPopulation = new ArrayList<String>();
// elitism: keep the best individual from previous generation
String bestIndividual = getBestIndividual();
newPopulation.add(bestIndividual);
// create new individuals through crossover
while (newPopulation.size() < populationSize) {
String parent1 = selectIndividual();
String parent2 = selectIndividual();
String child = crossover(parent1, parent2);
mutate(child);
newPopulation.add(child);
}
// replace the old population with the new population
population = newPopulation;
}
return getBestIndividual();
}
private String generateRandomIndividual() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < geneLength; i++) {
sb.append(randomGenerator.nextInt(2));
}
return sb.toString();
}
private String selectIndividual() {
// roulette wheel selection
double totalFitness = 0;
for (String individual : population) {
totalFitness += getFitness(individual);
}
double randomFitness = randomGenerator.nextDouble() * totalFitness;
double currentFitness = 0;
for (String individual : population) {
currentFitness += getFitness(individual);
if (currentFitness > randomFitness) {
return individual;
}
}
return population.get(randomGenerator.nextInt(populationSize));
}
private String crossover(String parent1, String parent2) {
// single point crossover
int crossoverPoint = randomGenerator.nextInt(geneLength);
String child = parent1.substring(0, crossoverPoint) + parent2.substring(crossoverPoint);
return child;
}
private void mutate(String individual) {
// flip one bit at random
int mutationPoint = randomGenerator.nextInt(geneLength);
char[] genes = individual.toCharArray();
genes[mutationPoint] = (genes[mutationPoint] == '0') ? '1' : '0';
individual = new String(genes);
}
private double getFitness(String individual) {
// example fitness function: count the number of ones in the individual
double fitness = 0;
for (char gene : individual.toCharArray()) {
if (gene == '1') {
fitness++;
}
}
return fitness;
}
private String getBestIndividual() {
// return the individual with the highest fitness
String bestIndividual = population.get(0);
double bestFitness = getFitness(bestIndividual);
for (String individual : population) {
double fitness = getFitness(individual);
if (fitness > bestFitness) {
bestIndividual = individual;
bestFitness = fitness;
}
}
return bestIndividual;
}
public static void main(String[] args) {
GeneticAlgorithm ga = new GeneticAlgorithm(50, 20);
String bestIndividual = ga.evolve(100);
System.out.println("Best individual: " + bestIndividual);
}
}
```
这是一个基本的遗传算法,它通过随机生成个体来创建初始种群,然后进行多代进化以产生更好的个体。在每一代中,它首先保存上一代中的最佳个体,然后通过交叉和突变创建新个体,并使用轮盘赌选择父代。它还包括一个简单的适应度函数,它计算个体中1的数量。
阅读全文