onemax.ez 1.4 KB
Newer Older
moh_lo's avatar
moh_lo committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
\User declarations:
#define SIZE 100      
float pMutPerGene=0.1;

inline void swap(bool& a, bool& b)
  {bool c=a; a=b; b=c;}
\end

\User functions:

\end

\Initialisation function:

\end

\User classes:
GenomeClass
{
  bool x[SIZE]; 
}
\end

\GenomeClass::initialiser:
  for (int i=0;i<SIZE;i++) Genome.x[i]=tossCoin(.5)?1:0;
\end

\GenomeClass::crossover:
  int CrossoverPosition=random(0,SIZE);

  for(int i=0;i<CrossoverPosition+1;i++)
    swap(child1.x[i],child2.x[i]);
\end

\GenomeClass::mutator:
  int NbMut=0;
  for (int i=0;i<SIZE;i++)
    if (tossCoin(pMutPerGene)){
       NbMut++;
       Genome.x[i]=Genome.x[i]?0:1;
    }
  return NbMut;
\end

\GenomeClass::evaluator:
  int Score=0;         
  for (int i=0; i<SIZE;i++)
    Score+=(int)Genome.x[i];
  return Score;
\end

\GenomeClass::display:

\end

\At each new generation:

\end

\Default run parameters:

moh_lo's avatar
moh_lo committed
62
// Variation operators
moh_lo's avatar
moh_lo committed
63 64 65 66
  Mutation probability: 0.3
  Crossover probability: 0.8

// Evolution Engine:
moh_lo's avatar
moh_lo committed
67
  Evaluator goal: Minimise       // or maximise
moh_lo's avatar
moh_lo committed
68
  Number of generations: 2000
moh_lo's avatar
moh_lo committed
69 70 71 72 73 74
  Population: 100
  Elite: 1 
  Genitors: 80                   // those who can be parents
  Genitors selector: sequential
  Parents selector: Tournament 2 // Selection of a parent to create a child
  Offspring: 100
moh_lo's avatar
moh_lo committed
75 76 77 78 79 80 81
  Reduce parents: Tournament 2
  Surviving parents: 20
  Reduce offspring: Tournament 2
  Surviving offspring: 80
  Final reduce: Tournament 2
  Elitism: Strong
\end