From c2d2aec6e4a6cf3669e55bdd0e648275ff204976 Mon Sep 17 00:00:00 2001 From: andreimesquita Date: Mon, 3 Aug 2020 15:50:31 -0300 Subject: [PATCH 1/7] Draft implementation of the Genetic Algorithm --- CMakeLists.txt | 1 + .../Genetic Algorithm/CMakeLists.txt | 2 + Search Heuristic/Genetic Algorithm/Main.cpp | 50 +++++++++++++++++++ 3 files changed, 53 insertions(+) create mode 100644 Search Heuristic/Genetic Algorithm/CMakeLists.txt create mode 100644 Search Heuristic/Genetic Algorithm/Main.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 5d8a7ee..6c5a712 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -6,3 +6,4 @@ add_subdirectory(AdversarialSearch/Minimax) add_subdirectory(NeuralNetwork/Common) add_subdirectory(NeuralNetwork/Perceptron) add_subdirectory(NeuralNetwork/MLP) +add_subdirectory("Search Heuristic/Genetic Algorithm") diff --git a/Search Heuristic/Genetic Algorithm/CMakeLists.txt b/Search Heuristic/Genetic Algorithm/CMakeLists.txt new file mode 100644 index 0000000..9452a40 --- /dev/null +++ b/Search Heuristic/Genetic Algorithm/CMakeLists.txt @@ -0,0 +1,2 @@ +set(FILES Main.cpp) +add_executable(GeneticAlgorithm ${FILES}) diff --git a/Search Heuristic/Genetic Algorithm/Main.cpp b/Search Heuristic/Genetic Algorithm/Main.cpp new file mode 100644 index 0000000..5fc4465 --- /dev/null +++ b/Search Heuristic/Genetic Algorithm/Main.cpp @@ -0,0 +1,50 @@ +#include +#include +#include +#include + +using namespace std; + +typedef float (*FitnessFunction)(float); + +void selectedSorting() +{ + //TODO +} + +class GeneticAlgorithm +{ +private: + void generateInitialPopulation(vector &data); + int getFitnessScore(int &data); + //Order list elements based on fitness score (first == best, last == worst) + void orderByFitnessScore(vector &data, FitnessFunction &fitnessFunction); + void selection(vector &data); + void farrow(vector &data); + void crossover(vector &data); + void mutation(vector &data); +}; + +int main() +{ + /* + * 1. Initial population + * 2. Fitness function + * 3. Selection + * 4. Crossover + * 5. Mutation + */ + vector asd; + for(int i = 0; i < 10; i++) + { + int number = i % 3; + asd.push_back(number); + } + sort(asd.begin(), asd.end()); + for(int i = 0; i < 10; i++) + { + cout << asd[i] << " "; + } + cout << endl << "[Genetic Algorithm]" << endl; + return 0; +} From 8ae67b454b14b338d6392c5326892abc2466e5f4 Mon Sep 17 00:00:00 2001 From: AndreiSchuch Date: Mon, 24 Jan 2022 13:26:55 -0300 Subject: [PATCH 2/7] Moving Random h and cpp into the global Common folder --- Common/Random.cpp | 15 ++++++++++++++ Common/Random.h | 21 ++++++++++++++++++++ NeuralNetwork/Common/Random.cpp | 17 ---------------- NeuralNetwork/Common/Random.h | 8 -------- NeuralNetwork/MLP/CMakeLists.txt | 2 +- NeuralNetwork/MLP/Main.cpp | 6 +++--- NeuralNetwork/Perceptron/CMakeLists.txt | 2 +- NeuralNetwork/Perceptron/Perceptron.cpp | 2 +- NeuralNetwork/Perceptron/PerceptronTests.cpp | 4 ++-- 9 files changed, 44 insertions(+), 33 deletions(-) create mode 100644 Common/Random.cpp create mode 100644 Common/Random.h delete mode 100644 NeuralNetwork/Common/Random.cpp delete mode 100644 NeuralNetwork/Common/Random.h diff --git a/Common/Random.cpp b/Common/Random.cpp new file mode 100644 index 0000000..e8cdd4e --- /dev/null +++ b/Common/Random.cpp @@ -0,0 +1,15 @@ +#include +#include + +#include "Random.h" + +unsigned int random::GetTime() +{ + const auto currentTime = static_cast(time(nullptr)); + return currentTime; +} + +void random::InitSeed(const unsigned int seed) +{ + srand(seed); +} diff --git a/Common/Random.h b/Common/Random.h new file mode 100644 index 0000000..d0b1124 --- /dev/null +++ b/Common/Random.h @@ -0,0 +1,21 @@ +#ifndef AIENGINE_RANDOM_H +#define AIENGINE_RANDOM_H + +#include + +namespace random +{ + unsigned int GetTime(); + + void InitSeed(unsigned int seed); + + template ::value, TArithmeticType>::type> + TArithmeticType range(const TArithmeticType minValue, const TArithmeticType maxValue) + { + const float randomValue = (std::rand() / (float) RAND_MAX); + const TArithmeticType result = minValue + randomValue * (maxValue - minValue); + return result; + } +} +#endif //AIENGINE_RANDOM_H diff --git a/NeuralNetwork/Common/Random.cpp b/NeuralNetwork/Common/Random.cpp deleted file mode 100644 index 69e66fc..0000000 --- a/NeuralNetwork/Common/Random.cpp +++ /dev/null @@ -1,17 +0,0 @@ -#include -#include -#include "Random.h" - -void random::initRandomSeed() -{ - unsigned int seed = static_cast(time(NULL)); - srand(seed); -} - -float random::range(float a, float b) -{ - float randomValue = ((float) std::rand()) / (float) RAND_MAX; - float difference = b - a; - float result = randomValue * difference; - return a + result; -} diff --git a/NeuralNetwork/Common/Random.h b/NeuralNetwork/Common/Random.h deleted file mode 100644 index fc7a352..0000000 --- a/NeuralNetwork/Common/Random.h +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef AIENGINE_RANDOM_H -#define AIENGINE_RANDOM_H -namespace random -{ - void initRandomSeed(); - float range(float a, float b); -} -#endif //AIENGINE_RANDOM_H diff --git a/NeuralNetwork/MLP/CMakeLists.txt b/NeuralNetwork/MLP/CMakeLists.txt index 254cb1c..0db9630 100644 --- a/NeuralNetwork/MLP/CMakeLists.txt +++ b/NeuralNetwork/MLP/CMakeLists.txt @@ -1,7 +1,7 @@ set(FILES ../Common/ActivationFunctions.cpp - ../Common/Random.cpp ../Common/Matrix.cpp + ../../Common/Random.cpp ../Perceptron/Neuron.cpp ../Perceptron/Perceptron.cpp Layer.cpp diff --git a/NeuralNetwork/MLP/Main.cpp b/NeuralNetwork/MLP/Main.cpp index e1a9d30..75ced3a 100644 --- a/NeuralNetwork/MLP/Main.cpp +++ b/NeuralNetwork/MLP/Main.cpp @@ -1,6 +1,6 @@ #include #include -#include "../Common/Random.h" +#include "../../Common/Random.h" #include "MLP.h" int propagate(MultiLayerPerceptron& mlp, std::vector inputs, bool printResult) @@ -37,7 +37,7 @@ void loadWeightsFromFile(const char* filePath, MultiLayerPerceptron& mlp) void trainXor2LayersMLP(const char* filePath) { std::cout << "XOR - 2 Layers (2, 1)" << std::endl; - random::initRandomSeed(); + random::InitSeed(random::GetTime()); std::vector neuronsByLayerArr = std::vector {2,1}; bool isNetworkTrained = false; unsigned long iterations = 0; @@ -65,7 +65,7 @@ void trainXor2LayersMLP(const char* filePath) void trainXor3LayersMLP(const char* filePath) { std::cout << "XOR - 3 Layers (2, 2, 1)" << std::endl; - random::initRandomSeed(); + random::InitSeed(random::GetTime()); std::vector neuronsByLayerArr = std::vector {2,2,1}; bool isNetworkTrained = false; unsigned long iterations = 0; diff --git a/NeuralNetwork/Perceptron/CMakeLists.txt b/NeuralNetwork/Perceptron/CMakeLists.txt index 39c891e..ac94b26 100644 --- a/NeuralNetwork/Perceptron/CMakeLists.txt +++ b/NeuralNetwork/Perceptron/CMakeLists.txt @@ -1,7 +1,7 @@ set(FILES ../Common/ActivationFunctions.cpp - ../Common/Random.cpp ../Common/Matrix.cpp + ../../Common/Random.cpp Neuron.cpp Perceptron.cpp PerceptronTests.cpp diff --git a/NeuralNetwork/Perceptron/Perceptron.cpp b/NeuralNetwork/Perceptron/Perceptron.cpp index 76c93e4..49da1a1 100644 --- a/NeuralNetwork/Perceptron/Perceptron.cpp +++ b/NeuralNetwork/Perceptron/Perceptron.cpp @@ -1,5 +1,5 @@ #include "Perceptron.h" -#include "../Common/Random.h" +#include "../../Common/Random.h" Perceptron::Perceptron(int weightsLength) { diff --git a/NeuralNetwork/Perceptron/PerceptronTests.cpp b/NeuralNetwork/Perceptron/PerceptronTests.cpp index 64bc6e2..2d77ff1 100644 --- a/NeuralNetwork/Perceptron/PerceptronTests.cpp +++ b/NeuralNetwork/Perceptron/PerceptronTests.cpp @@ -1,5 +1,5 @@ #include "Perceptron.h" -#include "../Common/Random.h" +#include "../../Common/Random.h" ///returns the expected output from given inputs float toExpectedOutput(Matrix& inputs) @@ -23,7 +23,7 @@ void populateRandomInput(Matrix& matrix) int main(int argc, char* argv[]) { - random::initRandomSeed(); + random::InitSeed(random::GetTime()); auto perceptron = std::make_unique(2); perceptron->setActivationFunction(sign); int correctGuesses = 0; From 3f6a88a95db51565fe2a5e48bb99fbec6802b328 Mon Sep 17 00:00:00 2001 From: AndreiSchuch Date: Mon, 24 Jan 2022 13:27:35 -0300 Subject: [PATCH 3/7] Moving all contents from NeuralNetwork/Common folder into the global Common folder --- CMakeLists.txt | 2 +- {NeuralNetwork/Common => Common}/CMakeLists.txt | 0 {NeuralNetwork/Common => Common}/Matrix.cpp | 0 {NeuralNetwork/Common => Common}/Matrix.h | 0 {NeuralNetwork/Common => Common}/MatrixTests.cpp | 0 NeuralNetwork/MLP/CMakeLists.txt | 2 +- NeuralNetwork/Perceptron/CMakeLists.txt | 2 +- NeuralNetwork/Perceptron/Neuron.h | 2 +- 8 files changed, 4 insertions(+), 4 deletions(-) rename {NeuralNetwork/Common => Common}/CMakeLists.txt (100%) rename {NeuralNetwork/Common => Common}/Matrix.cpp (100%) rename {NeuralNetwork/Common => Common}/Matrix.h (100%) rename {NeuralNetwork/Common => Common}/MatrixTests.cpp (100%) diff --git a/CMakeLists.txt b/CMakeLists.txt index 6c5a712..beb581b 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(AIEngine) set(CMAKE_CXX_STANDARD 14) set(EXECUTABLE_OUTPUT_PATH ${PROJECT_BINARY_DIR}/build) add_subdirectory(AdversarialSearch/Minimax) -add_subdirectory(NeuralNetwork/Common) +add_subdirectory(Common) add_subdirectory(NeuralNetwork/Perceptron) add_subdirectory(NeuralNetwork/MLP) add_subdirectory("Search Heuristic/Genetic Algorithm") diff --git a/NeuralNetwork/Common/CMakeLists.txt b/Common/CMakeLists.txt similarity index 100% rename from NeuralNetwork/Common/CMakeLists.txt rename to Common/CMakeLists.txt diff --git a/NeuralNetwork/Common/Matrix.cpp b/Common/Matrix.cpp similarity index 100% rename from NeuralNetwork/Common/Matrix.cpp rename to Common/Matrix.cpp diff --git a/NeuralNetwork/Common/Matrix.h b/Common/Matrix.h similarity index 100% rename from NeuralNetwork/Common/Matrix.h rename to Common/Matrix.h diff --git a/NeuralNetwork/Common/MatrixTests.cpp b/Common/MatrixTests.cpp similarity index 100% rename from NeuralNetwork/Common/MatrixTests.cpp rename to Common/MatrixTests.cpp diff --git a/NeuralNetwork/MLP/CMakeLists.txt b/NeuralNetwork/MLP/CMakeLists.txt index 0db9630..a357e8a 100644 --- a/NeuralNetwork/MLP/CMakeLists.txt +++ b/NeuralNetwork/MLP/CMakeLists.txt @@ -1,7 +1,7 @@ set(FILES ../Common/ActivationFunctions.cpp - ../Common/Matrix.cpp ../../Common/Random.cpp + ../../Common/Matrix.cpp ../Perceptron/Neuron.cpp ../Perceptron/Perceptron.cpp Layer.cpp diff --git a/NeuralNetwork/Perceptron/CMakeLists.txt b/NeuralNetwork/Perceptron/CMakeLists.txt index ac94b26..a8f0b2f 100644 --- a/NeuralNetwork/Perceptron/CMakeLists.txt +++ b/NeuralNetwork/Perceptron/CMakeLists.txt @@ -1,7 +1,7 @@ set(FILES ../Common/ActivationFunctions.cpp - ../Common/Matrix.cpp ../../Common/Random.cpp + ../../Common/Matrix.cpp Neuron.cpp Perceptron.cpp PerceptronTests.cpp diff --git a/NeuralNetwork/Perceptron/Neuron.h b/NeuralNetwork/Perceptron/Neuron.h index c7afb1f..5065786 100644 --- a/NeuralNetwork/Perceptron/Neuron.h +++ b/NeuralNetwork/Perceptron/Neuron.h @@ -1,7 +1,7 @@ #ifndef NEURALNETWORK_NEURON_H #define NEURALNETWORK_NEURON_H -#include "../Common/Matrix.h" +#include "../../Common/Matrix.h" class Neuron { From 8c5369176f08df9b0b8b650ce7f48ae12f802ddc Mon Sep 17 00:00:00 2001 From: AndreiSchuch Date: Mon, 24 Jan 2022 13:28:13 -0300 Subject: [PATCH 4/7] Applying some legibility improvements in the GeneticAlgorithm implementation --- .../Genetic Algorithm/CMakeLists.txt | 3 +- Search Heuristic/Genetic Algorithm/Main.cpp | 41 +++++++++---------- 2 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Search Heuristic/Genetic Algorithm/CMakeLists.txt b/Search Heuristic/Genetic Algorithm/CMakeLists.txt index 9452a40..00cc537 100644 --- a/Search Heuristic/Genetic Algorithm/CMakeLists.txt +++ b/Search Heuristic/Genetic Algorithm/CMakeLists.txt @@ -1,2 +1,3 @@ -set(FILES Main.cpp) +set(FILES Main.cpp + ../../Common/Random.cpp) add_executable(GeneticAlgorithm ${FILES}) diff --git a/Search Heuristic/Genetic Algorithm/Main.cpp b/Search Heuristic/Genetic Algorithm/Main.cpp index 5fc4465..3e9bcdb 100644 --- a/Search Heuristic/Genetic Algorithm/Main.cpp +++ b/Search Heuristic/Genetic Algorithm/Main.cpp @@ -2,31 +2,30 @@ #include #include #include +#include -using namespace std; +#include "../../Common/Random.h" -typedef float (*FitnessFunction)(float); - -void selectedSorting() -{ - //TODO -} +// Population { [010], [1100], [1010] } +// -> Chromosome [0001] +// -> Gene [0] class GeneticAlgorithm { private: - void generateInitialPopulation(vector &data); - int getFitnessScore(int &data); + void generateInitialPopulation(std::vector& population); + int getFitnessScore(int& chromosome); //Order list elements based on fitness score (first == best, last == worst) - void orderByFitnessScore(vector &data, FitnessFunction &fitnessFunction); - void selection(vector &data); - void farrow(vector &data); - void crossover(vector &data); - void mutation(vector &data); + void orderByFitnessScore(std::vector& population); + void selection(std::vector& population); + void farrow(std::vector& population); + void crossover(std::vector& population); + void mutation(std::vector& population); }; int main() { + random::InitSeed(random::GetTime()); /* * 1. Initial population * 2. Fitness function @@ -34,17 +33,17 @@ int main() * 4. Crossover * 5. Mutation */ - vector asd; - for(int i = 0; i < 10; i++) + std::cout << std::endl << "[Genetic Algorithm]" << std::endl; + const int VECTOR_LENGTH = 10; + std::vector vector(VECTOR_LENGTH); + for(int i = 0; i < VECTOR_LENGTH; i++) { - int number = i % 3; - asd.push_back(number); + vector[i] = random::range(0, 200); } - sort(asd.begin(), asd.end()); + sort(vector.begin(), vector.end()); for(int i = 0; i < 10; i++) { - cout << asd[i] << " "; + std::cout << vector[i] << " "; } - cout << endl << "[Genetic Algorithm]" << endl; return 0; } From 4682636af85c7ad551022e657c72b46187ab9b89 Mon Sep 17 00:00:00 2001 From: AndreiSchuch Date: Fri, 4 Feb 2022 14:31:06 -0300 Subject: [PATCH 5/7] Renaming 'random' namespace to 'RandomHelper' --- Common/Random.cpp | 4 ++-- Common/Random.h | 7 +++---- NeuralNetwork/MLP/Main.cpp | 4 ++-- NeuralNetwork/Perceptron/Perceptron.cpp | 4 ++-- NeuralNetwork/Perceptron/PerceptronTests.cpp | 6 +++--- 5 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Common/Random.cpp b/Common/Random.cpp index e8cdd4e..c0f0bdc 100644 --- a/Common/Random.cpp +++ b/Common/Random.cpp @@ -3,13 +3,13 @@ #include "Random.h" -unsigned int random::GetTime() +unsigned int RandomUtils::GetTime() { const auto currentTime = static_cast(time(nullptr)); return currentTime; } -void random::InitSeed(const unsigned int seed) +void RandomUtils::InitSeed(const unsigned int seed) { srand(seed); } diff --git a/Common/Random.h b/Common/Random.h index d0b1124..5069ac9 100644 --- a/Common/Random.h +++ b/Common/Random.h @@ -1,9 +1,9 @@ -#ifndef AIENGINE_RANDOM_H -#define AIENGINE_RANDOM_H +#pragma once +#include #include -namespace random +namespace RandomUtils { unsigned int GetTime(); @@ -18,4 +18,3 @@ namespace random return result; } } -#endif //AIENGINE_RANDOM_H diff --git a/NeuralNetwork/MLP/Main.cpp b/NeuralNetwork/MLP/Main.cpp index 75ced3a..f98c6f3 100644 --- a/NeuralNetwork/MLP/Main.cpp +++ b/NeuralNetwork/MLP/Main.cpp @@ -37,7 +37,7 @@ void loadWeightsFromFile(const char* filePath, MultiLayerPerceptron& mlp) void trainXor2LayersMLP(const char* filePath) { std::cout << "XOR - 2 Layers (2, 1)" << std::endl; - random::InitSeed(random::GetTime()); + RandomUtils::InitSeed(RandomUtils::GetTime()); std::vector neuronsByLayerArr = std::vector {2,1}; bool isNetworkTrained = false; unsigned long iterations = 0; @@ -65,7 +65,7 @@ void trainXor2LayersMLP(const char* filePath) void trainXor3LayersMLP(const char* filePath) { std::cout << "XOR - 3 Layers (2, 2, 1)" << std::endl; - random::InitSeed(random::GetTime()); + RandomUtils::InitSeed(RandomUtils::GetTime()); std::vector neuronsByLayerArr = std::vector {2,2,1}; bool isNetworkTrained = false; unsigned long iterations = 0; diff --git a/NeuralNetwork/Perceptron/Perceptron.cpp b/NeuralNetwork/Perceptron/Perceptron.cpp index 49da1a1..3448f77 100644 --- a/NeuralNetwork/Perceptron/Perceptron.cpp +++ b/NeuralNetwork/Perceptron/Perceptron.cpp @@ -7,11 +7,11 @@ Perceptron::Perceptron(int weightsLength) auto weights = std::make_unique(1, weightsLength); for (int c = 0; c < weights->getColumns(); ++c) { - float randomValue = random::range(-1.0f, 1.0f); + float randomValue = RandomUtils::range(-1.0f, 1.0f); weights->set(0,c,randomValue); } _neuron->setWeights(*weights); - float randomBias = random::range(-0.1f,0.1f); + float randomBias = RandomUtils::range(-0.1f, 0.1f); _neuron->setBias(randomBias); _activationFunction = noActivation; } diff --git a/NeuralNetwork/Perceptron/PerceptronTests.cpp b/NeuralNetwork/Perceptron/PerceptronTests.cpp index 2d77ff1..f8d58fa 100644 --- a/NeuralNetwork/Perceptron/PerceptronTests.cpp +++ b/NeuralNetwork/Perceptron/PerceptronTests.cpp @@ -11,19 +11,19 @@ float toExpectedOutput(Matrix& inputs) return 1.0f; } -///Populate 'matrix' with random inputs in range -500.0/500.0 +///Populate 'matrix' with RandomUtils inputs in range -500.0/500.0 void populateRandomInput(Matrix& matrix) { for (int r = 0; r < matrix.getRows(); ++r) { - float value = random::range(-500.0f, 500.0f); + float value = RandomUtils::range(-500.0f, 500.0f); matrix.set(r,0,value); } } int main(int argc, char* argv[]) { - random::InitSeed(random::GetTime()); + RandomUtils::InitSeed(RandomUtils::GetTime()); auto perceptron = std::make_unique(2); perceptron->setActivationFunction(sign); int correctGuesses = 0; From d822ca8b6d1948a9f28724b793ed57a5d8ae102e Mon Sep 17 00:00:00 2001 From: AndreiSchuch Date: Fri, 4 Feb 2022 14:31:50 -0300 Subject: [PATCH 6/7] Adding 'cmake-build-release' into the .gitignore file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index f28f2b5..1ec14b6 100644 --- a/.gitignore +++ b/.gitignore @@ -2,6 +2,7 @@ AdversarialSearch/Minimax/Main AdversarialSearch/Minimax/Main.dSYM/ cmake-build-debug +cmake-build-release build *.asta *.asta.lock From a505c40684072c2c1af6612b8028512996502294 Mon Sep 17 00:00:00 2001 From: AndreiSchuch Date: Fri, 4 Feb 2022 14:33:17 -0300 Subject: [PATCH 7/7] Creating a Genetic Algorithm example --- .../Genetic Algorithm/CMakeLists.txt | 2 +- .../Genetic Algorithm/GeneticAlgorithm.cpp | 5 +++ .../Genetic Algorithm/GeneticAlgorithm.h | 39 +++++++++++++++++ Search Heuristic/Genetic Algorithm/Main.cpp | 42 +++++++------------ 4 files changed, 61 insertions(+), 27 deletions(-) create mode 100644 Search Heuristic/Genetic Algorithm/GeneticAlgorithm.cpp create mode 100644 Search Heuristic/Genetic Algorithm/GeneticAlgorithm.h diff --git a/Search Heuristic/Genetic Algorithm/CMakeLists.txt b/Search Heuristic/Genetic Algorithm/CMakeLists.txt index 00cc537..a1ae609 100644 --- a/Search Heuristic/Genetic Algorithm/CMakeLists.txt +++ b/Search Heuristic/Genetic Algorithm/CMakeLists.txt @@ -1,3 +1,3 @@ set(FILES Main.cpp - ../../Common/Random.cpp) + ../../Common/Random.cpp GeneticAlgorithm.cpp GeneticAlgorithm.h) add_executable(GeneticAlgorithm ${FILES}) diff --git a/Search Heuristic/Genetic Algorithm/GeneticAlgorithm.cpp b/Search Heuristic/Genetic Algorithm/GeneticAlgorithm.cpp new file mode 100644 index 0000000..334f085 --- /dev/null +++ b/Search Heuristic/Genetic Algorithm/GeneticAlgorithm.cpp @@ -0,0 +1,5 @@ +// +// Created by andrei.schuch on 1/30/2022. +// + +#include "GeneticAlgorithm.h" diff --git a/Search Heuristic/Genetic Algorithm/GeneticAlgorithm.h b/Search Heuristic/Genetic Algorithm/GeneticAlgorithm.h new file mode 100644 index 0000000..536f88a --- /dev/null +++ b/Search Heuristic/Genetic Algorithm/GeneticAlgorithm.h @@ -0,0 +1,39 @@ +#pragma once + +#include "../../Common/Random.h" + +class GeneticAlgorithm +{ +public: + void Crossover(int& lhs, int& rhs, const int index) + { + const int leftMask = 0xffffffff >> index; + const int rightMask = ~leftMask; + + const int newLhs = (lhs & leftMask) | (rhs & rightMask); + const int newRhs = (rhs & leftMask) | (lhs & rightMask); + + lhs = newLhs; + rhs = newRhs; + } + + void Mutate(int& chromossome) + { + for (int i = 0; i < 32; i++) + { + if (RandomUtils::range(0.0, 1.0) > 0.001) continue; + const int geneMask = 1 << i; + + //TODO + } + } + + //void generateInitialPopulation(std::vector& population); + //int getFitnessScore(int& chromosome); + //Order list elements based on fitness score (first == best, last == worst) + //void orderByFitnessScore(std::vector& population); + //void selection(std::vector& population); + //void farrow(std::vector& population); + //void crossover(std::vector& population); + //void mutation(std::vector& population); +}; diff --git a/Search Heuristic/Genetic Algorithm/Main.cpp b/Search Heuristic/Genetic Algorithm/Main.cpp index 3e9bcdb..fd09732 100644 --- a/Search Heuristic/Genetic Algorithm/Main.cpp +++ b/Search Heuristic/Genetic Algorithm/Main.cpp @@ -5,27 +5,16 @@ #include #include "../../Common/Random.h" +#include "bitset" +#include "GeneticAlgorithm.h" // Population { [010], [1100], [1010] } // -> Chromosome [0001] // -> Gene [0] -class GeneticAlgorithm -{ -private: - void generateInitialPopulation(std::vector& population); - int getFitnessScore(int& chromosome); - //Order list elements based on fitness score (first == best, last == worst) - void orderByFitnessScore(std::vector& population); - void selection(std::vector& population); - void farrow(std::vector& population); - void crossover(std::vector& population); - void mutation(std::vector& population); -}; - int main() { - random::InitSeed(random::GetTime()); + RandomUtils::InitSeed(RandomUtils::GetTime()); /* * 1. Initial population * 2. Fitness function @@ -33,17 +22,18 @@ int main() * 4. Crossover * 5. Mutation */ - std::cout << std::endl << "[Genetic Algorithm]" << std::endl; - const int VECTOR_LENGTH = 10; - std::vector vector(VECTOR_LENGTH); - for(int i = 0; i < VECTOR_LENGTH; i++) - { - vector[i] = random::range(0, 200); - } - sort(vector.begin(), vector.end()); - for(int i = 0; i < 10; i++) - { - std::cout << vector[i] << " "; - } + + GeneticAlgorithm genericAlgorithm; + int lhs = 0xffff; + int rhs = 0xffff << 16; + + std::cout << std::bitset<32>(lhs) << std::endl; + std::cout << std::bitset<32>(rhs) << std::endl << std::endl; + + genericAlgorithm.Crossover(lhs, rhs, 1); + + std::cout << std::bitset<32>(lhs) << std::endl; + std::cout << std::bitset<32>(rhs) << std::endl; + return 0; }