2011-05-04 100 views
3

我倒沒與Java列表和ArrayList熟悉..我只是需要一些工作的順利開展追加和排序。問題與Java的List <>

我的算法很簡單:

set a father string 
add father to speciesList 
    mutate father to some new child 
    make this new child the future father 
    go to step 2 

ga_ga_struct的定義在這裏給出

​​

我一直在做這個

 ga_ newSpecies = new ga_(); 
     Random r= new Random(10); 
     ga_struct father= new ga_struct(); 
     father.gene="123"; 
     newSpecies.vector.add(father); 

     for (int i = 1; i < 10; i++) { 
      ga_struct ng = new ga_struct();   
      ng=newSpecies.mutate(father); 
      ng.fitness=i; 
      newSpecies.vector.add(ng); 
      father=ng;   
      System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness); 

     } 

     newSpecies.sortspecies(); 
     System.out.println("\ncurrent population\n"); 

     for (int i = 0; i < 10; i++) { 
      System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness); 
     } 

增變函數只是改變一次只能有一個角色。我在第一個循環中從「父親」突變了9個新物種。但是..我不知道爲什麼代碼的輸出是給我這個 -

133 with fitness factor 1 
433 with fitness factor 2 
433 with fitness factor 3 
443 with fitness factor 4 
453 with fitness factor 5 
553 with fitness factor 6 
563 with fitness factor 7 
563 with fitness factor 8 
573 with fitness factor 9 

current population 

573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 
573 with fitness factor 9 

第一個循環是證明突變會慢慢..我也有突變後立即加入,那麼爲什麼後來所有這些都被最新版本覆蓋了?

+1

如果我理解正確,那麼在兩組printlns之間唯一發生的事情就是對'sortSpecies()'的調用。所以也許你應該告訴我們這個方法在做什麼。 – 2011-05-04 15:54:56

回答

3

首先,您的對象使用有點奇怪。

在變異中,你似乎在改變和回報父親。

這意味着您的列表將包含對同一實例的多個引用。

澄清:

public ga_struct mutate(ga_struct parent) //takes in reference to parent 
{ 
    Random r= new Random(); //modifies parent 
    ......  do some modification to the parent 
    return parent; //return reference to parent 
} 

而在你的主:

ga_ newSpecies = new ga_(); 
    Random r= new Random(10); 
    ga_struct father= new ga_struct();//instantiate father 
    father.gene="123"; 
    newSpecies.vector.add(father); 

    for (int i = 1; i < 10; i++) { 
     ga_struct ng = new ga_struct();//create new instance for child 
     ng=newSpecies.mutate(father);//set ng as reference to same instance as father, instance instantiated on previous line is discarded 
     ng.fitness=i; 
     newSpecies.vector.add(ng); 
     father=ng;   
     System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness); 

    } 

更多類似這樣的勇於嘗試:

public ga_struct mutate(ga_struct parent) 
{ 
    ga_struct ng = new ga_struct(); 
    ng.gene = father.gene; 
    Random r= new Random(); 
    //do some modification to ng 
    return ng; 
} 

,並在您的主:

a_ newSpecies = new ga_(); 
    Random r= new Random(10); 
    ga_struct father= new ga_struct(); 
    father.gene="123"; 
    newSpecies.vector.add(father); 

    for (int i = 1; i < 10; i++) {  
     ga_struct ng=newSpecies.mutate(father); 
     ng.fitness=i; 
     newSpecies.vector.add(ng); 
     father=ng;   
     System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness); 

    } 

    newSpecies.sortspecies(); 
    System.out.println("\ncurrent population\n"); 

    for (int i = 0; i < 10; i++) { 
     System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness); 
    } 
+0

非常感謝..我的道歉是這麼差的編碼器!無論如何,以下解決了我的問題 - parent.gene = origin.gene; 而不是 parent = origin; 爲什麼呢?實際上 – shababhsiddique 2011-05-04 16:06:53

+0

父=起源意味着父母和起源將指向同一個對象。 parent.gene = origin.gene將改變原始基因的值。如果您仔細閱讀其中的一些內容,可能會有所幫助:http://download.oracle.com/javase/tutorial/java/javaOO/ – Taylor 2011-05-10 00:19:59

0

你與單個對象到處工作,你從來沒有一個新的ga_struct實例添加到列表中。你mutate()方法看起來簡單修改parent參數,並返回它 - 它仍然是相同的對象,只是修改,這意味着它無處不修改。

public ga_struct mutate(ga_struct parent) 
{ 
    Random r= new Random(); 
    ......  do some modification to the parent 
    return parent; 
} 

你做創建的ga_struct一個新的實例,但是你馬上通過設置參考突變father(這仍然是相同的實例,只是修改)覆蓋它:在

for (int i = 1; i < 10; i++) { 
     ga_struct ng = new ga_struct();   
     ng=newSpecies.mutate(father); //the new ga_struct is overwritten 
     ng.fitness=i; 
     newSpecies.vector.add(ng); 
     father=ng;   
     System.out.println(newSpecies.vector.get(i).gene+" with fitness factor "+newSpecies.vector.get(i).fitness); 

    } 

你的輸出此循環似乎工作,因爲您看到按其發生順序對father所做的修改。 但是,你實際上做的是隻需添加引用相同的(修改)對象遍地到List

因此,當您最終全部打印出來時,您會在List中看到10個重複條目。


我的建議是要改變mutate()返回的ga_struct一個新的實例 - 你可以創建一個新對象,並設置它的gene場是從parent突變gene領域。或者你可以cloneparent然後改變克隆的基因串。在這兩種情況下,你最終將返回的ga_struct一個新的實例應該解決這個問題。

public ga_struct mutate(ga_struct parent) 
{ 
    Random r= new Random(); 
    ga_struct mutant = parent.clone(); 
    //or 
    //ga_struct mutant = new ga_struct(); 
    //mutant.gene = parent.gene; 

    ......  do some modification to the mutant 
    return mutant; //now you'll be returning a new object not just a modified one 
} 
+0

從技術上講,他確實做到了,但立即放棄了。但這是正確的想法。 – DHall 2011-05-04 15:58:04

+0

他在代碼中的至少兩處創建了新的'ga_struct'。 – 2011-05-04 15:58:24

+1

@Joachim Sauer你說得對,我應該改說一下 - 我的意思是指出他似乎在任何地方都在使用同一個實例(他將@DHall指出的新對象覆蓋掉) - 從而在每次迭代中修改它在'List'中有10個條目,它們都是同一個對象。 – 2011-05-04 16:00:36

1

您並未創建新對象,您已將父對象添加到向量9次。

你已經有了本質是什麼

父親 - > OBJ @ 123

您列出對象的樣子是 [OBJ @ 123,OBJ @ 123,OBJ @ 123,...]

您將需要創建新的實例來記錄它。我會建議實施「克隆()」方法來做到這一點。

+0

感謝您的回覆..我<3堆棧溢出! – shababhsiddique 2011-05-04 16:09:32

相關問題