我对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);
}
mutator函数一次只改变一个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
第一个循环证明突变正在缓慢进行..而且我也在突变后立即添加,那么为什么稍后所有这些都被最新版本覆盖?
答案 0 :(得分: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);
}
答案 1 :(得分:1)
您没有创建新对象,您已将父对象添加了9次到向量。
基本上你得到的是
父亲 - &gt;物镜@ 123您的List对象是什么样的 [obj @ 123,obj @ 123,obj @ 123,...]
您将需要创建新实例来记录此事件。我建议实现“clone()”方法来执行此操作。
答案 2 :(得分: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
字段设置为变异gene
来自parent
的字段。或者您可以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
}