Ejemplo n.º 1
0
        private bool AddNode(NetworkGenome temp)
        {
            List <LinkGene> list = new List <LinkGene>(temp.MutatableLinks);

            if (list.Count < 1)
            {
                return(false);
            }
            LinkGene split      = list[(int)(Utilities.Rnd.NextDouble() * (double)list.Count)];
            NodeGene NodeGenes1 = temp.Nodes.Find((Predicate <NodeGene>)(g => g.Id == split.Source));
            NodeGene NodeGenes2 = temp.Nodes.Find((Predicate <NodeGene>)(g => g.Id == split.Target));

            if (Math.Abs(NodeGenes1.Layer - NodeGenes2.Layer) <= 1)
            {
                return(false);
            }
            NodeGene NodeGenes3 = new NodeGene("Identity", 0, Utilities.Rnd.Next(Math.Min(NodeGenes1.Layer, NodeGenes2.Layer) + 1, Math.Max(NodeGenes1.Layer, NodeGenes2.Layer)), NodeType.Hidden);

            NodeGenes3.Id       = HistoricalMarkings.GetNodeMarking(split.Id, NodeGenes3.Layer, this.Generation);
            NodeGenes3.Function = this.SelectFunction();
            LinkGene LinkGenes1 = new LinkGene(HistoricalMarkings.GetLinkMarking(NodeGenes1.Id, NodeGenes3.Id, this.Generation), NodeGenes1.Id, NodeGenes3.Id, split.Weight);
            LinkGene LinkGenes2 = new LinkGene(HistoricalMarkings.GetLinkMarking(NodeGenes3.Id, NodeGenes2.Id, this.Generation), NodeGenes3.Id, NodeGenes2.Id, Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, Utilities.Rnd.NextGuassian(0.0, 0.2) * this.Mutate.WeightRange / 2.0)));

            temp.Nodes.Add(NodeGenes3);
            temp.Links.Add(LinkGenes1);
            temp.Links.Add(LinkGenes2);
            if (Utilities.Rnd.NextDouble() > this.Mutate.KeepLinkOnAddNode)
            {
                temp.Links.Remove(split);
            }
            return(true);
        }
Ejemplo n.º 2
0
 private void IncrementGeneration()
 {
     ++this.Generation;
     for (int index = 0; index < this.Current.Genomes.Count; ++index)
     {
         ++this.Current.Genomes[index].Age;
     }
     HistoricalMarkings.CleanUp(this.Generation, this.Speciate.MarkingAge);
 }
Ejemplo n.º 3
0
 private void InitLinks(NetworkGenome temp)
 {
     foreach (NodeGene NodeGenes1 in Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)temp.Nodes, (Func <NodeGene, bool>)(nodes =>
     {
         if (nodes.Type != NodeType.Bias)
         {
             return(nodes.Type == NodeType.Input);
         }
         return(true);
     })))
     {
         foreach (NodeGene NodeGenes2 in Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)temp.Nodes, (Func <NodeGene, bool>)(nodes => nodes.Type == NodeType.Output)))
         {
             if (Utilities.Rnd.NextDouble() < this.Init.InitalConnectionPercentage)
             {
                 temp.Links.Add(new LinkGene(HistoricalMarkings.GetLinkMarking(NodeGenes1.Id, NodeGenes2.Id, 0), NodeGenes1.Id, NodeGenes2.Id, Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 2.0, this.Mutate.WeightRange / 2.0)));
             }
         }
     }
 }
Ejemplo n.º 4
0
        private bool AddConnection(NetworkGenome temp)
        {
            List <NodeGene> list1  = new List <NodeGene>((IEnumerable <NodeGene>)temp.Nodes);
            NodeGene        source = list1[(int)(Utilities.Rnd.NextDouble() * (double)list1.Count)];
            List <NodeGene> list2  = new List <NodeGene>(Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)temp.Nodes, (Func <NodeGene, bool>)(nodes =>
            {
                if (nodes.Type == NodeType.Bias || nodes.Type == NodeType.Input)
                {
                    return(false);
                }
                if (!this.Mutate.Recurrence)
                {
                    return(nodes.Layer > source.Layer);
                }
                return(true);
            })));

            if (list2.Count < 1)
            {
                return(false);
            }
            NodeGene target = list2[(int)(Utilities.Rnd.NextDouble() * (double)list2.Count)];

            if (Enumerable.Any <LinkGene>((IEnumerable <LinkGene>)temp.Links, (Func <LinkGene, bool>)(l =>
            {
                if (l.Source == source.Id)
                {
                    return(l.Target == target.Id);
                }
                return(false);
            })))
            {
                return(false);
            }
            temp.Links.Add(new LinkGene(0, source.Id, target.Id, Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, Utilities.Rnd.NextGuassian(0.0, 0.1) * this.Mutate.WeightRange / 2.0)))
            {
                Id = HistoricalMarkings.GetLinkMarking(source.Id, target.Id, this.Generation)
            });
            return(true);
        }