Пример #1
0
        public void GraphMetisTest_CalculateGlobalCostFunction_Success()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_metisTest);
            var randomMock = new Mock <Random>();

            randomMock.Setup(s => s.Next(It.IsAny <int>(), It.IsAny <int>())).Returns(1);

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

            graph.InitializeGraph();

            graph.Vertices[0].Color = 2;
            graph.Vertices[1].Color = 2;
            graph.Vertices[2].Color = 2;
            graph.Vertices[3].Color = 1;
            graph.Vertices[4].Color = 2;
            graph.Vertices[5].Color = 1;
            graph.Vertices[6].Color = 1;

            var globalCost = graph.GetGlobalCostFunction();

            Assert.AreEqual(3, globalCost);
        }
        public void Graph_GlobalCostFunction_OneColor()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_dummyFile);
            var randomMock = new Mock <Random>();

            randomMock.Setup(s => s.Next(It.IsAny <int>(), It.IsAny <int>())).Returns(1);

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

            graph.InitializeGraph();
            graph.ColorVerticesRandomly(_optionOneColors.NumberOfPartitions);
            var globalCost = graph.GetGlobalCostFunction();

            Assert.AreEqual(0, globalCost);
        }
Пример #3
0
        public void GraphQueen5_5_CalculateGlobalCostFunction_Success()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_queen55Metis);
            var randomMock = new Mock <Random>();

            randomMock.Setup(s => s.Next(It.IsAny <int>(), It.IsAny <int>())).Returns(1);

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

            graph.InitializeGraph();

            graph.Vertices[0].Color  = 2;
            graph.Vertices[1].Color  = 2;
            graph.Vertices[2].Color  = 1;
            graph.Vertices[3].Color  = 1;
            graph.Vertices[4].Color  = 1;
            graph.Vertices[5].Color  = 2;
            graph.Vertices[6].Color  = 2;
            graph.Vertices[7].Color  = 1;
            graph.Vertices[8].Color  = 2;
            graph.Vertices[9].Color  = 2;
            graph.Vertices[10].Color = 1;
            graph.Vertices[11].Color = 1;
            graph.Vertices[12].Color = 2;
            graph.Vertices[13].Color = 2;
            graph.Vertices[14].Color = 2;
            graph.Vertices[15].Color = 2;
            graph.Vertices[16].Color = 1;
            graph.Vertices[17].Color = 1;
            graph.Vertices[18].Color = 2;
            graph.Vertices[19].Color = 2;
            graph.Vertices[20].Color = 2;
            graph.Vertices[21].Color = 1;
            graph.Vertices[22].Color = 1;
            graph.Vertices[23].Color = 1;
            graph.Vertices[24].Color = 1;

            var globalCost = graph.GetGlobalCostFunction();

            Assert.AreEqual(84, globalCost);
        }
Пример #4
0
        public void GraphMyciel4_CalculateGlobalCostFunction_28()
        {
            var loaderMock = new Mock <IDataLoader>();

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

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

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

            graph.InitializeGraph();

            graph.Vertices[0].Color  = 1;
            graph.Vertices[1].Color  = 1;
            graph.Vertices[2].Color  = 1;
            graph.Vertices[3].Color  = 0;
            graph.Vertices[4].Color  = 1;
            graph.Vertices[5].Color  = 0;
            graph.Vertices[6].Color  = 0;
            graph.Vertices[7].Color  = 0;
            graph.Vertices[8].Color  = 0;
            graph.Vertices[9].Color  = 0;
            graph.Vertices[10].Color = 0;
            graph.Vertices[11].Color = 0;
            graph.Vertices[12].Color = 1;
            graph.Vertices[13].Color = 1;
            graph.Vertices[14].Color = 1;
            graph.Vertices[15].Color = 0;
            graph.Vertices[16].Color = 0;
            graph.Vertices[17].Color = 1;
            graph.Vertices[18].Color = 1;
            graph.Vertices[19].Color = 1;
            graph.Vertices[20].Color = 0;
            graph.Vertices[21].Color = 0;
            graph.Vertices[22].Color = 1;

            var globalCost = graph.GetGlobalCostFunction();

            Assert.AreEqual(28, globalCost);
        }
        public void Graph_AntOneFullRun()
        {
            var loaderMock = new Mock <IDataLoader>();

            loaderMock.Setup(m => m.LoadData()).Returns(_dummyFile);
            var randomMock = new Mock <Random>();

            randomMock.Setup(s => s.Next(It.IsAny <int>(), It.IsAny <int>())).Returns(1);

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

            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);
        }