Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void crossover()
- {
- if(ThreadLocalRandom.current().nextInt(1, 101)<86)
- {
- for(int i=0; i<pop.size(); i++)
- {
- if(i%2==0)
- {
- Individual tmp1=ordered_crossover(pop.get(i), pop.get(i+1));
- Individual tmp2=ordered_crossover(pop.get(i+1), pop.get(i));
- pop.set(i, tmp1);
- pop.set(i+1, tmp2);
- }
- }
- }
- }
- public static Individual ordered_crossover(Individual a, Individual b)
- {
- //cutpoints
- int c1=0;
- int c2=0;
- int gen_size=a.getGenotype().size();
- int r1=ThreadLocalRandom.current().nextInt(0, gen_size);
- int r2=ThreadLocalRandom.current().nextInt(0, gen_size);
- while(r2==r1)
- {
- r2=ThreadLocalRandom.current().nextInt(0, gen_size);
- }
- c1=Math.min(r1, r2);
- c2=Math.max(r1, r2);
- List<Integer> child_a=new ArrayList<>(a.getGenotype());
- for(int i=0; i<gen_size;i++)
- {
- if(i<c1)
- {
- child_a.set(i, gen_size);
- }
- if((i>c1-1)&&(i<c2+1))
- {
- child_a.set(i, a.getGenotype().get(i));
- }
- if(i>c2)
- {
- child_a.set(i, gen_size);
- }
- }
- for(int i=0; i<gen_size;i++)
- {
- if(child_a.get(i)==gen_size)
- {
- for(int j=0; j<gen_size;j++)
- {
- if(child_a.contains(b.getGenotype().get(j))==false)
- {
- child_a.set(i, b.getGenotype().get(j));
- break;
- }
- }
- }
- }
- while(child_a.contains(52))
- {
- for(int i : b.genotype)
- {
- if(!child_a.contains(i))
- {
- child_a.set(child_a.indexOf(52), i);
- }
- }
- }
- Individual child = new Individual(a, child_a);
- return child;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement