This commit is contained in:
2014-10-19 22:30:08 +02:00
parent 0a901c7e4b
commit 0238312a5b
15 changed files with 561 additions and 37 deletions

View File

@@ -3,7 +3,7 @@ CXXFLAGS+= -Wall -std=c++14
CXXFLAGS+= -g
CXXFLAGS+= -O2
#CXXFLAGS+= -pg -fPIC
CXXFLAGS+= -fPIC
CXXFLAGS+= -fPIC -pthread
%.o : %.cpp %.h
$(CXX) $(CXXFLAGS) -c $< -o $@

19
src/Genetics/Except.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef _GENETICS_EXCEPT_H_
#define _GENETICS_EXCEPT_H_
#include <string>
namespace Shin
{
namespace Genetics
{
class Exception
{
public:
Exception(const std::string &s): str(s) {};
const char* what()const noexcept {return str.c_str();};
protected:
std::string str;
};
}
}
#endif

79
src/Genetics/Generation.h Normal file
View File

@@ -0,0 +1,79 @@
#ifndef _GENETICS_GENERATION_H_
#define _GENETICS_GENERATION_H_
#include <vector>
#include <float.h>
#include <cstddef>
#include "Individual.h"
namespace Shin
{
namespace Genetics
{
template <class _T>
class Generation
{
public:
/* constructors and so */
Generation ():individual() {}
Generation (const Generation &old):individual(old.individual) {}
Generation& operator=(const Generation &g) {individual=g.individual; return *this;}
/* generation manipulation */
size_t size() {return individual.size();}
_T& operator[](const size_t i) { return individual[i];}
void add(const _T &a) { individual.push_back(a);}
inline auto begin() {return individual.begin();};
inline auto end() {return individual.end();};
/* different fitness counting */
double absoluteFitness()
{
return 0.0;
}
double relativeFiness()
{
return 0.0;
}
double bestFitness()
{
double f=DBL_MIN;
for(const _T &a:individual)
if(f < a.getFitness())
f=a.getFitness();
return f;
}
double worstFitness()
{
double f=DBL_MAX;
for(const _T &a:individual)
if(f > a.getFitness())
f=a.getFitness();
return f;
}
double averageFitness()
{
double f=0;
for(const _T &a:individual)
f+=a.getFitness();
return f/individual.size();
}
protected:
std::vector<_T> individual;
};
}
}
#endif

View File

@@ -0,0 +1,94 @@
#ifndef _GENETICS_GENCREATOR_H_
#define _GENETICS_GENCREATOR_H_
#include "Generation.h"
#include "Individual.h"
namespace Shin
{
namespace Genetics
{
template <class _T>
class GenerationCreater
{
public:
Generation<_T> operator()(Generation<_T> &g) {return generate(g);}
virtual Generation<_T> generate(Generation<_T> &gen)=0;
virtual ~GenerationCreater() {}
protected:
unsigned maxGenerationSize=150;
/* static void run(GenerationCreater* r,unsigned long from, unsigned long to,unsigned long fitness, Generation *gen, Generation *s)
{ r->runner(from,to,fitness,gen,s); }
virtual void runner(unsigned long from, unsigned long to,unsigned long fitness, Generation *gen, Generation *s)=0;
*/
};
template <class _T>
class Roulete: public GenerationCreater<_T>
{
public:
Generation<_T> generate(Generation<_T> &gen) override;
protected:
};
}
}
template <class _T>
Shin::Genetics::Generation< _T > Shin::Genetics::Roulete<_T>::generate(Shin::Genetics::Generation< _T >& gen)
{
Generation<_T> newGen;
long fitness=0;
for(const _T &a:gen)
{
fitness+=a.fitness();
}
double avFitness=gen.averageFitness();
for(unsigned int i=0;i<gen.size() && i < this->maxGenerationSize*2/3;i++)
{
if(gen[i].getFitness() >= avFitness)
{
newGen.add(gen[i]);
if(rand()%20==0)
{
newGen.add(gen[i].combine(gen[i]));
newGen[newGen.size()-1].mutate();
}
}
}
if(fitness==0)
fitness++;
while(newGen.size()< this->maxGenerationSize)
{
unsigned int x=rand()%(fitness+1);
unsigned int y=rand()%(fitness+1);
unsigned int xIndividual=0;
unsigned int yIndividual=0;
while( x > 0 && xIndividual < gen.size())
{
x-=gen[xIndividual].fitness();
if(x>0)
++xIndividual;
}
while(y>0 && yIndividual <gen.size())
{
y-=gen[yIndividual].getFitness();
if(y>0)
yIndividual++;
}
xIndividual=xIndividual%(gen.size());
yIndividual=yIndividual%(gen.size());
newGen.add(gen[xIndividual].combine(gen[yIndividual]));
if(rand()%20==0)
newGen[newGen.size()-1].mutate();
}
return newGen;
}
#endif

View File

@@ -2,12 +2,97 @@
using namespace Shin::Genetics;
/*S::XXY S::Genetics::getSolution()
/*
Generation Roulete::generate(Generation& gen)
{
while(1)
Generation s;
long fitness=0;
for (unsigned int i=0;i<gen.size();i++)
fitness+=abs(gen[i]->getFitness());
unsigned int size=150;
double avFitness=(gen.bestFitness()+gen.averageFitness())/2;
for(unsigned int i=0;i<gen.size() && i < size*2/3;i++)
{
if(gen[i]->getFitness() > avFitness)
{
s.add(gen[i]);
if(rand()%20==0)
{
s.add(gen[i]->combine(gen[i]));
s[s.size()-1]->mutate();
}
}
}
size-=s.size();
if(size>gen.size())
size=gen.size();//+(gen.size()+1)/2;
if(fitness==0)
fitness++;
std::vector <std::thread *> threads;
if((size > 1000))
{
long step=size/4;
for(int i=0;i<4;i++)
{
std::cout << "f: "<< (i*step) << ", t: "<< ((i+1)*step) << "\n";
std::thread *a= new std::thread(run,this,i*step,i==3?(size+1):((i+1)*step+1),fitness,&gen,&s);
// a->join();
// delete a;
threads.push_back(a);
}
}else
{
this->runner(0,size+1,fitness,&gen,&s);
// std::thread *a= new std::thread(run,this,0,size+1,fitness,&gen,&s);
// threads.push_back(a);
}
for(std::thread *a:threads)
{
a->join();
delete a;
}
return s;
}
void Roulete::runner(long unsigned int from, long unsigned int to,unsigned long fitness, Generation *gen, Generation *s)
{
for(int i=from;i<to;i++)
{
unsigned int x=rand()%(fitness+1);
unsigned int y=rand()%(fitness+1);
unsigned int xIndividual=0;
unsigned int yIndividual=0;
while( x > 0 && xIndividual < gen->size())
{
x-=gen->operator[](xIndividual)->getFitness();
if(x>0)
++xIndividual;
}
while(y>0 && yIndividual <gen->size())
{
y-=gen->operator[](yIndividual)->getFitness();
if(y>0)
yIndividual++;
}
xIndividual=xIndividual%(gen->size());
yIndividual=yIndividual%(gen->size());
// std::cout << fitness << " - " << xIndividual <<" - " << yIndividual << "\n";
s->add(gen->operator[](xIndividual)->combine(gen->operator[](yIndividual)));
if(rand()%20==0)
s->operator[](s->size()-1)->mutate();
}
}
*/

View File

@@ -1,44 +1,73 @@
#ifndef _GENETICS_H_
#define _GENETICS_H_
#include "Generation.h"
#include "Except.h"
#include "GenerationCreater.h"
#include <vector>
#include <iostream>
#include <thread>
#include <mutex>
namespace Shin
{
namespace Genetics
{
class XXY
{
};
class Entity
/*
class Roulete: public GenerationCreater
{
public:
virtual void mutate()=0;
virtual Entity& combine(const Entity &s)=0;
virtual double getFitness()=0;
private:
};
class Generation
{
public:
double absoluteFitness();
double relativeFiness();
double bestFitness();
double worstFitness();
double averageFitness();
unsigned long size();
Generation generate(Generation &gen) override;
protected:
virtual void runner(unsigned long from, unsigned long to,unsigned long fitness, Generation *gen, Generation *s);
};
template <typename _T>
*/
template <class _T, typename _C=Roulete<_T>>
class Genetics
{
public:
XXY getSolution(_T);
Genetics():c(*new _C()),generation(),deleteCreater(1) {}
Genetics(GenerationCreater<_T> *gc):c(gc),generation() {}
virtual ~Genetics()
{
if(deleteCreater)
delete &c;
}
void addIndividual (const _T &ind) { generation.add(ind); }
_T& getSolution(int maxGenerations,int targetFitness)
{
srand(time(NULL));
for(int round=0;round<maxGenerations;round++)
{
makeRound(round);
std::cout << "Round: " << round << " " << generation.size() << " " << generation.bestFitness() <<" - " << generation.worstFitness() << "\n";
for(_T& t:generation)
{
if(t.getFitness()>=targetFitness)
return t;
}
}
double maxFitness=generation.bestFitness();
for(_T& t:generation)
{
if(t.getFitness()==maxFitness)
return t;
};
throw Exception("Error finding individual with best Fitess");
}
void makeRound(const int round)
{
if(round%500==1)
srand(time(NULL));
generation=c(generation);
}
protected:
GenerationCreater<_T> &c;
Generation<_T> generation;
bool deleteCreater=0;
private:
};

View File

@@ -0,0 +1,3 @@
#include "Individual.h"

18
src/Genetics/Individual.h Normal file
View File

@@ -0,0 +1,18 @@
#ifndef _GENETICS_INDIVIDUAL_H
#define _GENETICS_INDIVIDUAL_H
namespace Shin
{
namespace Genetics
{
class Individual
{
public:
virtual void mutate()=0;
virtual double getFitness() const=0;
inline double fitness() const {return getFitness();}
private:
};
}
}
#endif // INDIVIDUAL_H

View File

@@ -1,4 +1,4 @@
OBJFILES=./Genetics.o
OBJFILES=./Individual.o ./Genetics.o
LIBNAME=Genetics
@@ -8,10 +8,10 @@ all: lib
lib: $(LIBNAME).so $(LIBNAME).a
$(LIBNAME).so: $(OBJFILES)
$(CXX) -shared $(CXXFLAGS) $(OBJFILES) -o $(LIBNAME).so
$(LIBNAME).so: $(OBJFILES) ./Genetics.h ./Generation.h ./GenerationCreater.h
$(CXX) -shared $(CXXFLAGS) $(OBJFILES) -o $(LIBNAME).so -lpthread
$(LIBNAME).a: $(OBJFILES)
$(LIBNAME).a: $(OBJFILES) ./Genetics.h ./Generation.h ./GenerationCreater.h
rm -f $(LIBNAME).a # create new library
ar rcv $(LIBNAME).a $(OBJFILES)
ranlib $(LIBNAME).a

View File

@@ -1,7 +1,11 @@
include ../Makefile.const
LIB_DIR = ../lib
ALL_TESTS=01 02 03
GEN_TESTS=g-01
#g-02
NN_TESTS=nn-01 nn-02 nn-03
ALL_TESTS=$(NN_TESTS) $(GEN_TESTS)
LIBS=$(LIB_DIR)/Genetics.a $(LIB_DIR)/NeuronNetwork.a
#LIBS=-lGenetics.so -lNeuronNetwork
@@ -9,11 +13,17 @@ CXXFLAGS += -I$(LIB_DIR)
all:| lib $(ALL_TESTS);
gen: $(GEN_TESTS)
test: all
@for i in $(ALL_TESTS); do echo -n ./$$i; echo -n " - "; ./$$i; echo ""; done
%: %.cpp $(LIBS)
$(CXX) $(CXXFLAGS) -o $@ $< $ $(LIBS) -lm
g-%: g-%.cpp $(LIB_DIR)/Genetics.a
$(CXX) $(CXXFLAGS) -o $@ $< $ $(LIB_DIR)/Genetics.a $(LIB_DIR)/NeuronNetwork.a -lm
nn-%: nn-%.cpp $(LIB_DIR)/NeuronNetwork.a
$(CXX) $(CXXFLAGS) -o $@ $< $ $(LIB_DIR)/NeuronNetwork.a -lm
lib:
make -C ../
make -C ../

69
tests/g-01.cpp Normal file
View File

@@ -0,0 +1,69 @@
#include "../src/Genetics/Genetics.h"
#include <iostream>
class S: public Shin::Genetics::Individual
{
public:
S(const double &a, const double &b) :Sa(a),Q(b)
{
};
void mutate()
{
if(rand()%2==1)
{
Sa=(Sa*(double)(80.0+(double)(rand()%61)))/100.0+(40-rand()%80);
}
if(rand()%2==1)
{
Sa=-Sa;
}
if(rand()%2==1)
{
Q=(Q*(double)(60.0+(double)(rand()%81)))/100.0+(40-rand()%80);
}
if(rand()%2==1)
{
Q=-Q;
}
};
S combine(S &s)
{
double a;
double b;
if( rand()%2)
a=s.Sa;
else
a=Sa;
if( rand()%2)
b=s.Q;
else
b=Q;
return S(a,b);
}
double getFitness() const
{
return (double)1.0/(double)(Sa);
//double s=abs(Sa-98545)+abs(Q+85);
//return Sa*100-Q*5;
//return 985258-s;
//return s < 0?0:s;
}
double Sa;
double Q;
void dump()
{
std::cout<< " Sa: " << Sa <<" Q: " << Q << " fitness: " <<getFitness() << "\n";
}
};
int main()
{
Shin::Genetics::Genetics<S> g;
g.addIndividual(S(1,0));
g.addIndividual(S(1,50));
g.addIndividual(S(50,50));
S &s=g.getSolution(10000,99999999);
s.dump();
}

118
tests/g-02.cpp Normal file
View File

@@ -0,0 +1,118 @@
#include "../src/Genetics/Genetics.h"
#include "../src/NeuronNetwork/Network"
#include <iostream>
class X: public Shin::NeuronNetwork::Problem
{
public:
X(bool a,bool b):a(a),b(b)
{
}
protected:
std::vector<bool> representation() const
{
return std::vector<bool>({a,b});
}
bool a,b;
};
class S: public Shin::Genetics::Individual
{
public:
S():n(2,4,1)
{
}
void mutate()
{
for(int i=0;i<3;i++)
{
for (int j=0;j<n[i]->size();j++)
{
if(rand()%20==1)
{
if(rand()%2==1)
n[i]->operator[](j)->setPotential(n[i]->operator[](j)->getPotential()-0.5);
else
n[i]->operator[](j)->setPotential(n[i]->operator[](j)->getPotential()+0.5);
}
int k;
if(i==0)
k=0;
else if(i==1)
k=2;
else
k=3;
for(;k>=0;--k)
if(rand()%20==0)
{
if(rand()%2)
n[i]->operator[](j)->setWeight(k,n[i]->operator[](j)->getWeight(k)-1);
else
n[i]->operator[](j)->setWeight(k,n[i]->operator[](j)->getWeight(k)+1);
}
}
}
};
S* SQ(S *s)
{
S * a= new S();
for(int i=0;i<3;i++)
{
for (int j=0;j<n[i]->size();j++)
{
Shin::NeuronNetwork::Neuron *q;
if(rand()%2==1)
{
q=n[i]->operator[](j);
}else
{
q=s->n[i]->operator[](j);
}
a->n[i]->operator[](j)->setPotential(q->getPotential());
int k;
if(i==0)
k=0;
else if(i==1)
k=2;
else
k=3;
for(;k>=0;--k)
a->n[i]->operator[](j)->setWeight(k,q->getWeight(k));
}
}
return a;
}
Individual* combine(Individual *s)
{
return SQ(dynamic_cast<S*>(s));
}
Shin::NeuronNetwork::FeedForwardNetwork n;
double getFitness()
{
int a=0;
if(n.solve(X(1,1))[0]==0)
a++;
if(n.solve(X(1,0))[0]==1)
a++;
if(n.solve(X(0,1))[0]==1)
a++;
if(n.solve(X(0,0))[0]==0)
a++;
return a;
}
void dump()
{
}
};
int main()
{
Shin::Genetics::Genetics<S> g;
S* s=(S*)g.getSolution(99999,999999);
s->dump();
}