public NodeGraph GenerateDesignGraph(int count) { if (DataSource.Count < count) { ResetDataSource(count); } //if (DataSource.Count < count) var result = new NodeGraph(); Random rand = new Random(); try { //Create and add vertices using some DataSource for ID's foreach (var item in DataSource.Take(count)) { result.AddVertex(new DataVertex() { ID = item.ID, Text = item.Text }); } var vlist = result.Vertices.ToList(); //Generate random edges for the vertices int c = 0; foreach (var vertex1 in vlist) { //if (rand.Next(0, count) > count/10) continue; var vertex2 = vlist[rand.Next(0, result.VertexCount - 1)]; result.AddEdge(new DataEdge(vertex1, vertex2, 1) { Text = string.Format("{0} -> {1}", vertex1, vertex2) }); if (c % 5 == 0) { var vertex3 = vlist[rand.Next(0, result.VertexCount - 1)]; result.AddEdge(new DataEdge(vertex1, vertex3, 0.5) { Text = string.Format("{0} -> {1}", vertex1, vertex2) }); } c++; } } catch (Exception ex) { Console.WriteLine(ex.Message); } return(result); }
private void Awake() { m_NodeGraph = new NodeGraph(); PathNode[] nodes = GraphRoot.GetComponentsInChildren <PathNode>(); for (int i = 0; i < nodes.Length; ++i) { nodes[i].Id = m_NodeGraph.AddNode(nodes[i].name, 0, nodes[i].transform.position); nodes[i].OnClick = OnNodeClick; nodes[i].OnEnableOrDisable = OnNodeEnableOrDisable; } for (int i = 0; i < nodes.Length; ++i) { foreach (var connected in nodes[i].ConnectedNodes) { Vector3 center = (nodes[i].transform.position + connected.transform.position) / 2; Vector3 vector = connected.transform.position - nodes[i].transform.position; float distance = vector.magnitude; ushort linkId = m_NodeGraph.AddEdge(nodes[i].Id, connected.Id, distance); Transform link = Instantiate(LinkPrefab, center, Quaternion.identity, GraphRoot); link.localEulerAngles = new Vector3(0, 0, Mathf.Rad2Deg * (float)Math.Atan2(vector.y, vector.x)); link.localScale = new Vector3(distance, link.localScale.y, 1); } } m_CurrentNode = 0; Actor.position = m_NodeGraph.Node(0).Position; m_NodeGraph.OptimizeEdgeOrder(); m_CurrentPath = new NodePath(8); StartCoroutine(MoveActor(m_CurrentPath)); }
static public void EdgesCanBeAddedOutOfOrder() { NodeGraph graph = new NodeGraph(); ushort start = graph.AddNode(); ushort mid = graph.AddNode(); ushort end = graph.AddNode(); graph.AddEdge(start, mid); graph.AddEdge(mid, start); graph.AddEdge(end, mid); graph.AddEdge(mid, end); graph.OptimizeEdgeOrder(); Assert.AreEqual(0, graph.Node(start).EdgeStartIndex); Assert.AreEqual(1, graph.Node(start).EdgeCount); Assert.AreEqual(2, graph.Node(mid).EdgeCount); }
static public void AStarPathCanBeRun() { NodeGraph graph = new NodeGraph(); ushort start = graph.AddNode(); ushort mid = graph.AddNode(); ushort end = graph.AddNode(); graph.AddEdge(start, mid); graph.AddEdge(mid, start); graph.AddEdge(end, mid); graph.AddEdge(mid, end); graph.OptimizeEdgeOrder(); NodePath path = new NodePath(); bool bFoundPath = Pathfinder.AStar(graph, ref path, start, end); Assert.IsTrue(bFoundPath); }
private void initGraphButton_Click(object sender, EventArgs e) { graph = new NodeGraph(); try { adjmatrix = Helper.MatrixFromString(matrixBox.Text); } catch (FormatException ex) { MessageBox.Show(ex.Message, "Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } int nodeCount = adjmatrix.GetLength(0); vertices = new Vertex[nodeCount]; for (int k = 0; k < nodeCount; k++) { vertices[k] = new Vertex(k.ToString(), 1.0, 0); graph.AddVertex(vertices[k]); } //dreadnaut für orbits starten string dreadnautcmd = "n=" + nodeCount + " g "; for (int irow = 0; irow < nodeCount; irow++) { for (int icol = 0; icol < nodeCount; icol++) { if (adjmatrix[irow, icol] != 0) { graph.AddEdge(new Edge<Vertex>(vertices[irow], vertices[icol])); dreadnautcmd += icol + " "; } } dreadnautcmd += ";"; } dreadnautcmd += "x o q"; string path = System.IO.Path.Combine(System.IO.Path.GetTempPath(), "dreadnaut.exe"); ProcessStartInfo startInfo = new ProcessStartInfo(); startInfo.UseShellExecute = false; startInfo.RedirectStandardInput = true; startInfo.RedirectStandardOutput = true; startInfo.WindowStyle = ProcessWindowStyle.Hidden; startInfo.FileName = path; Process process = new Process(); process.StartInfo = startInfo; try { System.IO.File.WriteAllBytes(path, ClusterNum.Properties.Resources.dreadnaut); process.Start(); } catch (Exception ex) { MessageBox.Show("dreadnaut konnte nicht gestartet werden", "dreadnaut-Fehler", MessageBoxButtons.OK, MessageBoxIcon.Error); return; } process.StandardInput.WriteLine(dreadnautcmd); process.WaitForExit(); System.IO.File.Delete(path); string ergebnis = process.StandardOutput.ReadToEnd(); //cluster auslesen cluster = Helper.dreadnaut2cluster(ergebnis); rmsChart.Series.Clear(); gseries = new Series[nodeCount]; clusterBox.Text = ""; for (int nodenum = 0; nodenum < nodeCount; nodenum++) { gseries[nodenum] = new Series(); gseries[nodenum].IsVisibleInLegend = false; gseries[nodenum].ChartArea = "ChartArea1"; rmsChart.Series.Add(gseries[nodenum]); } for (int i = 0; i < cluster.Length; i++) { clusterBox.Text += "Cluster " + i + " mit Knoten: "; foreach (int k in cluster[i]) { clusterBox.Text += +k + " "; vertices[k].Cluster = i; } clusterBox.Text += "\r\n"; System.Windows.Media.Color coltmp = Vertex.cluster_colors[i % Vertex.cluster_colors.Length]; foreach (int nodenum in cluster[i]) { if (nodenum == cluster[i][0]) { gseries[nodenum].LegendText = "Cluster " + i.ToString(); gseries[nodenum].IsVisibleInLegend = true; } gseries[nodenum].BorderWidth = 2; gseries[nodenum].ChartType = SeriesChartType.FastLine; gseries[nodenum].Color = Color.FromArgb(255, coltmp.R, coltmp.G, coltmp.B); } } //Tmat erstellen if (networkDropdown.SelectedIndex == 3) { //nutze relativkoordinaten wenn custom ausgewählt TMat = Helper.TMat(cluster); } else {//nutze eingespeicherte TMat = Helper.TMat(networkDropdown.SelectedIndex); } //Parameter für die anordnung. einfach irgendwelche genommen. nochmal drüber nachdenken/nachlesen GraphControl = new GraphSharpControl(); GraphControl.layout.LayoutMode = LayoutMode.Simple; GraphControl.layout.LayoutAlgorithmType = "CompoundFDP"; GraphSharp.Algorithms.Layout.Compound.FDP.CompoundFDPLayoutParameters layoutParam = new GraphSharp.Algorithms.Layout.Compound.FDP.CompoundFDPLayoutParameters(); layoutParam.ElasticConstant *= 1.5; GraphControl.layout.LayoutParameters = layoutParam; GraphControl.layout.OverlapRemovalConstraint = AlgorithmConstraints.Must; GraphControl.layout.OverlapRemovalAlgorithmType = "FSA"; GraphSharp.Algorithms.OverlapRemoval.OverlapRemovalParameters overlapParam = new GraphSharp.Algorithms.OverlapRemoval.OverlapRemovalParameters(); overlapParam.HorizontalGap = 25; overlapParam.VerticalGap = 25; GraphControl.layout.OverlapRemovalParameters = overlapParam; GraphControl.layout.HighlightAlgorithmType = "Simple"; GraphControl.layout.Graph = graph; elementHost1.Child = GraphControl; graph_loaded = true; iterator_init(); runButton.Enabled = true; iterateButton.Enabled = true; layoutButton.Enabled = true; betaRunButton.Enabled = true; }