NeuralNetworkGenetic/Genetic.cpp

606 lines
15 KiB
C++
Raw Normal View History

2019-12-15 16:55:17 +03:00
#include <iostream>
#include <time.h>
#define RandomRange 1
2019-12-16 20:37:58 +03:00
#define InitialSynapseValue 0.0
2019-12-15 16:55:17 +03:00
#define MutationRate 0.0001
class Synapse;
class Neuron;
class Layer;
class Input;
class Output;
class NeuralNetwork;
float RandomFloat(int min, int max)
{
float result;
int value;
static unsigned long int counter = time(0);
srand(time(0) * counter++);
value = ((rand() * counter) % ((max - min) * 100000));
result = (float)value / 100000.0 + (float)min;
2019-12-16 20:37:58 +03:00
// std::cout << "random is " << result << "\n";
2019-12-15 16:55:17 +03:00
return result;
}
#pragma region Synapse
class Synapse
{
private:
float weight;
float value;
float bias;
public:
Synapse();
void SetValue(float);
void SetWeight(float);
void SetBias(float);
float Fire();
};
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
Synapse::Synapse()
{
this -> value = this -> weight = this -> bias = InitialSynapseValue;
}
void Synapse::SetValue(float value)
{
this -> value = value;
}
void Synapse::SetWeight(float weight)
{
this -> weight = weight;
}
void Synapse::SetBias(float bias)
{
this -> bias = bias;
}
float Synapse::Fire()
{
float result = 0.0;
result = this -> value * this -> weight + this -> bias;
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
return result;
}
#pragma endregion
#pragma region Neuron
class Neuron
{
private:
Synapse *incomings;
Synapse *forwards;
int incomingsSize;
int forwardsSize;
int layerSize;
public:
Neuron();
void ConnectIncomings(Synapse *, int);
void ConnectForwards(Synapse *, int, int);
void SetValue(float);
float GetValue();
};
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
Neuron::Neuron()
{
incomings = forwards = NULL;
incomingsSize = forwardsSize = layerSize = 0;
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
void Neuron::SetValue(float value)
{
for (int i = 0; i < forwardsSize; i++)
(forwards + i) -> SetValue(value);
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
void Neuron::ConnectIncomings(Synapse *incomings, int incomingsSize)
{
this -> incomings = incomings;
this -> incomingsSize = incomingsSize;
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
void Neuron::ConnectForwards(Synapse *forwards, int forwardsSize, int layerSize)
{
this -> forwards = forwards;
this -> forwardsSize = forwardsSize;
this -> layerSize = layerSize;
}
float Neuron::GetValue()
{
float result = 0.0;
if(!incomings) return result;
for (int i = 0; i < incomingsSize; i++)
result += (incomings + i) -> Fire();
if(!forwards) return result;
for (int i = 0; i < forwardsSize; i++)
(forwards + i * layerSize) -> SetValue(result);
return result;
}
#pragma endregion
#pragma region Layer
class Layer
{
protected:
Neuron *neurons;
Synapse *synapses;
int neuronSize;
int synapseSize;
Neuron *_CreateNeurons(int);
public:
Layer();
Layer(int);
~Layer();
void FireLayer();
void Mutate();
void RandomizeValues();
bool CreateNeurons(int);
bool ConnectPrevious(Layer *);
bool ConnectForwards(Layer *);
int GetSize();
};
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
Layer::Layer()
{
neuronSize = synapseSize = 0;
neurons = NULL;
synapses = NULL;
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
Layer::Layer(int size)
{
neuronSize = synapseSize = 0;
synapses = NULL;
neurons = _CreateNeurons(size);
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
Layer::~Layer()
{
if(neurons) delete neurons;
if(synapses) delete synapses;
}
Neuron *Layer::_CreateNeurons(int size)
{
Neuron *newNeurons = NULL;
newNeurons = (Neuron *) new char[sizeof(Neuron) * size];
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
if(newNeurons)
for (int i = 0; i < size; i++)
*(newNeurons + i) = Neuron();
return newNeurons;
}
void Layer::FireLayer()
{
for (int i = 0; i < neuronSize; i++)
(neurons + i) -> GetValue();
}
void Layer::RandomizeValues()
{
float bias;
float weight;
for (int i = 0; i < synapseSize; i++)
{
bias = RandomFloat(-RandomRange, RandomRange);
2019-12-16 20:37:58 +03:00
weight = RandomFloat(-RandomRange, RandomRange);
2019-12-15 16:55:17 +03:00
(synapses + i) -> SetBias(bias);
(synapses + i) -> SetWeight(weight);
}
}
void Layer::Mutate()
{
float bias = 0.0;
float weight = 0.0;
float mutationValue = 0.0;
for (int i = 0; i < synapseSize; i++)
{
mutationValue = RandomFloat(0, 1);
if(mutationValue <= MutationRate)
{
bias = RandomFloat(-RandomRange, RandomRange);
weight = RandomFloat(-RandomRange, RandomRange);
(synapses + i) -> SetBias(bias);
(synapses + i) -> SetWeight(weight);
}
}
}
bool Layer::CreateNeurons(int size)
{
if(neurons = _CreateNeurons(size))
neuronSize = size;
return neurons;
}
bool Layer::ConnectPrevious(Layer *previous)
{
int previousSize = previous -> GetSize();
int synapseCount = (this -> neuronSize) * previousSize;
int currentIndex = 0;
Synapse *currentSynapse = NULL;
Neuron *currentNeuron = NULL;
if(synapses) delete synapses;
synapses = (Synapse *) new char[sizeof(Synapse) * synapseCount];
if(!synapses) return false;
for (int thisNeuron = 0; thisNeuron < this -> neuronSize; thisNeuron++)
{
for (int prevNeuron = 0; prevNeuron < previousSize; prevNeuron++)
{
currentIndex = thisNeuron * previousSize + prevNeuron;
currentSynapse = (synapses + currentIndex);
currentNeuron = (previous -> neurons) + prevNeuron;
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
*currentSynapse = Synapse();
}
currentNeuron = (neurons + thisNeuron);
currentNeuron -> ConnectIncomings((synapses + thisNeuron * previousSize), previousSize);
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
synapseSize = synapseCount;
return previous -> ConnectForwards(this);
}
bool Layer::ConnectForwards(Layer *forwards)
{
int forwardsSize = forwards -> neuronSize;
Neuron *currentNeuron = NULL;
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
for (int thisNeuron = 0; thisNeuron < this -> neuronSize; thisNeuron++)
{
currentNeuron = (neurons + thisNeuron);
for (int forwardNeuron = 0; forwardNeuron < forwardsSize; forwardNeuron++)
currentNeuron -> ConnectForwards(forwards -> synapses + thisNeuron, forwardsSize, this -> neuronSize);
}
return true;
}
int Layer::GetSize()
{
return neuronSize;
}
#pragma region Input-Output
class Input : public Layer
{
public:
Input();
2019-12-16 20:37:58 +03:00
void SetValue(float, int);
2019-12-15 16:55:17 +03:00
};
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
Input::Input() : Layer() {}
2019-12-16 20:37:58 +03:00
void Input::SetValue(float value, int index = 0)
2019-12-15 16:55:17 +03:00
{
if(index >= this -> neuronSize || index < 0)
return;
(neurons + index) -> SetValue(value);
}
class Output : public Layer
{
public:
Output();
float GetValue(int);
};
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
Output::Output() : Layer() {}
2019-12-16 20:37:58 +03:00
float Output::GetValue(int index = 0)
2019-12-15 16:55:17 +03:00
{
float result = 0.0;
if(index >= this -> neuronSize || index < 0)
return result;
result = (neurons + index) -> GetValue();
return result;
}
#pragma endregion
#pragma endregion
#pragma region NeuralNetwork
class NeuralNetwork
{
private:
Input *input;
Layer *hidden;
Output *output;
int hiddenSize;
public:
NeuralNetwork();
NeuralNetwork(int);
~NeuralNetwork();
void FireNetwork();
void RandomizeValues();
void MutateNetwork();
bool SetInputNeurons(int);
bool SetHiddenNeurons(int, int);
bool SetOutputNeurons(int);
bool ConnectLayers();
float GetOutput(int);
2019-12-16 20:37:58 +03:00
float GetScore(float, int);
void SetInput(float, int);
2019-12-15 16:55:17 +03:00
};
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
NeuralNetwork::NeuralNetwork()
{
hiddenSize = 0;
input = NULL;
hidden = NULL;
output = NULL;
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
NeuralNetwork::NeuralNetwork(int hiddenSize)
{
this -> hiddenSize = hiddenSize;
input = new Input();
hidden = new Layer(hiddenSize);
output = new Output();
}
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
NeuralNetwork::~NeuralNetwork()
{
if(input) delete input;
if(hidden) delete hidden;
if(output) delete output;
}
void NeuralNetwork::FireNetwork()
{
for (int i = 0; i < hiddenSize; i++)
(hidden + i) -> FireLayer();
output -> FireLayer();
}
void NeuralNetwork::MutateNetwork()
{
input -> Mutate();
for (int i = 0; i < hiddenSize; i++)
(hidden + i) -> Mutate();
output -> Mutate();
}
void NeuralNetwork::RandomizeValues()
{
input -> RandomizeValues();
for (int i = 0; i < hiddenSize; i++)
(hidden + i) -> RandomizeValues();
output -> RandomizeValues();
}
bool NeuralNetwork::SetInputNeurons(int size)
{
return input -> CreateNeurons(size);
}
bool NeuralNetwork::SetHiddenNeurons(int index, int size)
{
return (hidden + index) -> CreateNeurons(size);
}
bool NeuralNetwork::SetOutputNeurons(int size)
{
return output -> CreateNeurons(size);
}
bool NeuralNetwork::ConnectLayers()
{
if(!hidden -> ConnectPrevious(input))
return false;
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
for (int i = 1; i < hiddenSize; i++)
if(!(hidden + i) -> ConnectPrevious((hidden + i - 1)))
return false;
2019-12-16 20:37:58 +03:00
if(!output -> ConnectPrevious((hidden + hiddenSize - 1)))
2019-12-15 16:55:17 +03:00
return false;
2019-12-16 20:37:58 +03:00
2019-12-15 16:55:17 +03:00
return true;
}
2019-12-16 20:37:58 +03:00
float NeuralNetwork::GetOutput(int index = 0)
2019-12-15 16:55:17 +03:00
{
return output -> GetValue(index);
}
2019-12-16 20:37:58 +03:00
float NeuralNetwork::GetScore(float target, int index = 0)
{
float result = GetOutput(index) - target;
return result < 0.0 ? -result : result;
}
void NeuralNetwork::SetInput(float value, int index = 0)
2019-12-15 16:55:17 +03:00
{
2019-12-16 20:37:58 +03:00
input -> SetValue(value, index);
2019-12-15 16:55:17 +03:00
}
#pragma endregion
2019-12-16 20:37:58 +03:00
#pragma region Generation
class Generation
{
private:
NeuralNetwork *networks;
int size;
int step;
float target;
void SwapNetworks(NeuralNetwork *, NeuralNetwork *);
NeuralNetwork *_CreateNetworks(int, int);
public:
Generation();
Generation(int, int);
~Generation();
void Randomize();
void Fire();
void SortByScore(int);
void DisplayScores(int);
void SetTarget(float);
void SetInput(float, int);
bool CreateNetworks(int, int);
bool ConnectNetworks();
bool SetInputNeurons(int);
bool SetHiddenNeurons(int, int);
bool SetOutputNeurons(int);
};
Generation::Generation()
{
step = 0;
networks = NULL;
size = 0;
target = 0.0;
}
Generation::Generation(int size, int hiddenSizes)
{
step = 0;
target = 0.0;
this -> size = size;
networks = _CreateNetworks(size, hiddenSizes);
}
Generation::~Generation()
{
if(networks) delete networks;
}
NeuralNetwork *Generation::_CreateNetworks(int size, int hiddenSizes)
{
NeuralNetwork *newNetworks = NULL;
newNetworks = (NeuralNetwork *) new char[sizeof(NeuralNetwork) * size];
2019-12-15 16:55:17 +03:00
2019-12-16 20:37:58 +03:00
if(newNetworks)
for (int i = 0; i < size; i++)
*(newNetworks + i) = NeuralNetwork(hiddenSizes);
return newNetworks;
}
void Generation::Randomize()
{
for (int i = 0; i < this -> size; i++)
(networks + i) -> RandomizeValues();
}
void Generation::Fire()
{
for (int i = 0; i < this -> size; i++)
(networks + i) -> FireNetwork();
}
void Generation::SwapNetworks(NeuralNetwork *first, NeuralNetwork *second)
{
NeuralNetwork temp;
temp = *first;
*first = *second;
*second = temp;
}
void Generation::DisplayScores(int index = 0)
{
std::cout << "----Scores----\n";
for (int i = 0; i < this -> size; i++)
std::cout << i << " -> " << (networks + i) -> GetScore(target, index) << "\n";
}
void Generation::SortByScore(int index = 0)
{
for (int i = 0; i < size - 1; i++)
for (int j = i + 1; j < size; j++)
if((networks + i) -> GetScore(target, index) < (networks + j) -> GetScore(target, index))
SwapNetworks((networks + i), (networks + j));
}
void Generation::SetTarget(float target)
{
this -> target = target;
}
void Generation::SetInput(float value, int index = 0)
{
for (int i = 0; i < this -> size; i++)
(networks + i) -> SetInput(value, index);
}
bool Generation::CreateNetworks(int size, int hiddenSizes)
{
if((networks = _CreateNetworks(size, hiddenSizes)))
this -> size = size;
return networks;
}
bool Generation::ConnectNetworks()
{
for (int i = 0; i < this -> size; i++)
if(!(networks + i) -> ConnectLayers())
return false;
return true;
}
bool Generation::SetInputNeurons(int size)
{
for (int i = 0; i < this -> size; i++)
if(!(networks + i) -> SetInputNeurons(size))
return false;
return true;
}
bool Generation::SetHiddenNeurons(int index, int size)
{
for (int i = 0; i < this -> size; i++)
if(!(networks + i) -> SetHiddenNeurons(index, size))
return false;
return true;
}
bool Generation::SetOutputNeurons(int size)
{
for (int i = 0; i < this -> size; i++)
if(!(networks + i) -> SetOutputNeurons(size))
return false;
return true;
}
#pragma endregion
2019-12-15 16:55:17 +03:00
int main(int argc, char const *argv[])
{
2019-12-16 20:37:58 +03:00
Generation generation(50, 3);
std::cout << "1 - " << generation.SetInputNeurons(1) << "\n";
std::cout << "2 - " << generation.SetHiddenNeurons(0, 2) << "\n";
std::cout << "3 - " << generation.SetHiddenNeurons(1, 3) << "\n";
std::cout << "4 - " << generation.SetHiddenNeurons(2, 2) << "\n";
std::cout << "5 - " << generation.SetOutputNeurons(1) << "\n";
std::cout << "6 - " << generation.ConnectNetworks() << "\n";
2019-12-15 16:55:17 +03:00
2019-12-16 20:37:58 +03:00
// generation.SetTarget(12.30);
2019-12-15 16:55:17 +03:00
2019-12-16 20:37:58 +03:00
generation.DisplayScores();
generation.SortByScore();
2019-12-15 16:55:17 +03:00
2019-12-16 20:37:58 +03:00
generation.Randomize();
generation.Fire();
generation.DisplayScores();
2019-12-15 16:55:17 +03:00
return 0;
}