Skip to content
This repository has been archived by the owner on Nov 22, 2018. It is now read-only.

Commit

Permalink
Merge pull request #27 from SirRade/development
Browse files Browse the repository at this point in the history
Merge
  • Loading branch information
janhohenheim committed Jun 13, 2016
2 parents dfaa9e2 + 435297a commit 50c9a4b
Show file tree
Hide file tree
Showing 21 changed files with 478 additions and 384 deletions.
47 changes: 29 additions & 18 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
cmake_minimum_required(VERSION 3.5)
include (GenerateExportHeader)
project(JNF_NEAT)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++14")
set(dir ${CMAKE_CURRENT_SOURCE_DIR})
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${dir}/bin")
set(lib_name JNF_NEAT)

OPTION (BUILD_SHARED_LIBS "Build Shared Libraries" ON)
SET (LIB_TYPE STATIC)
IF (BUILD_SHARED_LIBS)
SET (LIB_TYPE SHARED)
ENDIF (BUILD_SHARED_LIBS)
SET (WINDOWS_EXPORT_ALL_SYMBOLS ON)
set(SOURCE_FILES
JNF_NEAT/main.cpp
JNF_NEAT/body.h
JNF_NEAT/gene.cpp
JNF_NEAT/gene.h
JNF_NEAT/gene.cpp
JNF_NEAT/neural_network.h
JNF_NEAT/neural_network.cpp
JNF_NEAT/trained_neural_network.h
JNF_NEAT/trained_neural_network.cpp
JNF_NEAT/neural_network_trainer.h
JNF_NEAT/neural_network_trainer.cpp
JNF_NEAT/neuron.h
JNF_NEAT/genome.cpp
JNF_NEAT/genome.h
JNF_NEAT/neural_network.cpp
JNF_NEAT/neural_network.h
JNF_NEAT/neural_network_trainer.cpp
JNF_NEAT/neural_network_trainer.h
JNF_NEAT/neuron.cpp
JNF_NEAT/body.h
JNF_NEAT/xor_solver.h
JNF_NEAT/xor_solver.cpp
JNF_NEAT/neuron.h
JNF_NEAT/organism.cpp
JNF_NEAT/organism.h
JNF_NEAT/organism.cpp
JNF_NEAT/species.h
JNF_NEAT/species.cpp
JNF_NEAT/genome.h
JNF_NEAT/genome.cpp)

JNF_NEAT/species.h
JNF_NEAT/trained_neural_network.cpp
JNF_NEAT/trained_neural_network.h
JNF_NEAT/training_parameters.h
)
add_definitions(-std=c++14)
add_executable(JNF_NEAT ${SOURCE_FILES})

ADD_LIBRARY(${lib_name} ${LIB_TYPE} ${SOURCE_FILES})

generate_export_header(${lib_name})
3 changes: 0 additions & 3 deletions JNF_NEAT/JNF_NEAT.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -150,13 +150,11 @@
<ClCompile Include="gene.cpp" />
<ClCompile Include="genome.cpp" />
<ClCompile Include="organism.cpp" />
<ClCompile Include="main.cpp" />
<ClCompile Include="neural_network_trainer.cpp" />
<ClCompile Include="neural_network.cpp" />
<ClCompile Include="neuron.cpp" />
<ClCompile Include="species.cpp" />
<ClCompile Include="trained_neural_network.cpp" />
<ClCompile Include="xor_solver.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="gene.h" />
Expand All @@ -169,7 +167,6 @@
<ClInclude Include="neuron.h" />
<ClInclude Include="trained_neural_network.h" />
<ClInclude Include="training_parameters.h" />
<ClInclude Include="xor_solver.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
Expand Down
9 changes: 0 additions & 9 deletions JNF_NEAT/JNF_NEAT.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@
</Filter>
</ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="neural_network.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand All @@ -30,9 +27,6 @@
<ClCompile Include="neuron.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="xor_solver.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="species.cpp">
<Filter>Source Files</Filter>
</ClCompile>
Expand Down Expand Up @@ -62,9 +56,6 @@
<ClInclude Include="training_parameters.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="xor_solver.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="species.h">
<Filter>Header Files</Filter>
</ClInclude>
Expand Down
21 changes: 12 additions & 9 deletions JNF_NEAT/body.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
#pragma once
#include <vector>

class IBody
{
public:
virtual ~IBody() = default;
namespace JNF_NEAT {

virtual void Reset() = 0;
virtual void Update(const std::vector<float>& networkOutputs) = 0;
virtual double GetFitness() const = 0;
class IBody {
public:
virtual ~IBody() = default;

virtual std::vector<float> ProvideNetworkWithInputs() const = 0;
};
virtual void Reset() = 0;
virtual void Update(const std::vector<float>& networkOutputs) = 0;
virtual double GetFitness() const = 0;

virtual std::vector<float> ProvideNetworkWithInputs() const = 0;
};

}
3 changes: 3 additions & 0 deletions JNF_NEAT/gene.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#include "gene.h"
#include <stdlib.h>

using namespace JNF_NEAT;
using namespace std;

size_t Gene::numberOfExistingGenes = 0U;

Gene::Gene() {
Expand Down
42 changes: 23 additions & 19 deletions JNF_NEAT/gene.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
#pragma once
#include <cstddef>

struct Gene {
public:
Gene();
Gene(const Gene& other) = default;
Gene(Gene&& other) = default;
~Gene() = default;

Gene& operator=(const Gene& other) = default;

std::size_t from = 0;
std::size_t to = 0;
float weight = 0.0f;
std::size_t historicalMarking = numberOfExistingGenes;
bool isEnabled = true;
bool isRecursive = false;
void SetRandomWeight();
namespace JNF_NEAT {

private:
static std::size_t numberOfExistingGenes;
};
struct Gene {
public:
Gene();
Gene(const Gene& other) = default;
Gene(Gene&& other) = default;
~Gene() = default;

Gene& operator=(const Gene& other) = default;

std::size_t from = 0;
std::size_t to = 0;
float weight = 0.0f;
std::size_t historicalMarking = numberOfExistingGenes;
bool isEnabled = true;
bool isRecursive = false;
void SetRandomWeight();

private:
static std::size_t numberOfExistingGenes;
};

}
12 changes: 8 additions & 4 deletions JNF_NEAT/genome.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#include <cmath>
#include "genome.h"
#include <algorithm>

using namespace JNF_NEAT;
using namespace std;

Genome::Genome(const TrainingParameters& parameters) :
parameters(parameters),
genes((parameters.numberOfInputs + parameters.advanced.structure.numberOfBiasNeurons) * parameters.numberOfOutputs),
Expand All @@ -16,7 +20,7 @@ Genome::Genome(const TrainingParameters& parameters) :
}
}

std::size_t Genome::GetGeneCount() const {
size_t Genome::GetGeneCount() const {
return genes.size();
}

Expand All @@ -32,18 +36,18 @@ double Genome::GetGeneticalDistanceFrom(const Genome& other) const {
double totalWeightDifference = 0.0;
size_t numberOfOverlapingGenes = 0;

size_t sizeOfSmallerGenome = std::min(this->GetGeneCount(), other.GetGeneCount());
size_t sizeOfSmallerGenome = min(this->GetGeneCount(), other.GetGeneCount());
auto IsHistoricalMarkingSameAt = [&](size_t i) {
return this->GetGeneAt(i).historicalMarking == other[i].historicalMarking;
};

for (size_t i = 0; i < sizeOfSmallerGenome && IsHistoricalMarkingSameAt(i); ++i) {
totalWeightDifference += (double)std::abs(this->GetGeneAt(i).weight - other[i].weight);
totalWeightDifference += (double)abs(this->GetGeneAt(i).weight - other[i].weight);
++numberOfOverlapingGenes;
}

auto numberOfDisjointGenes = this->GetGeneCount() + other.GetGeneCount() - (size_t)2 * numberOfOverlapingGenes;
auto sizeOfBiggerGenome = std::max(this->GetGeneCount(), other.GetGeneCount());
auto sizeOfBiggerGenome = max(this->GetGeneCount(), other.GetGeneCount());
// TODO jnf: Think how we'll handle the next line
auto disjointGenesInfluence = (double)numberOfDisjointGenes /* / (double)sizeOfBiggerGenome*/;

Expand Down
76 changes: 40 additions & 36 deletions JNF_NEAT/genome.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,39 +4,43 @@
#include "training_parameters.h"
#include <vector>

class Genome {
private:
const TrainingParameters& parameters;
std::vector<Gene> genes;
size_t neuronCount = 0U;

public:
Genome() = delete;
explicit Genome(const TrainingParameters& parameters);
Genome(const Genome& other) = default;
Genome(Genome&& other) = default;
~Genome() = default;

Genome& operator=(const Genome& other);
const Gene& operator[](std::size_t index) const { return GetGeneAt(index); }
Gene& operator[](std::size_t index) { return GetGeneAt(index); }
const Gene& GetGeneAt(std::size_t index) const { return genes[index]; }
Gene& GetGeneAt(std::size_t index) { return genes[index]; }
auto begin() { return genes.begin(); }
auto begin() const { return genes.begin(); }
auto end() { return genes.end(); }
auto end() const { return genes.end(); }

std::size_t GetNeuronCount() const { return neuronCount; };
std::size_t GetGeneCount() const;

void AppendGene(Gene gene) { AdjustNeuronCount(gene); genes.push_back(std::move(gene)); }
void InsertGeneAt(Gene gene, size_t index) { AdjustNeuronCount(gene); genes.insert(genes.begin() + index, std::move(gene)); }

const TrainingParameters& GetTrainingParameters() const { return parameters; }
double GetGeneticalDistanceFrom(const Genome& other) const;
bool DoesContainGene(const Gene& gene) const;

private:
inline void AdjustNeuronCount(const Gene& gene){ if (gene.to + 1 > neuronCount) neuronCount = gene.to + 1; }
};
namespace JNF_NEAT {

class Genome {
private:
const TrainingParameters& parameters;
std::vector<Gene> genes;
size_t neuronCount = 0U;

public:
Genome() = delete;
explicit Genome(const TrainingParameters& parameters);
Genome(const Genome& other) = default;
Genome(Genome&& other) = default;
~Genome() = default;

Genome& operator=(const Genome& other);
const Gene& operator[](std::size_t index) const { return GetGeneAt(index); }
Gene& operator[](std::size_t index) { return GetGeneAt(index); }
const Gene& GetGeneAt(std::size_t index) const { return genes[index]; }
Gene& GetGeneAt(std::size_t index) { return genes[index]; }
auto begin() { return genes.begin(); }
auto begin() const { return genes.begin(); }
auto end() { return genes.end(); }
auto end() const { return genes.end(); }

std::size_t GetNeuronCount() const { return neuronCount; };
std::size_t GetGeneCount() const;

void AppendGene(Gene gene) { AdjustNeuronCount(gene); genes.push_back(std::move(gene)); }
void InsertGeneAt(Gene gene, size_t index) { AdjustNeuronCount(gene); genes.insert(genes.begin() + index, std::move(gene)); }

const TrainingParameters& GetTrainingParameters() const { return parameters; }
double GetGeneticalDistanceFrom(const Genome& other) const;
bool DoesContainGene(const Gene& gene) const;

private:
inline void AdjustNeuronCount(const Gene& gene) { if (gene.to + 1 > neuronCount) neuronCount = gene.to + 1; }
};

}
Loading

0 comments on commit 50c9a4b

Please sign in to comment.