public void TestFastGraph() { var graph = new FastGraph <int, TestNode, TestConnectionData>(100, 100); var graph2 = new FastGraph <int, TestNode, TestConnectionData>(3, 1); TestGraph <int, TestNode, TestConnectionData, FastGraph <int, TestNode, TestConnectionData> >(graph, (idx) => new TestNode(idx, UnityEngine.Random.Range(0, 100000)), (idx) => idx, () => new TestConnectionData() { cost = UnityEngine.Random.value }, (node) => node.index); // Test memory management TestNode node1 = new TestNode(0, 1); TestNode node2 = new TestNode(1, 2); TestNode node3 = new TestNode(2, 3); TestConnectionData cData1 = new TestConnectionData() { cost = 0.5f }; TestConnectionData cData2 = new TestConnectionData() { cost = 1f }; graph2.Add(0, node1); graph2.Add(1, node2); graph2.Add(2, node3); graph2.Connect(node1, node2, cData1); bool exceptionThrown = false; try { graph2.Connect(node1, node3, cData2); } catch (OutOfMemoryException ex) { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); exceptionThrown = false; try { graph2.Add(3, node1); } catch (OutOfMemoryException ex) { exceptionThrown = true; } Assert.IsTrue(exceptionThrown); }
protected override void Prepare() { // Prep graphs this.graph = new Graph <int, float, float>(); this.fastGraph = new FastGraph <int, float, float>(NODECOUNT, CONNECTIONSMAX); // Generate random test nodes List <float> nodes = new List <float>(); for (int i = 0; i < NODECOUNT; i++) { var v = 1f / (float)i; nodes.Add(v); graph.Add(i, v); fastGraph.Add(i, v); } // Connect them at random int ctr = 0; foreach (var node in nodes) { for (int i = 0; i < NODECOUNT; i++) { if (Random.value > 0.25f) { // Connect graph.Connect(node, graph.Get(i), Random.value); fastGraph.Connect(node, graph.Get(i), Random.value); ctr++; if (ctr >= CONNECTIONSMAX) { break; } } } } }
public IList <ILayout <TNode, TPolygon, TPosition, IntLine> > GetLayouts(FastGraph <int> graph, int minimumLayouts = 10) { // MapDescription = mapDescription; Graph = graph; iterationsCount = 0; var stopwatch = new Stopwatch(); stopwatch.Start(); var stack = new Stack <LayoutNode>(); var fullLayouts = new List <Layout>(); var graphChains = GetChains(graph); var initialLayout = GetInitialLayout(graphChains[0]); stack.Push(new LayoutNode() { Layout = AddChainToLayout(initialLayout, graphChains[0]), NumberOfChains = 0 }); if (WithDebug) { Console.WriteLine("--- Simulation has started ---"); } while (stack.Count > 0) { var layoutNode = stack.Pop(); var extendedLayouts = GetExtendedLayouts(layoutNode.Layout, graphChains[layoutNode.NumberOfChains], layoutNode.NumberOfChains == graphChains.Count); if (layoutNode.NumberOfChains + 1 == graphChains.Count) { foreach (var layout in extendedLayouts) { if (fullLayouts.TrueForAll(x => x.GetDifference(layout) > 2 * MinimumDifference) ) { if (fullLayouts.Count == 0) { timeFirst = stopwatch.ElapsedMilliseconds; } fullLayouts.Add(layout); } } } else { var sorted = extendedLayouts.Select(x => AddChainToLayout(x, graphChains[layoutNode.NumberOfChains + 1])) .OrderByDescending(x => x.GetEnergy()); foreach (var extendedLayout in sorted) { stack.Push(new LayoutNode() { Layout = extendedLayout, NumberOfChains = layoutNode.NumberOfChains + 1 }); } } if (fullLayouts.Count >= minimumLayouts) { break; } } stopwatch.Stop(); timeTen = stopwatch.ElapsedMilliseconds; layoutsCount = fullLayouts.Count; if (WithDebug) { Console.WriteLine($"{fullLayouts.Count} layouts generated"); Console.WriteLine($"Total time: {stopwatch.ElapsedMilliseconds} ms"); Console.WriteLine($"Total iterations: {iterationsCount}"); Console.WriteLine($"Iterations per second: {(int)(iterationsCount / (stopwatch.ElapsedMilliseconds / 1000f))}"); } AddDoors(fullLayouts); return(fullLayouts.Select(x => (ILayout <TNode, TPolygon, TPosition, IntLine>)x).ToList()); }
protected virtual List <List <int> > GetChains(FastGraph <int> graph) { throw new NotImplementedException(); }