请用java写一个遗传算法
时间: 2023-06-29 21:11:35 浏览: 76
以下是一个简单的遗传算法的 Java 实现,用于求解函数 f(x) = x^2,其中 x 是一个二进制数,位数为 8 位。
```java
import java.util.Random;
public class GeneticAlgorithm {
private static final int POPULATION_SIZE = 10;
private static final int CHROMOSOME_LENGTH = 8;
private static final int MAX_GENERATIONS = 50;
private static final double MUTATION_RATE = 0.25;
private static final Random random = new Random();
private static int[][] population = new int[POPULATION_SIZE][CHROMOSOME_LENGTH];
private static int[] fitness = new int[POPULATION_SIZE];
public static void main(String[] args) {
initializePopulation();
int generation = 0;
while (generation < MAX_GENERATIONS) {
evaluateFitness();
printPopulation(generation);
int[][] newPopulation = new int[POPULATION_SIZE][CHROMOSOME_LENGTH];
for (int i = 0; i < POPULATION_SIZE; i++) {
int[] parent1 = selectParent();
int[] parent2 = selectParent();
int[] child = crossover(parent1, parent2);
mutate(child);
newPopulation[i] = child;
}
population = newPopulation;
generation++;
}
evaluateFitness();
int bestIndex = findBestIndividual();
System.out.println("Best individual: " + binaryToString(population[bestIndex]));
}
private static void initializePopulation() {
for (int i = 0; i < POPULATION_SIZE; i++) {
for (int j = 0; j < CHROMOSOME_LENGTH; j++) {
population[i][j] = random.nextInt(2);
}
}
}
private static int evaluateIndividual(int[] chromosome) {
int x = binaryToDecimal(chromosome);
return x * x;
}
private static void evaluateFitness() {
for (int i = 0; i < POPULATION_SIZE; i++) {
fitness[i] = evaluateIndividual(population[i]);
}
}
private static int[] selectParent() {
int totalFitness = 0;
for (int i = 0; i < POPULATION_SIZE; i++) {
totalFitness += fitness[i];
}
int randomFitness = random.nextInt(totalFitness) + 1;
int sumFitness = 0;
for (int i = 0; i < POPULATION_SIZE; i++) {
sumFitness += fitness[i];
if (sumFitness >= randomFitness) {
return population[i];
}
}
return population[POPULATION_SIZE - 1];
}
private static int[] crossover(int[] parent1, int[] parent2) {
int crossoverPoint = random.nextInt(CHROMOSOME_LENGTH);
int[] child = new int[CHROMOSOME_LENGTH];
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
if (i < crossoverPoint) {
child[i] = parent1[i];
} else {
child[i] = parent2[i];
}
}
return child;
}
private static void mutate(int[] chromosome) {
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
if (random.nextDouble() < MUTATION_RATE) {
chromosome[i] = 1 - chromosome[i];
}
}
}
private static int binaryToDecimal(int[] binary) {
int decimal = 0;
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
decimal += binary[i] * Math.pow(2, CHROMOSOME_LENGTH - i - 1);
}
return decimal;
}
private static String binaryToString(int[] binary) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
sb.append(binary[i]);
}
return sb.toString();
}
private static int findBestIndividual() {
int bestIndex = 0;
int bestFitness = fitness[0];
for (int i = 1; i < POPULATION_SIZE; i++) {
if (fitness[i] > bestFitness) {
bestIndex = i;
bestFitness = fitness[i];
}
}
return bestIndex;
}
private static void printPopulation(int generation) {
System.out.println("Generation " + generation + ":");
for (int i = 0; i < POPULATION_SIZE; i++) {
System.out.println(binaryToString(population[i]) + " -> " + fitness[i]);
}
System.out.println();
}
}
```
该实现中,initializePopulation() 方法初始化了种群,evaluateIndividual() 方法计算个体的适应度,evaluateFitness() 方法计算整个种群的适应度,selectParent() 方法选择父亲个体,crossover() 方法进行交叉操作,mutate() 方法进行变异操作,binaryToDecimal() 方法将二进制数转换为十进制数,binaryToString() 方法将二进制数转换为字符串,findBestIndividual() 方法查找最优个体,printPopulation() 方法输出当前种群的状况。
在 main() 方法中,遗传算法的主要流程是一个循环,每一次循环代表一代,循环内部进行了种群的评估、选择、交叉和变异操作,最后输出最优个体。
阅读全文