C   39

gen c

Guest on 29th June 2022 08:11:07 AM

  1. #include <stdio.h>
  2.  
  3. #include "type.h"
  4.  
  5. int roulette(IPTR pop, double sumFitness, int popsize);
  6. void crossover(POPULATION *p, IPTR p1, IPTR p2, IPTR c1, IPTR c2);
  7. double eval(POPULATION *p, IPTR pj);
  8.  
  9. int generation(POPULATION *p, int t)
  10. {
  11.  
  12.   int i, p1, p2, c;
  13.  
  14.   IPTR pi, piPlus1, om1, om2;
  15.  
  16.   for(i = 0; i < p->popSize; i += 2){
  17.  
  18.     p1 = roulette(p->op, p->sumFitness, p->popSize);
  19.     p2 = roulette(p->op, p->sumFitness, p->popSize);
  20.  
  21.     pi = &(p->np[i]);
  22.     piPlus1 = &(p->np[i+1]);
  23.     om1 = &(p->op[p1]);
  24.     om2 = &(p->op[p2]);
  25.  
  26.     crossover(p, om1, om2, pi, piPlus1);
  27.  
  28.     pi->fitness = eval(p, pi);
  29.     pi->parent1 = p1;
  30.     pi->parent2 = p2;
  31.  
  32.    
  33.     piPlus1->fitness = eval(p, piPlus1);
  34.     piPlus1->parent1 = p2;
  35.     piPlus1->parent2 = p1;
  36.  
  37.   }
  38.  
  39. }

Raw Paste


Login or Register to edit or fork this paste. It's free.