cleaning
This commit is contained in:
@@ -1,71 +0,0 @@
|
||||
#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()
|
||||
{
|
||||
// return fit;
|
||||
return abs(Sa-98545)+abs(Q+85);
|
||||
return (double)1.0/(double)(Sa);
|
||||
//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.getCreator().setMaxGenerationSize(30);
|
||||
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
118
tests/g-02.cpp
@@ -1,118 +0,0 @@
|
||||
#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();
|
||||
}
|
||||
@@ -13,6 +13,7 @@ class X: public Shin::Problem
|
||||
X(const std::vector<bool> &a):Problem() { for (bool s:a) data.push_back((float)s);}
|
||||
protected:
|
||||
};
|
||||
|
||||
int main(int argc,char**)
|
||||
{
|
||||
srand(time(NULL));
|
||||
|
||||
Reference in New Issue
Block a user