public void GraphToCSP(GraphLib.Definitions.Graph graph) { int verticesCount = graph.VerticesCount; int edgesCount = 0; for (int i = 0; i < verticesCount; i++) { edgesCount += graph.GetNeighbors(i).Count; } edgesCount /= 2; CSP.CspInstance cspInstance3 = Converter.GraphToCSP(graph, 3); CSP.CspInstance cspInstance4 = Converter.GraphToCSP(graph, 4); Assert.Equal(verticesCount, cspInstance3.Variables.Count); Assert.Equal(3 * edgesCount, cspInstance3.Restrictions.Count); Assert.Equal(verticesCount, cspInstance4.Variables.Count); Assert.Equal(4 * edgesCount, cspInstance4.Restrictions.Count); }
public static IEnumerable <object[]> GetDataGraphToCSP() { var data = new List <object[]>(); Random r = new(123); for (int i = 0; i < 50; i++) { var graph = new GraphLib.Definitions.Graph(r.Next(1, 100)); for (int j = 0; j < graph.VerticesCount * (graph.VerticesCount - 1) / 2; j++) { var from = r.Next(0, graph.VerticesCount); var to = r.Next(0, graph.VerticesCount); if (from != to && !graph.ContainsEdge(from, to)) { graph.AddEdge(from, to); } } data.Add(new object[] { graph }); } return(data); }
public static CspInstance GraphToCSP(GraphLib.Definitions.Graph graph, int numberOfColors = 3) { List <Variable> addedVariables = new(); CspInstance cspInstance = new(); for (int i = 0; i < graph.VerticesCount; i++) { addedVariables.Add(new Variable(numberOfColors)); addedVariables[i].Id = i; cspInstance.AddVariable(addedVariables[i]); foreach (var neighbour in graph.GetNeighbors(i)) { if (neighbour < i) { for (int j = 0; j < numberOfColors; j++) { cspInstance.AddRestriction(new Pair(addedVariables[i], addedVariables[i].AvalibleColors[j]), new Pair(addedVariables[neighbour], addedVariables[neighbour].AvalibleColors[j])); } } } } return(cspInstance); }