118 lines
2.0 KiB
C++
118 lines
2.0 KiB
C++
#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(unsigned int i=0;i<n.size();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)
|
|
continue;
|
|
else if(i==1)
|
|
k=1;
|
|
else
|
|
k=3;
|
|
for(;k>=0;--k)
|
|
{
|
|
std::cerr << "i: "<<i <<" " << k << std::endl;
|
|
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 combine(S &s)
|
|
{
|
|
S a;
|
|
for(int i=0;i<3;i++)
|
|
{
|
|
for (int j=0;j<s.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;
|
|
}
|
|
|
|
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;
|
|
g.addIndividual(S());
|
|
S &s=g.getSolution(99999,999999);
|
|
s.dump();
|
|
} |