Cleaning_1

This commit is contained in:
Asrın Doğan 2019-12-24 18:57:26 +03:00
parent 25b1839763
commit df6028922a
1 changed files with 63 additions and 79 deletions

View File

@ -3,9 +3,9 @@
#define RandomRange 1 #define RandomRange 1
#define InitialSynapseValue 0.0 #define InitialSynapseValue 0.0
#define MutationRate 0.15 #define MutationRate 0.25
#define CrossOverRate 0.1 #define CrossOverRate 0.25
#define PopCrossOverRate 0.95 #define PopCrossOverRate 0.7
class Synapse; class Synapse;
class Neuron; class Neuron;
@ -23,7 +23,6 @@ float RandomFloat(int min, int max)
srand(time(0) * counter++); srand(time(0) * counter++);
value = ((rand() * counter) % ((max - min) * 100000)); value = ((rand() * counter) % ((max - min) * 100000));
result = (float)value / 100000.0 + (float)min; result = (float)value / 100000.0 + (float)min;
// std::cout << "random is " << result << "\n";
return result; return result;
} }
@ -48,13 +47,11 @@ float RandomFloat(int min, int max)
Synapse::Synapse() Synapse::Synapse()
{ {
// std::cout << "Created Synapse\n";
this -> value = this -> weight = this -> bias = InitialSynapseValue; this -> value = this -> weight = this -> bias = InitialSynapseValue;
} }
Synapse::~Synapse() Synapse::~Synapse()
{ {
// std::cout << "Deleted Synapse\n";
} }
void Synapse::SetValue(float value) void Synapse::SetValue(float value)
@ -117,7 +114,6 @@ float RandomFloat(int min, int max)
Neuron::Neuron() Neuron::Neuron()
{ {
// std::cout << "Created Neuron\n";
incomings = forwards = NULL; incomings = forwards = NULL;
incomingsSize = forwardsSize = layerSize = 0; incomingsSize = forwardsSize = layerSize = 0;
} }
@ -125,7 +121,6 @@ float RandomFloat(int min, int max)
Neuron::~Neuron() Neuron::~Neuron()
{ {
// std::cout << "Deleted Neuron\n";
} }
void Neuron::Reset() void Neuron::Reset()
@ -202,7 +197,6 @@ float RandomFloat(int min, int max)
Layer::Layer() Layer::Layer()
{ {
// std::cout << "Created Layer\n";
neuronSize = synapseSize = 0; neuronSize = synapseSize = 0;
neurons = NULL; neurons = NULL;
synapses = NULL; synapses = NULL;
@ -210,7 +204,6 @@ float RandomFloat(int min, int max)
Layer::Layer(int size) Layer::Layer(int size)
{ {
// std::cout << "Deleted Layer\n";
neuronSize = synapseSize = 0; neuronSize = synapseSize = 0;
synapses = NULL; synapses = NULL;
neurons = _CreateNeurons(size); neurons = _CreateNeurons(size);
@ -218,7 +211,6 @@ float RandomFloat(int min, int max)
Layer::~Layer() Layer::~Layer()
{ {
// std::cout << "Deleted Layer\n";
if(neurons) delete neurons; if(neurons) delete neurons;
if(synapses) delete synapses; if(synapses) delete synapses;
} }
@ -330,7 +322,6 @@ float RandomFloat(int min, int max)
delete synapses; delete synapses;
synapses = NULL; synapses = NULL;
} }
// synapses = (Synapse *) new char[sizeof(Synapse) * synapseCount];
synapses = _CreateSynapses(synapseCount); synapses = _CreateSynapses(synapseCount);
if(!synapses) return false; if(!synapses) return false;
@ -413,6 +404,7 @@ float RandomFloat(int min, int max)
Layer *hidden; Layer *hidden;
Output *output; Output *output;
int hiddenSize; int hiddenSize;
float score;
Input *_CreateInput(); Input *_CreateInput();
Layer *_CreateLayers(int); Layer *_CreateLayers(int);
Output *_CreateOutput(); Output *_CreateOutput();
@ -424,9 +416,9 @@ float RandomFloat(int min, int max)
void FireNetwork(); void FireNetwork();
void RandomizeValues(); void RandomizeValues();
void MutateNetwork(); void MutateNetwork();
friend void WriteToFile(NeuralNetwork *);
void Reset(); void Reset();
void CrossOverNetwork(NeuralNetwork *); void CrossOverNetwork(NeuralNetwork *);
friend void WriteToFile(NeuralNetwork *);
bool SetInputNeurons(int); bool SetInputNeurons(int);
bool SetHiddenNeurons(int, int); bool SetHiddenNeurons(int, int);
bool SetOutputNeurons(int); bool SetOutputNeurons(int);
@ -434,8 +426,10 @@ float RandomFloat(int min, int max)
bool SetLayer(int); bool SetLayer(int);
float GetOutput(int); float GetOutput(int);
float GetError(int, float); float GetError(int, float);
float GetScore(int); float GetPrediction(int);
float GetScore();
int GetHiddenSize(); int GetHiddenSize();
void SetScore(float);
void SetInput(float, int); void SetInput(float, int);
}; };
@ -465,7 +459,6 @@ float RandomFloat(int min, int max)
NeuralNetwork::NeuralNetwork() NeuralNetwork::NeuralNetwork()
{ {
// std::cout << "Created NeuralNetwork\n";
hiddenSize = 0; hiddenSize = 0;
input = NULL; input = NULL;
hidden = NULL; hidden = NULL;
@ -474,7 +467,6 @@ float RandomFloat(int min, int max)
NeuralNetwork::NeuralNetwork(int hiddenSize) NeuralNetwork::NeuralNetwork(int hiddenSize)
{ {
// std::cout << "Created NeuralNetwork\n";
this -> hiddenSize = hiddenSize; this -> hiddenSize = hiddenSize;
input = _CreateInput(); input = _CreateInput();
hidden = _CreateLayers(hiddenSize); hidden = _CreateLayers(hiddenSize);
@ -483,7 +475,6 @@ float RandomFloat(int min, int max)
NeuralNetwork::~NeuralNetwork() NeuralNetwork::~NeuralNetwork()
{ {
// std::cout << "Deleted NeuralNetwork\n";
if(input) delete input; if(input) delete input;
if(hidden) delete hidden; if(hidden) delete hidden;
if(output) delete output; if(output) delete output;
@ -644,12 +635,17 @@ float RandomFloat(int min, int max)
return result < 0.0 ? -result : result; return result < 0.0 ? -result : result;
} }
float NeuralNetwork::GetScore(int index = 0) float NeuralNetwork::GetPrediction(int index = 0)
{ {
float result = GetOutput(index); float result = GetOutput(index);
return result; return result;
} }
float NeuralNetwork::GetScore()
{
return score;
}
int NeuralNetwork::GetHiddenSize() int NeuralNetwork::GetHiddenSize()
{ {
return hiddenSize; return hiddenSize;
@ -659,6 +655,11 @@ float RandomFloat(int min, int max)
{ {
input -> SetValue(value, index); input -> SetValue(value, index);
} }
void NeuralNetwork::SetScore(float value)
{
score = value;
}
#pragma endregion #pragma endregion
#pragma region Generation #pragma region Generation
class Generation class Generation
@ -676,27 +677,27 @@ float RandomFloat(int min, int max)
~Generation(); ~Generation();
void Randomize(); void Randomize();
void Fire(); void Fire();
void SortByScore(int); void SortByScore();
void SortByScoreArray(float *, int, int);
void DisplayScores(int); void DisplayScores(int);
void DisplayBest(int); void DisplayBest(int);
void SetTarget(float); void SetTarget(float);
void SetInput(float, int); void SetInput(float, int);
void NextGeneration(); void NextGeneration();
void WriteBestToFile(); void WriteBestToFile();
void UpdateScores();
void ResetScores();
bool CreateNetworks(int, int); bool CreateNetworks(int, int);
bool ConnectNetworks(); bool ConnectNetworks();
bool SetInputNeurons(int); bool SetInputNeurons(int);
bool SetHiddenNeurons(int, int); bool SetHiddenNeurons(int, int);
bool SetOutputNeurons(int); bool SetOutputNeurons(int);
float GetBest(int); float GetBestPrediction(int);
float GetError(int); float GetError(int);
int GetStep(); int GetStep();
}; };
Generation::Generation() Generation::Generation()
{ {
// std::cout << "Created Generation\n";
step = 0; step = 0;
networks = NULL; networks = NULL;
size = 0; size = 0;
@ -705,7 +706,6 @@ float RandomFloat(int min, int max)
Generation::Generation(int size, int hiddenSizes) Generation::Generation(int size, int hiddenSizes)
{ {
// std::cout << "Created Generation\n";
step = 0; step = 0;
target = 0.0; target = 0.0;
this -> size = size; this -> size = size;
@ -714,7 +714,6 @@ float RandomFloat(int min, int max)
Generation::~Generation() Generation::~Generation()
{ {
// std::cout << "Deleted Generation\n";
if(networks) delete networks; if(networks) delete networks;
} }
@ -764,44 +763,45 @@ float RandomFloat(int min, int max)
void Generation::DisplayBest(int index = 0) void Generation::DisplayBest(int index = 0)
{ {
std::cout << "Target -> " << target << "\tBest -> " << networks -> GetScore(index) << "\n"; std::cout << "Target -> " << target << "\tBest -> " << networks -> GetPrediction(index) << "\n";
} }
float Generation::GetBest(int index = 0) void Generation::UpdateScores()
{ {
return networks -> GetScore(index); float scoreToAdd;
int i;
for (i = 0; i < size; i++)
{
scoreToAdd = (networks + i) -> GetError(0, target);
(networks + i) -> SetScore((networks + i) -> GetScore() + scoreToAdd);
}
}
void Generation::ResetScores()
{
int i;
for (i = 0; i < size; i++)
(networks + i) -> SetScore(0.0);
}
float Generation::GetBestPrediction(int index = 0)
{
return networks -> GetPrediction(index);
} }
float Generation::GetError(int index = 0) float Generation::GetError(int index = 0)
{ {
return (networks + index) -> GetError(0, target); return (networks + index) -> GetError(0, target);
} }
void Generation::SortByScore(int index = 0) void Generation::SortByScore()
{ {
int i; int i;
int j; int j;
for (i = 0; i < size - 1; i++) for (i = 0; i < size - 1; i++)
for (j = i + 1; j < size; j++) for (j = i + 1; j < size; j++)
if((networks + i) -> GetError(index, target) > (networks + j) -> GetError(index, target)) if((networks + i) -> GetScore() > (networks + j) -> GetScore())
_SwapNetworks((networks + i), (networks + j)); _SwapNetworks((networks + i), (networks + j));
} }
void Generation::SortByScoreArray(float *array, int size, int index = 0)
{
int i;
int j;
float temp;
for (i = 0; i < size - 1; i++)
for (j = i + 1; j < size; j++)
if(*(array + i) > *(array + j))
{
temp = *(array + i);
*(array + i) = *(array + j);
*(array + j) = temp;
_SwapNetworks((networks + i), (networks + j));
}
// if((networks + i) -> GetError(index, target) > (networks + j) -> GetError(index, target))
}
void Generation::SetTarget(float target) void Generation::SetTarget(float target)
{ {
@ -832,7 +832,6 @@ float RandomFloat(int min, int max)
NeuralNetwork *second = NULL; NeuralNetwork *second = NULL;
Fire(); Fire();
// for (; i < size; i+=2)
for (i = 2; i < crossOverCount; i+=2) for (i = 2; i < crossOverCount; i+=2)
{ {
first = (networks + i); first = (networks + i);
@ -849,7 +848,6 @@ float RandomFloat(int min, int max)
second -> MutateNetwork(); second -> MutateNetwork();
} }
} }
// SortByScore();
for (; i < size; i++) for (; i < size; i++)
(networks + i) -> RandomizeValues(); (networks + i) -> RandomizeValues();
@ -905,8 +903,6 @@ float RandomFloat(int min, int max)
} }
#pragma endregion #pragma endregion
int main() int main()
{ {
FILE *inputFile; FILE *inputFile;
@ -917,20 +913,18 @@ int main()
int inputCounter; int inputCounter;
int floatCounter; int floatCounter;
int i; int groupSize;
int j;
int generationCounter;
float trainInputs[30][5]; float trainInputs[30][5];
float scores[50];
float testInputs[120][5]; float 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 (floatCounter = 0; floatCounter < 5; floatCounter++)
fscanf(inputFile, "%f,", &trainInputs[inputCounter][floatCounter]); fscanf(inputFile, "%f,", &trainInputs[inputCounter][floatCounter]);
fclose(inputFile); fclose(inputFile);
inputFile = fopen("Data/test.data", "r"); inputFile = fopen("Data/test.data", "r");
for (inputCounter = 0; inputCounter < 150; inputCounter++) for (inputCounter = 0; inputCounter < 120; inputCounter++)
for (floatCounter = 0; floatCounter < 5; floatCounter++) for (floatCounter = 0; floatCounter < 5; floatCounter++)
fscanf(inputFile, "%f,", &testInputs[inputCounter][floatCounter]); fscanf(inputFile, "%f,", &testInputs[inputCounter][floatCounter]);
fclose(inputFile); fclose(inputFile);
@ -971,31 +965,23 @@ int main()
std::cout << trainCounter++ << "\n"; std::cout << trainCounter++ << "\n";
for (inputCounter = 0; inputCounter < 10; inputCounter++) for (inputCounter = 0; inputCounter < 10; inputCounter++)
{ {
// for (generationCounter = 0; generationCounter < 25; generationCounter++) generation.ResetScores();
// { for (groupSize = 0; groupSize < 3; groupSize++)
for (j = 0; j < 50; j++) {
scores[j] = 0.0; for (floatCounter = 0; floatCounter < 4; floatCounter++)
for (i = 0; i < 3; i++) generation.SetInput(trainInputs[inputCounter * 3 + groupSize][floatCounter], floatCounter);
{ generation.SetTarget(trainInputs[inputCounter * 3 + groupSize][4]);
for (floatCounter = 0; floatCounter < 4; floatCounter++) generation.Fire();
generation.SetInput(trainInputs[inputCounter * 3 + i][floatCounter], floatCounter); generation.UpdateScores();
generation.SetTarget(trainInputs[inputCounter * 3 + i][4]); }
generation.Fire(); generation.SortByScore();
for (j = 0; j < 50; j++) generation.NextGeneration();
scores[j] += generation.GetError(j);
}
generation.SortByScoreArray(scores, 50);
generation.NextGeneration();
// generation.NextGeneration();
// }
} }
} }
std::cout << "Best -> " << generation.GetError() << "\n";
std::cout << "Best -> " << scores[0] << "\n";
std::cout << "Train is Over!\n"; std::cout << "Train is Over!\n";
case -2: case -2:
outputFile = fopen("Data/results.data", "w"); outputFile = fopen("Data/results.data", "w");
trainCounter = 0;
for (inputCounter = 0; inputCounter < 120; inputCounter++) for (inputCounter = 0; inputCounter < 120; inputCounter++)
{ {
for (floatCounter = 0; floatCounter < 4; floatCounter++) for (floatCounter = 0; floatCounter < 4; floatCounter++)
@ -1003,10 +989,8 @@ int main()
generation.SetTarget(testInputs[inputCounter][4]); generation.SetTarget(testInputs[inputCounter][4]);
generation.Fire(); generation.Fire();
// generation.DisplayBest(); currentError = testInputs[inputCounter][4] - generation.GetBestPrediction() < 0 ? generation.GetBestPrediction() - testInputs[inputCounter][4] : testInputs[inputCounter][4] - generation.GetBestPrediction();
currentError = testInputs[inputCounter][4] - generation.GetBest() < 0 ? generation.GetBest() - testInputs[inputCounter][4] : testInputs[inputCounter][4] - generation.GetBest(); fprintf(outputFile, "%f,%f,%f\n", testInputs[inputCounter][4], generation.GetBestPrediction(), currentError);
// fprintf(outputFile, "Original = %f\t->\tTrained -> %f\t Error = %f\n", testInputs[inputCounter][4], generation.GetBest(), currentError);
fprintf(outputFile, "%f,%f,%f\n", testInputs[inputCounter][4], generation.GetBest(), currentError);
} }
fclose(outputFile); fclose(outputFile);
std::cout << "Test is Over!\n"; std::cout << "Test is Over!\n";