Exemplo 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);
        }
Exemplo n.º 2
0
        public int CompareTo(object obj)
        {
            LinkGene linkGene = obj as LinkGene;

            if (linkGene == null)
            {
                throw new InvalidOperationException("Object being compared to is not a Link Gene!");
            }
            return(this.Id.CompareTo(linkGene.Id));
        }
Exemplo n.º 3
0
        private bool DelectConnection(NetworkGenome temp)
        {
            List <LinkGene> list = new List <LinkGene>(temp.MutatableLinks);

            if (list.Count < 1)
            {
                return(false);
            }
            LinkGene LinkGenes = list[(int)(Utilities.Rnd.NextDouble() * (double)list.Count)];

            temp.Links.Remove(LinkGenes);
            return(true);
        }
Exemplo n.º 4
0
        //phillip
        static void LinkCreation(NetworkGenome sub)
        {
            LinkGene temp;
            int      linkId = 0;

            for (int i = 0; i < sub.Nodes.Count; i++)
            {
                for (int j = 0; j < sub.Nodes.Count && sub.Nodes[i].Layer > sub.Nodes[j].Layer; j++)
                {
                    if (sub.Nodes[i].Layer - sub.Nodes[j].Layer == 1)
                    {
                        temp    = new LinkGene();
                        temp.Id = linkId;
                        linkId++;
                        temp.Source = sub.Nodes[j].Id;
                        temp.Target = sub.Nodes[i].Id;
                        temp.Weight = 0;
                        sub.Links.Add(temp);
                    }
                }
            }
        }
Exemplo n.º 5
0
        private void MutateWeights(NetworkGenome temp)
        {
            double num1 = 0.0;

            for (int index = 0; index < this.Mutate.WeightProbabilities.Count; ++index)
            {
                num1 += this.Mutate.WeightProbabilities[index].Probability;
            }
            double num2 = Utilities.Rnd.NextDouble() * num1;

            Mutation.WeightMutation weightMutation = new Mutation.WeightMutation();
            for (int index = 0; index < this.Mutate.WeightProbabilities.Count; ++index)
            {
                num2 -= this.Mutate.WeightProbabilities[index].Probability;
                if (num2 <= 0.0)
                {
                    weightMutation = this.Mutate.WeightProbabilities[index];
                    break;
                }
            }
            if (weightMutation.Proportional)
            {
                List <LinkGene> list = new List <LinkGene>(temp.MutatableLinks);
                for (int index = 0; index < list.Count; ++index)
                {
                    if (Utilities.Rnd.NextDouble() < weightMutation.Amount)
                    {
                        if (weightMutation.Delta)
                        {
                            list[index].Weight += weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 0.3) * this.Mutate.WeightRange / 10.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 10.0, this.Mutate.WeightRange / 10.0);
                        }
                        else
                        {
                            list[index].Weight = weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 0.3) * this.Mutate.WeightRange / 2.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 2.0, this.Mutate.WeightRange / 2.0);
                        }
                        list[index].Weight = Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, list[index].Weight));
                    }
                }
            }
            else
            {
                for (int index1 = 0; (double)index1 < weightMutation.Amount && Enumerable.Count <LinkGene>(temp.MutatableLinks) > 0; ++index1)
                {
                    int      index2    = Utilities.Rnd.Next(Enumerable.Count <LinkGene>(temp.MutatableLinks));
                    LinkGene LinkGenes = Enumerable.ElementAt <LinkGene>(temp.MutatableLinks, index2);
                    LinkGenes.Mutated = true;
                    if (weightMutation.Delta)
                    {
                        LinkGenes.Weight += weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 1.0) * this.Mutate.WeightRange / 4.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 8.0, this.Mutate.WeightRange / 8.0);
                    }
                    else
                    {
                        LinkGenes.Weight = weightMutation.Normal ? Utilities.Rnd.NextGuassian(0.0, 0.3) * this.Mutate.WeightRange / 2.0 : Utilities.Rnd.NextDouble(-this.Mutate.WeightRange / 2.0, this.Mutate.WeightRange / 2.0);
                    }
                    LinkGenes.Weight = Math.Max(-this.Mutate.WeightRange / 2.0, Math.Min(this.Mutate.WeightRange / 2.0, LinkGenes.Weight));
                }
                for (int index = 0; index < temp.Links.Count; ++index)
                {
                    temp.Links[index].Mutated = false;
                }
            }
        }
Exemplo n.º 6
0
 public int CompareTo(LinkGene obj)
 {
     return(this.Id.CompareTo(obj.Id));
 }
Exemplo n.º 7
0
 public LinkGene(LinkGene copy)
     : this(copy.Id, copy.Source, copy.Target, copy.Weight, copy.Fixed, copy.Mutated)
 {
 }
Exemplo n.º 8
0
        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog openFileDialog = new OpenFileDialog();

            if (openFileDialog.ShowDialog() != DialogResult.OK)
            {
                return;
            }
            NetworkGenome networkGenome = NetworkGenome.LoadFromFile(openFileDialog.FileName);
            Graph         graph         = new Graph("Genome: " + (object)networkGenome.Id);

            graph.GraphAttr.LayerDirection = LayerDirection.BT;
            graph.Directed           = true;
            graph.GraphAttr.LayerSep = 10.0;
            graph.BuildNodeHierarchy = true;
            graph.GraphAttr.OptimizeLabelPositions = true;
            graph.GraphAttr.NodeAttr.Padding       = 1.0;
            int       num        = networkGenome.Nodes.Find((Predicate <NodeGene>)(n => n.Type == NodeType.Output)).Layer;
            ArrayList arrayList1 = new ArrayList();

            for (int i = 0; i < num + 1; ++i)
            {
                ArrayList arrayList2 = new ArrayList();
                foreach (NodeGene nodeGene in Enumerable.Where <NodeGene>((IEnumerable <NodeGene>)networkGenome.Nodes, (Func <NodeGene, bool>)(node => node.Layer == i)))
                {
                    arrayList2.Add((object)nodeGene.Id.ToString());
                }
                graph.AddSameLayer((IEnumerable)arrayList2);
            }
            graph.GraphAttr.Orientation       = Microsoft.Glee.Drawing.Orientation.Landscape;
            graph.NeedCalculateLayout         = true;
            graph.GraphAttr.Fontsize          = 8;
            graph.GraphAttr.EdgeAttr.Fontsize = 7;
            graph.Cluster = true;
            graph.GraphAttr.LabelFloat = LabelFloat.Float;
            using (List <LinkGene> .Enumerator enumerator = networkGenome.Links.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    LinkGene link = enumerator.Current;
                    if (link.Weight != 0.0 && link.Source != -1)
                    {
                        //Edge edge = graph.AddEdge(link.Source.ToString(), link.Id.ToString(), link.Target.ToString());
                        Edge edge = graph.AddEdge(link.Source.ToString(), link.Weight.ToString("0.##"), link.Target.ToString());
                        edge.Attr.Fontsize     = 7;
                        edge.EdgeAttr.Fontsize = 7;
                        if (link.Weight <= 0.0)
                        {
                            edge.Attr.Color = Microsoft.Glee.Drawing.Color.Red;
                        }
                        NodeGene nodeGene1 = networkGenome.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Source));
                        NodeGene nodeGene2 = networkGenome.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Target));
                        edge.Attr.Weight = Math.Max(nodeGene1.Layer - nodeGene2.Layer, 0);
                        edge.UserData    = (object)link;
                    }
                }
            }
            for (int index = 0; index < networkGenome.Nodes.Count; ++index)
            {
                (graph.FindNode(networkGenome.Nodes[index].Id.ToString()) ?? graph.AddNode(networkGenome.Nodes[index].Id.ToString())).UserData = (object)networkGenome.Nodes[index];
            }
            foreach (NodeGene nodeGene in networkGenome.Nodes)
            {
                if (nodeGene.Layer == 0)
                {
                    Visualizers.CreateSourceNode(graph.FindNode(nodeGene.Id.ToString()));
                }
                else if (nodeGene.Layer == num)
                {
                    this.CreateTargetNode(graph.FindNode(nodeGene.Id.ToString()));
                }
                else
                {
                    this.CreateHiddenNode(graph.FindNode(nodeGene.Id.ToString()));
                }
            }
            this.gViewer.Graph = graph;
        }
Exemplo n.º 9
0
        public DecodedNetworks GenerateNetwork(MapWeights create, DecodedNetworks cppn)
        {
            for (int index = 0; index < this.net.ActivatingNodes.Count; ++index)
            {
                this.net.ActivatingNodes[index].links.Clear();
            }
            create(this.sub, cppn);
            double val1 = 0.0;

            foreach (LinkGene linkGene in this.sub.Links)
            {
                val1 = Math.Max(val1, Math.Abs(linkGene.Weight));
            }
            List <LinkGene> list             = new List <LinkGene>();
            double          staticThreshold1 = Substrates.param.StaticThreshold;

            DecodedNetworks.Link link1;
            using (List <LinkGene> .Enumerator enumerator = this.sub.Links.GetEnumerator())
            {
                while (enumerator.MoveNext())
                {
                    LinkGene link = enumerator.Current;
                    link.Weight *= Substrates.param.WeightRange / (2.0 * val1);
                    double staticThreshold2 = Substrates.param.StaticThreshold;
                    if (Substrates.param.ThresholdByDistance)
                    {
                        SubstrateNodes substrateNode1 = this.sub.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Source)) as SubstrateNodes;
                        SubstrateNodes substrateNode2 = this.sub.Nodes.Find((Predicate <NodeGene>)(n => n.Id == link.Target)) as SubstrateNodes;
                        double         d = 0.0;
                        for (int index = 0; index < substrateNode1.Coordinate.Count; ++index)
                        {
                            double num = substrateNode1.Coordinate[index] - substrateNode2.Coordinate[index];
                            d += num * num;
                        }
                        double num1 = Math.Sqrt(d);
                        staticThreshold2 += num1 * Substrates.param.StaticThreshold;
                    }
                    if (Math.Abs(link.Weight) >= staticThreshold2)
                    {
                        link1.sourceNode = link.Source;
                        link1.weight     = link.Weight;
                        this.net.Neurons[link.Target].links.Add(link1);
                    }
                }
            }
            for (int index = 0; index < this.net.ActivatingNodes.Count; ++index)
            {
                link1.sourceNode = -1;
                link1.weight     = 0.0;
                this.net.ActivatingNodes[index].links.Add(link1);
            }
            if (Substrates.param.Normalize)
            {
                double num1 = 1.0;
                for (int index1 = 0; index1 < this.net.ActivatingNodes.Count; ++index1)
                {
                    double d = 0.0;
                    for (int index2 = 0; index2 < this.net.ActivatingNodes[index1].links.Count - 1; ++index2)
                    {
                        d += this.net.ActivatingNodes[index1].links[index2].weight * this.net.ActivatingNodes[index1].links[index2].weight;
                    }
                    num1 = Math.Max(num1, Math.Sqrt(d));
                }
                double num2 = Math.Sqrt(num1);
                for (int index1 = 0; index1 < this.net.ActivatingNodes.Count; ++index1)
                {
                    for (int index2 = 0; index2 < this.net.ActivatingNodes[index1].links.Count - 1; ++index2)
                    {
                        DecodedNetworks.Link link2;
                        link2.sourceNode = this.net.ActivatingNodes[index1].links[index2].sourceNode;
                        link2.weight     = this.net.ActivatingNodes[index1].links[index2].weight / num2;
                        this.net.ActivatingNodes[index1].links[index2] = link2;
                    }
                }
            }
            return(this.net);
        }