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

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();
}