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