Skip to content

Commit 5d26484

Browse files
committed
Generational mode fix
1 parent a511d4e commit 5d26484

3 files changed

Lines changed: 55 additions & 39 deletions

File tree

src/Main.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ int main()
251251
double bestf = -999999;
252252
Genome gx;
253253

254-
Genome* baby;
254+
/* Genome* baby;
255255
baby = pop.Tick(gx);
256256
257257
double f = xortest(*baby);
@@ -263,7 +263,7 @@ int main()
263263
if (f > bestf)
264264
{
265265
bestf = f;
266-
}
266+
}*/
267267

268268
/*for(unsigned int i=0; i < pop.m_Species.size(); i++)
269269
{
@@ -290,7 +290,7 @@ int main()
290290

291291
printf("Tick: %d, best fitness: %3.5f\n", k, bestf);
292292
printf("Species: %d CT: %3.3f\n", pop.m_Species.size(), pop.m_Parameters.CompatTreshold);
293-
//pop.Epoch();
293+
pop.Epoch();
294294
}
295295

296296
for(int i=0; i<100; i++)

src/Population.cpp

Lines changed: 8 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -667,35 +667,26 @@ void Population::Epoch()
667667
/////////////////////////////
668668
// Reproduction
669669
/////////////////////////////
670-
671-
672-
// Kill all bad performing individuals
673-
// Todo: this baby/adult/killworst scheme is complicated and basically sucks,
674-
// I should remove it completely.
675-
// for(unsigned int i=0; i<m_Species.size(); i++) m_Species[i].KillWorst(m_Parameters);
676-
670+
677671
// Perform reproduction for each species
678672
m_TempSpecies.clear();
679673
m_TempSpecies = m_Species;
680674
for(unsigned int i=0; i<m_TempSpecies.size(); i++)
681675
{
682676
m_TempSpecies[i].Clear();
677+
m_TempSpecies[i].AddIndividual(m_Species[i].m_Individuals[0]);
683678
}
684679

685680
for(unsigned int i=0; i<m_Species.size(); i++)
686681
{
687-
m_Species[i].Reproduce(*this, m_Parameters, //m_Species[i].m_Parameters,
688-
m_RNG);
682+
m_Species[i].Reproduce(*this, m_Parameters, m_RNG);
683+
}
684+
for(unsigned int i=0; i<m_TempSpecies.size(); i++)
685+
{
686+
m_TempSpecies[i].RemoveIndividual(0);
689687
}
690688
m_Species = m_TempSpecies;
691689

692-
693-
// Now we kill off the old parents
694-
// Todo: this baby/adult scheme is complicated and basically sucks,
695-
// I should remove it completely.
696-
// for(unsigned int i=0; i<m_Species.size(); i++) m_Species[i].KillOldParents();
697-
698-
// Here we kill off any empty species too
699690
// Remove all empty species (cleanup routine for every case..)
700691
for(unsigned int i=0; i<m_Species.size(); i++)
701692
{
@@ -968,16 +959,7 @@ void Population::ReassignSpecies(int a_genome_idx)
968959
t_to_compare = t_cur_species->GetRepresentative();
969960
break;
970961
}
971-
/*else
972-
{
973-
t_cur_species++;
974-
}*/
975-
};/// while( (t_cur_species != m_Species.end()) && ((t_cur_species->NumIndividuals() > 0)) );
976-
977-
/*if (t_cur_species != m_Species.end())
978-
{
979-
t_to_compare = t_cur_species->GetRepresentative();
980-
}*/
962+
}
981963
}
982964
}
983965

src/Species.cpp

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -481,12 +481,32 @@ namespace NEAT
481481
if ((a_RNG.RandFloat() < a_Parameters.InterspeciesCrossoverRate) &&
482482
(a_Pop.m_Species.size() > 1))
483483
{
484-
// Find different species (random one) // !!!!!!!!!!!!!!!!!
485-
int t_diffspec = a_RNG.RandInt(0, static_cast<int>(a_Pop.m_Species.size() - 1));
486-
487-
t_mom = GetIndividual(a_Parameters, a_RNG);
488-
t_dad = a_Pop.m_Species[t_diffspec].GetIndividual(a_Parameters, a_RNG);
489-
t_interspecies = true;
484+
/// Find different species via roulette over average fitness as probability
485+
std::vector<double> probs;
486+
double allp=0;
487+
for(int i=0; i<a_Pop.m_Species.size(); i++)
488+
{
489+
if ((a_Pop.m_Species[i].m_ID == m_ID))
490+
{
491+
probs.push_back(0.0);
492+
}
493+
else
494+
{
495+
probs.push_back(a_Pop.m_Species[i].m_AverageFitness);
496+
}
497+
allp += probs[probs.size()-1];
498+
}
499+
if (allp > 0)
500+
{
501+
int t_diffspec = a_RNG.Roulette(probs);
502+
t_mom = GetIndividual(a_Parameters, a_RNG);
503+
t_dad = a_Pop.m_Species[t_diffspec].GetIndividual(a_Parameters, a_RNG);
504+
t_interspecies = true;
505+
}
506+
else
507+
{
508+
continue;
509+
}
490510
}
491511
else
492512
{
@@ -496,7 +516,7 @@ namespace NEAT
496516

497517
// The other parent should be a different one
498518
// number of tries to find different parent
499-
int t_tries = 10;
519+
int t_tries = 32;
500520
while (((t_mom.GetID() == t_dad.GetID())) && (t_tries--))
501521
{
502522
t_mom = GetIndividual(a_Parameters, a_RNG);
@@ -552,7 +572,7 @@ namespace NEAT
552572
{
553573
if (
554574
(t_baby.CompatibilityDistance(a_Pop.m_TempSpecies[i].m_Individuals[j],
555-
a_Parameters) <= a_Parameters.MinDeltaCompatEqualGenomes) // identical genome?
575+
a_Parameters) < a_Parameters.MinDeltaCompatEqualGenomes) // identical genome?
556576
)
557577
{
558578
t_baby_exists_in_pop = true;
@@ -569,7 +589,7 @@ namespace NEAT
569589
{
570590
if (
571591
(t_baby.CompatibilityDistance(a_Pop.m_GenomeArchive[i],
572-
a_Parameters) <= a_Parameters.MinDeltaCompatEqualGenomes) // identical genome?
592+
a_Parameters) < a_Parameters.MinDeltaCompatEqualGenomes) // identical genome?
573593
)
574594
{
575595
t_baby_exists_in_pop = true;
@@ -652,10 +672,24 @@ namespace NEAT
652672
else
653673
{
654674
// keep searching for a matching species
655-
t_cur_species++;
675+
/*t_cur_species++;
656676
if (t_cur_species != a_Pop.m_TempSpecies.end())
657677
{
658678
t_to_compare = t_cur_species->GetRepresentative(); // was GetRepresentative()
679+
}*/
680+
681+
while(1)
682+
{
683+
t_cur_species++;
684+
if (t_cur_species == a_Pop.m_TempSpecies.end())
685+
{
686+
break;
687+
}
688+
if (t_cur_species->NumIndividuals() > 0)
689+
{
690+
t_to_compare = t_cur_species->GetRepresentative();
691+
break;
692+
}
659693
}
660694
}
661695
}

0 commit comments

Comments
 (0)