CPopulation.h 4.15 KB
Newer Older
kruger's avatar
kruger 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
/*
 * CPopulation.h
 *
 *  Created on: 22 juin 2009
 *      Author: maitre
 */

#ifndef CPOPULATION_H_
#define CPOPULATION_H_


#ifdef DEBUG
#define DEBUG_PRT(format, args...) fprintf (stdout,"***DBG***  %s-%d: "format"\n",__FILE__,__LINE__,##args)
#define DEBUG_YACC(format, args...) fprintf (stdout,"***DBG_YACC***  %s-%d: "format"\n",__FILE__,__LINE__,##args)
#else
#ifndef WIN32
#define DEBUG_PRT(format, args...)
#define DEBUG_YACC(format, args...)
#endif
#endif

#include <vector>
#include "CSelectionOperator.h"

class Parameters;
26
class CStats;
kruger's avatar
kruger committed
27 28 29 30 31 32 33 34 35 36

class CPopulation {

public:

  float pCrossover;
  float pMutation;
  float pMutationPerGene;

  CIndividual* Best;
37
  CIndividual* Worst;
38 39
  float currentAverageFitness;
  float currentSTDEV;
kruger's avatar
kruger committed
40 41 42 43

  CIndividual** parents;
  CIndividual** offsprings;

Ogier Maitre's avatar
Ogier Maitre committed
44 45
  unsigned parentPopulationSize;
  unsigned offspringPopulationSize;
kruger's avatar
kruger committed
46

Ogier Maitre's avatar
Ogier Maitre committed
47 48
  unsigned actualParentPopulationSize;
  unsigned actualOffspringPopulationSize;
kruger's avatar
kruger committed
49 50 51 52 53 54

  static CSelectionOperator* selectionOperator;
  static CSelectionOperator* replacementOperator;
  static CSelectionOperator* parentReductionOperator;
  static CSelectionOperator* offspringReductionOperator;

Ogier Maitre's avatar
Ogier Maitre committed
55
  unsigned currentEvaluationNb;
kruger's avatar
kruger committed
56 57 58 59
  CRandomGenerator* rg;
  std::vector<CIndividual*> pop_vect;

  Parameters* params;
60
  CStats* cstats;
kruger's avatar
kruger committed
61 62 63

 public:
  CPopulation();
Ogier Maitre's avatar
Ogier Maitre committed
64
  CPopulation(unsigned parentPopulationSize, unsigned offspringPopulationSize,
65
	     float pCrossover, float pMutation, float pMutationPerGene, CRandomGenerator* rg, Parameters* params, CStats* cstats);
kruger's avatar
kruger committed
66 67 68
  virtual ~CPopulation();

  //virtual void initializeParentPopulation() = 0;
Ogier Maitre's avatar
Ogier Maitre committed
69
  void addIndividualParentPopulation(CIndividual* indiv, unsigned id);
70
  void addIndividualParentPopulation(CIndividual* indiv);
Ogier Maitre's avatar
Ogier Maitre committed
71 72
  void evaluatePopulation(CIndividual** population, unsigned populationSize);
  virtual void optimisePopulation(CIndividual** population, unsigned populationSize);
kruger's avatar
kruger committed
73
  virtual void evaluateParentPopulation();
74
  virtual void optimiseParentPopulation();
kruger's avatar
kruger committed
75

Ogier Maitre's avatar
Ogier Maitre committed
76 77
  void strongElitism(unsigned elitismSize, CIndividual** population, unsigned populationSize, CIndividual** outPopulation, unsigned outPopulationSize);
  void weakElitism(unsigned elitismSize, CIndividual** parentsPopulation, CIndividual** offspringPopulation, unsigned* parentPopSize, unsigned* offPopSize, CIndividual** outPopulation, unsigned outPopulationSize);
kruger's avatar
kruger committed
78 79

  virtual void evaluateOffspringPopulation();
80
  virtual void optimiseOffspringPopulation();
81
  CIndividual** reducePopulations(CIndividual** population, unsigned populationSize,CIndividual** reducedPopulation, unsigned obSize,int pressure);
Ogier Maitre's avatar
Ogier Maitre committed
82 83
  CIndividual** reduceParentPopulation(unsigned obSize);
  CIndividual** reduceOffspringPopulation(unsigned obSize);
maitre's avatar
maitre committed
84
  void reduceTotalPopulation(CIndividual** elitPop);
kruger's avatar
kruger committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98
  void evolve();

  static float selectionPressure;
  static float replacementPressure;
  static float parentReductionPressure;
  static float offspringReductionPressure;

  static void initPopulation(CSelectionOperator* selectionOperator,
			     CSelectionOperator* replacementOperator,
			     CSelectionOperator* parentReductionOperator,
			     CSelectionOperator* offspringReductionOperator,
			     float selectionPressure, float replacementPressure,
			     float parentReductionPressure, float offspringReductionPressure);

Ogier Maitre's avatar
Ogier Maitre committed
99
  static void sortPopulation(CIndividual** population, unsigned populationSize);
kruger's avatar
kruger committed
100

Ogier Maitre's avatar
Ogier Maitre committed
101
  static void sortRPopulation(CIndividual** population, unsigned populationSize);
kruger's avatar
kruger committed
102

kruger's avatar
kruger committed
103
  void serializePopulation();
104
  int getWorstIndividualIndex(CIndividual** population);
kruger's avatar
kruger committed
105 106 107

  void sortParentPopulation(){ CPopulation::sortPopulation(parents,actualParentPopulationSize);}

108
  virtual void produceOffspringPopulation();
kruger's avatar
kruger committed
109 110 111 112

  friend std::ostream& operator << (std::ostream& O, const CPopulation& B);


Ogier Maitre's avatar
Ogier Maitre committed
113
  void setParentPopulation(CIndividual** population, unsigned actualParentPopulationSize){
kruger's avatar
kruger committed
114 115 116 117
    this->parents = population;
    this->actualParentPopulationSize = actualParentPopulationSize;
  }

Ogier Maitre's avatar
Ogier Maitre committed
118 119
  static void reducePopulation(CIndividual** population, unsigned populationSize,
				       CIndividual** reducedPopulation, unsigned obSize,
120
				       CSelectionOperator* replacementOperator,int pressure);
kruger's avatar
kruger committed
121 122 123 124 125 126
  void syncOutVector();
  void syncInVector();

};

#endif /* CPOPULATION_H_ */