Skip to content

Commit

Permalink
Fin du debogage, création des premières stratégies
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennedeg committed Feb 14, 2018
1 parent a81f040 commit a87ef9f
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 42 deletions.
Binary file added core
Binary file not shown.
4 changes: 1 addition & 3 deletions src/AGContreStratFixe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ void AGContreStratFixe::selection(){
triIndividu.push_back(individu);
}
sort(triIndividu.begin(), triIndividu.end(), Individu<GeneDouble>::comparaisonIndividu);
for (int i = 0; i < p/2; ++i){
delete(getIndividu(i));
}
affichage();
triIndividu.erase(triIndividu.begin(), triIndividu.begin() + getTaillePop()/2);
setPopulation(triIndividu);
}
1 change: 1 addition & 0 deletions src/AlgoGenetique.inl
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ template<class T> void AlgoGenetique<T>::addIndividu(T &unIndividu){


template<class T> void AlgoGenetique<T>::setPopulation(std::vector<T*> &unePopulation){
m_population.clear();
m_population = unePopulation;
}

Expand Down
25 changes: 14 additions & 11 deletions src/AlgoGenetiqueTDJ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,17 +94,19 @@ void AlgoGenetiqueTDJ::croisement(){
for (int i = 0; i < t/2; i+=2){
addIndividu();
addIndividu();
for(int j = 0; j < getIndividu(i)->getTailleChromosome(); ++i){
for(int j = 0; j < getIndividu(i)->getTailleChromosome(); ++j){
double r = rand()/(double)RAND_MAX;
GeneDouble gene1 = getIndividu(i)->getGene(j);
GeneDouble gene2 = getIndividu(i + 1)->getGene(j);
GeneDouble *gene1 = new GeneDouble();
gene1->copie(*getIndividu(i)->getGene(j));
GeneDouble *gene2 = new GeneDouble();
gene2->copie(*getIndividu(i + 1)->getGene(j));
if (r < 0.75){
getIndividu(t + i)->setGene(j, gene1);
getIndividu(t + i + 1)->setGene(j, gene2);
getIndividu(t/2 + i)->setGene(j, *gene1);
getIndividu(t/2 + i + 1)->setGene(j, *gene2);
}
else{
getIndividu(t + i)->setGene(j, gene2);
getIndividu(t + i + 1)->setGene(j, gene1);
getIndividu(t/2 + i)->setGene(j, *gene2);
getIndividu(t/2 + i + 1)->setGene(j, *gene1);
}
}
}
Expand All @@ -113,17 +115,18 @@ void AlgoGenetiqueTDJ::croisement(){
void AlgoGenetiqueTDJ::mutation(){
int t = getTaillePop();
for (int i = 0; i < t; ++i){
for(int j = 0; j < getIndividu(i)->getTailleChromosome(); ++i){
double valeurGene = getIndividu(i)->getGene(j).getValeur();
for(int j = 0; j < getIndividu(i)->getTailleChromosome(); ++j){
double valeurGene = getIndividu(i)->getGene(j)->getValeur();
double r = rand()/(double)RAND_MAX*0.1 - 0.05;
getIndividu(i)->getGene(j).setValeur( std::max(1., std::min(0., valeurGene + r)));
getIndividu(i)->getGene(j)->setValeur( std::max(0., std::min(1., valeurGene + r)));
}
}
}

void AlgoGenetiqueTDJ::affichage() {
int t = getTaillePop();
for (int i = 0; i < t; ++i){
std::cout << getIndividu(i)->getGain() << std::endl;
std::cout << i << ": " << getIndividu(i)->getGain() << " " << getIndividu(i)->getGene(0)->getValeur() <<" " << getIndividu(i)->getGene(1)->getValeur() <<" " << getIndividu(i)->getGene(2)->getValeur() <<" " << getIndividu(i)->getGene(3)->getValeur() << std::endl;
}
std::cout << " " << std::endl;
}
4 changes: 3 additions & 1 deletion src/Gene.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Gene.cpp
*
* Created on: 9 févr. 2018
* Created on: 9 f�vr. 2018
* Author: et
*/

Expand All @@ -14,3 +14,5 @@ Gene::Gene(){
Gene::~Gene(){

}


6 changes: 5 additions & 1 deletion src/GeneDouble.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@ double GeneDouble::getValeur() const{
return m_valeur;
}

void GeneDouble::setValeur(const int uneValeur){
void GeneDouble::setValeur(const double uneValeur){
m_valeur = uneValeur;
}

void GeneDouble::copie(const GeneDouble &unGene){
m_valeur = unGene.getValeur();
}
3 changes: 2 additions & 1 deletion src/GeneDouble.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class GeneDouble : public Gene {
GeneDouble(const double uneValeur);
virtual ~GeneDouble();
double getValeur() const;
void setValeur(const int uneValeur);
void setValeur(const double uneValeur);
void copie(const GeneDouble &unGene);
};
#endif /* GENEDOUBLE_H_ */
8 changes: 4 additions & 4 deletions src/Individu.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@
template<class T> class Individu {
protected:
double m_gain;
std::vector<T> m_chromosome;
std::vector<T*> m_chromosome;

public:
Individu(const int uneTailleChromosome);
Individu(const std::vector<T> &unChromosome, const int uneTailleChromosome);
Individu(std::vector<T*> &unChromosome, const int uneTailleChromosome);
virtual ~Individu();
int getTailleChromosome() const;
T getGene(const int unGene)const;
T *getGene(const int unGene);
void setGene(const int unePosition, T &unGene);
void setChromosome(const std::vector<T> &unChromosome);
void setChromosome(std::vector<T*> &unChromosome);
double getGain() const;
void setGain(const double unGain);

Expand Down
10 changes: 5 additions & 5 deletions src/Individu.inl
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@

template<class T> Individu<T>::Individu(const int uneTailleChromosome):m_gain(0){
for (int i = 0; i < uneTailleChromosome; ++i){
T gene;
T *gene = new T;
m_chromosome.push_back(gene);
} // allouer la memoire
}

}

template<class T> Individu<T>::Individu(const std::vector<T> &unChromosome, const int uneTailleChromosome):m_gain(0), m_chromosome(unChromosome){
template<class T> Individu<T>::Individu(std::vector<T*> &unChromosome, const int uneTailleChromosome):m_gain(0), m_chromosome(unChromosome){

}

Expand All @@ -26,7 +26,7 @@ template<class T> int Individu<T>::getTailleChromosome() const{
return m_chromosome.size();
}

template<class T> T Individu<T>::getGene(const int unGene) const {
template<class T> T *Individu<T>::getGene(const int unGene) {
return m_chromosome.at(unGene);

}
Expand All @@ -36,7 +36,7 @@ template<class T> void Individu<T>::setGene(const int unePosition, T &unGene){
}


template<class T> void Individu<T>::setChromosome(const std::vector<T> &unChromosome){
template<class T> void Individu<T>::setChromosome(std::vector<T*> &unChromosome){
m_chromosome = unChromosome;
}

Expand Down
13 changes: 7 additions & 6 deletions src/IndividuTDJ.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,32 +14,33 @@ IndividuTDJ::IndividuTDJ(const int uneTailleMemoire):Individu<GeneDouble>(pow(2,

}

IndividuTDJ::IndividuTDJ(const std::vector<GeneDouble> &unChromosome, const int uneTailleMemoire): Individu<GeneDouble>(unChromosome, uneTailleMemoire), m_strategie(*this, uneTailleMemoire){
IndividuTDJ::IndividuTDJ(std::vector<GeneDouble*> &unChromosome, const int uneTailleMemoire): Individu<GeneDouble>(unChromosome, uneTailleMemoire), m_strategie(*this, uneTailleMemoire){
// TODO Auto-generated destructor stub
}

IndividuTDJ::~IndividuTDJ(){
// TODO Auto-generated destructor stub
}

GeneDouble IndividuTDJ::getGene(const int unGene){
GeneDouble *IndividuTDJ::getGene(const int unGene){
return m_chromosome.at(unGene);
}

void IndividuTDJ::setGene(const int unePosition, GeneDouble &unGene){
m_chromosome.at(unePosition) = unGene;
delete(m_chromosome.at(unePosition));
m_chromosome.at(unePosition) = &unGene;
}

void IndividuTDJ::setChromosome(const std::vector<GeneDouble> &unChromosome){
void IndividuTDJ::setChromosome(std::vector<GeneDouble*> &unChromosome){
m_chromosome = unChromosome;
}

void IndividuTDJ::setRandomChromosome(){
int t = getTailleChromosome();
for (int i = 0; i < t; ++i){
double r = rand()/(double)RAND_MAX;
GeneDouble gene(r);
setGene(i, gene);
GeneDouble *gene = new GeneDouble(r);
setGene(i, *gene);
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/IndividuTDJ.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ class IndividuTDJ: public Individu<GeneDouble>{

public:
IndividuTDJ(const int uneTailleMemoire);
IndividuTDJ(const std::vector<GeneDouble> &unChromosome, const int uneTailleMemoire);
IndividuTDJ(std::vector<GeneDouble*> &unChromosome, const int uneTailleMemoire);
virtual ~IndividuTDJ();
GeneDouble getGene(const int unGene);
GeneDouble *getGene(const int unGene);
void setGene(const int unePosition, GeneDouble &unGene);
void setChromosome(const std::vector<GeneDouble> &unChromosome);
void setChromosome(std::vector<GeneDouble*> &unChromosome);
void setRandomChromosome();
StrategieEvo getStrategie() const;

Expand Down
16 changes: 10 additions & 6 deletions src/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,32 +1,36 @@
/*
* main.cpp
*
* Created on: 9 févr. 2018
* Created on: 9 f�vr. 2018
* Author: et
*/

#include <vector>
#include <iostream>

#include "StratChoix1.h"
#include "Strat01.h"
#include "StratRep.h"
#include "StratOpp.h"
#include "StratOpport.h"
#include "AGContreStratFixe.h"
#include "Jeu.h"

int main(int argc, char *argv[]) {
std::vector<int> matriceGains;
matriceGains.push_back(-1);
matriceGains.push_back(-1);
matriceGains.push_back(-10);
matriceGains.push_back(0);
matriceGains.push_back(0);
matriceGains.push_back(-10);
matriceGains.push_back(-10);
matriceGains.push_back(0);
matriceGains.push_back(-5);
matriceGains.push_back(-5);
Jeu jeu(matriceGains);
StratChoix1 strat;
StratOpport strat;
AGContreStratFixe algo(strat, jeu, 20, 20, 4);
algo.affichage();
algo.doOneGeneration();
algo.affichage();
algo.doGenerations(200);
return 0;
}

30 changes: 30 additions & 0 deletions src/Strat01.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Strat01.cpp
*
* Created on: 14 févr. 2018
* Author: emacedegastines
*/

#include "Strat01.h"

Strat01::Strat01():m_etat(1) {
// TODO Auto-generated constructor stub

}

Strat01::~Strat01() {
// TODO Auto-generated destructor stub
}

int Strat01::actionSuivante(){
m_etat = 1 - m_etat;
return m_etat;
}

void Strat01::memoriser(const int uneAction){

}

void Strat01::reinitialiser(){
m_etat = 1;
}
25 changes: 25 additions & 0 deletions src/Strat01.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
* Strat01.h
*
* Created on: 14 févr. 2018
* Author: emacedegastines
*/

#ifndef SRC_STRAT01_H_
#define SRC_STRAT01_H_

#include "Strategie.h"

class Strat01 : public Strategie{
int m_etat;
public:
Strat01();
virtual ~Strat01();
int actionSuivante();
void memoriser(const int uneAction);
void reinitialiser();

};


#endif /* SRC_STRAT01_H_ */
30 changes: 30 additions & 0 deletions src/StratOpp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* StratOpp.cpp
*
* Created on: 14 févr. 2018
* Author: emacedegastines
*/

#include "StratOpp.h"

StratOpp::StratOpp(): m_dernierChoix(0){
// TODO Auto-generated constructor stub

}

StratOpp::~StratOpp() {
// TODO Auto-generated destructor stub
}


int StratOpp::actionSuivante(){
return (1 - m_dernierChoix);
}

void StratOpp::memoriser(const int uneAction){
m_dernierChoix = uneAction;
}

void StratOpp::reinitialiser(){
m_dernierChoix = 0;
}
24 changes: 24 additions & 0 deletions src/StratOpp.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* StratOpp.h
*
* Created on: 14 févr. 2018
* Author: emacedegastines
*/

#ifndef STRATOPP_H_
#define STRATOPP_H_

#include "Strategie.h"

class StratOpp : public Strategie{
int m_dernierChoix;

public:
StratOpp();
virtual ~StratOpp();
int actionSuivante();
void memoriser(const int uneAction);
void reinitialiser();
};

#endif /* STRATOPP_H_ */
Loading

0 comments on commit a87ef9f

Please sign in to comment.