需要帮助Java中的遗传算法单点交叉机制

时间:2016-10-06 22:56:41

标签: java arrays genetic-algorithm

我一直在使用Java实现一个简单的遗传算法(GA)。我的GA的步骤基本上是二进制编码,锦标赛选择,单点交叉和逐位变异。群体中的每个个体由二元基因和适合度值组成的类别表示。

public class Individual {
    int gene[];
    int fitness;

    public Individual(int n){
        this.gene = new int[n];
    }
}

下面的代码不包括按位变异部分,因为我在GA的单点交叉部分遇到了问题。我实现单点交叉算法的方法是随机为两个连续的单个数组元素找到一个点,然后交换它们的尾部。然后对每对个体重复尾部交换。我还创建了printGenome()方法来打印出要比较的所有数组,交叉过程后得到的数组没有正确交换。我已经单独测试了我的单点交叉算法,它可以工作。但是,当我尝试在下面的代码中运行它时,交叉根本不起作用。我可以知道是因为锦标赛选择算法中有什么问题吗?还是别的(愚蠢的错误)?我一直在努力,仍然无法查明错误。

如能提供任何帮助和信息,我将不胜感激! :)

public class GeneticAlgorithm {

    public static void main(String[] args) {
        int p = 10;
        int n = 10;
        Individual population[];

        //create new population
        population = new Individual[p];

        for (int i = 0; i < p; i++) {
            population[i] = new Individual(n);
        }

        //fills individual's gene with binary randomly
        for (int i = 0; i < p; i++) {
            for (int j = 0; j < n; j++) {
                population[i].gene[j] = (Math.random() < 0.5) ? 0 : 1;
            }
            population[i].fitness = 0;
        }

        //evaluate each individual
        for (int i = 0; i < p; i++) {
            for (int j = 0; j < n; j++) {
                if (population[i].gene[j] == 1) {
                    population[i].fitness++;
                }
            }
        }

        //total fitness check
        System.out.println("Total fitness check #1 before tournament selection: " + getTotalFitness(population, p));
        System.out.println("Mean fitness check #1 before tournament selection: " + getMeanFitness(population, p));
        System.out.println("");

        //tournament selection
        Individual offspring[] = new Individual[p];

        for (int i = 0; i < p; i++) {
            offspring[i] = new Individual(n);
        }

        int parent1, parent2;
        Random rand = new Random();
        for (int i = 0; i < p; i++) {
            parent1 = rand.nextInt(p); //randomly choose parent
            parent2 = rand.nextInt(p); //randomly choose parent

            if (population[parent1].fitness >= population[parent2].fitness) {
                offspring[i] = population[parent1];
            } else {
                offspring[i] = population[parent2];
            }
        }

        //total fitness check
        System.out.println("Total fitness check #2 after tournament selection: " + getTotalFitness(offspring, p));
        System.out.println("Mean fitness check #2 after tournament selection: " + getMeanFitness(offspring, p));
        System.out.println("");

        //genome check
        System.out.println("Before Crossover: ");
        printGenome(offspring, p, n);

        //crossover
        for (int i = 0; i < p; i = i + 2) {
            int splitPoint = rand.nextInt(n);
            for (int j = splitPoint; j < n; j++) {
                int temp = offspring[i].gene[j];
                offspring[i].gene[j] = offspring[i + 1].gene[j];
                offspring[i + 1].gene[j] = temp;
            }
        }

        //genome check
        System.out.println("After Crossover:");
        printGenome(offspring, p, n);

        //evaluate each individual by counting the number of 1s after crossover
        for (int i = 0; i < p; i++) {
            offspring[i].fitness = 0;
            for (int j = 0; j < n; j++) {
                if (offspring[i].gene[j] == 1) {
                    offspring[i].fitness++;
                }
            }
        }

        //total fitness check
        System.out.println("Total fitness check #3 after crossover: " + getTotalFitness(offspring, p));
        System.out.println("Mean fitness check #3 after crossover: " + getMeanFitness(offspring, p));
    }

    public static void printGenome(Individual pop[], int p, int n) {
        for (int i = 0; i < p; i++) {
            for (int j = 0; j < n; j++) {
                System.out.print(pop[i].gene[j]);
            }
            System.out.println("");
        }
    }

    public static int getTotalFitness(Individual pop[], int p) {
        int totalFitness = 0;
        for (int i = 0; i < p; i++) {
            totalFitness = totalFitness + pop[i].fitness;
        }
        return totalFitness;
    }

    public static double getMeanFitness(Individual pop[], int p) {
        double meanFitness = getTotalFitness(pop, p) / (double) p;
        return meanFitness;
    }

}

1 个答案:

答案 0 :(得分:0)

问题是,在您的选择中,当您说:

时,您(很可能)会复制个人。
  

后代[i] =人口[parent1]

您实际上在后代[i]中存储了对[parent1]的引用。因此,您的后代数组可以多次包含相同的引用,因此同一个对象将与多个合作伙伴多次参与交叉。

作为解决方案,您可以存储克隆而不是对同一对象的引用。在个人添加:

    public Individual clone(){
        Individual clone = new Individual(gene.length);
        clone.gene = gene.clone();
        return clone;
    }

在您的选择中(注意添加的.clone()):

    for (int i = 0; i < p; i++) {
        parent1 = rand.nextInt(p); //randomly choose parent
        parent2 = rand.nextInt(p); //randomly choose parent

        if (population[parent1].fitness >= population[parent2].fitness) {
            offspring[i] = population[parent1].clone();
        } else {
            offspring[i] = population[parent2].clone();
        }
    }

这样,即使基因组是相同的,后代中的每个元素都是不同的对象。

解决了Java部分问题。关于GA理论,我希望有些事情,例如你的适应度量只是占位符,对吗?