我倒沒與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
第一個循環是證明突變會慢慢..我也有突變後立即加入,那麼爲什麼後來所有這些都被最新版本覆蓋了?
如果我理解正確,那麼在兩組printlns之間唯一發生的事情就是對'sortSpecies()'的調用。所以也許你應該告訴我們這個方法在做什麼。 – 2011-05-04 15:54:56