java遗传算法d代码实现
时间: 2024-06-05 12:05:45 浏览: 99
遗传算法是一种通过模拟自然选择和基因遗传机制来搜索最优解的算法。Java语言也提供了很多相关的类库和工具来支持遗传算法的实现。
以下是一个简单的Java遗传算法的代码实现示例,实现了求解函数y=x^2在区间[-5,5]上的最大值:
```java
import java.util.Random;
public class GeneticAlgorithm {
private static final double MUTATION_RATE = 0.01; // 变异率
private static final int POPULATION_SIZE = 50; // 种群大小
private static final int CHROMOSOME_LENGTH = 16; // 染色体长度
private static final int MAX_GENERATIONS = 100; // 最大迭代次数
private static final double X_MIN = -5.0; // 自变量范围下限
private static final double X_MAX = 5.0; // 自变量范围上限
private static final double Y_MIN = -25.0; // 因变量范围下限
private static final double Y_MAX = 25.0; // 因变量范围上限
private static final Random random = new Random();
private static int[] createChromosome() {
int[] chromosome = new int[CHROMOSOME_LENGTH];
for (int i = 0; i < CHROMOSOME_LENGTH; i++) {
chromosome[i] = random.nextInt(2);
}
return chromosome;
}
private static double decodeChromosome(int[] chromosome) {
double x = X_MIN + (X_MAX - X_MIN) * binaryToDouble(chromosome);
return x * x;
}
private static double binaryToDouble(int[] binary) {
double d = 0.0;
for (int i = 0; i < binary.length; i++) {
d += binary[i] * Math.pow(2, -(i + 1));
}
return d;
}
private static int[][] createPopulation() {
int[][] population = new int[POPULATION_SIZE][CHROMOSOME_LENGTH];
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i] = createChromosome();
}
return population;
}
private static double[] evaluatePopulation(int[][] population) {
double[] fitnessValues = new double[POPULATION_SIZE];
for (int i = 0; i < POPULATION_SIZE; i++) {
fitnessValues[i] = decodeChromosome(population[i]);
}
return fitnessValues;
}
private static int[][] selectParents(int[][] population, double[] fitnessValues) {
int[][] parents = new int[CHROMOSOME_LENGTH];
int parentIndex1 = random.nextInt(POPULATION_SIZE);
int parentIndex2 = random.nextInt(POPULATION_SIZE);
while (parentIndex2 == parentIndex1) {
parentIndex2 = random.nextInt(POPULATION_SIZE);
}
if (fitnessValues[parentIndex1] > fitnessValues[parentIndex2]) {
parents = population[parentIndex1];
} else {
parents = population[parentIndex2];
}
parentIndex1 = random.nextInt(POPULATION_SIZE);
parentIndex2 = random.nextInt(POPULATION_SIZE);
while (parentIndex2 == parentIndex1) {
parentIndex2 = random.nextInt(POPULATION_SIZE);
}
if (fitnessValues[parentIndex1] > fitnessValues[parentIndex2]) {
parents = population[parentIndex1];
} else {
parents = population[parentIndex2];
}
return parents;
}
private static int[] crossover(int[] parent1, int[] parent2) {
int[] child = new int[CHROMOSOME_LENGTH];
int crossoverPoint = random.nextInt(CHROMOSOME_LENGTH - 1) + 1;
for (int i = 0; i < crossoverPoint; i++) {
child[i] = parent1[i];
}
for (int i = crossoverPoint; i < CHROMOSOME_LENGTH; i++) {
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;
}
}
}
private static void evolve(int[][] population, double[] fitnessValues) {
int[][] newPopulation = new int[POPULATION_SIZE][CHROMOSOME_LENGTH];
for (int i = 0; i < POPULATION_SIZE / 2; i++) {
int[][] parents = selectParents(population, fitnessValues);
int[] child1 = crossover(parents, parents);
int[] child2 = crossover(parents, parents);
mutate(child1);
mutate(child2);
newPopulation[i * 2] = child1;
newPopulation[i * 2 + 1] = child2;
}
System.arraycopy(newPopulation, 0, population, 0, POPULATION_SIZE);
}
public static void main(String[] args) {
int[][] population = createPopulation();
for (int generation = 0; generation < MAX_GENERATIONS; generation++) {
double[] fitnessValues = evaluatePopulation(population);
double maxFitnessValue = Double.NEGATIVE_INFINITY;
int maxFitnessIndex = -1;
for (int i = 0; i < POPULATION_SIZE; i++) {
if (fitnessValues[i] > maxFitnessValue) {
maxFitnessValue = fitnessValues[i];
maxFitnessIndex = i;
}
}
System.out.println("Generation " + generation + ": max fitness value=" + maxFitnessValue);
if (maxFitnessValue >= Y_MAX) {
System.out.println("Solution found in generation " + generation + ": x=" + binaryToDouble(population[maxFitnessIndex]) + ", y=" + maxFitnessValue);
break;
}
evolve(population, fitnessValues);
}
}
}
```
阅读全文