Esempio n. 1
0
            public int mate(Chromosome father, Chromosome offspring1, Chromosome offspring2)
            {
                int crossoverPostion1 = (int)((randObj.NextDouble()) * (double)(CityList.Length - crossoverPoint));
                int crossoverPostion2 = crossoverPostion1 + crossoverPoint;
                int[] offset1 = new int[CityList.Length];
                int[] offset2 = new int[CityList.Length];
                bool[] taken1 = new bool[CityList.Length];
                bool[] taken2 = new bool[CityList.Length];
                for (int i = 0; i < CityList.Length; i++)
                {
                    taken1[i] = false;
                    taken2[i] = false;
                }

                for (int i = 0; i < CityList.Length; i++)
                {
                    if (i < crossoverPostion1 || i >= crossoverPostion2)
                    {
                        offset1[i] = -1;
                        offset2[i] = -1;
                    }
                    else
                    {
                        int imother = CityList[i];
                        int ifather = father.getCity(i);
                        offset1[i] = ifather;
                        offset2[i] = imother;
                        taken1[ifather] = true;
                        taken2[imother] = true;
                    }
                }

                for (int i = 0; i < crossoverPostion1; i++)
                {
                    if (offset1[i] == -1)
                    {
                        for (int j = 0; j < CityList.Length; j++)
                        {
                            int imother = CityList[j];
                            if (!taken1[imother])
                            {
                                offset1[i] = imother;
                                taken1[imother] = true;
                                break;
                            }
                        }
                    }

                    if (offset2[i] == -1)
                    {
                        for (int j = 0; j < CityList.Length; j++)
                        {
                            int ifather = father.getCity(j);
                            if (!taken2[ifather])
                            {
                                offset2[i] = ifather;
                                taken2[ifather] = true;
                                break;
                            }
                        }
                    }
                }

                for (int i = CityList.Length - 1; i >= crossoverPostion2; i--)
                {
                    if (offset1[i] == -1)
                    {
                        for (int j = CityList.Length - 1; j >= 0; j--)
                        {
                            int imother = CityList[j];
                            if (!taken1[imother])
                            {
                                offset1[i] = imother;
                                taken1[imother] = true;
                                break;
                            }
                        }
                    }

                    if (offset2[i] == -1)
                    {
                        for (int j = CityList.Length - 1; j >= 0; j--)
                        {
                            int ifather = father.getCity(j);
                            if (!taken2[ifather])
                            {
                                offset2[i] = ifather;
                                taken2[ifather] = true;
                                break;
                            }
                        }
                    }
                }

                offspring1.assignCities(offset1);
                offspring2.assignCities(offset2);

                int mutate = 0;
                int swapPoint1 = 0;
                int swapPoint2 = 0;

                if (randObj.NextDouble() < mutationPercent)
                {
                    swapPoint1 = (int)(randObj.NextDouble() * (double)(CityList.Length));
                    swapPoint2 = (int)(randObj.NextDouble() * (double)CityList.Length);
                    int i = offset1[swapPoint1];
                    offset1[swapPoint1] = offset1[swapPoint2];
                    offset1[swapPoint2] = i;
                    mutate++;
                }

                if (randObj.NextDouble() < mutationPercent)
                {
                    swapPoint1 = (int)(randObj.NextDouble() * (double)(CityList.Length));
                    swapPoint2 = (int)(randObj.NextDouble() * (double)CityList.Length);

                    int i = offset2[swapPoint1];

                    offset2[swapPoint1] = offset2[swapPoint2];
                    offset2[swapPoint2] = i;

                    mutate++;
                }

                return mutate;

            }