编程语言
首页 > 编程语言> > Java List <>问题

Java List <>问题

作者:互联网

我对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

第一个循环证明突变正在缓慢进行..而且在突变之后我也立即添加了一个词,那么为什么后来所有这些词都被最新版本覆盖?

解决方法:

首先,您的对象用法有点奇怪.

在变异中,您似乎正在改变并返回父亲.

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

澄清:

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

标签:list,arraylist,genetic-algorithm,java
来源: https://codeday.me/bug/20191102/1993158.html