private static void AddConnectionToGraphFromConsole(string path, Graphs.Graph g) { //TO DO: can also have some kind of confirmation... int ID; List <int> stations; List <int> edgeValues; AddConnectionToGraphID(path, g, out ID); AddConnectionToGraphStations(path, g, out stations); AddConnectionToGraphCosts(path, g, out edgeValues); if (stations.Count - 1 != edgeValues.Count) { AddConnecitonToGraphBadCount(path, g, stations.Count, edgeValues.Count); return; } AddConnectionToGraph(g, stations, edgeValues, ID); }
private static void AnalyseTheSelectedOption(int option, string path, Graphs.Graph g) { switch (option) { case 1: { Graphs.Graph.PrintGraph(g); Console.WriteLine("Press enter to continue"); Console.ReadLine(); ContinueWorking(path, g); return; //ClearTheDB } case 2: { AddConnectionToGraphFromConsole(path, g); ContinueWorking(path, g); return; } case 3: { Console.WriteLine("Are you sure? Write \"y\" to confirm"); string s = Console.ReadLine(); if (s == "y") { Console.WriteLine("Deleting current graph"); g = new Graphs.Graph(); } else { Console.WriteLine("Aborting operation"); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); ContinueWorking(path, g); return; } case 4: { g.SaveToFile(path); return; } } }
public Graphs.Graph BuildWinTree(Graphs.Graph g, SuperSlimMind mind, SuperSlimBoard board, Bijection <int, string> numbering) { var tree = mind.BuildGameTree(board, true); var gg = g.Clone(); foreach (var v in gg.Vertices) { v.X = v.X / Scale; v.Y = v.Y / Scale; } var bounds = new Bounds(gg); var potSize = board.Stacks.Value.SelectMany(l => l.ToSet()).Distinct().Count(); var treeG = BuildWinTree(tree, g, mind, bounds, 0, numbering, new Choosability.Utility.Permutation(Enumerable.Range(0, potSize).ToList()), null, potSize); return(treeG); }
public Bounds(Graphs.Graph g) { Left = double.MaxValue; Right = double.MinValue; Top = double.MaxValue; Bottom = double.MinValue; foreach (var v in g.Vertices) { Left = Math.Min(Left, v.X); Right = Math.Max(Right, v.X); Top = Math.Min(Top, v.Y); Bottom = Math.Max(Bottom, v.Y); } Width = Right - Left; Height = Bottom - Top; }
public LayoutAnimation(Action update, Action finalUpdate, List <Graphs.Vector> layout, Graphs.Graph G) { _G = G; _update = update; _finalUpdate = finalUpdate; _layout = layout; try { _xstep = new double[_layout.Count]; _ystep = new double[_layout.Count]; for (int i = 0; i < _layout.Count; i++) { _xstep[i] = (_layout[i].X - _G.Vertices[i].X) / Steps; _ystep[i] = (_layout[i].Y - _G.Vertices[i].Y) / Steps; } } catch { } }
public static int Main() { //Graph init string path = "../../../connectionDB.txt"; Graphs.Graph g = ConnectionSearcherDatabaseReader.ReadGraphFromDB(path); //Input and finding the path int from, to; ConnectionPrinter.ReadFromTo(out from, out to); List <Graphs.Node> stations = Graphs.Algo.DijkstraAlgorithm(from, to, g); //Printing var p = ConnectionPrinter.AnalyseTheResult(stations); ConnectionPrinter.PrintTheConnection(p, from, to); return(0); }
string GetFileName(Graphs.Graph g) { return(Guid.NewGuid().ToString() + ".svg"); }
private static void ContinueWorking(string path, Graphs.Graph g) { int option = PrintMenu(true, true); AnalyseTheSelectedOption(option, path, g); }
Graphs.Graph BuildWinTree(GameTree tree, Graphs.Graph g, SuperSlimMind mind, Bounds original, int level, Bijection <int, string> numbering, Choosability.Utility.Permutation pp, List <string> lastListStrings, int potSize) { var clone = g.Clone(); var lists = tree.Board.Stacks.Value.Select(s => s.ToSet()).ToList(); var ppp = pp; if (tree.Parent != null) { var bestScore = int.MinValue; var allbad = true; foreach (var p in Choosability.Utility.Permutation.EnumerateAll(potSize)) { var bad = false; foreach (var sw in tree.Info.SwapVertices) { var lll = lists[sw].Select(ll => p[ll]).ToList(); var colors = new List <int>() { pp[tree.Info.Alpha], pp[tree.Info.Beta] }; if (Choosability.Utility.ListUtility.IntersectionCount(lll, colors) != 1) { bad = true; break; } } if (bad) { continue; } var strings = lists.Select(ll => string.Join(",", ll.Select(n => numbering[p[n]]).OrderBy(s => s))).ToList(); int score = 0; for (int ii = 0; ii < strings.Count; ii++) { if (strings[ii] == lastListStrings[ii]) { if (!tree.Info.SwapVertices.Contains(ii)) { score += 100; } } } if (score > bestScore) { bestScore = score; ppp = p; allbad = false; } } if (allbad) { System.Diagnostics.Debugger.Break(); } lists = lists.Select(l => l.Select(vv => ppp[vv]).ToList()).ToList(); } var listStrings = lists.Select(ll => string.Join(",", ll.Select(n => numbering[n]).OrderBy(s => s))).ToList(); if (tree.IsColorable) { Dictionary <int, long> coloring; mind.ColoringAnalyzer.Analyze(tree.Board, out coloring); for (int jj = 0; jj < clone.Edges.Count; jj++) { var v1 = mind.G.Edges.Value[jj].Item1; var v2 = mind.G.Edges.Value[jj].Item2; var c = coloring[jj].LeastSignificantBit(); if (ppp != null) { c = ppp[c]; } lists[v1].Remove(c); lists[v2].Remove(c); var e = clone.Edges.First(ee => Choosability.Utility.ListUtility.Equal(new List <int>() { clone.Vertices.IndexOf(ee.V1), clone.Vertices.IndexOf(ee.V2) }, new List <int>() { v1, v2 })); e.Label = numbering[c]; } } else if (tree.Reduction != null) { var c = tree.Reduction.Color.LeastSignificantBit(); if (ppp != null) { c = ppp[c]; } lists[tree.Reduction.Leaf].Remove(c); lists[tree.Reduction.Stem].Remove(c); foreach (var ee in clone.Edges) { ee.Label = ""; } var e = clone.Edges.First(ee => Choosability.Utility.ListUtility.Equal(new List <int>() { clone.Vertices.IndexOf(ee.V1), clone.Vertices.IndexOf(ee.V2) }, new List <int>() { tree.Reduction.Leaf, tree.Reduction.Stem })); e.Label = numbering[c] + " reduce"; e.Thickness = 5; } else { foreach (var e in clone.Edges) { var v1 = clone.Vertices.IndexOf(e.V1); var v2 = clone.Vertices.IndexOf(e.V2); if (!string.IsNullOrWhiteSpace(e.Label)) { var c = numbering[e.Label]; if (lists[v1].Contains(c) && lists[v2].Contains(c)) { lists[v1].Remove(c); lists[v2].Remove(c); } else { e.Label = ""; } } } foreach (var e in clone.Edges) { if (!string.IsNullOrWhiteSpace(e.Label) || string.IsNullOrWhiteSpace(g.Edges[clone.Edges.IndexOf(e)].Label)) { continue; } var v1 = clone.Vertices.IndexOf(e.V1); var v2 = clone.Vertices.IndexOf(e.V2); var common = Choosability.Utility.ListUtility.Intersection(lists[v1], lists[v2]); if (common.Count > 0) { e.Label = numbering[common[0]]; lists[v1].Remove(common[0]); lists[v2].Remove(common[0]); } } } for (int q = 0; q < lists.Count; q++) { clone.Vertices[q].Label = string.Join(",", numbering.Apply(lists[q].OrderBy(x => x))); } clone.Translate(new Graphs.Vector(-original.Left, -original.Top)); if (tree.Children.Count <= 0) { return(clone); } var children = tree.Children.Distinct().ToList(); var childGraphs = children.Select(child => BuildWinTree(child, g, mind, original, level++, numbering, ppp, listStrings, potSize)).ToList(); var childBounds = childGraphs.Select(cg => new Bounds(cg)).ToList(); var min = Math.Max(original.Width * Scale, original.Height * Scale); var W = Math.Max(min, childBounds.Max(cb => cb.Width)); var H = Math.Max(min, childBounds.Max(cb => cb.Height)); var uberGraph = new Graphs.Graph(); uberGraph.DisjointUnion(clone); int i = 0; double xoff = 0.0; foreach (var cg in childGraphs) { var gg = cg; gg.Translate(new Graphs.Vector(xoff, 2 * min)); uberGraph.DisjointUnion(gg); xoff += childBounds[i].Width + min; var a = children[i].Info == null ? "" : numbering[ppp[children[i].Info.Alpha]]; var b = children[i].Info == null ? "" : numbering[ppp[children[i].Info.Beta]]; var maxYY = clone.Vertices.Max(vvv => vvv.Y); var pvs = clone.Vertices.Where(vvv => vvv.Y == maxYY).ToList(); var avgXX = pvs.Sum(vvv => vvv.X) / pvs.Count; var daXX = pvs.Min(vvv => Math.Abs(vvv.X - avgXX)); var pv = pvs.FirstOrDefault(vvv => Math.Abs(vvv.X - avgXX) == daXX); if (pv == null) { pv = pvs.First(); } var maxYY2 = gg.Vertices.Min(vvv => vvv.Y); var pvs2 = gg.Vertices.Where(vvv => vvv.Y == maxYY2).ToList(); var avgXX2 = pvs2.Sum(vvv => vvv.X) / pvs2.Count; var daXX2 = pvs2.Min(vvv => Math.Abs(vvv.X - avgXX2)); var pv2 = pvs2.FirstOrDefault(vvv => Math.Abs(vvv.X - avgXX2) == daXX2); if (pv2 == null) { pv2 = pvs2.First(); } uberGraph.AddEdge(pv, pv2, Graphs.Edge.Orientations.Forward, 1, 1, "blue", a + " - " + b); foreach (var vi in children[i].Info.SwapVertices) { gg.Vertices[vi].Color = new GraphicsLayer.ARGB(120, 0, 255, 0); gg.Vertices[vi].Style = "green"; } i++; } clone.Translate(new Graphs.Vector((xoff / 2) - min - original.Width / 2, 0)); return(uberGraph); }
/// <summary> /// Creates a new transfers db. /// </summary> private TransfersDb(Graphs.Graph transfers) { _transfers = transfers; }