2013-04-18 39 views
-2

http://i.imgur.com/PWVruQ0.png爲什麼我的基因突變密碼不能重新調整整個基因? Java的

/* 
* 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 
+2

請在此處添加您的代碼,並解釋是錯誤。 – BobTheBuilder

回答

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變量是(陣列中的隨機位置)分配給代替last + parts.get(x)。這造成了你的長度問題。
  • 你做了很多工作來按塊處理數組,但是隨後你會爲每個元素而不是每個塊選擇父項。

輸出現在看起來像:

------------------------- 
ABBBBBBBBBBBBBBBBBBBBBBBA 
AAAAAAAAAABBBBBBBBBBBBBBB 
AAAAAAAAAAAAAAAAAAAAAAAAA 
AAAAAAAAAAAAAAABBBBBBBBBB 
BBBBBBBBBBBBBBBBBB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BBBBBBBBBAA 
BBBBBBBBBBBBBBBBBBBBBBBBA 
AAAAAAAAAAAAAAAAAABBBBBBB 
BBBBBBBBBBBBBBBBBBBBBBBBB 
AAAABBBBBBBBBBBBBBBAAABBB 
AAAAAAAAAAAAAAAAAAAAAAAAA 
AAAAAAAAAABBBBBBBBBBBAAAA 
+0

你說你糾正了「你做了大量的工作來處理數組塊,但隨後你爲每個元素而不是每個塊選擇父對象。」這到底是什麼意思?除了格式化之外,我沒有看到太多差異。 – user1832787

+0

你說過:「你做了很多工作來處理數據塊」。你會如何接近它? – user1832787

+0

@ user1832787與你的原始代碼,你隨機選擇一個父母爲每個字符,這使你的塊無用。我在for循環之外移動了父項選擇,以便同一個父項用於一個塊(我想這是你想要的)。再次在循環中移動父項選擇,您將在輸出中看到結果。 – WilQu