init
This commit is contained in:
15
src/Genetics
Normal file
15
src/Genetics
Normal file
@@ -0,0 +1,15 @@
|
||||
#ifndef _GENETICS_H_
|
||||
#define _GENETICS_H_
|
||||
|
||||
namespace S
|
||||
{
|
||||
class Genetics
|
||||
{
|
||||
public:
|
||||
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
2
src/Genetics.cpp
Normal file
2
src/Genetics.cpp
Normal file
@@ -0,0 +1,2 @@
|
||||
#include "Genetics"
|
||||
|
||||
1
src/Genetics.h
Symbolic link
1
src/Genetics.h
Symbolic link
@@ -0,0 +1 @@
|
||||
./Genetics
|
||||
61
src/Network
Normal file
61
src/Network
Normal file
@@ -0,0 +1,61 @@
|
||||
#ifndef _NN_H_
|
||||
#define _NN_H_
|
||||
|
||||
#include "Problem"
|
||||
#include "Solution"
|
||||
#include "Neuron"
|
||||
#include <cstdarg>
|
||||
#include <vector>
|
||||
|
||||
namespace S
|
||||
{
|
||||
class Network
|
||||
{
|
||||
public:
|
||||
virtual Solution solve(const S::Problem&)=0;
|
||||
virtual void learn(const S::Problem & p, const S::Solution &s)=0;
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
class ACyclicNetwork : public Network
|
||||
{
|
||||
public:
|
||||
protected:
|
||||
private:
|
||||
};
|
||||
|
||||
class Layer
|
||||
{
|
||||
public:
|
||||
Layer(int a);
|
||||
~Layer();
|
||||
Solution solve(const std::vector<bool> &input);
|
||||
Neuron* operator[](int neuron) const;
|
||||
int size() const {return neurons.size();};
|
||||
protected:
|
||||
std::vector<Neuron*> neurons;
|
||||
};
|
||||
|
||||
// template <typename _NT>
|
||||
class FeedForwardNetwork : public ACyclicNetwork
|
||||
{
|
||||
public:
|
||||
|
||||
template<typename... Args>inline FeedForwardNetwork(Args &&... args) {pass((addLayer(args),1)...);};
|
||||
//inline FeedForwardNetwork(std::vector<int> q);
|
||||
~FeedForwardNetwork();
|
||||
virtual Solution solve(const S::Problem& p) override;
|
||||
virtual void learn(const S::Problem & p, const S::Solution &s) override;
|
||||
const Layer* operator[](int layer);
|
||||
protected:
|
||||
template<typename... Args> inline void pass(Args&&...) {};
|
||||
void addLayer(int neurons);
|
||||
private:
|
||||
Layer* first;
|
||||
Layer* last ;
|
||||
std::vector<Layer*> layers;
|
||||
};
|
||||
|
||||
}
|
||||
#endif
|
||||
67
src/Network.cpp
Normal file
67
src/Network.cpp
Normal file
@@ -0,0 +1,67 @@
|
||||
#include "Network"
|
||||
|
||||
S::FeedForwardNetwork::~FeedForwardNetwork()
|
||||
{
|
||||
for(Layer *l:layers)
|
||||
{
|
||||
delete l;
|
||||
}
|
||||
}
|
||||
|
||||
S::Solution S::FeedForwardNetwork::solve(const S::Problem& p)
|
||||
{
|
||||
Solution s=Solution(p);
|
||||
for (Layer *l:layers)
|
||||
{
|
||||
s=l->solve(s);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
void S::FeedForwardNetwork::learn(const S::Problem & problem, const S::Solution &actual)
|
||||
{
|
||||
//S::Solution s= solve(p);
|
||||
}
|
||||
|
||||
const S::Layer* S::FeedForwardNetwork::operator[](int layer)
|
||||
{
|
||||
return layers[layer];
|
||||
}
|
||||
|
||||
void S::FeedForwardNetwork::addLayer(int neurons)
|
||||
{
|
||||
layers.push_back(new Layer(neurons));
|
||||
}
|
||||
|
||||
S::Layer::Layer(int a):neurons()
|
||||
{
|
||||
while(a--)
|
||||
{
|
||||
neurons.push_back(new Neuron());
|
||||
}
|
||||
}
|
||||
|
||||
S::Layer::~Layer()
|
||||
{
|
||||
for(Neuron *n:neurons)
|
||||
{
|
||||
delete n;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
S::Solution S::Layer::solve(const std::vector<bool> &input)
|
||||
{
|
||||
std::vector <bool> ret;
|
||||
for(Neuron *n:neurons)
|
||||
{
|
||||
ret.push_back(n->activates(input));
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
S::Neuron* S::Layer::operator[](int neuron) const
|
||||
{
|
||||
return neurons[neuron];
|
||||
}
|
||||
|
||||
1
src/Network.h
Symbolic link
1
src/Network.h
Symbolic link
@@ -0,0 +1 @@
|
||||
./Network
|
||||
25
src/Neuron
Normal file
25
src/Neuron
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef _NEURON_H_
|
||||
#define _NEURON_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace S{
|
||||
class Neuron
|
||||
{
|
||||
public:
|
||||
Neuron();
|
||||
double getPotential() const;
|
||||
void setPotential(double p);
|
||||
double getWeight(unsigned int) const;
|
||||
void setWeight(unsigned int i,double p);
|
||||
bool activates(const std::vector<bool>);
|
||||
protected:
|
||||
double potential;
|
||||
private:
|
||||
std::vector<double> weights;
|
||||
};
|
||||
class SimpleNeuron: public Neuron
|
||||
{
|
||||
};
|
||||
}
|
||||
#endif
|
||||
51
src/Neuron.cpp
Normal file
51
src/Neuron.cpp
Normal file
@@ -0,0 +1,51 @@
|
||||
#include "./Neuron"
|
||||
|
||||
#include <iostream>
|
||||
|
||||
S::Neuron::Neuron(): potential(1),weights()
|
||||
{
|
||||
|
||||
}
|
||||
double S::Neuron::getPotential() const
|
||||
{
|
||||
return potential;
|
||||
}
|
||||
|
||||
void S::Neuron::setPotential(double p)
|
||||
{
|
||||
potential=p;
|
||||
}
|
||||
|
||||
double S::Neuron::getWeight(unsigned int i) const
|
||||
{
|
||||
if(i >= weights.size())
|
||||
{
|
||||
return 1.0;
|
||||
}
|
||||
return weights[0];
|
||||
}
|
||||
|
||||
void S::Neuron::setWeight(unsigned int i,double p)
|
||||
{
|
||||
if(i >= weights.size())
|
||||
{
|
||||
// std::cout << "resize to " << i;
|
||||
weights.resize(i+1);
|
||||
}
|
||||
// std::cerr << "Set " << i << " to " << p << "\n";
|
||||
weights[i]=p;
|
||||
}
|
||||
|
||||
bool S::Neuron::activates(std::vector<bool> input)
|
||||
{
|
||||
double sum=0;
|
||||
for(unsigned int i=0;i<input.size();i++)
|
||||
{
|
||||
// std::cerr << "W: " << getWeight(i) <<"\n";
|
||||
sum+=getWeight(i)*input[i];
|
||||
}
|
||||
//std::cerr << "X: " << sum <<"\n";
|
||||
if(sum <= getPotential())
|
||||
return 0;
|
||||
return 1;
|
||||
}
|
||||
1
src/Neuron.h
Symbolic link
1
src/Neuron.h
Symbolic link
@@ -0,0 +1 @@
|
||||
./Neuron
|
||||
19
src/Problem
Normal file
19
src/Problem
Normal file
@@ -0,0 +1,19 @@
|
||||
#ifndef _P_H_
|
||||
#define _P_H_
|
||||
|
||||
#include <vector>
|
||||
|
||||
namespace S
|
||||
{
|
||||
class Problem
|
||||
{
|
||||
public:
|
||||
Problem();
|
||||
operator std::vector<bool>() const;
|
||||
protected:
|
||||
virtual std::vector<bool> representation() const =0;
|
||||
private:
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
11
src/Problem.cpp
Normal file
11
src/Problem.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "Problem"
|
||||
|
||||
S::Problem::Problem()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
S::Problem::operator std::vector<bool>() const
|
||||
{
|
||||
return representation();
|
||||
}
|
||||
1
src/Problem.h
Symbolic link
1
src/Problem.h
Symbolic link
@@ -0,0 +1 @@
|
||||
./Problem
|
||||
21
src/Solution
Normal file
21
src/Solution
Normal file
@@ -0,0 +1,21 @@
|
||||
#ifndef _SOL_H_
|
||||
#define _SOL_H_
|
||||
|
||||
#include <vector>
|
||||
#include "Problem"
|
||||
|
||||
namespace S
|
||||
{
|
||||
class Solution
|
||||
{
|
||||
public:
|
||||
Solution(std::vector<bool> solution);
|
||||
int size();
|
||||
bool operator[] (int pos);
|
||||
operator std::vector<bool>();
|
||||
protected:
|
||||
std::vector<bool> solution;
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
||||
21
src/Solution.cpp
Normal file
21
src/Solution.cpp
Normal file
@@ -0,0 +1,21 @@
|
||||
#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;
|
||||
}
|
||||
1
src/Solution.h
Symbolic link
1
src/Solution.h
Symbolic link
@@ -0,0 +1 @@
|
||||
./Solution
|
||||
Reference in New Issue
Block a user