Exemple #1
0
        public int Fitness(UndirectedGraph graph)
        {
            var usedColors = new List <Color>();

            for (int i = 0; i < graph.GetNumberOfVertices(); i++)
            {
                if (!usedColors.Contains((Color)graph.GetColor(i)))
                {
                    usedColors.Add((Color)graph.GetColor(i));
                }
            }
            return(usedColors.Count);
        }
Exemple #2
0
        public bool IsBadEdge(int i, UndirectedGraph graph)
        {
            var color = graph.GetColor(i);

            if (graph.GetNeighbourColors(i).Contains(color))
            {
                return(true);
            }
            return(false);
        }
Exemple #3
0
 public bool ContainsBadEdge(UndirectedGraph graph)
 {
     for (int i = 0; i < graph.GetNumberOfVertices(); i++)
     {
         var color = graph.GetColor(i);
         if (graph.GetNeighbourColors(i).Contains(color))
         {
             return(true);
         }
     }
     return(false);
 }
        public Dictionary <int, Color> GenerateGreedySolutionDict(UndirectedGraph graph)
        {
            var greedy = new GraphColoringGreedy(graph);

            greedy.Color();
            var res = new Dictionary <int, Color>();

            for (int i = 0; i < graph.GetNumberOfVertices(); i++)
            {
                res.Add(i, (Color)graph.GetColor(i));
            }

            return(res);
        }
Exemple #5
0
        public UndirectedGraph Crossover(UndirectedGraph parent1, UndirectedGraph parent2)
        {
            var random     = new Random();
            var crosspoint = random.Next(0, parent1.GetNumberOfVertices() - 1);
            var graph      = new UndirectedGraph(parent1.GetNumberOfVertices());

            for (int i = 0; i < parent1.GetNumberOfVertices(); i++)
            {
                if (i < crosspoint)
                {
                    graph.SetColor(i, parent1.GetColor(i).Value);
                }
                else
                {
                    graph.SetColor(i, parent2.GetColor(i).Value);
                }
            }
            return(graph);
        }
Exemple #6
0
        public void Mutate(UndirectedGraph graph)
        {
            var random = new Random();

            for (int i = 0; i < graph.GetNumberOfVertices(); i++)
            {
                var neightboursColors = graph.GetNeighbourColors(i);
                var color             = graph.GetColor(i);
                if (neightboursColors.Contains(color))
                {
                    var colors = Enum.GetValues(typeof(Color)).Cast <Color>().ToList();
                    foreach (var neighbourColor in neightboursColors)
                    {
                        colors.Remove((Color)neighbourColor);
                    }

                    colors.OrderBy(x => random.Next(0, colors.Count - 1));
                    graph.SetColor(i, colors.FirstOrDefault());
                }
            }
        }