自然选择的过程始于从种群中选择最适应的个体。 它们产生的后代继承了父母的特征,并将被添加到下一代。 如果父母有更好的身体素质,他们的后代就会比父母更好,有更好的生存机会。 这个过程不断迭代,最终会找到最适应个体的一代。
下面给出了 Java 中遗传算法的示例实现。给定一组 5 个基因,每个基因可以保存二进制值 0 和 1 之一。适应度值计算为基因组中存在的 1 的数量。如果有五个 1,那么它具有最大适应度。如果没有 1,则它具有最小适应度。该遗传算法试图最大化适应度函数,以提供由最适应个体(即具有五个 1 的个体)组成的种群。注意:在这个例子中,经过交叉和变异后,最不适应的个体被新的最适应的后代所取代。import java.util.Random;
//Main class
public class SimGA {
Population population = new Population();
int generationCount = 0;
public static void main(String[] args) {
Random rn = new Random();
SimpleDemoGA demo = new SimpleDemoGA();
demo.population.initializePopulation(10);
demo.population.calculateFitness();
System.out.println("Generation: " + demo.generationCount + " Fittest: " + demo.population.fittest);
while (demo.population.fittest < 5) {
++demo.generationCount;
demo.selection();
demo.crossover();
if (rn.nextInt()%7 < 5) {
demo.mutation();
}
demo.addFittestOffspring();
demo.population.calculateFitness();
System.out.println("Generation: " + demo.generationCount + " Fittest: " + demo.population.fittest);
}
System.out.println("\nSolution found in generation " + demo.generationCount);
System.out.print("Genes: ");
for (int i = 0; i < 5; i++) {
System.out.print(demo.population.getFittest().genes[i]);
}
System.out.println("");
}
void selection() {
fittest = population.getFittest();
secondFittest = population.getSecondFittest();
}
void crossover() {
Random rn = new Random();
//Select a random crossover point
int crossOverPoint = rn.nextInt(population.individuals[0].geneLength);
//Swap values among parents
for (int i = 0; i < crossOverPoint; i++) {
int temp = fittest.genes[i];
fittest.genes[i] = secondFittest.genes[i];
secondFittest.genes[i] = temp;
}
}
void mutation() {
Random rn = new Random();
int mutationPoint = rn.nextInt(population.individuals[0].geneLength);
//Flip values at the mutation point
if (fittest.genes[mutationPoint] == 0) {
fittest.genes[mutationPoint] = 1;
} else {
fittest.genes[mutationPoint] = 0;
}
mutationPoint = rn.nextInt(population.individuals[0].geneLength);
if (secondFittest.genes[mutationPoint] == 0) {
secondFittest.genes[mutationPoint] = 1;
} else {
secondFittest.genes[mutationPoint] = 0;
}
}
Individual getFittestOffspring() {
if (fittest.fitness > secondFittest.fitness) {
return fittest;
}
return secondFittest;
}
void addFittestOffspring() {
fittest.calcFitness();
secondFittest.calcFitness();
int leastFittestIndex = population.getLeastFittestIndex();
population.individuals[leastFittestIndex] = getFittestOffspring();
}
}
class Individual {
int fitness = 0;
int[] genes = new int[5];
int geneLength = 5;
public Individual() {
Random rn = new Random();
for (int i = 0; i < genes.length; i++) {
genes[i] = Math.abs(rn.nextInt() % 2);
}
fitness = 0;
}
public void calcFitness() {
fitness = 0;
for (int i = 0; i < 5; i++) {
if (genes[i] == 1) {
++fitness;
}
}
}
}
class Population {
int popSize = 10;
Individual[] individuals = new Individual[10];
int fittest = 0;
public void initializePopulation(int size) {
for (int i = 0; i < individuals.length; i++) {
individuals[i] = new Individual();
}
}
public Individual getFittest() {
int maxFit = Integer.MIN_VALUE;
int maxFitIndex = 0;
for (int i = 0; i < individuals.length; i++) {
if (maxFit <= individuals[i].fitness) {
maxFit = individuals[i].fitness;
maxFitIndex = i;
}
}
fittest = individuals[maxFitIndex].fitness;
return individuals[maxFitIndex];
}
public Individual getSecondFittest() {
int maxFit1 = 0;
int maxFit2 = 0;
for (int i = 0; i < individuals.length; i++) {
if (individuals[i].fitness > individuals[maxFit1].fitness) {
maxFit2 = maxFit1;
maxFit1 = i;
} else if (individuals[i].fitness > individuals[maxFit2].fitness) {
maxFit2 = i;
}
}
return individuals[maxFit2];
}
public int getLeastFittestIndex() {
int minFitVal = Integer.MAX_VALUE;
int minFitIndex = 0;
for (int i = 0; i < individuals.length; i++) {
if (minFitVal >= individuals[i].fitness) {
minFitVal = individuals[i].fitness;
minFitIndex = i;
}
}
return minFitIndex;
}
public void calculateFitness() {
for (int i = 0; i < individuals.length; i++) {
individuals[i].calcFitness();
}
getFittest();
}
}