002
18.11.2009, 14:14 Uhr
lordZ
|
hallo,
mittlerweile bin ich soweit gekommen dass der debugger auch abschmiert
hab leider immer noch keinen plan warum das fehlverhalten auftritt;
hehe, code gerne, is halt kommentarlos "war nicht mehr wirklich motitiert gestern um 5
Gen.h:
C++: |
#pragma once #include <string> #include <iostream>
#include <ctime> #include <cstdlib>
using namespace std; class Gen { public: std::size_t lenght; std::string* dna; double fitness; Gen(std::string* init,std::size_t lenght,double fitness); ~Gen(void); Gen* getPointer(void);
private: std::string* getRandomDNA(std::size_t lenght); };
|
GenPool.h:
C++: |
#pragma once #include <iostream> #include <boost/ptr_container/ptr_vector.hpp> #include <string> #include "Gen.h" using namespace std;
class GenPool { private: boost::ptr_vector<Gen> Population; std::size_t poolSize; std::size_t dnalenght; std::size_t Generation; boost::ptr_vector<Gen> Roulette; std::string* dna_combine(std::string* dna1, std::string* dna2); public: void fillRoulette(void); void setNextGeneration(void); GenPool(std::size_t poolsize,std::size_t dnalenght); ~GenPool(void); void print(void); };
|
NeuroNetToolbox.h
C++: |
#pragma once #include <iostream> #include <boost/ptr_container/ptr_vector.hpp> #include "GenPool.h" using namespace std;
class NeuroNetToolbox { const static std::size_t GenPools_count = 2; const static std::size_t GenPools_size = 5; const static std::size_t GenPools_DNA_SIZE = 10; boost::ptr_vector<GenPool> GenPools; bool training; public: NeuroNetToolbox(bool training); ~NeuroNetToolbox(void); void NeuroNetEvolve(void); void print(void); };
|
Gen.cpp
C++: |
#include "Gen.h"
int get_rnd(void){ return(rand()); }
Gen::Gen(std::string* init,std::size_t lenght,double fitness) { if(fitness!=-1){ this->lenght=lenght; this->dna=init; this->fitness=fitness; }else{ this->fitness=rand()%100; this->lenght=lenght; this->dna=this->getRandomDNA(this->lenght); } } std::string* Gen::getRandomDNA(std::size_t lenght){ std::string* tmp= new std::string(); for(std::size_t i=0;i<lenght;i++){ if(((get_rnd()%2)==1)){ tmp->push_back(char('1')); }else{ tmp->push_back(char('0')); } } return (tmp); }
Gen::~Gen(void) { delete (this->dna); this->dna=0; }
Gen* Gen::getPointer(void){ return this; }
|
GenPool.cpp
C++: |
#include "GenPool.h"
GenPool::GenPool(std::size_t poolsize,std::size_t dnalenght) { this->poolSize=poolsize; this->dnalenght=dnalenght; this->Generation=0; this->Population.clear(); for(std::size_t i=0;i<this->poolSize;i++){ this->Population.push_back(new Gen(NULL,this->dnalenght,-1)); } }
void GenPool::print(void){ std::cout << " DNA Population:"<<this->Population.size()<< ", Generation:"<<this->Generation<<":: "; for(std::size_t i=0;i<this->Population.size();i++){ for(std::size_t j=0;j<this->Population[i].dna->size();j++){ std::cout << this->Population[i].dna->at(j); } std::cout << " "; } std::cout << "\n"; }
void GenPool::fillRoulette(){ //std::cout << "\nFILLROULETTE "; for(boost::ptr_vector<Gen>::iterator i=this->Population.begin();i!=this->Population.end();i++){ for(std::size_t j=0;j<i->fitness;j++){ this->Roulette.push_back(i->getPointer()); } } //std::cout << this->Roulette.size()<< "\n"; }
void GenPool::setNextGeneration(){ this->Generation++; std::size_t count=this->Population.size();
std::string** tmp=new std::string*[count]; for(std::size_t i=0;i<count;i++){ std::size_t gen1=rand()%(this->Roulette.size()); std::size_t gen2=rand()%(this->Roulette.size()); tmp[i]=this->dna_combine(this->Roulette[gen1].dna,this->Roulette[gen2].dna); }
//CLEAN OLD GENS Gen* tmpgen=0; for(std::size_t i=0;i<this->Population.size();i++){ tmpgen=this->Population[i].getPointer(); tmpgen->~Gen(); delete tmpgen; } for(std::size_t i=0;i<this->Roulette.size();i++){ tmpgen=this->Roulette[i].getPointer(); tmpgen->~Gen(); delete tmpgen; }
//PUSH NEW POPULATION for(std::size_t i=0;i<count;i++){ this->Population.push_back(new Gen(tmp[i],this->dnalenght,(rand()%100))); } }
std::string* GenPool::dna_combine(std::string* dna1, std::string* dna2){ std::string* tmp = new std::string(); std::size_t cut=rand()%(this->dnalenght); /* for(std::size_t j=0;j<dna1->size();j++){ std::cout << dna1->at(j); } std::cout << " "; for(std::size_t j=0;j<dna2->size();j++){ std::cout << dna2->at(j); } */ for(std::size_t i=0;i<cut;i++){ tmp->push_back(dna1->at(i)); } for(std::size_t i=cut;i<dna2->size();i++){ tmp->push_back(dna2->at(i)); } /* std::cout << " "; for(std::size_t j=0;j<tmp->size();j++){ std::cout << tmp->at(j); } std::cout << "\n"; */ return tmp; }
GenPool::~GenPool(void) { }
|
NeuroNetToolbox.cpp
C++: |
#include "NeuroNetToolbox.h" #include <iostream> NeuroNetToolbox::NeuroNetToolbox(bool training) { this->training=training; for(std::size_t i=0;i<this->GenPools_count;i++){ this->GenPools.push_back(new GenPool(this->GenPools_size,this->GenPools_DNA_SIZE)); } }
void NeuroNetToolbox::print(void){ std::cout << "####DUMP OF NEURONET####\n\nPopulations:" <<this->GenPools_count<<", Populationsize:"<<this->GenPools_size<<", DNAlenght:"<<this->GenPools_DNA_SIZE<<"\n"; for(std::size_t i=0;i<this->GenPools_count;i++){ this->GenPools[i].print(); } }
void NeuroNetToolbox::NeuroNetEvolve(void){ for(std::size_t i=0;i<this->GenPools_count;i++){ this->GenPools[i].fillRoulette(); } for(std::size_t i=0;i<this->GenPools_count;i++){ this->GenPools[i].setNextGeneration(); } }
NeuroNetToolbox::~NeuroNetToolbox(void) {
}
|
AI_start.cpp:
C++: |
#include "NeuroNetToolbox.h" //#include <boost/ptr_container/ptr_vector.hpp>
int main(void){ srand((unsigned) time(NULL)); NeuroNetToolbox* NN = new NeuroNetToolbox(true); int i=0; while(i<10){ NN->print(); NN->NeuroNetEvolve(); i++; } }
|
wär auch an generellen informationen bezüglich debugen intressiert, da ich mal davon ausgehe dass das nicht mein einzigstes problem sein wird. Dieser Post wurde am 18.11.2009 um 14:29 Uhr von lordZ editiert. |