遗传算法:仅在基因型的一部分中进行均匀交叉

时间:2012-06-02 04:49:30

标签: java genetic-algorithm

我需要实现一个“统一交叉”遗传算子。

编辑:我意识到如果两个人都出现一个数字,那么重复(因为随机交换)是正常的。 所以我添加了这个:

            if(anyDuplicate(p0_genome,minIndex) || anyDuplicate(p1_genome,minIndex)){
                                //rollback: swap again
                                swap(p0_genome,p1_genome,i);
                            }

但它仍然会产生重复(基因的大部分时间位于minIndex位置(从循环中排除!!!)。当然我测试了anyDuplicate函数,它运行得很好

我尝试使用此代码

> Note: Individual 1 and 2 have the same length but a different number
> of valid bits.
> 
> Foe example: genotype length (of both individuals) = 10 ,
> representation as numbers from 1 to 10 without anyone repeated,the
> start delimiter is 1 and the end delimiter should be 2. Not used genes
> are = 0
> 
> individual 1(p0_genome) = {1,4,5,3,2,0,0,0,0,0}
> individual 2(p1_genome) = {1,4,6,3,8,2,0,0,0,0}

Desideres输出:

Individual 1(p0_genome): **1** <some genes ALL DIFFERENTS> **2** 0,0,0,.....
Individual 2(p1_genome): **1** <some genes ALL DIFFERENTS> **2** 0,0,0,.....

主要代码:

            int indexOfLastP0 = findLast(p0_genome,gl); // last valid bit (the one = 2) of first individual
            int indexOfLastP1 = findLast(p1_genome,gl); // last valid bit (the one = 2) of second individual

            int minIndex = Math.min(indexOfLastP0,indexOfLastP1); // last valid bit of the "smaller" of the inviduals

    // Building sons
  /* exchange bit without considering delimiters bit (1 and 2)
   and according to the smaller individual */
            int threshold = 0.60;

    for (int i=1; i<minIndex; i++) {
        if (Math.Random()>threshold) {
            swap(p0_genome,p1_genome,i);
        }
    // when exiting the loop the remaining of genes remain the same

交换代码:

    public void swap(int[] array1, int[] array2 ,int i){
        int aux=array1[i];
        if (array2[i]!=2){
        array1[i]=array2[i];
                }
        if (aux!=2){
        array2[i]=aux;
                }            

anyDuplicate()代码:

 public boolean anyDuplicate(int[] genoma,int min){
        for (int i=0;i<=min;i++){
            for (int j=0;j<=min;j++){
               if (genoma[i]==genoma[j] && i!=j){
                  return true;
               }
            }
        }
        return false;
    }        

findLast code:

    public int findLast(int[] mgenome,int genotypeLength){
        int k=1; // 1 element is not considered
        while (k<genotypeLength && mgenome[k]!=0){
            k++;
        }
        return k-1; // **I also tried returning k;**
    }

问题在于我在两个人中都获得了大量重复数字

我还尝试过“重复”(从父母到孩子的阵列复制)的“父亲”:

    // Creating sons genotypes
    int [] s0_genome = new int[gl];
    int [] s1_genome = new int[gl];
    // Building sons
          int threshold = 0.60;
    for (int i=0; i<minIndex; i++) {
        if (Math.Random()>threshold)) {
            s0_genome[i] = p1_genome[i];
            s1_genome[i] = p0_genome[i];
        }
        else {
            s0_genome[i] = p0_genome[i];
            s1_genome[i] = p1_genome[i];
        }
             for (int i=minIndex; i<10; i++) {
               // copy what's left
            s0_genome[i] = p0_genome[i];
            s1_genome[i] = p1_genome[i];
        }

我做错了吗?谢谢你的提示!

1 个答案:

答案 0 :(得分:0)

好的,所以你尝试交换一次,如果得到的基因组中的任何一个包含重复值,你再次尝试交换。如果第二次尝试后仍有重复,则放弃。这样效率不高,基因组越长,就越不可能发挥作用。

解决方案A :如果交换的值不在目标基因组中,您可以尝试仅进行交换。那会产生这样的交换函数:

public void swap(int[] array1, int[] array2 ,int i){
    int aux=array1[i];
    if (array2[i]!=2 && !Arrays.asList(array1).contains(array2[i]){
    array1[i]=array2[i];
            }
    if (aux!=2 && !Arrays.asList(array2).contains(array1[i]){
    array2[i]=aux;
            }

这个问题是它可能会完全锁定在不同位置包含相同值的基因组。对于您的示例,使用

g1 = {1, 4, 8, 9, 3, 2, 0, 0}
g2 = { 1, 3, 9, 8, 4, 2, 0, 0}

根本没有有效的交换,交叉将返回原始基因组。

解决方案B :如果要交换的值已经存在于目标基因组中,请在目标基因组中找到该基因的索引,并交换它。这可能会级联以在基因组的大部分区域中进行交换,当然,当i = j时,它不应该发生。

取决于所需的行为。对于上面的示例基因组,成功的交叉是什么样的?