@@ -2088,7 +2088,7 @@ namespace NEAT
20882088 // Helper functions for the pruning procedure
20892089
20902090 // Removes the link with the specified innovation ID
2091- void Genome::RemoveLinkGene (int a_InnovID)
2091+ /* void Genome::RemoveLinkGene(int a_InnovID)
20922092 {
20932093 // for iterating through the genes
20942094 std::vector<LinkGene>::iterator t_curlink = m_LinkGenes.begin();
@@ -2104,6 +2104,21 @@ namespace NEAT
21042104
21052105 t_curlink++;
21062106 }
2107+ }*/
2108+
2109+ // this version uses a simple index
2110+ void Genome::RemoveLinkGene (int a_idx)
2111+ {
2112+ // for iterating through the genes
2113+ auto t_curlink = m_LinkGenes.begin ();
2114+ if (a_idx > 0 )
2115+ {
2116+ m_LinkGenes.erase (m_LinkGenes.begin () + a_idx);
2117+ }
2118+ else
2119+ {
2120+ m_LinkGenes.clear ();
2121+ }
21072122 }
21082123
21092124
@@ -2113,22 +2128,31 @@ namespace NEAT
21132128 {
21142129 // the list of links connected to this neuron
21152130 std::vector<int > t_link_removal_queue;
2116-
2117- // OK find all links connected to this neuron ID
2118- for (unsigned int i = 0 ; i < NumLinks (); i++)
2131+
2132+ bool removed=false ;
2133+
2134+ do
21192135 {
2120- if ((m_LinkGenes[i].FromNeuronID () == a_ID) || (m_LinkGenes[i].ToNeuronID () == a_ID))
2136+ removed=false ;
2137+ // Remove all links connected to this neuron ID
2138+ for (int i = 0 ; i < NumLinks (); i++)
21212139 {
2122- // found one, add it
2123- t_link_removal_queue.emplace_back (m_LinkGenes[i].InnovationID ());
2140+ if ((m_LinkGenes[i].FromNeuronID () == a_ID) || (m_LinkGenes[i].ToNeuronID () == a_ID))
2141+ {
2142+ // found one, remove it
2143+ // t_link_removal_queue.emplace_back(i);//m_LinkGenes[i].InnovationID());
2144+ RemoveLinkGene (i);
2145+ removed=true ;
2146+ break ;
2147+ }
21242148 }
2125- }
2149+ } while (removed);
21262150
21272151 // Now remove them
2128- for (unsigned int i = 0 ; i < t_link_removal_queue.size (); i++)
2152+ /* for (unsigned int i = 0; i < t_link_removal_queue.size(); i++)
21292153 {
21302154 RemoveLinkGene(t_link_removal_queue[i]);
2131- }
2155+ }*/
21322156
21332157 // Now is safe to remove the neuron
21342158 // find it first
@@ -2338,7 +2362,7 @@ namespace NEAT
23382362
23392363 // Replaces a hidden neuron having only one input and only one output with
23402364 // a direct link between them.
2341- bool Genome::Mutate_RemoveSimpleNeuron (InnovationDatabase &a_Innovs, RNG &a_RNG)
2365+ bool Genome::Mutate_RemoveSimpleNeuron (InnovationDatabase &a_Innovs, const Parameters &a_Parameters, RNG &a_RNG)
23422366 {
23432367 // At least one hidden node must be present
23442368 if (NumNeurons () == (NumInputs () + NumOutputs ()))
@@ -2411,29 +2435,39 @@ namespace NEAT
24112435 // a novel innovation?
24122436 if (t_innovid == -1 )
24132437 {
2414- // Add the innovation and the link gene
2415- int t_newinnov = a_Innovs.AddLinkInnovation (m_LinkGenes[t_l1idx].FromNeuronID (),
2416- m_LinkGenes[t_l2idx].ToNeuronID ());
2417- m_LinkGenes.emplace_back (
2418- LinkGene (m_LinkGenes[t_l1idx].FromNeuronID (), m_LinkGenes[t_l2idx].ToNeuronID (), t_newinnov,
2419- t_weight, false ));
2420-
2421- // Remove the neuron now
2438+ // Save the IDs for a while
2439+ int from = m_LinkGenes[t_l1idx].FromNeuronID ();
2440+ int to = m_LinkGenes[t_l2idx].ToNeuronID ();
2441+
2442+ // Remove the neuron and its links now
24222443 RemoveNeuronGene (m_NeuronGenes[t_neurons_to_delete[t_choice]].ID ());
24232444
2445+ // Add the innovation and the link gene
2446+ int t_newinnov = a_Innovs.AddLinkInnovation (from, to);
2447+ LinkGene lg = LinkGene (from, to, t_newinnov, t_weight, false );
2448+ lg.InitTraits (a_Parameters.LinkTraits , a_RNG);
2449+
2450+ m_LinkGenes.emplace_back (lg);
2451+
24242452 // bye
24252453 return true ;
24262454 }
2427- // not a novel innovation
2455+ // not a novel innovation
24282456 else
24292457 {
2430- // Add the link and remove the neuron
2431- m_LinkGenes.emplace_back (
2432- LinkGene (m_LinkGenes[t_l1idx].FromNeuronID (), m_LinkGenes[t_l2idx].ToNeuronID (), t_innovid,
2433- t_weight, false ));
2434-
2435- // Remove the neuron now
2458+ // Save the IDs for a while
2459+ int from = m_LinkGenes[t_l1idx].FromNeuronID ();
2460+ int to = m_LinkGenes[t_l2idx].ToNeuronID ();
2461+
2462+ // Remove the neuron and its links now
24362463 RemoveNeuronGene (m_NeuronGenes[t_neurons_to_delete[t_choice]].ID ());
2464+
2465+ // Add the link
2466+ LinkGene lg = LinkGene (from, to, t_innovid, t_weight, false );
2467+ lg.InitTraits (a_Parameters.LinkTraits , a_RNG);
2468+ m_LinkGenes.emplace_back (lg);
2469+
2470+ // TODO: Maybe inherit the traits from one of the links
24372471
24382472 // bye
24392473 return true ;
@@ -2477,21 +2511,22 @@ namespace NEAT
24772511 {
24782512 if ((!t_severe_mutation) && (a_RNG.RandFloat () < a_Parameters.WeightMutationRate ))
24792513 {
2480- bool ontail = (i >= t_genometail);
2514+ bool ontail = false ; // (i >= t_genometail);
24812515 double t_LinkGenesWeight = m_LinkGenes[i].GetWeight ();
24822516
24832517 if (ontail || (a_RNG.RandFloat () < a_Parameters.WeightReplacementRate ))
24842518 {
2485- // t_LinkGenesWeight = a_RNG.RandFloatSigned() * a_Parameters.WeightReplacementMaxPower;
2486- t_LinkGenesWeight = a_RNG.RandFloat ();
2487- Scale (t_LinkGenesWeight, 0 , 1 , a_Parameters.MinWeight , a_Parameters.MaxWeight );
2519+ t_LinkGenesWeight = a_RNG.RandFloatSigned () * a_Parameters.WeightReplacementMaxPower ;
2520+
2521+ // t_LinkGenesWeight = a_RNG.RandFloat();
2522+ // Scale(t_LinkGenesWeight, 0.0, 1.0, a_Parameters.MinWeight, a_Parameters.MaxWeight);
24882523 }
24892524 else
24902525 {
24912526 t_LinkGenesWeight += a_RNG.RandFloatSigned () * a_Parameters.WeightMutationMaxPower ;
2492- Clamp (t_LinkGenesWeight, a_Parameters.MinWeight , a_Parameters.MaxWeight );
24932527 }
2494-
2528+
2529+ Clamp (t_LinkGenesWeight, a_Parameters.MinWeight , a_Parameters.MaxWeight );
24952530 m_LinkGenes[i].SetWeight (t_LinkGenesWeight);
24962531
24972532 did_mutate = true ;
@@ -2501,7 +2536,7 @@ namespace NEAT
25012536 if (a_RNG.RandFloat () < a_Parameters.WeightMutationRate )
25022537 {
25032538 double t_LinkGenesWeight = a_RNG.RandFloat ();
2504- Scale (t_LinkGenesWeight, 0 , 1 , a_Parameters.MinWeight , a_Parameters.MaxWeight );
2539+ Scale (t_LinkGenesWeight, 0.0 , 1.0 , a_Parameters.MinWeight , a_Parameters.MaxWeight );
25052540 m_LinkGenes[i].SetWeight (t_LinkGenesWeight);
25062541
25072542 did_mutate = true ;
@@ -2521,7 +2556,7 @@ namespace NEAT
25212556 {
25222557 double nf=0 ;
25232558 nf = a_RNG.RandFloat ();
2524- Scale (nf, 0 , 1 , a_Parameters.MinWeight , a_Parameters.MaxWeight );
2559+ Scale (nf, 0.0 , 1.0 , a_Parameters.MinWeight , a_Parameters.MaxWeight );
25252560 m_LinkGenes[i].SetWeight (nf);
25262561 }
25272562 }
0 commit comments