Example #1
0
        public void MetisGraph_FirstIteration_Success()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_dummyFile);

            var randomMock = new StubRandom()
            {
                NextInt32Int32 = (a, b) => 1
            };

            var graph = new MetisGraph(loaderMock.Object, randomMock);

            graph.InitializeGraph();
            graph.InitializeAnts(_optionTwoColors.NumberOfAnts);
            graph.ColorVerticesRandomly(_optionTwoColors.NumberOfPartitions);
            graph.CalculateLocalCostFunction();

            // First ant.
            Vertex vertexWithFirstAnt         = graph.MoveAntToVertexWithLowestCost(0);
            int    oldColorFirstAnt           = vertexWithFirstAnt.Color;
            int    vertexWithAntIDFirstAnt    = vertexWithFirstAnt.ID;
            Vertex vertexWithNewColorFirstAnt = graph.ColorVertexWithBestColor(0);

            Vertex vertexWhichKeepBalanceFirstAnt = graph.KeepBalance(_optionTwoColors.NumberOfVerticesForBalance,
                                                                      vertexWithAntIDFirstAnt, oldColorFirstAnt, vertexWithNewColorFirstAnt.Color);

            graph.UpdateLocalCostFunction(vertexWhichKeepBalanceFirstAnt, vertexWithNewColorFirstAnt);

            // Second ant.
            Vertex vertexWithSecondAnt         = graph.MoveAntToVertexWithLowestCost(1);
            int    oldColorSecondAnt           = vertexWithSecondAnt.Color;
            int    vertexWithAntIDSecondAnt    = vertexWithSecondAnt.ID;
            Vertex vertexWithNewColorSecondAnt = graph.ColorVertexWithBestColor(1);

            Vertex vertexWhichKeepBalanceSecondAnt = graph.KeepBalance(_optionTwoColors.NumberOfVerticesForBalance,
                                                                       vertexWithAntIDSecondAnt, oldColorSecondAnt, vertexWithNewColorSecondAnt.Color);

            graph.UpdateLocalCostFunction(vertexWhichKeepBalanceSecondAnt, vertexWithNewColorSecondAnt);

            Assert.AreEqual(3 / 4D, graph.Vertices[0].LocalCost);
            Assert.AreEqual(1D, graph.Vertices[1].LocalCost);
            Assert.AreEqual(3 / 4D, graph.Vertices[2].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[3].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[4].LocalCost);
            Assert.AreEqual(3 / 4D, graph.Vertices[5].LocalCost);
            Assert.AreEqual(3 / 4D, graph.Vertices[6].LocalCost);
        }
Example #2
0
        public void Graph_KeepBalance_EquallyBalanced()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_dummyFile);

            var randomMock = new StubRandom()
            {
                NextInt32Int32 = (a, b) => 1
            };

            var graph = new MetisGraph(loaderMock.Object, randomMock);

            graph.InitializeGraph();
            graph.InitializeAnts(_optionTwoColors.NumberOfAnts);
            graph.ColorVerticesRandomly(_optionTwoColors.NumberOfPartitions);
            Vertex vertexWithAnt   = graph.MoveAntToAnyAdjacentVertex(0);
            int    oldColor        = vertexWithAnt.Color;
            int    vertexWithAntID = vertexWithAnt.ID;
            Vertex newVertex       = graph.ColorVertexWithBestColor(0);

            graph.KeepBalance(_optionTwoColors.NumberOfVerticesForBalance, vertexWithAntID, oldColor, newVertex.Color);

            var numberOfVerticesWithFirstColor  = graph.Vertices.Count(vertex => vertex.Color == 1);
            var numberOfVerticesWithSecondColor = graph.Vertices.Count(vertex => vertex.Color == 2);

            Assert.AreEqual(4, numberOfVerticesWithFirstColor);
            Assert.AreEqual(3, numberOfVerticesWithSecondColor);
        }
Example #3
0
        public void Graph_UpdateLocalCostFunction_Success()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_dummyFile);

            var randomMock = new StubRandom()
            {
                NextInt32Int32 = (a, b) => 1
            };

            var graph = new MetisGraph(loaderMock.Object, randomMock);

            graph.InitializeGraph();
            graph.InitializeAnts(_optionTwoColors.NumberOfAnts);
            graph.ColorVerticesRandomly(_optionTwoColors.NumberOfPartitions);
            Vertex vertexWithAnt          = graph.MoveAntToAnyAdjacentVertex(0);
            int    oldColor               = vertexWithAnt.Color;
            int    vertexWithAntID        = vertexWithAnt.ID;
            Vertex vertexWithNewColor     = graph.ColorVertexWithBestColor(0);
            Vertex vertexWhichKeepBalance = graph.KeepBalance(_optionTwoColors.NumberOfVerticesForBalance, vertexWithAntID,
                                                              oldColor, vertexWithNewColor.Color);

            graph.UpdateLocalCostFunction(vertexWhichKeepBalance, vertexWithNewColor);

            Assert.AreEqual(0.5, graph.Vertices[0].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[1].LocalCost);
            Assert.AreEqual(0.25, graph.Vertices[2].LocalCost);
            Assert.AreEqual(0D, graph.Vertices[3].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[4].LocalCost);
            Assert.AreEqual(0.75, graph.Vertices[5].LocalCost);
            Assert.AreEqual(0, graph.Vertices[6].LocalCost);
        }
Example #4
0
        public void Graph_KeepBalance_CorrectlyColored()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_dummyFile);

            var randomMock = new StubRandom()
            {
                NextInt32Int32 = (a, b) => 1
            };

            var graph = new MetisGraph(loaderMock.Object, randomMock);

            graph.InitializeGraph();
            graph.InitializeAnts(_optionTwoColors.NumberOfAnts);
            graph.ColorVerticesRandomly(_optionTwoColors.NumberOfPartitions);
            Vertex vertexWithAnt   = graph.MoveAntToAnyAdjacentVertex(0);
            int    oldColor        = vertexWithAnt.Color;
            int    vertexWithAntID = vertexWithAnt.ID;
            Vertex newVertex       = graph.ColorVertexWithBestColor(0);

            graph.KeepBalance(_optionTwoColors.NumberOfVerticesForBalance, vertexWithAntID, oldColor, newVertex.Color);

            Assert.AreEqual(2, graph.Vertices[0].Color);
            Assert.AreEqual(1, graph.Vertices[1].Color);
            Assert.AreEqual(2, graph.Vertices[2].Color);
            Assert.AreEqual(1, graph.Vertices[3].Color);
            Assert.AreEqual(1, graph.Vertices[4].Color);
            Assert.AreEqual(1, graph.Vertices[5].Color);
            Assert.AreEqual(2, graph.Vertices[6].Color);
        }
Example #5
0
        public void Graph_AntOneFullRun()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_dummyFile);

            var randomMock = new StubRandom()
            {
                NextInt32Int32 = (a, b) => 1
            };

            var graph = new MetisGraph(loaderMock.Object, randomMock);

            graph.InitializeGraph();
            graph.InitializeAnts(_optionTwoColors.NumberOfAnts);
            graph.ColorVerticesRandomly(_optionTwoColors.NumberOfPartitions);
            graph.CalculateLocalCostFunction();

            Assert.AreEqual(1 / 2D, graph.Vertices[0].LocalCost);
            Assert.AreEqual(3 / 4D, graph.Vertices[1].LocalCost);
            Assert.AreEqual(1 / 4D, graph.Vertices[2].LocalCost);
            Assert.AreEqual(1 / 2D, graph.Vertices[3].LocalCost);
            Assert.AreEqual(1 / 2D, graph.Vertices[4].LocalCost);
            Assert.AreEqual(1 / 2D, graph.Vertices[5].LocalCost);
            Assert.AreEqual(1 / 2D, graph.Vertices[6].LocalCost);


            var globalCost = graph.GetGlobalCostFunction();

            Assert.AreEqual(7, globalCost);

            Vertex vertexWithAnt = graph.MoveAntToVertexWithLowestCost(0);

            Assert.AreEqual(2, vertexWithAnt.ID);

            int    oldColor           = vertexWithAnt.Color;
            int    vertexWithAntID    = vertexWithAnt.ID;
            Vertex vertexWithNewColor = graph.ColorVertexWithBestColor(0);

            Assert.AreEqual(1, vertexWithNewColor.Color);

            Vertex vertexWhichKeepBalance = graph.KeepBalance(_optionTwoColors.NumberOfVerticesForBalance, vertexWithAntID, oldColor, vertexWithNewColor.Color);

            Assert.AreEqual(2, graph.Vertices[0].Color);
            Assert.AreEqual(1, graph.Vertices[1].Color);
            Assert.AreEqual(1, graph.Vertices[2].Color);
            Assert.AreEqual(1, graph.Vertices[3].Color);
            Assert.AreEqual(2, graph.Vertices[4].Color);
            Assert.AreEqual(1, graph.Vertices[5].Color);
            Assert.AreEqual(2, graph.Vertices[6].Color);

            graph.UpdateLocalCostFunction(vertexWhichKeepBalance, vertexWithNewColor);

            Assert.AreEqual(0.5, graph.Vertices[0].LocalCost);
            Assert.AreEqual(0.75, graph.Vertices[1].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[2].LocalCost);
            Assert.AreEqual(0.75, graph.Vertices[3].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[4].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[5].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[6].LocalCost);

            var globalCostAfterMove = graph.GetGlobalCostFunction();

            Assert.AreEqual(6, globalCostAfterMove);
        }