java列表问题<&燃气轮机;

java列表问题<&燃气轮机;,java,list,arraylist,genetic-algorithm,Java,List,Arraylist,Genetic Algorithm,我对java列表和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_结构的定义 public class ga_struct { public St

我对java列表和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_结构
的定义

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_struct
实例。您的
mutate()
方法似乎只是修改
父参数并返回它-它仍然是同一个对象,只是被修改了,这意味着它到处都被修改了

public ga_struct mutate(ga_struct parent)
{
    Random r= new Random();
    ......     do some modification to the parent
    return parent;
}
您确实创建了一个新的
ga_struct
实例,但通过将引用设置为变异的
父对象来立即覆盖它(该实例仍然是相同的,刚刚修改):


您没有创建新对象,而是将父对象添加到向量中9次

基本上你得到的是

父亲->obj@123

列表对象的外观是 [ obj@123, obj@123, obj@123,…]


您将需要创建新实例来记录这一点。我建议实现“clone()”方法来实现这一点。

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

在《变异》中,你似乎正在改变并回归父亲

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

澄清:

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

    }
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);
    }
主要是:

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

    }
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);
    }
a_uu9;newSpecies=new ga_u9;();
随机r=新随机(10);
ga_结构父对象=新ga_结构();
父亲。基因=“123”;
newSpecies.vector.add(父);
对于(inti=1;i<10;i++){
ga_struct ng=新物种变异(父亲);
ng.适合度=i;
新物种.载体.添加(ng);
父亲=ng;
System.out.println(newSpecies.vector.get(i).gene+“带适应度因子”+newSpecies.vector.get(i).fitness);
}
新物种。sortspecies();
System.out.println(“\n当前人口\n”);
对于(int i=0;i<10;i++){
System.out.println(newSpecies.vector.get(i).gene+“带适应度因子”+newSpecies.vector.get(i).fitness);
}

如果我理解正确,两组println之间发生的唯一事情就是调用
sortSpecies()
。所以,也许你应该让我们看看这个方法在做什么。从技术上来说,他是这样做的,但是他马上就放弃了。但这是正确的想法。他在代码中至少两个地方创建了新的
ga_struct
。@Joachim Sauer你说得对,我应该重新表述一下-我的意思是,他似乎到处都在使用同一个实例(他覆盖了@DHall指出的新对象)-因此,在每次迭代中修改它会导致
列表中有10个条目,所有条目都是相同的对象。非常感谢。。我为自己是一个如此糟糕的程序员而道歉!。。不管怎样,下面解决了我的问题-parent.gene=origin.gene;而不是父=源;为什么?actuallyparent=原点意味着父对象和原点将指向同一个对象。parent.gene=origin.gene将更改原始基因的值。如果您仔细阅读其中一些内容,可能会有所帮助:
    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);
    }