gpt4 book ai didi

java - Java List<> 的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:25:56 26 4
gpt4 key购买 nike

我不太熟悉 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

第一个循环证明变异进展缓慢..而且我也是在变异后立即添加的,那为什么后来所有的都被最新版本覆盖了?

最佳答案

首先,您的对象使用有点奇怪。

在 mutate 中,你似乎在改变和返回父亲。

这意味着您的列表将包含对同一实例的多个引用。

澄清:

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);
}

关于java - Java List<> 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5886235/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com