Floats to Double and Signature

This commit is contained in:
Asrın Doğan 2019-12-24 21:36:20 +03:00
parent 4475fd3c7f
commit 29da090c99
1 changed files with 99 additions and 91 deletions

View File

@ -1,3 +1,7 @@
/*
Author: Asrın "Syntriax" Doğan
Mail: asrindogan99@gmail.com
*/
#include <iostream> #include <iostream>
#include <time.h> #include <time.h>
@ -5,7 +9,7 @@
#define InitialSynapseValue 0.0 #define InitialSynapseValue 0.0
#define MutationRate 0.25 #define MutationRate 0.25
#define CrossOverRate 0.25 #define CrossOverRate 0.25
#define PopCrossOverRate 0.7 #define PopCrossOverRate 0.75
class Synapse; class Synapse;
class Neuron; class Neuron;
@ -15,14 +19,14 @@ class Output;
class NeuralNetwork; class NeuralNetwork;
class Generation; class Generation;
float RandomFloat(int min, int max) double RandomDouble(int min, int max)
{ {
float result; double result;
int value; long int value;
static unsigned long int counter = time(0); static unsigned long int counter = time(0);
srand(time(0) * counter++); srand(time(0) * counter++);
value = ((rand() * counter) % ((max - min) * 100000)); value = ((rand() * counter) % ((max - min) * 100000000));
result = (float)value / 100000.0 + (float)min; result = (double)value / 100000000.0 + (double)min;
return result; return result;
} }
@ -30,19 +34,19 @@ float RandomFloat(int min, int max)
class Synapse class Synapse
{ {
private: private:
float weight; double weight;
float value; double value;
float bias; double bias;
public: public:
Synapse(); Synapse();
~Synapse(); ~Synapse();
void SetValue(float); void SetValue(double);
void SetWeight(float); void SetWeight(double);
void SetBias(float); void SetBias(double);
float GetWeight(); double GetWeight();
float GetValue(); double GetValue();
float GetBias(); double GetBias();
float Fire(); double Fire();
}; };
Synapse::Synapse() Synapse::Synapse()
@ -54,39 +58,39 @@ float RandomFloat(int min, int max)
{ {
} }
void Synapse::SetValue(float value) void Synapse::SetValue(double value)
{ {
this -> value = value; this -> value = value;
} }
void Synapse::SetWeight(float weight) void Synapse::SetWeight(double weight)
{ {
this -> weight = weight; this -> weight = weight;
} }
void Synapse::SetBias(float bias) void Synapse::SetBias(double bias)
{ {
this -> bias = bias; this -> bias = bias;
} }
float Synapse::GetWeight() double Synapse::GetWeight()
{ {
return weight; return weight;
} }
float Synapse::GetValue() double Synapse::GetValue()
{ {
return value; return value;
} }
float Synapse::GetBias() double Synapse::GetBias()
{ {
return bias; return bias;
} }
float Synapse::Fire() double Synapse::Fire()
{ {
float result = 0.0; double result = 0.0;
result = this -> value * this -> weight + this -> bias; result = this -> value * this -> weight + this -> bias;
@ -107,9 +111,9 @@ float RandomFloat(int min, int max)
~Neuron(); ~Neuron();
void ConnectIncomings(Synapse *, int); void ConnectIncomings(Synapse *, int);
void ConnectForwards(Synapse *, int, int); void ConnectForwards(Synapse *, int, int);
void SetValue(float); void SetValue(double);
void Reset(); void Reset();
float GetValue(); double GetValue();
}; };
Neuron::Neuron() Neuron::Neuron()
@ -129,7 +133,7 @@ float RandomFloat(int min, int max)
incomingsSize = forwardsSize = layerSize = 0; incomingsSize = forwardsSize = layerSize = 0;
} }
void Neuron::SetValue(float value) void Neuron::SetValue(double value)
{ {
int i; int i;
for (i = 0; i < forwardsSize; i++) for (i = 0; i < forwardsSize; i++)
@ -149,10 +153,10 @@ float RandomFloat(int min, int max)
this -> layerSize = layerSize; this -> layerSize = layerSize;
} }
float Neuron::GetValue() double Neuron::GetValue()
{ {
int i; int i;
float result = 0.0; double result = 0.0;
if(!incomings) return result; if(!incomings) return result;
@ -261,14 +265,14 @@ float RandomFloat(int min, int max)
void Layer::RandomizeValues() void Layer::RandomizeValues()
{ {
float bias; double bias;
float weight; double weight;
int i; int i;
for (i = 0; i < synapseSize; i++) for (i = 0; i < synapseSize; i++)
{ {
bias = RandomFloat(-RandomRange, RandomRange); bias = RandomDouble(-RandomRange, RandomRange);
weight = RandomFloat(-RandomRange, RandomRange); weight = RandomDouble(-RandomRange, RandomRange);
(synapses + i) -> SetBias(bias); (synapses + i) -> SetBias(bias);
(synapses + i) -> SetWeight(weight); (synapses + i) -> SetWeight(weight);
} }
@ -276,18 +280,18 @@ float RandomFloat(int min, int max)
void Layer::Mutate() void Layer::Mutate()
{ {
float bias = 0.0; double bias = 0.0;
float weight = 0.0; double weight = 0.0;
float mutationValue = 0.0; double mutationValue = 0.0;
int i; int i;
for (i = 0; i < synapseSize; i++) for (i = 0; i < synapseSize; i++)
{ {
mutationValue = RandomFloat(0, 1); mutationValue = RandomDouble(0, 1);
if(mutationValue <= MutationRate) if(mutationValue <= MutationRate)
{ {
bias = RandomFloat(-RandomRange, RandomRange); bias = RandomDouble(-RandomRange, RandomRange);
weight = RandomFloat(-RandomRange, RandomRange); weight = RandomDouble(-RandomRange, RandomRange);
(synapses + i) -> SetBias(bias); (synapses + i) -> SetBias(bias);
(synapses + i) -> SetWeight(weight); (synapses + i) -> SetWeight(weight);
} }
@ -298,7 +302,7 @@ float RandomFloat(int min, int max)
{ {
int thisCounter; int thisCounter;
for (thisCounter = 0; thisCounter < synapseSize; thisCounter++) for (thisCounter = 0; thisCounter < synapseSize; thisCounter++)
if(RandomFloat(0, 1) < CrossOverRate) if(RandomDouble(0, 1) < CrossOverRate)
_SwapSynapses((synapses + thisCounter), (other -> synapses + thisCounter)); _SwapSynapses((synapses + thisCounter), (other -> synapses + thisCounter));
} }
@ -364,11 +368,11 @@ float RandomFloat(int min, int max)
{ {
public: public:
Input(); Input();
void SetValue(float, int); void SetValue(double, int);
}; };
Input::Input() : Layer() {} Input::Input() : Layer() {}
void Input::SetValue(float value, int index = 0) void Input::SetValue(double value, int index = 0)
{ {
if(index >= this -> neuronSize || index < 0) if(index >= this -> neuronSize || index < 0)
return; return;
@ -380,13 +384,13 @@ float RandomFloat(int min, int max)
{ {
public: public:
Output(); Output();
float GetValue(int); double GetValue(int);
}; };
Output::Output() : Layer() {} Output::Output() : Layer() {}
float Output::GetValue(int index = 0) double Output::GetValue(int index = 0)
{ {
float result = 0.0; double result = 0.0;
if(index >= this -> neuronSize || index < 0) if(index >= this -> neuronSize || index < 0)
return result; return result;
@ -404,7 +408,7 @@ float RandomFloat(int min, int max)
Layer *hidden; Layer *hidden;
Output *output; Output *output;
int hiddenSize; int hiddenSize;
float score; double score;
Input *_CreateInput(); Input *_CreateInput();
Layer *_CreateLayers(int); Layer *_CreateLayers(int);
Output *_CreateOutput(); Output *_CreateOutput();
@ -424,13 +428,13 @@ float RandomFloat(int min, int max)
bool SetOutputNeurons(int); bool SetOutputNeurons(int);
bool ConnectLayers(); bool ConnectLayers();
bool SetLayer(int); bool SetLayer(int);
float GetOutput(int); double GetOutput(int);
float GetError(int, float); double GetError(int, double);
float GetPrediction(int); double GetPrediction(int);
float GetScore(); double GetScore();
int GetHiddenSize(); int GetHiddenSize();
void SetScore(float); void SetScore(double);
void SetInput(float, int); void SetInput(double, int);
}; };
Input *NeuralNetwork::_CreateInput() Input *NeuralNetwork::_CreateInput()
@ -548,7 +552,7 @@ float RandomFloat(int min, int max)
FILE *file = fopen("Data/BestSynapses.txt", "w"); FILE *file = fopen("Data/BestSynapses.txt", "w");
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
fprintf(file, "%f, %f, ", synapsePtr -> GetWeight(), synapsePtr -> GetBias()); fprintf(file, "%lf, %lf, ", synapsePtr -> GetWeight(), synapsePtr -> GetBias());
synapsePtr++; synapsePtr++;
} }
@ -559,7 +563,7 @@ float RandomFloat(int min, int max)
synapsePtr = (network -> hidden + j) -> synapses; synapsePtr = (network -> hidden + j) -> synapses;
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
fprintf(file, "%f, %f, ", synapsePtr -> GetWeight(), synapsePtr -> GetBias()); fprintf(file, "%lf, %lf, ", synapsePtr -> GetWeight(), synapsePtr -> GetBias());
synapsePtr++; synapsePtr++;
} }
} }
@ -570,7 +574,7 @@ float RandomFloat(int min, int max)
std::cout << count << "\n"; std::cout << count << "\n";
for (i = 0; i < count; i++) for (i = 0; i < count; i++)
{ {
fprintf(file, "%f, %f, ", synapsePtr -> GetWeight(), synapsePtr -> GetBias()); fprintf(file, "%lf, %lf, ", synapsePtr -> GetWeight(), synapsePtr -> GetBias());
synapsePtr++; synapsePtr++;
} }
fclose(file); fclose(file);
@ -624,24 +628,24 @@ float RandomFloat(int min, int max)
return input && hidden && output; return input && hidden && output;
} }
float NeuralNetwork::GetOutput(int index = 0) double NeuralNetwork::GetOutput(int index = 0)
{ {
return output -> GetValue(index); return output -> GetValue(index);
} }
float NeuralNetwork::GetError(int index = 0, float target = 0.0) double NeuralNetwork::GetError(int index = 0, double target = 0.0)
{ {
float result = GetOutput(index) - target; double result = GetOutput(index) - target;
return result < 0.0 ? -result : result; return result < 0.0 ? -result : result;
} }
float NeuralNetwork::GetPrediction(int index = 0) double NeuralNetwork::GetPrediction(int index = 0)
{ {
float result = GetOutput(index); double result = GetOutput(index);
return result; return result;
} }
float NeuralNetwork::GetScore() double NeuralNetwork::GetScore()
{ {
return score; return score;
} }
@ -651,12 +655,12 @@ float RandomFloat(int min, int max)
return hiddenSize; return hiddenSize;
} }
void NeuralNetwork::SetInput(float value, int index = 0) void NeuralNetwork::SetInput(double value, int index = 0)
{ {
input -> SetValue(value, index); input -> SetValue(value, index);
} }
void NeuralNetwork::SetScore(float value) void NeuralNetwork::SetScore(double value)
{ {
score = value; score = value;
} }
@ -668,7 +672,7 @@ float RandomFloat(int min, int max)
NeuralNetwork *networks; NeuralNetwork *networks;
int size; int size;
int step; int step;
float target; double target;
void _SwapNetworks(NeuralNetwork *, NeuralNetwork *); void _SwapNetworks(NeuralNetwork *, NeuralNetwork *);
NeuralNetwork *_CreateNetworks(int, int); NeuralNetwork *_CreateNetworks(int, int);
public: public:
@ -680,8 +684,8 @@ float RandomFloat(int min, int max)
void SortByScore(); void SortByScore();
void DisplayScores(int); void DisplayScores(int);
void DisplayBest(int); void DisplayBest(int);
void SetTarget(float); void SetTarget(double);
void SetInput(float, int); void SetInput(double, int);
void NextGeneration(); void NextGeneration();
void WriteBestToFile(); void WriteBestToFile();
void UpdateScores(); void UpdateScores();
@ -691,8 +695,8 @@ float RandomFloat(int min, int max)
bool SetInputNeurons(int); bool SetInputNeurons(int);
bool SetHiddenNeurons(int, int); bool SetHiddenNeurons(int, int);
bool SetOutputNeurons(int); bool SetOutputNeurons(int);
float GetBestPrediction(int); double GetBestPrediction(int);
float GetError(int); double GetError(int);
int GetStep(); int GetStep();
}; };
@ -768,7 +772,7 @@ float RandomFloat(int min, int max)
void Generation::UpdateScores() void Generation::UpdateScores()
{ {
float scoreToAdd; double scoreToAdd;
int i; int i;
for (i = 0; i < size; i++) for (i = 0; i < size; i++)
{ {
@ -784,15 +788,16 @@ float RandomFloat(int min, int max)
(networks + i) -> SetScore(0.0); (networks + i) -> SetScore(0.0);
} }
float Generation::GetBestPrediction(int index = 0) double Generation::GetBestPrediction(int index = 0)
{ {
return networks -> GetPrediction(index); return networks -> GetPrediction(index);
} }
float Generation::GetError(int index = 0) double Generation::GetError(int index = 0)
{ {
return (networks + index) -> GetError(0, target); return (networks + index) -> GetError(0, target);
} }
void Generation::SortByScore() void Generation::SortByScore()
{ {
int i; int i;
@ -803,12 +808,12 @@ float RandomFloat(int min, int max)
_SwapNetworks((networks + i), (networks + j)); _SwapNetworks((networks + i), (networks + j));
} }
void Generation::SetTarget(float target) void Generation::SetTarget(double target)
{ {
this -> target = target; this -> target = target;
} }
void Generation::SetInput(float value, int index = 0) void Generation::SetInput(double value, int index = 0)
{ {
int i; int i;
for (i = 0; i < this -> size; i++) for (i = 0; i < this -> size; i++)
@ -840,7 +845,7 @@ float RandomFloat(int min, int max)
first -> Copy(*(networks + 0)); first -> Copy(*(networks + 0));
second -> Copy(*(networks + 1)); second -> Copy(*(networks + 1));
if(RandomFloat(0, 1) < 0.5) if(RandomDouble(0, 1) < 0.5)
first -> CrossOverNetwork(second); first -> CrossOverNetwork(second);
else else
{ {
@ -897,6 +902,7 @@ float RandomFloat(int min, int max)
return false; return false;
return true; return true;
} }
int Generation::GetStep() int Generation::GetStep()
{ {
return step; return step;
@ -908,26 +914,26 @@ int main()
FILE *inputFile; FILE *inputFile;
FILE *outputFile; FILE *outputFile;
int decision; int decision;
float currentError; double currentError;
int trainCounter; int trainCounter;
int inputCounter; int inputCounter;
int floatCounter; int doubleCounter;
int groupSize; int groupSize;
float trainInputs[30][5]; double trainInputs[30][5];
float testInputs[120][5]; double testInputs[120][5];
Generation generation(50, 5); Generation generation(50, 5);
inputFile = fopen("Data/train.data", "r"); inputFile = fopen("Data/train.data", "r");
for (inputCounter = 0; inputCounter < 30; inputCounter++) for (inputCounter = 0; inputCounter < 30; inputCounter++)
for (floatCounter = 0; floatCounter < 5; floatCounter++) for (doubleCounter = 0; doubleCounter < 5; doubleCounter++)
fscanf(inputFile, "%f,", &trainInputs[inputCounter][floatCounter]); fscanf(inputFile, "%lf,", &trainInputs[inputCounter][doubleCounter]);
fclose(inputFile); fclose(inputFile);
inputFile = fopen("Data/test.data", "r"); inputFile = fopen("Data/test.data", "r");
for (inputCounter = 0; inputCounter < 120; inputCounter++) for (inputCounter = 0; inputCounter < 120; inputCounter++)
for (floatCounter = 0; floatCounter < 5; floatCounter++) for (doubleCounter = 0; doubleCounter < 5; doubleCounter++)
fscanf(inputFile, "%f,", &testInputs[inputCounter][floatCounter]); fscanf(inputFile, "%lf,", &testInputs[inputCounter][doubleCounter]);
fclose(inputFile); fclose(inputFile);
std::cout << "Inputs Are Getting Set: "; std::cout << "Inputs Are Getting Set: ";
@ -947,11 +953,13 @@ int main()
std::cout << "Networks Are Getting Connected: "; std::cout << "Networks Are Getting Connected: ";
std::cout << (generation.ConnectNetworks() ? "Successfull!" : "Failed!") << "\n"; std::cout << (generation.ConnectNetworks() ? "Successfull!" : "Failed!") << "\n";
std::cout << "Networks Are Getting Randomized: ";
generation.Randomize(); generation.Randomize();
std::cout << "Done!\n";
do do
{ {
std::cout << "[-1] Test\n[-2] Best to File\n[-3] Exit\nAny Positive Number for train count\nDecision: "; std::cout << "\n[-1] Test\n[-2] Best to File\n[-3] Exit\nAny Positive Number for train count\nDecision: ";
std::cin >> decision; std::cin >> decision;
switch (decision) switch (decision)
@ -971,8 +979,8 @@ int main()
generation.ResetScores(); generation.ResetScores();
for (groupSize = 0; groupSize < 3; groupSize++) for (groupSize = 0; groupSize < 3; groupSize++)
{ {
for (floatCounter = 0; floatCounter < 4; floatCounter++) for (doubleCounter = 0; doubleCounter < 4; doubleCounter++)
generation.SetInput(trainInputs[inputCounter * 3 + groupSize][floatCounter], floatCounter); generation.SetInput(trainInputs[inputCounter * 3 + groupSize][doubleCounter], doubleCounter);
generation.SetTarget(trainInputs[inputCounter * 3 + groupSize][4]); generation.SetTarget(trainInputs[inputCounter * 3 + groupSize][4]);
generation.Fire(); generation.Fire();
generation.UpdateScores(); generation.UpdateScores();
@ -988,13 +996,13 @@ int main()
outputFile = fopen("Data/results.data", "w"); outputFile = fopen("Data/results.data", "w");
for (inputCounter = 0; inputCounter < 120; inputCounter++) for (inputCounter = 0; inputCounter < 120; inputCounter++)
{ {
for (floatCounter = 0; floatCounter < 4; floatCounter++) for (doubleCounter = 0; doubleCounter < 4; doubleCounter++)
generation.SetInput(testInputs[inputCounter][floatCounter], floatCounter); generation.SetInput(testInputs[inputCounter][doubleCounter], doubleCounter);
generation.SetTarget(testInputs[inputCounter][4]); generation.SetTarget(testInputs[inputCounter][4]);
generation.Fire(); generation.Fire();
currentError = testInputs[inputCounter][4] - generation.GetBestPrediction() < 0 ? generation.GetBestPrediction() - testInputs[inputCounter][4] : testInputs[inputCounter][4] - generation.GetBestPrediction(); currentError = testInputs[inputCounter][4] - generation.GetBestPrediction() < 0 ? generation.GetBestPrediction() - testInputs[inputCounter][4] : testInputs[inputCounter][4] - generation.GetBestPrediction();
fprintf(outputFile, "%f,%f,%f\n", testInputs[inputCounter][4], generation.GetBestPrediction(), currentError); fprintf(outputFile, "%lf,%lf,%lf\n", testInputs[inputCounter][4], generation.GetBestPrediction(), currentError);
} }
fclose(outputFile); fclose(outputFile);
std::cout << "Test is Over!\n"; std::cout << "Test is Over!\n";