用java写一段遗传算法
时间: 2023-06-29 16:01:57 浏览: 52
以下是一个简单的遗传算法的 Java 实现,用于求解函数 f(x) = x^2 在 [-5, 5] 区间内的最大值:
```java
import java.util.Random;
public class GeneticAlgorithm {
private static final int POPULATION_SIZE = 100;
private static final double MUTATION_RATE = 0.01;
private static final int TOURNAMENT_SIZE = 5;
private static final int MAX_GENERATIONS = 1000;
private static final Random random = new Random();
private static double fitnessFunction(double x) {
return x * x;
}
private static double randomDouble(double min, double max) {
return min + (max - min) * random.nextDouble();
}
private static int randomInt(int min, int max) {
return min + random.nextInt(max - min);
}
private static double[] createIndividual() {
double[] individual = new double[1];
individual[0] = randomDouble(-5.0, 5.0);
return individual;
}
private static double[][] createPopulation() {
double[][] population = new double[POPULATION_SIZE][1];
for (int i = 0; i < POPULATION_SIZE; i++) {
population[i] = createIndividual();
}
return population;
}
private static double evaluateIndividual(double[] individual) {
return fitnessFunction(individual[0]);
}
private static double[][] evaluatePopulation(double[][] population) {
double[][] evaluatedPopulation = new double[POPULATION_SIZE][2];
for (int i = 0; i < POPULATION_SIZE; i++) {
evaluatedPopulation[i][0] = evaluateIndividual(population[i]);
evaluatedPopulation[i][1] = i;
}
return evaluatedPopulation;
}
private static double[][] selectParents(double[][] population) {
double[][] parents = new double[2][1];
for (int i = 0; i < 2; i++) {
double[][] tournament = new double[TOURNAMENT_SIZE][2];
for (int j = 0; j < TOURNAMENT_SIZE; j++) {
tournament[j] = population[randomInt(0, POPULATION_SIZE)];
}
parents[i] = tournament[0][0] > tournament[1][0] ? tournament[0] : tournament[1];
}
return parents;
}
private static double[][] crossover(double[][] parents) {
double[][] child = new double[1][1];
child[0][0] = (parents[0][0] + parents[1][0]) / 2.0;
return child;
}
private static void mutate(double[][] individual) {
if (random.nextDouble() < MUTATION_RATE) {
individual[0][0] += randomDouble(-0.1, 0.1);
}
}
private static double[][] evolve(double[][] population) {
double[][] newPopulation = new double[POPULATION_SIZE][1];
double[][] evaluatedPopulation = evaluatePopulation(population);
for (int i = 0; i < POPULATION_SIZE; i++) {
double[][] parents = selectParents(evaluatedPopulation);
double[][] child = crossover(parents);
mutate(child);
newPopulation[i] = child[0];
}
return newPopulation;
}
public static void main(String[] args) {
double[][] population = createPopulation();
for (int generation = 0; generation < MAX_GENERATIONS; generation++) {
population = evolve(population);
}
double[][] evaluatedPopulation = evaluatePopulation(population);
double[] bestIndividual = population[(int) evaluatedPopulation[0][1]];
System.out.println("Best individual: " + bestIndividual[0]);
}
}
```
在此实现中,每个个体由一个 double 数组表示,数组长度为 1,表示 x 的值。初始种群由 createPopulation() 方法生成,每个个体的 x 值在 [-5, 5] 区间内随机生成。
每个个体的适应度由 evaluateIndividual() 方法计算,即 f(x) = x^2。
选择操作采用锦标赛选择策略,即从种群中随机选择 TOURNAMENT_SIZE 个个体,然后选择其中适应度最高的个体作为父母。
交叉操作采用简单的平均值交叉,即将两个父母的 x 值取平均值作为孩子的 x 值。
变异操作在每个孩子的 x 值上进行,以一定概率 MUTATION_RATE 随机加上一个小的偏移量。
最终,遗传算法的结果为最终种群中适应度最高的个体的 x 值。