我对java List和arrayList不是很熟悉。我只是需要一些东西来顺利地工作,以追加和排序。
我的算法很简单:
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的定义
public class ga_struct {
public String gene;
public int fitness;
}
public class ga_{
public List<ga_struct> vector= new ArrayList<ga_struct>();
public void sortspecies()
{
Collections.sort(vector,new Comparator<ga_struct>() {
@Override
public int compare(ga_struct o1, ga_struct o2) {
int res;
if(o1.fitness<o2.fitness)
res=-1;
else if(o1.fitness>o2.fitness)
res=1;
else
res=0;
return res;
}
}
);
}
public ga_struct mutate(ga_struct parent)
{
Random r= new Random();
...... do some modification to the parent
return parent;
}
}我一直在这么做
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);
}赋值函数一次只修改String(gene)中的一个字符。我刚刚从第一个循环中的“父亲”变异了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第一个循环证明变异进行得很慢。我还在突变后立即添加了,那么为什么后来所有这些都被最新版本覆盖了呢?
发布于 2011-05-04 23:59:22
首先,你的对象用法有点奇怪。
在变异中,你似乎在改变和回归父亲。
这意味着您的列表将包含对同一实例的多个引用。
澄清一下:
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);
}发布于 2011-05-04 23:58:52
你不是在创建一个新的对象,你已经在向量中添加了9次父对象。
从本质上讲,你得到的是
->父亲电子邮件: obj@123
你的列表对象看起来像obj@123,...
您将需要创建新的实例来记录此操作。我建议实现"clone()“方法来实现这一点。
发布于 2011-05-04 23:57:15
您在任何地方都在使用单个对象,您永远不会向列表中添加新的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字段。或者你可以clone parent然后改变克隆人的基因字符串。在任何一种情况下,您最终都将返回一个新的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
}https://stackoverflow.com/questions/5886235
复制相似问题