Skip to content

Implementing Genetic Algorithm #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 7 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
AdversarialSearch/Minimax/Main
AdversarialSearch/Minimax/Main.dSYM/
cmake-build-debug
cmake-build-release
build
*.asta
*.asta.lock
Expand Down
3 changes: 2 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +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")
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions Common/Random.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <cstdlib>
#include <ctime>

#include "Random.h"

unsigned int RandomUtils::GetTime()
{
const auto currentTime = static_cast<unsigned int>(time(nullptr));
return currentTime;
}

void RandomUtils::InitSeed(const unsigned int seed)
{
srand(seed);
}
20 changes: 20 additions & 0 deletions Common/Random.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#pragma once

#include <cstdlib>
#include <type_traits>

namespace RandomUtils
{
unsigned int GetTime();

void InitSeed(unsigned int seed);

template <typename TArithmeticType,
typename = typename std::enable_if<std::is_arithmetic<TArithmeticType>::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;
}
}
17 changes: 0 additions & 17 deletions NeuralNetwork/Common/Random.cpp

This file was deleted.

8 changes: 0 additions & 8 deletions NeuralNetwork/Common/Random.h

This file was deleted.

4 changes: 2 additions & 2 deletions NeuralNetwork/MLP/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(FILES
../Common/ActivationFunctions.cpp
../Common/Random.cpp
../Common/Matrix.cpp
../../Common/Random.cpp
../../Common/Matrix.cpp
../Perceptron/Neuron.cpp
../Perceptron/Perceptron.cpp
Layer.cpp
Expand Down
6 changes: 3 additions & 3 deletions NeuralNetwork/MLP/Main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include <iostream>
#include <fstream>
#include "../Common/Random.h"
#include "../../Common/Random.h"
#include "MLP.h"

int propagate(MultiLayerPerceptron& mlp, std::vector<float> inputs, bool printResult)
Expand Down Expand Up @@ -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();
RandomUtils::InitSeed(RandomUtils::GetTime());
std::vector<int> neuronsByLayerArr = std::vector<int> {2,1};
bool isNetworkTrained = false;
unsigned long iterations = 0;
Expand Down Expand Up @@ -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();
RandomUtils::InitSeed(RandomUtils::GetTime());
std::vector<int> neuronsByLayerArr = std::vector<int> {2,2,1};
bool isNetworkTrained = false;
unsigned long iterations = 0;
Expand Down
4 changes: 2 additions & 2 deletions NeuralNetwork/Perceptron/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set(FILES
../Common/ActivationFunctions.cpp
../Common/Random.cpp
../Common/Matrix.cpp
../../Common/Random.cpp
../../Common/Matrix.cpp
Neuron.cpp
Perceptron.cpp
PerceptronTests.cpp
Expand Down
2 changes: 1 addition & 1 deletion NeuralNetwork/Perceptron/Neuron.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef NEURALNETWORK_NEURON_H
#define NEURALNETWORK_NEURON_H

#include "../Common/Matrix.h"
#include "../../Common/Matrix.h"

class Neuron
{
Expand Down
6 changes: 3 additions & 3 deletions NeuralNetwork/Perceptron/Perceptron.cpp
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#include "Perceptron.h"
#include "../Common/Random.h"
#include "../../Common/Random.h"

Perceptron::Perceptron(int weightsLength)
{
_neuron = std::make_unique<Neuron>();
auto weights = std::make_unique<Matrix>(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;
}
Expand Down
8 changes: 4 additions & 4 deletions NeuralNetwork/Perceptron/PerceptronTests.cpp
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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::initRandomSeed();
RandomUtils::InitSeed(RandomUtils::GetTime());
auto perceptron = std::make_unique<Perceptron>(2);
perceptron->setActivationFunction(sign);
int correctGuesses = 0;
Expand Down
3 changes: 3 additions & 0 deletions Search Heuristic/Genetic Algorithm/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
set(FILES Main.cpp
../../Common/Random.cpp GeneticAlgorithm.cpp GeneticAlgorithm.h)
add_executable(GeneticAlgorithm ${FILES})
5 changes: 5 additions & 0 deletions Search Heuristic/Genetic Algorithm/GeneticAlgorithm.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//
// Created by andrei.schuch on 1/30/2022.
//

#include "GeneticAlgorithm.h"
39 changes: 39 additions & 0 deletions Search Heuristic/Genetic Algorithm/GeneticAlgorithm.h
Original file line number Diff line number Diff line change
@@ -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<int>& population);
//int getFitnessScore(int& chromosome);
//Order list elements based on fitness score (first == best, last == worst)
//void orderByFitnessScore(std::vector<int>& population);
//void selection(std::vector<int>& population);
//void farrow(std::vector<int>& population);
//void crossover(std::vector<int>& population);
//void mutation(std::vector<int>& population);
};
39 changes: 39 additions & 0 deletions Search Heuristic/Genetic Algorithm/Main.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <vector>
#include <ctime>

#include "../../Common/Random.h"
#include "bitset"
#include "GeneticAlgorithm.h"

// Population { [010], [1100], [1010] }
// -> Chromosome [0001]
// -> Gene [0]

int main()
{
RandomUtils::InitSeed(RandomUtils::GetTime());
/*
* 1. Initial population
* 2. Fitness function
* 3. Selection
* 4. Crossover
* 5. Mutation
*/

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;
}