private void button1_Click(object sender, EventArgs e) { OpenFileDialog openFileDialog = new OpenFileDialog(); if (openFileDialog.ShowDialog() != DialogResult.OK) { return; } XmlDocument xmlDocu = new XmlDocument(); xmlDocu.Load(openFileDialog.FileName); XmlNode newNode = xmlDocu.SelectSingleNode("//Root//Networks//Network"); NeatGenome networkGenome = NeatGenomeXmlIO.LoadGenome(newNode, false); 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 = 10.0; //int num = networkGenome.NeuronGeneList.Find((Predicate<NeuronGene>)(n => n.NodeType == NodeType.Output)); ; ArrayList arrayList1 = new ArrayList(); // for (int i = 0; i < 3 + 1; ++i) //{ ArrayList arrayList2 = new ArrayList(); foreach (NeuronGene nodeGene in Enumerable.Where <NeuronGene>((IEnumerable <NeuronGene>)networkGenome.NeuronGeneList, (Func <NeuronGene, bool>)(node => node.NodeType == NodeType.Output))) //node => node.layer=i { arrayList2.Add((object)nodeGene.Id.ToString()); } graph.AddSameLayer((IEnumerable)arrayList2); ArrayList arrayList3 = new ArrayList(); foreach (NeuronGene nodeGene in Enumerable.Where <NeuronGene>((IEnumerable <NeuronGene>)networkGenome.NeuronGeneList, (Func <NeuronGene, bool>)(node => node.NodeType == NodeType.Input))) //node => node.layer=i { arrayList3.Add((object)nodeGene.Id.ToString()); } graph.AddSameLayer((IEnumerable)arrayList3); ArrayList arrayList4 = new ArrayList(); foreach (NeuronGene nodeGene in Enumerable.Where <NeuronGene>((IEnumerable <NeuronGene>)networkGenome.NeuronGeneList, (Func <NeuronGene, bool>)(node => node.NodeType == NodeType.Hidden))) //node => node.layer=i { arrayList4.Add((object)nodeGene.Id.ToString()); } graph.AddSameLayer((IEnumerable)arrayList4); //} graph.GraphAttr.Orientation = Microsoft.Glee.Drawing.Orientation.Landscape; //graph.NeedCalculateLayout = true; graph.GraphAttr.Fontsize = 8; graph.GraphAttr.EdgeAttr.Fontsize = 4; graph.Cluster = true; graph.GraphAttr.LabelFloat = LabelFloat.Float; using (List <ConnectionGene> .Enumerator enumerator = networkGenome.ConnectionGeneList.GetEnumerator()) { while (enumerator.MoveNext()) { ConnectionGene link = enumerator.Current; //if (link.Weight != 0.0 )//&& link.SourceNodeId != -1) //{ if (link.SourceNodeId >= 14 && link.SourceNodeId <= 16) { continue; } Edge edge = graph.AddEdge(link.SourceNodeId.ToString(), link.Weight.ToString("0.##"), link.TargetNodeId.ToString()); edge.Attr.Fontsize = 4; edge.EdgeAttr.Fontsize = 4; if (link.Weight < 0.0) { edge.Attr.Color = Microsoft.Glee.Drawing.Color.Red; } if (link.Weight == 0.0) { edge.Attr.Color = Microsoft.Glee.Drawing.Color.White; } edge.UserData = (object)link; // } } } for (int index = 0; index < networkGenome.NeuronGeneList.Count; ++index) { (graph.FindNode(networkGenome.NeuronGeneList[index].Id.ToString()) ?? graph.AddNode(networkGenome.NeuronGeneList[index].Id.ToString())).UserData = (object)networkGenome.NeuronGeneList[index]; } foreach (NeuronGene nodeGene in networkGenome.NeuronGeneList) { if (nodeGene.NodeType == NodeType.Input || nodeGene.NodeType == NodeType.Bias) { Visualizers.CreateSourceNode(graph.FindNode(nodeGene.Id.ToString())); } else if (nodeGene.NodeType == NodeType.Output) { this.CreateTargetNode(graph.FindNode(nodeGene.Id.ToString())); } else { this.CreateHiddenNode(graph.FindNode(nodeGene.Id.ToString())); } } this.gViewer.Graph = graph; }
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; }