Bobot_ga.h

#ifndef _BOBOT_GA_H
#define _BOBOT_GA_H

// ALGORITHME GENETIQUE
#define BOBOT_MAX_PERTURBATION 0.3
#define BOBOT_NUM_COPIES_ELITE 2
#define BOBOT_NUM_ELITE 3
#define BOBOT_TIME_PER_GENERATION 200*1000 // X*1000 millisecondes (X*1 seconde)
//#define BOBOT_NUM_GENERATIONS 200
#define BOBOT_NUM_BOTS 24
#define BOBOT_CROSSOVER_RATE 0.7
#define BOBOT_MUTATION_RATE 0.1

#define BOBOT_BEST_SO_FAR 0
#define BOBOT_LAST_BEST 1

extern double RandomClamped();
extern double RandFloat();
extern int      RandInt(int x,int y);

extern int    numWeightsEvolving;

//-----------------------------------------------------------------------
//
//    create a structure to hold each genome
//-----------------------------------------------------------------------
typedef struct SGenome
{
    double vecWeights[BOBOT_MAX_NUM_WEIGHTS];

    double dFitness;
}SGenome;

//-----------------------------------------------------------------------
//
//    the genetic algorithm class
//-----------------------------------------------------------------------
typedef struct CGenAlg
{
    //this holds the entire population of chromosomes
    SGenome        vecPop[BOBOT_NUM_BOTS];

    //size of population
    int            popSize;
   
    //amount of weights per chromosome
    int            chromoLength;

    //total fitness of population
    double        totalFitness;
    double        reallyTotalFitness;

    //best fitness this population
    double        bestFitness;
    double        reallyBestFitness;

    //average fitness
    double        averageFitness;
    double        reallyAverageFitness;

    //worst
    double        worstFitness;
    double        reallyWorstFitness;

    //keeps track of the best genome
    int            fittestGenome;
    int            reallyFittestGenome;

    //probability that a chromosones bits will mutate.
    //Try figures around 0.05 to 0.3 ish
    double        mutationRate;

    //probability of chromosones crossing over bits
    //0.7 is pretty good
    double        crossoverRate;

    //generation counter
    int            generationCounter;

    int            brainType;

}CGenAlg;

void    BOBOT_changeTimePerGeneration();

void    Crossover(CGenAlg* genAlg,
                  const double* mum,
                  const double* dad,
                  double*        baby1,
                  double*        baby2);
   
void    Mutate(CGenAlg* genAlg,
               double* chromo);
   
void GetChromoRoulette(CGenAlg* genAlg, SGenome* TheChosenOne);

//use to introduce elitism
void    GrabNBest(    CGenAlg* genAlg,
                    int            NBest,
                    const int    NumCopies,
                    SGenome*    vecPop);

void    CalculateBestWorstAvTot(CGenAlg* genAlg);

void    Reset(CGenAlg* genAlg);

void    CreateSGenome(SGenome* genome,double* w, double f,int typeOfCreation, int numWeights);

void CreateCGenAlg(CGenAlg* genAlg,
                 int    popsize,
                 double    MutRat,
                 double    CrossRat,
                 int    numweights,
                 int    brainType);

//this runs the GA for one generation.
void    Epoch(CGenAlg* genAlg,
                  SGenome* old_pop);

//-------------------accessor methods
SGenome*    GetChromos(CGenAlg* genAlg);
double        AverageFitness(CGenAlg* genAlg);
double        BestFitness(CGenAlg* genAlg);

#endif

Créer un site gratuit avec e-monsite - Signaler un contenu illicite sur ce site