yeah
This commit is contained in:
@@ -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
69
tests/g-01.cpp
Normal 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
118
tests/g-02.cpp
Normal 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();
|
||||
}
|
||||
Reference in New Issue
Block a user