Пример #1
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);
        }
Пример #2
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());
                }
            }
        }
Пример #3
0
        public void ColorRandomly(UndirectedGraph graph)
        {
            var random = new Random();

            for (int i = 0; i < graph.GetNumberOfVertices(); i++)
            {
                var neightboursColors = graph.GetNeighbourColors(i);
                while (true)
                {
                    var colors = Enum.GetValues(typeof(Color)).Cast <Color>().ToList();
                    foreach (var neighbourColor in neightboursColors)
                    {
                        colors.Remove((Color)neighbourColor);
                    }
                    if (colors.Count == 0)
                    {
                        throw new Exception("Not enough colors");
                    }
                    colors.OrderBy(x => random.Next(0, colors.Count - 1));
                    graph.SetColor(i, colors.FirstOrDefault());
                    break;
                }
            }
        }
Пример #4
0
 public GraphColoringGreedy(UndirectedGraph graph)
 {
     _graph  = graph;
     _colors = Enum.GetValues(typeof(Color)).Cast <Color>();
 }