Genetic Fixes

This commit is contained in:
Asrın Doğan 2019-12-18 18:42:44 +03:00
parent ea71002d07
commit 90740194d0
1 changed files with 48 additions and 17 deletions

View File

@ -82,6 +82,7 @@ float RandomFloat(int min, int max)
void ConnectIncomings(Synapse *, int); void ConnectIncomings(Synapse *, int);
void ConnectForwards(Synapse *, int, int); void ConnectForwards(Synapse *, int, int);
void SetValue(float); void SetValue(float);
void Reset();
float GetValue(); float GetValue();
}; };
@ -91,6 +92,12 @@ float RandomFloat(int min, int max)
incomingsSize = forwardsSize = layerSize = 0; incomingsSize = forwardsSize = layerSize = 0;
} }
void Neuron::Reset()
{
incomings = forwards = NULL;
incomingsSize = forwardsSize = layerSize = 0;
}
void Neuron::SetValue(float value) void Neuron::SetValue(float value)
{ {
for (int i = 0; i < forwardsSize; i++) for (int i = 0; i < forwardsSize; i++)
@ -137,6 +144,7 @@ float RandomFloat(int min, int max)
int neuronSize; int neuronSize;
int synapseSize; int synapseSize;
Neuron *_CreateNeurons(int); Neuron *_CreateNeurons(int);
Synapse *_CreateSynapses(int);
public: public:
Layer(); Layer();
Layer(int); Layer(int);
@ -173,15 +181,23 @@ float RandomFloat(int min, int max)
Neuron *Layer::_CreateNeurons(int size) Neuron *Layer::_CreateNeurons(int size)
{ {
Neuron *newNeurons = NULL; Neuron *newNeurons = NULL;
newNeurons = (Neuron *) new char[sizeof(Neuron) * size]; newNeurons = new Neuron[size];
if(newNeurons) if(newNeurons)
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
*(newNeurons + i) = Neuron(); (newNeurons + i) -> Reset();
return newNeurons; return newNeurons;
} }
Synapse *Layer::_CreateSynapses(int size)
{
Synapse *newSynapses = NULL;
newSynapses = new Synapse[size];
return newSynapses;
}
void Layer::FireLayer() void Layer::FireLayer()
{ {
for (int i = 0; i < neuronSize; i++) for (int i = 0; i < neuronSize; i++)
@ -236,7 +252,8 @@ float RandomFloat(int min, int max)
Neuron *currentNeuron = NULL; Neuron *currentNeuron = NULL;
if(synapses) delete synapses; if(synapses) delete synapses;
synapses = (Synapse *) new char[sizeof(Synapse) * synapseCount]; // synapses = (Synapse *) new char[sizeof(Synapse) * synapseCount];
synapses = _CreateSynapses(synapseCount);
if(!synapses) return false; if(!synapses) return false;
@ -247,8 +264,7 @@ float RandomFloat(int min, int max)
currentIndex = thisNeuron * previousSize + prevNeuron; currentIndex = thisNeuron * previousSize + prevNeuron;
currentSynapse = (synapses + currentIndex); currentSynapse = (synapses + currentIndex);
currentNeuron = (previous -> neurons) + prevNeuron; currentNeuron = (previous -> neurons) + prevNeuron;
// *currentSynapse = Synapse();
*currentSynapse = Synapse();
} }
currentNeuron = (neurons + thisNeuron); currentNeuron = (neurons + thisNeuron);
@ -333,6 +349,7 @@ float RandomFloat(int min, int max)
bool SetHiddenNeurons(int, int); bool SetHiddenNeurons(int, int);
bool SetOutputNeurons(int); bool SetOutputNeurons(int);
bool ConnectLayers(); bool ConnectLayers();
bool SetLayer(int);
float GetOutput(int); float GetOutput(int);
float GetScore(float, int); float GetScore(float, int);
void SetInput(float, int); void SetInput(float, int);
@ -418,6 +435,14 @@ float RandomFloat(int min, int max)
return true; return true;
} }
bool NeuralNetwork::SetLayer(int hiddenSize)
{
this -> hiddenSize = hiddenSize;
input = new Input();
hidden = new Layer(hiddenSize);
output = new Output();
}
float NeuralNetwork::GetOutput(int index = 0) float NeuralNetwork::GetOutput(int index = 0)
{ {
return output -> GetValue(index); return output -> GetValue(index);
@ -485,11 +510,11 @@ float RandomFloat(int min, int max)
NeuralNetwork *Generation::_CreateNetworks(int size, int hiddenSizes) NeuralNetwork *Generation::_CreateNetworks(int size, int hiddenSizes)
{ {
NeuralNetwork *newNetworks = NULL; NeuralNetwork *newNetworks = NULL;
newNetworks = (NeuralNetwork *) new char[sizeof(NeuralNetwork) * size]; newNetworks = new NeuralNetwork[size];
if(newNetworks) if(newNetworks)
for (int i = 0; i < size; i++) for (int i = 0; i < size; i++)
*(newNetworks + i) = NeuralNetwork(hiddenSizes); (newNetworks + i) -> SetLayer(hiddenSizes);
return newNetworks; return newNetworks;
} }
@ -525,7 +550,7 @@ float RandomFloat(int min, int max)
{ {
for (int i = 0; i < size - 1; i++) for (int i = 0; i < size - 1; i++)
for (int j = i + 1; j < size; j++) for (int j = i + 1; j < size; j++)
if((networks + i) -> GetScore(target, index) < (networks + j) -> GetScore(target, index)) if((networks + i) -> GetScore(target, index) > (networks + j) -> GetScore(target, index))
SwapNetworks((networks + i), (networks + j)); SwapNetworks((networks + i), (networks + j));
} }
@ -585,21 +610,27 @@ float RandomFloat(int min, int max)
int main(int argc, char const *argv[]) int main(int argc, char const *argv[])
{ {
Generation generation(50, 3); Generation generation(50, 3);
std::cout << "1 - " << generation.SetInputNeurons(1) << "\n"; std::cout << "1 - ";
std::cout << "2 - " << generation.SetHiddenNeurons(0, 2) << "\n"; std::cout << generation.SetInputNeurons(1) << "\n";
std::cout << "3 - " << generation.SetHiddenNeurons(1, 3) << "\n"; std::cout << "2 - ";
std::cout << "4 - " << generation.SetHiddenNeurons(2, 2) << "\n"; std::cout << generation.SetHiddenNeurons(0, 2) << "\n";
std::cout << "5 - " << generation.SetOutputNeurons(1) << "\n"; std::cout << "3 - ";
std::cout << "6 - " << generation.ConnectNetworks() << "\n"; std::cout << generation.SetHiddenNeurons(1, 3) << "\n";
std::cout << "4 - ";
std::cout << generation.SetHiddenNeurons(2, 2) << "\n";
std::cout << "5 - ";
std::cout << generation.SetOutputNeurons(1) << "\n";
std::cout << "6 - ";
std::cout << generation.ConnectNetworks() << "\n";
// generation.SetTarget(12.30); // generation.SetTarget(12.30);
generation.DisplayScores();
generation.SortByScore();
generation.Randomize(); generation.Randomize();
generation.Fire(); generation.Fire();
generation.DisplayScores(); generation.DisplayScores();
std::cout << "-----------SORTING-----------\n";
generation.SortByScore();
generation.DisplayScores();
return 0; return 0;
} }