arraylist - Why is my genetic mutation code not retuning the entire gene? Java -


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

/*  * change template, choose tools | templates  * , open template in editor.  */ package parentmutation;  import java.util.arraylist; import java.util.random;  /**  *  * @author renter  */ public class parentmutation {      /**      * @param args command line arguments      */     static int population = 50;     static int genesize = 25;     public static void main(string[] args) {          char[] parenta = new char[genesize];                     //create parents pull genes         (int x = 0; x < genesize; x++) {             parenta[x] = 'a';             system.out.print("-");         }         system.out.println();         char[] parentb = new char[genesize];         (int x = 0; x < genesize; x++) {             parentb[x] = 'b';         }          char[][] people = new char[population][];                //how many children make         parentmutation p = new parentmutation();         (int x = 0; x < population; x++) {             people[x] = p.flopchild(parenta, parentb);           //save later             system.out.println(people[x]);                       //output         }         }      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 parts can swap genes 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;         (int x = 0; x < parts.size(); x++) {                //use pieces chunks parents var , b             (int z = last; z < last + parts.get(x); z++) {                 if (r.nextint(2) == 0) {                        //decied parent pull                     child[z] = a[z];                 } else {                     child[z] = b[z];                 }             }             last = parts.get(x);         }         return child;     } } 

so i'm trying create children based off 2 parents. goal take parent traits "aaaaa" , parent b traits "bbbbb" , randomly give them children. results "ababa", "aaaab", or other combination of those. code have swaps traits , returns them child not correct length. code included runs through once simplify things. here example results.

    run:      -------------------------      abbbbabbbbabaababbbaaab     bbaaaaababbbbabaaaaaa     baaaaaababbbb     baaaabbaabbababaabbababbb     bbaaaaabbabbabaabba     baabbaaabbaabbbaaaabaaab     bbababaababaabbbbbaaaa     bbbbabaaaabbbbbaabbaa     abaabbabbbbbaaababbabaab 

here corrected version of 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 parts can swap                             // genes 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;     (int x = 0; x < parts.size(); x++) { // use pieces chunks                                                 // parents var                                                 // , b         final int next = last + parts.get(x);         final char[] parent = r.nextint(2) == 0 ? : b; /*                                                          * want same                                                          * parent 1                                                          * given chunk,                                                          * right?                                                          */         (int z = last; z < next; z++) {             child[z] = parent[z];         }         last = next; // , not parts.get(x)     }     return child; } 

i corrected 2 things:

  • the last variable assigned parts.get(x) (a random place in array) instead of last + parts.get(x). caused length issue.
  • you lot of work process arrays chunks, choose parent @ random each element instead of each chunk.

the output looks like:

------------------------- abbbbbbbbbbbbbbbbbbbbbbba aaaaaaaaaabbbbbbbbbbbbbbb aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaabbbbbbbbbb bbbbbbbbbbbbbbbbbbbbbbbaa bbbbbbbbbbbbbbbbbbbbbbbbb aaaaaaaaaaaaaaaabbbbbbaaa aaaaaaaaabbbbbbbbbbbbbbbb bbbbbbbbbbbaaaaaaaaaaaabb aaaaaaaaaaaaaaaaaaaaaaaab aaaaaaaaaaaaaaaaaaaaabbbb aaaaaaaaaaaaaaaaaabbbbbbb aaaaaaaaabbbbbbbbbbbbbbbb aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbb aaaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbaab bbbbbbbbaaaabbbbbbbbbbbba bbbbbbbbbbbbbbbbbbaabbbbb aaaaaaaaaaaaaaaaaaaaaabaa bbbbbbbbbbbbbbbbbbbbbbbbb bbbbbbbbbbbbbbbbbbbaaaaaa aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaaabbabbb bbbbbbbbbbbbbbbbbbbbbbaaa bbbbbbbbbbbbbbbbbbbaaaabb bbbbbaaaaaaaaaaaaaaaaaaab aaaaaaaaaaaaaaaaaaaaaabbb aaaaaaaaaaaaaabbbbbbbbbbb aaaaaaaaabaaaaaaabbbbbaab bbbbbbbbbbbbbbbbbbbbbbbbb aaaaaaaaaaaaaaaaaaaaaabbb aaaaaaaaaaaaaaaaabaaaaaab bbbbbbbaaaaaaaaaaaaaaaaaa aaaaaaaaaabbbbbbbbbbbaaab abbbbbbbaaaaaaaabbbbbbbba bbbbbbbbbbbbbbbbbbbaaaaab aaaaaaaaaaaabbbbbbbbbabaa bbbbbbbbbbbbbbbaaaaaaaaab aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaaaaaaaaaabbbbbba bbbbaaaaaaaaaaaaaaaaaabbb bbbbbbbbbbbbbbbbbbbbbbbba aaaaaaaaaaabbbbbbbbbbbbaa bbbbbbbbbbbbbbbbbbbbbbbba aaaaaaaaaaaaaaaaaabbbbbbb bbbbbbbbbbbbbbbbbbbbbbbbb aaaabbbbbbbbbbbbbbbaaabbb aaaaaaaaaaaaaaaaaaaaaaaaa aaaaaaaaaabbbbbbbbbbbaaaa 

Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -