Example #1
0
        public MAP_COLOR_ARRAY Reproduce(MAP_COLOR_ARRAY[] Population, int x, int y)//CrossOver Operation in Genetic Algorithm
        {
            MAP_COLOR_ARRAY Result;

            //Select Real X and Y according to their name x and y
            MAP_COLOR_ARRAY X_MAP_COLOR = new MAP_COLOR_ARRAY(Node_Quantity);
            MAP_COLOR_ARRAY Y_MAP_COLOR = new MAP_COLOR_ARRAY(Node_Quantity);

            for (int i = 0; i < Node_Quantity; i++)
            {
                X_MAP_COLOR.Nodes_Colors[i] = Population[x].Nodes_Colors[i];
                Y_MAP_COLOR.Nodes_Colors[i] = Population[y].Nodes_Colors[i];
            }


            int c = f.RandomRange(0, Node_Quantity - 1);

            //CrossOver compound two Population
            for (int i = c; i < Node_Quantity; i++)
            {
                X_MAP_COLOR.Nodes_Colors[i] = Y_MAP_COLOR.Nodes_Colors[i];
            }
            Result = X_MAP_COLOR;
            return(Result);
        }
Example #2
0
        public MAP_COLOR_ARRAY Mutation(MAP_COLOR_ARRAY Child) //Mutation Operation in Genetic Algorithm
        {
            MAP_COLOR_ARRAY Result;
            int             Chance = f.RandomRange(0, 20);

            if (Chance > 1)
            {
                return(Child);
            }

            int Max_Change = f.RandomRange(0, Node_Quantity - 1);

            for (int i = 0; i < Max_Change; i++)
            {
                int Rand_Num       = f.RandomRange(0, Node_Quantity - 1);
                int Rand_Color_Num = f.RandomRange(0, Color_Num - 1);

                Child.Nodes_Colors[Rand_Num] = Rand_Color_Num;
            }
            Result = Child;
            return(Result);
        }
Example #3
0
        public void G_Algorithm(List <Edge> _edges, int _node_Quantitym, int _color_Num, int _population_Size)
        {
            Population_Size = _population_Size;
            Node_Quantity   = _node_Quantitym;
            Color_Num       = _color_Num;
            MAP             = new Graph(Node_Quantity);

            _result    = new int[Node_Quantity];
            _result[0] = 0;

            for (int u = 1; u < Node_Quantity; u++)
            {
                _result[u] = -1;
            }

            for (int i = Node_Quantity - 1; i >= 0; i--)
            {
                MAP.Nodes_Array[i].color = 0;
                MAP.Nodes_Array[i].name  = i;
            }

            foreach (Edge _edge in _edges)
            {
                int v = _edge.FirstNode.Mark;
                int w = _edge.SecondNode.Mark;

                MAP.Adjacency[v].Add(MAP.Nodes_Array[w]);
                MAP.Adjacency[w].Add(MAP.Nodes_Array[v]);
            }

            Random rand = new Random();

            bool Fitness_Func_Result = false;

            Population_Fitness_Value = new int[Population_Size];

            MAP_COLOR_ARRAY[] Initial_Population = new MAP_COLOR_ARRAY[Population_Size];
            for (int i = 0; i < Population_Size; ++i)
            {
                Initial_Population[i] = new MAP_COLOR_ARRAY(Node_Quantity);
            }

            //Initial Random Population Making Process
            for (int i = 0; i < Population_Size; i++)
            {
                for (int j = 0; j < Node_Quantity; j++)
                {
                    int temp = RandomNumber(0, Color_Num - 1); //rand() % Color_Num;
                    Initial_Population[i].Nodes_Colors[j] = temp;
                }
            }

            do
            {
                MAP_COLOR_ARRAY[] New_Population = new MAP_COLOR_ARRAY[Population_Size];
                for (int i = 0; i < Population_Size; ++i)
                {
                    New_Population[i] = new MAP_COLOR_ARRAY(Node_Quantity);
                }
                repeat++;

                for (int i = 0; i < Population_Size; i++)
                {
                    int x = Random_Selection(); //Using global variable Population_Fitness_Value
                    int y = Random_Selection();


                    MAP_COLOR_ARRAY Child = Reproduce(Initial_Population, x, y);
                    Child = Mutation(Child);

                    //Copy Array
                    for (int j = 0; j < Node_Quantity; j++)
                    {
                        New_Population[i].Nodes_Colors[j] = Child.Nodes_Colors[j];
                    }
                }
                //Copy Arrays
                for (int i = 0; i < Population_Size; i++)
                {
                    for (int j = 0; j < Node_Quantity; j++)
                    {
                        Initial_Population[i].Nodes_Colors[j] = New_Population[i].Nodes_Colors[j];
                    }
                }
                Pop_Fitness_Evaluator(Initial_Population);      //Fitness & Goodness evaluator
                Fitness_Func_Result = Fit_Enough();
            } while (repeat < 10000 && !Fitness_Func_Result);

            int Best_Individual = -1;

            for (int i = 0; i < Population_Size; i++)
            {
                if (Population_Fitness_Value[i] == 0)
                {
                    Best_Individual = i;
                }
            }

            if (Best_Individual == -1)
            {
                //Answer Not found :C
                if (!Fitness_Func_Result)
                {
                    //если ответ не найден, наверное стоит еще раз запустить процедуру
                }
            }
            else
            {
                //Print Best Population-Test
                for (int i = 0; i < Node_Quantity; i++)
                {
                    _result[i] = Initial_Population[Best_Individual].Nodes_Colors[i];
                    _iteration = repeat;
                }
            }
        }