为什么我的基因突变代码没有返回整个基因? Java的

时间:2013-04-18 08:54:24

标签: java arraylist genetic-programming

http://i.imgur.com/PWVruQ0.png

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package parentmutation;

import java.util.ArrayList;
import java.util.Random;

/**
 *
 * @author Renter
 */
public class Parentmutation {

    /**
     * @param args the command line arguments
     */
    static int population = 50;
    static int geneSize = 25;
    public static void main(String[] args) {

        char[] parenta = new char[geneSize];                     //Create parents to pull genes from
        for (int x = 0; x < geneSize; x++) {
            parenta[x] = 'A';
            System.out.print("-");
        }
        System.out.println();
        char[] parentb = new char[geneSize];
        for (int x = 0; x < geneSize; x++) {
            parentb[x] = 'B';
        }

        char[][] people = new char[population][];                //How many children to make
        Parentmutation p = new Parentmutation();
        for (int x = 0; x < population; x++) {
            people[x] = p.flopChild(parenta, parentb);           //Save it for later
            System.out.println(people[x]);                       //Output it for now
        }




    }

    public char[] flopChild(char[] a, char[] b) {
        Random r = new Random();
        int y = 0;
        ArrayList<Integer> parts = new ArrayList();
        char[] child = new char[geneSize];

        while (y < geneSize) {                                  //Break it into parts so you can easily swap genes from the parents
            int num = r.nextInt(geneSize + 1 - y);
            if (num + y > geneSize) {
                parts.add(num + y - geneSize);
                y = geneSize + 1;
            } else {
                if (num == 0) {
                } else {
                    parts.add(num);
                    y += num;
                }
            }
        }

        int last = 0;
        for (int x = 0; x < parts.size(); x++) {                //Use the pieces to get chunks from the parents var a and b
            for (int z = last; z < last + parts.get(x); z++) {
                if (r.nextInt(2) == 0) {                        //Decied which parent to pull from
                    child[z] = a[z];
                } else {
                    child[z] = b[z];
                }
            }
            last = parts.get(x);
        }
        return child;
    }
}

所以我试图在两个父母的基础上创建一些孩子。目标是让父母a具有特征“AAAAA”而父母b具有特征“BBBBB”并随机地将它们给予孩子。结果看起来像“ABABA”,“AAAAB”或其他任何组合。我现在的代码交换了特征并为孩子返回它们,但它们并不总是正确的长度。我包含的代码只运行一次以简化操作。以下是一些示例结果。

    run:

    -------------------------

    ABBBBABBBBABAABABBBAAAB
    BBAAAAABABBBBABAAAAAA
    BAAAAAABABBBB
    BAAAABBAABBABABAABBABABBB
    BBAAAAABBABBABAABBA
    BAABBAAABBAABBBAAAABAAAB
    BBABABAABABAABBBBBAAAA
    BBBBABAAAABBBBBAABBAA
    ABAABBABBBBBAAABABBABAAB

1 个答案:

答案 0 :(得分:1)

以下是flopChild方法的更正版本。

public static char[] flopChild(final char[] a, final char[] b) {
    final Random r = new Random();
    int y = 0;
    final ArrayList<Integer> parts = new ArrayList<Integer>();
    final char[] child = new char[geneSize];

    while (y < geneSize) { // Break it into parts so you can easily swap
                            // genes from the parents
        final int num = r.nextInt(geneSize + 1 - y);
        if (num + y > geneSize) {
            parts.add(num + y - geneSize);
            y = geneSize + 1;
        } else {
            if (num == 0) {
            } else {
                parts.add(num);
                y += num;
            }
        }
    }

    int last = 0;
    for (int x = 0; x < parts.size(); x++) { // Use the pieces to get chunks
                                                // from the parents var a
                                                // and b
        final int next = last + parts.get(x);
        final char[] parent = r.nextInt(2) == 0 ? a : b; /*
                                                         * You want the same
                                                         * parent for one
                                                         * given chunk,
                                                         * right?
                                                         */
        for (int z = last; z < next; z++) {
            child[z] = parent[z];
        }
        last = next; // And not parts.get(x)
    }
    return child;
}

我纠正了两件事:

  • last变量已分配给parts.get(x)(数组中的随机位置),而不是last + parts.get(x)。这导致了你的长度问题。
  • 你需要做很多工作来按块处理数组,但是你随后为每个元素而不是每个元素选择父元素。

输出现在看起来像:

-------------------------
ABBBBBBBBBBBBBBBBBBBBBBBA
AAAAAAAAAABBBBBBBBBBBBBBB
AAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAABBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBAA
BBBBBBBBBBBBBBBBBBBBBBBBB
AAAAAAAAAAAAAAAABBBBBBAAA
AAAAAAAAABBBBBBBBBBBBBBBB
BBBBBBBBBBBAAAAAAAAAAAABB
AAAAAAAAAAAAAAAAAAAAA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