-2

edit: problem solved, it came indeed from the sublist. Thanks!

I am doing a project about genetic algorithms. but I have the entitled error, and I don't really know where it happens, so I would need your help...

here is the function:

public synchronized void evolution(){
    //TreeSet containing the 2 fathers and children
    TreeChromosome t = new TreeChromosome();
    Chromosome father1 = selection();
    Chromosome father2;
    do{father2= selection();}
    while(father1==father2);

    t.add(father1);
    t.add(father2);


    Chromosome child1 = OperatorGen.crossRight(father1, father2);
    OperatorGen.swap(father1);

    Chromosome child2 = OperatorGen.crossLeft(father1, father2);        
    OperatorGen.swap(child2);

    t.add(fils1);
    t.add(fils2);

    // we add the best 2 in the population 
       Chromosome best1=t.pollFirst();
       genotype.add(best1);
        Chromosome best2=t.pollFirst();
       genotype.add(best2);

    //we remove the non selected chromosomes
    for (Chromosome chromo : t) {
        if (genotype.contains(chromo) && t.contains(chromo)){
            genotype.remove(chromo);
        }
    }
    genotype.updateRanks();
}

when I run this, it works fine but when I run this in a loop, I have the exception on OperatorGen.crossLeft ... here is the code of this function:

public static Chromosome crossLeft(Chromosome father1, Chromosome father2, int joint){
    List<Ville> listFather1 = father1.getCities();
    List<Ville> listFather2 = pere2.getCities();
    //we copy the first cities of father1
    List<Ville> listChild= listPere1.subList(0, joint);
    City nextCity;
    //we add the cities of father2  
    //block where the error appears, not always at the same line

  for(int i=0;i<listFather2.size();i++){           
        nextCity=listFather2.get(i);

        if(!listChild.contains(nextCity)){
            listChild.add(nextCity);
        }
    }
    Chromosome child= new Chromosome(listChild);
    return child;
 }

I tried adding syncronized everywhere but nothing works... So I would like to know where is the problem and how do I correct it? Thank you!

2 Answers2

2

You can't remove from a collection you are currently iterating over, unless you use the Iterator.remove() method, which requires that you don't use the enhanced for-loop.

Simulant
  • 19,190
  • 8
  • 63
  • 98
user207421
  • 305,947
  • 44
  • 307
  • 483
  • You sure this is the case? He is iterating over `t`, but removing from `genotype`, which from my understanding are separate instances. – mfaerevaag Jun 08 '13 at 10:52
0

Thoughout your code you keep re-using the same cities, as well as the same Lists. When you use subList, you are actually creating a view on the old list ( http://docs.oracle.com/javase/7/docs/api/java/util/List.html#subList(int, int]) ). It is no wonder that when you work all those lists concurrently, you get a ConcurrentModificationException.

You must either be very carefull with these lists (to really see where it goes wrong, we'd probably need the entire source code). Or you must create new List-instances instead of the subList.

By the way, the way you translated about half the french made it less easy to read your code rather than easier.. (pere-father, child-fils, etc..)

ljgw
  • 2,751
  • 1
  • 20
  • 39
  • sorry about that =) and thank you, I will modify this then. I don't really understand why it works when I run this function, but not when I call it in a while(true) for example... – user2466062 Jun 08 '13 at 12:42