Problem with java list <>

I am not very familiar with java List and arrayList .. I just need something to work smoothly to add and sort.

My algorithm is simple:

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

Definitions ga_and ga_structare given here.

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

I'm doing it

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

The mutator function simply changes String(gene)one character at a time. I just mutated 9 new species from the "father" in the first cycle. But .. I don't know why the code output gives me this -

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

The first cycle is proof that the mutation is slow. And I also added right after the mutation, then why does this happen when they are all just overwritten with the latest version?

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

, - 9 .

,

β†’ obj @123

List [obj @123, obj @123, obj @123,...]

. clone() .

+1

, 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.

, , , , 10 List.


My suggestion is to change mutate()to return a new instance ga_struct- you could either create a new object, or set it in the field geneas a mutated field genefrom parent. Or you can , and then change the clone gene string. In any case, you will return a new instance that should fix the problem.clone parentga_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
}
0
source

All Articles