Question

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;
    }
}

So I'm trying to create some children based off two parents. The goal is to take parent a with traits "AAAAA" and parent b with traits "BBBBB" and randomly give them to children. Results would look like "ABABA", "AAAAB", or any other combination of those. The code I have now swaps the traits and returns them for the child however they are not always the correct length. The code I included only runs through it once to simplify things. Here are some example results.

    run:

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

    ABBBBABBBBABAABABBBAAAB
    BBAAAAABABBBBABAAAAAA
    BAAAAAABABBBB
    BAAAABBAABBABABAABBABABBB
    BBAAAAABBABBABAABBA
    BAABBAAABBAABBBAAAABAAAB
    BBABABAABABAABBBBBAAAA
    BBBBABAAAABBBBBAABBAA
    ABAABBABBBBBAAABABBABAAB
Was it helpful?

Solution

Here is a corrected version of the flopChild method.

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;
}

I corrected two things:

  • The last variable was assigned to parts.get(x) (a random place in the array) instead of last + parts.get(x). That caused your length issue.
  • You do a lot of work to process the arrays by chunks, but then you choose the parent at random for each element instead of each chunk.

The output now looks like:

-------------------------
ABBBBBBBBBBBBBBBBBBBBBBBA
AAAAAAAAAABBBBBBBBBBBBBBB
AAAAAAAAAAAAAAAAAAAAAAAAA
AAAAAAAAAAAAAAABBBBBBBBBB
BBBBBBBBBBBBBBBBBBBBBBBAA
BBBBBBBBBBBBBBBBBBBBBBBBB
AAAAAAAAAAAAAAAABBBBBBAAA
AAAAAAAAABBBBBBBBBBBBBBBB
BBBBBBBBBBBAAAAAAAAAAAABB
AAAAAAAAAAAAAAAAAAAAAAAAB
AAAAAAAAAAAAAAAAAAAAABBBB
AAAAAAAAAAAAAAAAAABBBBBBB
AAAAAAAAABBBBBBBBBBBBBBBB
AAAAAAAAAA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