public void Graph_UpdateLocalCostFunction_Success()
        {
            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);
            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);
        }
        public void Graph_ChnageVertexColorWithBestColor_Success()
        {
            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();

            graph.MoveAntToVertexWithLowestCost(0);
            graph.ColorVertexWithBestColor(0);

            Assert.AreEqual(1, graph.Vertices[graph.Ants[0]].Color);

            graph.MoveAntToVertexWithLowestCost(1);
            graph.ColorVertexWithBestColor(1);

            Assert.AreEqual(2, graph.Vertices[graph.Ants[1]].Color);
        }
        public void Graph_KeepBalance_EquallyBalanced()
        {
            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);
            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);
        }
Esempio n. 4
0
        static void Main(string[] args)
        {
            XmlConfigurator.Configure();

            var rnd = new Random(Environment.TickCount);

            //var options = new BaseOptions(numberOfIterations: 10, numberOfRegions: 3, alfa: 1, beta: 5, ro: 0.6, delta: 0.1D);
            //var graph = new BasicGraph(BasicEdges, BasicVertexWeights);
            //graph.InitializeGraph();
            //var aspg = new Aspg(options, graph, rnd);
            //var resultBasic = aspg.GetQuality();

            var options    = new BaseOptions(numberOfIterations: 50, numberOfRegions: 2, alfa: 1, beta: 5, ro: 0.6, delta: 0.1D);
            var dataLoader = new FileLoader("Graphs/myciel4.col");
            var graph      = new DimacsGraph(dataLoader);

            graph.InitializeGraph();
            var aspg        = new AspgUnweighted(options, graph, rnd);
            var resultBasic = aspg.GetQuality();

            //var parallelOptimisationOptoins =
            //    new OptionsParallelOptimisation(numberOfIterations: 10, numberOfRegions: 3, alfa: 1, beta: 5, ro: 0.6, delta: 0.1D, numberOfInterSections: 5);
            //var graph = new Graph(AdvancedVertexWeights, AdvancedEdges);
            //graph.InitializeGraph();
            //var aspgParallelOptimisation = new AspgParallelOptimisation(parallelOptimisationOptoins, graph, rnd);
            //var resultParallelOptimisation = aspgParallelOptimisation.GetQuality();

            //var parallelOptimisationWithInheritanceOptoins =
            //    new OptionsParallelOptimisation(numberOfIterations: 10, numberOfRegions: 3, alfa: 1, beta: 5, ro: 0.6, delta: 0.1D, numberOfInterSections: 5);
            //var graph = new Graph(AdvancedVertexWeights, AdvancedEdges);
            //graph.InitializeGraph();
            //var aspgParallelOptimisationWithInheritance = new AspgParallelOptimisationWithInheritance(parallelOptimisationWithInheritanceOptoins,
            //    graph, rnd);
            //var resultParallelOptimisationWithInheritance = aspgParallelOptimisationWithInheritance.GetQuality();
        }
Esempio n. 5
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_BestLocalCostFunction_Success()
        {
            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.Vertices[0].Color = 1;
            graph.Vertices[1].Color = 1;
            graph.Vertices[2].Color = 1;
            graph.Vertices[3].Color = 2;
            graph.Vertices[4].Color = 1;
            graph.Vertices[5].Color = 2;
            graph.Vertices[6].Color = 2;

            graph.CalculateLocalCostFunction();

            Assert.AreEqual(1D, graph.Vertices[0].LocalCost);
            Assert.AreEqual(0.75, graph.Vertices[1].LocalCost);
            Assert.AreEqual(0.75, graph.Vertices[2].LocalCost);
            Assert.AreEqual(0.5, graph.Vertices[3].LocalCost);
            Assert.AreEqual(0.75, graph.Vertices[4].LocalCost);
            Assert.AreEqual(0.75, graph.Vertices[5].LocalCost);
            Assert.AreEqual(1D, graph.Vertices[6].LocalCost);
        }
Esempio n. 7
0
        static void Main(string[] args)
        {
            var rnd = new Random(Environment.TickCount);

            Func <AnalyzeData, DimacsGraph> graphFunc = data =>
            {
                var dataLoader = new FileLoader(data.GraphFilePath);
                var graph      = new DimacsGraph(dataLoader);
                graph.InitializeGraph();

                return(graph);
            };

            var analyzeData = AnalyzeDataAccess.GetAnalyzeData();

            while (analyzeData != null)
            {
                var startDate  = DateTime.UtcNow;
                var inputGraph = graphFunc(analyzeData);

                //var fileWriter = new FileWriter("graph.json");
                //var exportGraph = new GephiFileExport(fileWriter);

                var graphOptions = new BaseOptions(analyzeData.NumberOfIterations, analyzeData.NumberOfPartitions, analyzeData.Alfa,
                                                   analyzeData.Beta, analyzeData.Ro, analyzeData.Delta);

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(graphOptions);

                var aspg        = new AspgUnweighted(graphOptions, inputGraph, rnd);
                var resultBasic = aspg.GetQuality();

                var analyzeResult = new AnalyzeResult
                {
                    AnalyzeID         = analyzeData.ID,
                    BestCost          = resultBasic.BestCost,
                    BestCostIteration = resultBasic.BestCostIteration,
                    Duration          = resultBasic.ElapsedMilliseconds,
                    StartDate         = startDate,
                    EndDate           = DateTime.UtcNow
                };

                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine(analyzeResult);

                AnalyzeDataAccess.SaveAnalyzeResult(analyzeResult);

                analyzeData = AnalyzeDataAccess.GetAnalyzeData();
            }

            //var options = new BaseOptions(numberOfIterations: 10, numberOfRegions: 2, alfa: 1, beta: 5, ro: 0.6, delta: 0.1D);
            //var dataLoader = new FileLoader("C.txt");
            //var graph = new DimacsGraph(dataLoader);
            //graph.InitializeGraph();
            //var aspg = new AspgUnweighted(options, graph, rnd);
            //var resultBasic = aspg.GetQuality();
            //var globlaCost = resultBasic.BestCost;
        }
Esempio n. 8
0
        public void DimacsGraph_Vertices_Initialized()
        {
            var loaderMock = new Mock <IDataLoader>();

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

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

            graph.InitializeGraph();

            Assert.AreEqual(11, graph.Vertices.Length, "Not all vertices are initialized.");
        }
Esempio n. 9
0
        public void DimacsGraph_MaxNumberOfAdjacentVertices_Correct()
        {
            var loaderMock = new Mock <IDataLoader>();

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

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

            graph.InitializeGraph();

            Assert.AreEqual(5, graph.MaxNumberOfAdjacentVertices, "The number of maximum adjacent vertices of one vertex is not calculatef correctly.");
        }
Esempio n. 10
0
        public void DimacsGraph_FirstLineRead_Success()
        {
            var loaderMock = new Mock <IDataLoader>();

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

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

            graph.InitializeGraph();

            Assert.AreEqual(11, graph.Vertices.Length, "The number of vertices weights is not correct.");
            Assert.AreEqual(20, graph.NumberOfEdges, "The number of edges is not correct.");
        }
Esempio n. 11
0
        public void DimacsMyciel4_GetSumOfOptimalityCriterion_34()
        {
            var loaderMock = new Mock <IDataLoader>();

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

            var graph = new DimacsGraph(loaderMock.Object);

            graph.InitializeGraph();

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

            var options = new BaseOptions(numberOfIterations: 100, numberOfRegions: 2, alfa: 1, beta: 5, ro: 0.6, delta: 0.1D);

            var fragment = new UnweightedAntSystemFragmentFake(randomMock, options, graph);

            fragment.ClearTreil();
            fragment.Treil[0].Add(new Vertex(4, 1));
            fragment.Treil[0].Add(new Vertex(8, 1));
            fragment.Treil[0].Add(new Vertex(9, 1));
            fragment.Treil[0].Add(new Vertex(11, 1));
            fragment.Treil[0].Add(new Vertex(12, 1));
            fragment.Treil[0].Add(new Vertex(14, 1));
            fragment.Treil[0].Add(new Vertex(17, 1));
            fragment.Treil[0].Add(new Vertex(18, 1));
            fragment.Treil[0].Add(new Vertex(19, 1));
            fragment.Treil[0].Add(new Vertex(21, 1));
            fragment.Treil[0].Add(new Vertex(22, 1));

            fragment.Treil[1].Add(new Vertex(0, 1));
            fragment.Treil[1].Add(new Vertex(1, 1));
            fragment.Treil[1].Add(new Vertex(2, 1));
            fragment.Treil[1].Add(new Vertex(3, 1));
            fragment.Treil[1].Add(new Vertex(5, 1));
            fragment.Treil[1].Add(new Vertex(6, 1));
            fragment.Treil[1].Add(new Vertex(7, 1));
            fragment.Treil[1].Add(new Vertex(10, 1));
            fragment.Treil[1].Add(new Vertex(13, 1));
            fragment.Treil[1].Add(new Vertex(15, 1));
            fragment.Treil[1].Add(new Vertex(16, 1));
            fragment.Treil[1].Add(new Vertex(20, 1));

            var globalCost = fragment.SumOfOptimalityCriterion;

            Assert.AreEqual(34, globalCost);
        }
        public void Graph_InitializeAntsSeparately_Success()
        {
            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);

            Assert.AreEqual(0, graph.Ants[0]);
            Assert.AreEqual(6, graph.Ants[1]);
        }
        public void MetisGraph_FirstIteration_Success()
        {
            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();

            // 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);
        }
        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);
        }
        public void Graph_GetConnectedVertcies_Success()
        {
            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();

            // [0] 5 1 3 2 2 1
            Assert.AreEqual(4, graph.Vertices[0].ConnectedEdges.ElementAt(0).Key);
            Assert.AreEqual(2, graph.Vertices[0].ConnectedEdges.ElementAt(1).Key);
            Assert.AreEqual(1, graph.Vertices[0].ConnectedEdges.ElementAt(2).Key);
            // [1] 1 1 3 2 4 1
            Assert.AreEqual(0, graph.Vertices[1].ConnectedEdges.ElementAt(0).Key);
            Assert.AreEqual(2, graph.Vertices[1].ConnectedEdges.ElementAt(1).Key);
            Assert.AreEqual(3, graph.Vertices[1].ConnectedEdges.ElementAt(2).Key);
            // [2] 5 3 4 2 2 2 1 2
            Assert.AreEqual(4, graph.Vertices[2].ConnectedEdges.ElementAt(0).Key);
            Assert.AreEqual(3, graph.Vertices[2].ConnectedEdges.ElementAt(1).Key);
            Assert.AreEqual(1, graph.Vertices[2].ConnectedEdges.ElementAt(2).Key);
            Assert.AreEqual(0, graph.Vertices[2].ConnectedEdges.ElementAt(3).Key);
            // [3] 2 1 3 2 6 2 7 5
            Assert.AreEqual(1, graph.Vertices[3].ConnectedEdges.ElementAt(0).Key);
            Assert.AreEqual(2, graph.Vertices[3].ConnectedEdges.ElementAt(1).Key);
            Assert.AreEqual(5, graph.Vertices[3].ConnectedEdges.ElementAt(2).Key);
            Assert.AreEqual(6, graph.Vertices[3].ConnectedEdges.ElementAt(3).Key);
            // [4] 1 1 3 3 6 2
            Assert.AreEqual(0, graph.Vertices[4].ConnectedEdges.ElementAt(0).Key);
            Assert.AreEqual(2, graph.Vertices[4].ConnectedEdges.ElementAt(1).Key);
            Assert.AreEqual(5, graph.Vertices[4].ConnectedEdges.ElementAt(2).Key);
            // [5] 5 2 4 2 7 6
            Assert.AreEqual(4, graph.Vertices[5].ConnectedEdges.ElementAt(0).Key);
            Assert.AreEqual(3, graph.Vertices[5].ConnectedEdges.ElementAt(1).Key);
            Assert.AreEqual(6, graph.Vertices[5].ConnectedEdges.ElementAt(2).Key);
            // [6] 6 6 4 5
            Assert.AreEqual(5, graph.Vertices[6].ConnectedEdges.ElementAt(0).Key);
            Assert.AreEqual(3, graph.Vertices[6].ConnectedEdges.ElementAt(1).Key);
        }
Esempio n. 16
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);
        }
Esempio n. 17
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_RandomColorEachVertex_ThreeColors()
        {
            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(_optionThreeColors.NumberOfPartitions);

            Assert.AreEqual(1, graph.Vertices[0].Color);
            Assert.AreEqual(1, graph.Vertices[1].Color);
            Assert.AreEqual(3, graph.Vertices[2].Color);
            Assert.AreEqual(2, graph.Vertices[3].Color);
            Assert.AreEqual(1, graph.Vertices[4].Color);
            Assert.AreEqual(3, graph.Vertices[5].Color);
            Assert.AreEqual(2, graph.Vertices[6].Color);
        }
Esempio n. 19
0
        // part -t 0 -i "Graphs\DIMACS\myciel3.col" -a 1 -p 2 -c 0.9 -m 0.85 -s 6 -d 100
        // part -t 0 -i "Graphs\DIMACS\le450_15b.col" -a 5 -p 29 -c 0.9 -m 0.85 -s 6 -d 1
        // part -t 1 -i "Graphs\manual.txt" -a 1 -p 2 -c 0.9 -m 0.85 -s 4 -d 100
        static int Main(string[] args)
        {
            XmlConfigurator.Configure();

            Func <PartitionGraphOptions, int> partitionFunc = options =>
            {
                var rnd         = new Random(Environment.TickCount);
                var fileWriter  = new FileWriter("graph.json");
                var exportGraph = new GephiFileExport(fileWriter, Path.GetFileNameWithoutExtension(options.InputGraphFilePath));

                var       fileLoader = new FileLoader(options.InputGraphFilePath);
                BaseGraph graph;
                switch (options.ImportGraphFileType)
                {
                case GraphInputFileType.Dimacs:
                    graph = new DimacsGraph(fileLoader, rnd);
                    break;

                case GraphInputFileType.Metis:
                    graph = new MetisGraph(fileLoader, rnd);
                    break;

                case GraphInputFileType.MetisUnweighted:
                    graph = new MetisUnweightedGraph(fileLoader, rnd);
                    break;

                default:
                    throw new ApplicationException("Graph file format is not suppoted.");
                }

                var graphOptions = new Options(options.NumberOfAnts, options.NumberOfPartitions, options.ColoringProbability,
                                               options.MovingProbability, options.NumberOfVerticesForBalance, options.NumberOfIterations);

                ResultData resultData = Algorithm.Run(graph, graphOptions, rnd, exportGraph);
                return(resultData.BestCost);
            };

            var result = Parser.Default.ParseArguments <PartitionGraphOptions>(args);

            var exitCode = result.MapResult(options =>
            {
                if (options.Verbose)
                {
                    Console.WriteLine("Write additional information.");
                }
                else
                {
                    Console.WriteLine("Processing...");

                    var stopwatch = new Stopwatch();
                    stopwatch.Start();

                    var bestCost = partitionFunc(options);

                    stopwatch.Stop();
                    Log.Warn($"Algoritham run time: {stopwatch.ElapsedMilliseconds}");
                    Log.Warn($"Best cost: {bestCost}");

                    Console.WriteLine($"{args[0]} | {bestCost}");
                }
                return(0);
            }, errors => 1);

            return(exitCode);
        }
        static void Main()
        {
            var rnd = new Random(Environment.TickCount);

            Func <AnalyzeData, BaseGraph> graphFunc = data =>
            {
                var fileLoader = new FileLoader(data.GraphFilePath);

                BaseGraph graph;
                switch (data.GraphFileType)
                {
                case GraphFileType.Dimacs:
                    graph = new DimacsGraph(fileLoader, rnd);
                    break;

                case GraphFileType.Metis:
                    graph = new DimacsGraph(fileLoader, rnd);
                    break;

                default:
                    throw new ApplicationException("Graph file format is not suppoted.");
                }
                return(graph);
            };

            var analyzeData = AnalyzeDataAccess.GetAnalyzeData();

            while (analyzeData != null)
            {
                var startDate  = DateTime.UtcNow;
                var inputGraph = graphFunc(analyzeData);

                var fileWriter = new FileWriter("graph.json");
                //var exportGraph = new GephiFileExport(fileWriter);

                var graphOptions = new Options(analyzeData.NumberOfAnts, analyzeData.NumberOfPartitions, analyzeData.ColoringProbability,
                                               analyzeData.MovingProbability, analyzeData.NumberOfVerticesForBalance, analyzeData.NumberOfIterations);

                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(graphOptions);

                ResultData resultData = Algorithm.Run(inputGraph, graphOptions, rnd, null);

                var analyzeResult = new AnalyzeResult
                {
                    AnalyzeID         = analyzeData.ID,
                    BestCost          = resultData.BestCost,
                    BestCostIteration = resultData.BestCostIteration,
                    Duration          = resultData.ElapsedMilliseconds,
                    StartDate         = startDate,
                    EndDate           = DateTime.UtcNow
                };

                Console.ForegroundColor = ConsoleColor.DarkGreen;
                Console.WriteLine(analyzeResult);

                AnalyzeDataAccess.SaveAnalyzeResult(analyzeResult);

                analyzeData = AnalyzeDataAccess.GetAnalyzeData();
            }
        }
        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);
        }