Skip to content

Commit

Permalink
Refactoring [WIP]
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-ch committed Nov 21, 2017
1 parent a5c30b3 commit f059c1a
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 43 deletions.
2 changes: 1 addition & 1 deletion _MultiNEAT.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ cdef class Species:
def __get__(self): return self.thisptr.ID()

property Age:
def __get__(self): return self.thisptr.Age()
def __get__(self): return self.thisptr.AgeGens()


cdef class Population:
Expand Down
6 changes: 3 additions & 3 deletions cMultiNeat.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -403,16 +403,16 @@ cdef extern from "src/Species.h" namespace "NEAT":
double GetBestFitness()
void SetBestSpecies(bool t)
void SetWorstSpecies(bool t)
void IncreaseAge()
void ResetAge()
void IncreaseAgeGens()
void ResetAgeGens()
void IncreaseGensNoImprovement()
void SetOffspringRqd(double a_ofs)
double GetOffspringRqd()
unsigned int NumIndividuals()
void ClearIndividuals()
int ID()
int GensNoImprovement()
int Age()
int AgeGens()
Genome GetIndividualByIdx(int a_idx)
bool IsBestSpecies()
bool IsWorstSpecies()
Expand Down
2 changes: 1 addition & 1 deletion examples/DefaultConfig.NEAT
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ SpeciesDropoffAge 50
// Setting this value to 0.0 makes the system to behave like regular NEAT.
StagnationDelta 0.0

// Age threshold, meaning if a species is above it, it is considered old
// AgeGens threshold, meaning if a species is above it, it is considered old
OldAgeTreshold 30

// Multiplier that penalizes old species.
Expand Down
31 changes: 16 additions & 15 deletions examples/TestNEAT_xor.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,34 +56,34 @@ def evaluate(genome):


params = NEAT.Parameters()
params.PopulationSize = 150
params.PopulationSize = 100
params.DynamicCompatibility = True
params.NormalizeGenomeSize = True
params.WeightDiffCoeff = 0.02
params.WeightDiffCoeff = 0.1
params.CompatTreshold = 2.0
params.YoungAgeTreshold = 15
params.SpeciesMaxStagnation = 15
params.OldAgeTreshold = 35
params.MinSpecies = 5
params.MinSpecies = 2
params.MaxSpecies = 10
params.RouletteWheelSelection = False
params.RecurrentProb = 0.0
params.OverallMutationRate = 0.8
params.OverallMutationRate = 1.0

params.ArchiveEnforcement = False

params.MutateWeightsProb = 0.90
params.MutateWeightsProb = 0.05

params.WeightMutationMaxPower = 2.5
params.WeightReplacementMaxPower = 5.0
params.MutateWeightsSevereProb = 0.2
params.WeightMutationRate = 0.75
params.WeightReplacementRate = 0.333
params.WeightMutationMaxPower = 0.5
params.WeightReplacementMaxPower = 8.0
params.MutateWeightsSevereProb = 0.0
params.WeightMutationRate = 0.25
params.WeightReplacementRate = 0.9

params.MaxWeight = 8

params.MutateAddNeuronProb = 0.03
params.MutateAddLinkProb = 0.05
params.MutateAddNeuronProb = 0.001
params.MutateAddLinkProb = 0.3
params.MutateRemLinkProb = 0.0

params.MinActivationA = 4.9
Expand All @@ -94,14 +94,15 @@ def evaluate(genome):
params.ActivationFunction_Tanh_Prob = 0.0
params.ActivationFunction_SignedStep_Prob = 0.0

params.CrossoverRate = 0.75 # mutate only 0.25
params.MultipointCrossoverRate = 0.4
params.CrossoverRate = 0.0
params.MultipointCrossoverRate = 0.0
params.SurvivalRate = 0.2

params.MutateNeuronTraitsProb = 0
params.MutateLinkTraitsProb = 0

params.AllowLoops = False
params.AllowLoops = True
params.AllowClones = True

def getbest(i):
g = NEAT.Genome(0, 3, 0, 1, False, NEAT.ActivationFunction.UNSIGNED_SIGMOID,
Expand Down
4 changes: 2 additions & 2 deletions src/Parameters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ namespace NEAT
// GA Parameters
////////////////////////////////

// Age treshold, meaning if a species is below it, it is considered young
// AgeGens treshold, meaning if a species is below it, it is considered young
YoungAgeTreshold = 5;

// Fitness boost multiplier for young species (1.0 means no boost)
Expand All @@ -104,7 +104,7 @@ namespace NEAT
// Setting this value to 0.0 makes the system to behave like regular NEAT.
StagnationDelta = 0.0;

// Age threshold, meaning if a species is above it, it is considered old
// AgeGens threshold, meaning if a species is above it, it is considered old
OldAgeTreshold = 30;

// Multiplier that penalizes old species.
Expand Down
4 changes: 2 additions & 2 deletions src/Parameters.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class Parameters
// GA Parameters
////////////////////////////////

// Age treshold, meaning if a species is below it, it is considered young
// AgeGens treshold, meaning if a species is below it, it is considered young
unsigned int YoungAgeTreshold;

// Fitness boost multiplier for young species (1.0 means no boost)
Expand All @@ -122,7 +122,7 @@ class Parameters
// Setting this value to 0.0 makes the system to behave like regular NEAT.
double StagnationDelta;

// Age threshold, meaning if a species if above it, it is considered old
// AgeGens threshold, meaning if a species if above it, it is considered old
unsigned int OldAgeTreshold;

// Multiplier that penalizes old species.
Expand Down
18 changes: 9 additions & 9 deletions src/Population.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ void Population::UpdateSpecies()
for(unsigned int i=0; i<m_Species.size(); i++)
{
// Reset the species and update its age
m_Species[i].IncreaseAge();
m_Species[i].IncreaseAgeGens();
m_Species[i].IncreaseGensNoImprovement();
m_Species[i].SetOffspringRqd(0);

Expand All @@ -435,7 +435,7 @@ void Population::UpdateSpecies()
// so it will die off anyway.
if ((t_oldbestid != t_newbestid) && (t_oldbestid != -1))
{
m_Species[t_oldbestidx].ResetAge();
m_Species[t_oldbestidx].ResetAgeGens();
}
}

Expand Down Expand Up @@ -562,8 +562,8 @@ void Population::Epoch()
}

// Now reset the stagnation counter and species age
m_Species[0].ResetAge();
m_Species[1].ResetAge();
m_Species[0].ResetAgeGens();
m_Species[1].ResetAgeGens();
m_GensSinceBestFitnessLastChanged = 0;
}
}
Expand Down Expand Up @@ -619,7 +619,7 @@ void Population::Epoch()
// reset the age of species
for(unsigned int i=0; i<m_Species.size(); i++)
{
m_Species[i].ResetAge();
m_Species[i].ResetAgeGens();
}
}
}
Expand All @@ -639,7 +639,7 @@ void Population::Epoch()
// reset the age of species
for(unsigned int i=0; i<m_Species.size(); i++)
{
m_Species[i].ResetAge();
m_Species[i].ResetAgeGens();
}
}
}
Expand Down Expand Up @@ -934,7 +934,7 @@ Genome* Population::Tick(Genome& a_deleted_genome)
// Find and save the best genome and fitness
for(unsigned int i=0; i<m_Species.size(); i++)
{
m_Species[i].IncreaseGensNoImprovement();
m_Species[i].IncreaseEvalsNoImprovement();

for(unsigned int j=0; j<m_Species[i].m_Individuals.size(); j++)
{
Expand All @@ -949,7 +949,7 @@ Genome* Population::Tick(Genome& a_deleted_genome)
// Reset the stagnation counter only if the fitness jump is greater or equal to the delta.
if (fabs(t_Fitness - m_BestFitnessEver) >= m_Parameters.StagnationDelta)
{
m_GensSinceBestFitnessLastChanged = 0;
m_EvalsSinceBestFitnessLastChanged = 0;
}

m_BestFitnessEver = t_Fitness;
Expand All @@ -972,7 +972,7 @@ Genome* Population::Tick(Genome& a_deleted_genome)
if (m_Species[i].m_Individuals[j].GetFitness() >= m_Species[i].GetBestFitness())
{
m_Species[i].m_BestFitness = m_Species[i].m_Individuals[j].GetFitness();
m_Species[i].m_GensNoImprovement = 0;
m_Species[i].m_EvalsNoImprovement = 0;
}
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/Population.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,9 @@ class Population
// Number of generations since the best fitness changed
unsigned int m_GensSinceBestFitnessLastChanged;

// Number of evaluations since the best fitness changed
unsigned int m_EvalsSinceBestFitnessLastChanged;

// How many generations passed until the last change of MPC
unsigned int m_GensSinceMPCLastChanged;

Expand Down
2 changes: 1 addition & 1 deletion src/PythonBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ BOOST_PYTHON_MODULE(_MultiNEAT)
.def("NumIndividuals", &Species::NumIndividuals)
.def("GensNoImprovement", &Species::GensNoImprovement)
.def("ID", &Species::ID)
.def("Age", &Species::Age)
.def("AgeGens", &Species::AgeGens)
.def("IsBestSpecies", &Species::IsBestSpecies)
.def_readwrite("Individuals", &Species::m_Individuals)
.def_readonly("Red", &Species::m_R)
Expand Down
8 changes: 4 additions & 4 deletions src/Species.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ namespace NEAT
// add the first and only one individual
m_Individuals.push_back(a_Genome);

m_Age = 0;
m_AgeGenerations = 0;
m_GensNoImprovement = 0;
m_OffspringRqd = 0;
m_BestFitness = a_Genome.GetFitness();
Expand Down Expand Up @@ -99,7 +99,7 @@ namespace NEAT
m_WorstSpecies = a_S.m_WorstSpecies;
m_BestFitness = a_S.m_BestFitness;
m_GensNoImprovement = a_S.m_GensNoImprovement;
m_Age = a_S.m_Age;
m_AgeGenerations = a_S.m_AgeGenerations;
m_OffspringRqd = a_S.m_OffspringRqd;
m_R = a_S.m_R;
m_G = a_S.m_G;
Expand Down Expand Up @@ -278,13 +278,13 @@ namespace NEAT
}

// boost the fitness up to some young age
if (m_Age < a_Parameters.YoungAgeTreshold)
if (m_AgeGenerations < a_Parameters.YoungAgeTreshold)
{
t_fitness *= a_Parameters.YoungAgeFitnessBoost;
}

// penalty for old species
if (m_Age > a_Parameters.OldAgeTreshold)
if (m_AgeGenerations > a_Parameters.OldAgeTreshold)
{
t_fitness *= a_Parameters.OldAgePenalty;
}
Expand Down
19 changes: 14 additions & 5 deletions src/Species.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,10 @@ class Species
bool m_WorstSpecies;


// age of species
unsigned int m_Age;
// age of species (in generations)
unsigned int m_AgeGenerations;
// age of species (in evaluations)
unsigned int m_AgeEvaluations;

// how many of this species should be spawned for
// the next population
Expand All @@ -86,6 +88,8 @@ class Species
// generations since fitness has improved, we can use
// this info to kill off a species if required
unsigned int m_GensNoImprovement;
// evaluations since fitness has improved
unsigned int m_EvalsNoImprovement;

// Color. Useful for displaying
// Safe to access directly.
Expand Down Expand Up @@ -117,16 +121,21 @@ class Species
double GetBestFitness() const { return m_BestFitness; }
void SetBestSpecies(bool t) { m_BestSpecies = t; }
void SetWorstSpecies(bool t) { m_WorstSpecies = t; }
void IncreaseAge() { m_Age++; }
void ResetAge() { m_Age = 0; m_GensNoImprovement = 0; }
void IncreaseAgeGens() { m_AgeGenerations++; }
void ResetAgeGens() { m_AgeGenerations = 0; m_GensNoImprovement = 0; }
void IncreaseGensNoImprovement() { m_GensNoImprovement++; }
void IncreaseAgeEvals() { m_AgeEvaluations++; }
void ResetAgeEvals() { m_AgeEvaluations = 0; m_EvalsNoImprovement = 0; }
void IncreaseEvalsNoImprovement() { m_EvalsNoImprovement++; }
void SetOffspringRqd(double a_ofs) { m_OffspringRqd = a_ofs; }
double GetOffspringRqd() const { return m_OffspringRqd; }
unsigned int NumIndividuals() { return m_Individuals.size(); }
void ClearIndividuals() { m_Individuals.clear(); }
int ID() { return m_ID; }
int GensNoImprovement() { return m_GensNoImprovement; }
int Age() { return m_Age; }
int EvalsNoImprovement() { return m_EvalsNoImprovement; }
int AgeGens() { return m_AgeGenerations; }
int AgeEvals() { return m_AgeEvaluations; }
Genome GetIndividualByIdx(int a_idx) const { return (m_Individuals[a_idx]); }
bool IsBestSpecies() const { return m_BestSpecies; }
bool IsWorstSpecies() const { return m_WorstSpecies; }
Expand Down

0 comments on commit f059c1a

Please sign in to comment.