refactored

This commit is contained in:
2014-10-02 19:45:38 +02:00
parent c2ddda35d9
commit 0fd64ff7f6
23 changed files with 221 additions and 123 deletions

View File

@@ -1,29 +1,46 @@
LIBS=input constant element integrator system simulation
OBJFILES=./src/Solution.o ./src/Problem.o ./src/Network.o ./src/Neuron.o ./src/Genetics.o
include ./Makefile.const include ./Makefile.const
.PHONY: all .PHONY: all
all: lib all:|pre libs
pre:
@mkdir -p lib
libs: genetics nn
test: all test: all
make -C tests make -C tests
lib: $(LIBNAME).a $(LIBNAME).so nn: lib/NeuronNetwork.a lib/NeuronNetwork.so
lib/NeuronNetwork.so: nn_build
cp ./src/NeuronNetwork/NeuronNetwork.so ./lib/
lib/NeuronNetwork.a: nn_build
cp ./src/NeuronNetwork/NeuronNetwork.a ./lib/
cp ./src/NeuronNetwork/NeuronNetwork.nm ./lib/
nn_build:
make -C src/NeuronNetwork
genetics: lib/Genetics.a lib/Genetics.so
lib/Genetics.so: genetics_build
cp ./src/Genetics/Genetics.so ./lib/
lib/Genetics.a: genetics_build
cp ./src/Genetics/Genetics.a ./lib/
cp ./src/Genetics/Genetics.nm ./lib/
genetics_build:
make -C src/Genetics
clean: clean:
rm -f ./src/*.o @make -C src/Genetics clean
rm -f ./$(LIBNAME).so ./$(LIBNAME).a ./$(LIBNAME).nm @make -C src/NeuronNetwork clean
#@rm -f ./*.so ./*.a ./*.nm
%.o : %.cpp %.h @rm -f ./lib/*.so ./lib/*.a ./lib/*.nm
$(CXX) $(CXXFLAGS) -c $< -o $@ @echo "Cleaned....."
$(LIBNAME).so: $(OBJFILES)
$(CXX) -shared $(CXXFLAGS) $(OBJFILES) -o $(LIBNAME).so
$(LIBNAME).a: $(OBJFILES)
rm -f $(LIBNAME).a # create new library
ar rcv $(LIBNAME).a $(OBJFILES)
ranlib $(LIBNAME).a
nm --demangle $(LIBNAME).a > $(LIBNAME).nm

View File

@@ -5,4 +5,5 @@ CXXFLAGS+= -O2
#CXXFLAGS+= -pg -fPIC #CXXFLAGS+= -pg -fPIC
CXXFLAGS+= -fPIC CXXFLAGS+= -fPIC
LIBNAME=NN %.o : %.cpp %.h
$(CXX) $(CXXFLAGS) -c $< -o $@

View File

@@ -1,9 +0,0 @@
#include "Genetics"
S::XXY S::Genetics::getSolution()
{
while(1)
{
}
}

View File

@@ -1,33 +0,0 @@
#ifndef _GENETICS_H_
#define _GENETICS_H_
namespace S
{
class XXY
{
};
class PossibleSolution
{
public:
virtual void mutate()=0;
virtual PossibleSolution& combine(const PossibleSolution &s)=0;
virtual double getFitness()=0;
private:
};
template <typename _T>
class Genetics
{
public:
XXY getSolution(_T);
protected:
private:
};
}
#endif

13
src/Genetics/Genetics.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "Genetics"
using namespace Shin::Genetics;
/*S::XXY S::Genetics::getSolution()
{
while(1)
{
}
}
*/

48
src/Genetics/Genetics.h Normal file
View File

@@ -0,0 +1,48 @@
#ifndef _GENETICS_H_
#define _GENETICS_H_
namespace Shin
{
namespace Genetics
{
class XXY
{
};
class Entity
{
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();
protected:
};
template <typename _T>
class Genetics
{
public:
XXY getSolution(_T);
protected:
private:
};
}
}
#endif

21
src/Genetics/Makefile Normal file
View File

@@ -0,0 +1,21 @@
OBJFILES=./Genetics.o
LIBNAME=Genetics
include ../../Makefile.const
all: lib
lib: $(LIBNAME).so $(LIBNAME).a
$(LIBNAME).so: $(OBJFILES)
$(CXX) -shared $(CXXFLAGS) $(OBJFILES) -o $(LIBNAME).so
$(LIBNAME).a: $(OBJFILES)
rm -f $(LIBNAME).a # create new library
ar rcv $(LIBNAME).a $(OBJFILES)
ranlib $(LIBNAME).a
nm --demangle $(LIBNAME).a > $(LIBNAME).nm
clean:
@rm -f ./*.o ./*.so ./*.a ./*.nm

View File

@@ -0,0 +1,21 @@
OBJFILES=./Solution.o ./Problem.o ./Network.o ./Neuron.o
LIBNAME=NeuronNetwork
include ../../Makefile.const
all: lib
lib: $(LIBNAME).so $(LIBNAME).a
$(LIBNAME).so: $(OBJFILES)
$(CXX) -shared $(CXXFLAGS) $(OBJFILES) -o $(LIBNAME).so
$(LIBNAME).a: $(OBJFILES)
rm -f $(LIBNAME).a # create new library
ar rcv $(LIBNAME).a $(OBJFILES)
ranlib $(LIBNAME).a
nm --demangle $(LIBNAME).a > $(LIBNAME).nm
clean:
@rm -f ./*.o ./*.so ./*.a ./*.nm

View File

@@ -1,6 +1,8 @@
#include "Network" #include "Network"
S::FeedForwardNetwork::~FeedForwardNetwork() using namespace Shin::NeuronNetwork;
FeedForwardNetwork::~FeedForwardNetwork()
{ {
for(Layer *l:layers) for(Layer *l:layers)
{ {
@@ -8,7 +10,7 @@ S::FeedForwardNetwork::~FeedForwardNetwork()
} }
} }
S::Solution S::FeedForwardNetwork::solve(const S::Problem& p) Solution FeedForwardNetwork::solve(const Problem& p)
{ {
Solution s=Solution(p); Solution s=Solution(p);
for (Layer *l:layers) for (Layer *l:layers)
@@ -18,22 +20,22 @@ S::Solution S::FeedForwardNetwork::solve(const S::Problem& p)
return s; return s;
} }
void S::FeedForwardNetwork::learn(const S::Problem & problem, const S::Solution &actual) void FeedForwardNetwork::learn(const Problem & problem, const Solution &actual)
{ {
//S::Solution s= solve(p); //S::Solution s= solve(p);
} }
const S::Layer* S::FeedForwardNetwork::operator[](int layer) const Layer* FeedForwardNetwork::operator[](int layer)
{ {
return layers[layer]; return layers[layer];
} }
void S::FeedForwardNetwork::addLayer(int neurons) void FeedForwardNetwork::addLayer(int neurons)
{ {
layers.push_back(new Layer(neurons)); layers.push_back(new Layer(neurons));
} }
S::Layer::Layer(int a):neurons() Layer::Layer(int a):neurons()
{ {
while(a--) while(a--)
{ {
@@ -41,7 +43,7 @@ S::Layer::Layer(int a):neurons()
} }
} }
S::Layer::~Layer() Layer::~Layer()
{ {
for(Neuron *n:neurons) for(Neuron *n:neurons)
{ {
@@ -50,7 +52,7 @@ S::Layer::~Layer()
} }
S::Solution S::Layer::solve(const std::vector<bool> &input) Solution Layer::solve(const std::vector<bool> &input)
{ {
std::vector <bool> ret; std::vector <bool> ret;
for(Neuron *n:neurons) for(Neuron *n:neurons)
@@ -60,7 +62,7 @@ S::Solution S::Layer::solve(const std::vector<bool> &input)
return ret; return ret;
} }
S::Neuron* S::Layer::operator[](int neuron) const Neuron* Layer::operator[](int neuron) const
{ {
return neurons[neuron]; return neurons[neuron];
} }

View File

@@ -1,19 +1,22 @@
#ifndef _NN_H_ #ifndef _S_NN_NN_H_
#define _NN_H_ #define _S_NN_NN_H_
#include "Problem" #include "Problem"
#include "Solution" #include "Solution"
#include "Neuron" #include "Neuron"
#include <cstdarg> #include <cstdarg>
#include <vector> #include <vector>
namespace S namespace Shin
{
namespace NeuronNetwork
{ {
class Network class Network
{ {
public: public:
virtual Solution solve(const S::Problem&)=0; virtual Solution solve(const Problem&)=0;
virtual void learn(const S::Problem & p, const S::Solution &s)=0; virtual void learn(const Problem & p, const Solution &s)=0;
protected: protected:
private: private:
}; };
@@ -41,12 +44,12 @@ namespace S
class FeedForwardNetwork : public ACyclicNetwork class FeedForwardNetwork : public ACyclicNetwork
{ {
public: public:
template<typename... Args>inline FeedForwardNetwork(Args &&... args) {pass((addLayer(args),1)...);}; template<typename... Args>inline FeedForwardNetwork(Args &&... args) {pass((addLayer(args),1)...);};
//inline FeedForwardNetwork(std::vector<int> q); //inline FeedForwardNetwork(std::vector<int> q);
~FeedForwardNetwork(); ~FeedForwardNetwork();
virtual Solution solve(const S::Problem& p) override;
virtual void learn(const S::Problem & p, const S::Solution &s) override; virtual Solution solve(const Problem& p) override;
virtual void learn(const Problem & p, const Solution &s) override;
const Layer* operator[](int layer); const Layer* operator[](int layer);
protected: protected:
template<typename... Args> inline void pass(Args&&...) {}; template<typename... Args> inline void pass(Args&&...) {};
@@ -57,5 +60,6 @@ namespace S
std::vector<Layer*> layers; std::vector<Layer*> layers;
}; };
}
} }
#endif #endif

View File

@@ -1,22 +1,22 @@
#include "./Neuron" #include "./Neuron"
#include <iostream> using namespace Shin::NeuronNetwork;
S::Neuron::Neuron(): potential(1),weights() Neuron::Neuron(): potential(1),weights()
{ {
} }
double S::Neuron::getPotential() const double Neuron::getPotential() const
{ {
return potential; return potential;
} }
void S::Neuron::setPotential(double p) void Neuron::setPotential(double p)
{ {
potential=p; potential=p;
} }
double S::Neuron::getWeight(unsigned int i) const double Neuron::getWeight(unsigned int i) const
{ {
if(i >= weights.size()) if(i >= weights.size())
{ {
@@ -25,7 +25,7 @@ double S::Neuron::getWeight(unsigned int i) const
return weights[0]; return weights[0];
} }
void S::Neuron::setWeight(unsigned int i,double p) void Neuron::setWeight(unsigned int i,double p)
{ {
if(i >= weights.size()) if(i >= weights.size())
{ {
@@ -36,7 +36,7 @@ void S::Neuron::setWeight(unsigned int i,double p)
weights[i]=p; weights[i]=p;
} }
bool S::Neuron::activates(std::vector<bool> input) bool Neuron::activates(std::vector<bool> input)
{ {
double sum=0; double sum=0;
for(unsigned int i=0;i<input.size();i++) for(unsigned int i=0;i<input.size();i++)

View File

@@ -1,9 +1,12 @@
#ifndef _NEURON_H_ #ifndef _S_NN_NEURON_H_
#define _NEURON_H_ #define _S_NN_NEURON_H_
#include <vector> #include <vector>
namespace S{ namespace Shin
{
namespace NeuronNetwork
{
class Neuron class Neuron
{ {
public: public:
@@ -22,4 +25,5 @@ namespace S{
{ {
}; };
} }
}
#endif #endif

View File

@@ -0,0 +1,13 @@
#include "Problem"
using namespace Shin::NeuronNetwork;
Problem::Problem()
{
}
Problem::operator std::vector<bool>() const
{
return representation();
}

View File

@@ -3,7 +3,9 @@
#include <vector> #include <vector>
namespace S namespace Shin
{
namespace NeuronNetwork
{ {
class Problem class Problem
{ {
@@ -15,5 +17,5 @@ namespace S
private: private:
}; };
} }
}
#endif #endif

View File

@@ -0,0 +1,23 @@
#include "./Solution"
using namespace Shin::NeuronNetwork;
Solution::Solution(std::vector<bool>solution):solution(solution)
{
}
bool Solution::operator[](int pos)
{
return solution[pos];
}
int Solution::size()
{
return solution.size();
}
Solution::operator std::vector<bool>()
{
return solution;
}

View File

@@ -4,7 +4,9 @@
#include <vector> #include <vector>
#include "Problem" #include "Problem"
namespace S namespace Shin
{
namespace NeuronNetwork
{ {
class Solution class Solution
{ {
@@ -17,5 +19,6 @@ namespace S
std::vector<bool> solution; std::vector<bool> solution;
}; };
} }
}
#endif #endif

View File

@@ -1,11 +0,0 @@
#include "Problem"
S::Problem::Problem()
{
}
S::Problem::operator std::vector<bool>() const
{
return representation();
}

View File

@@ -1,21 +0,0 @@
#include "./Solution"
S::Solution::Solution(std::vector<bool>solution):solution(solution)
{
}
bool S::Solution::operator[](int pos)
{
return solution[pos];
}
int S::Solution::size()
{
return solution.size();
}
S::Solution::operator std::vector<bool>()
{
return solution;
}