Esempio n. 1
0
        public void Initialization()
        {
            Random randObj = new Random();
            thisCost = 500.0;
            oldCost = 0.0;
            dcost = 500.0;
            countSame = 0;
          

            try
            {
                cityCount = 10;
                populationSize = 2000;
                mutationPercent = 1.5;
            }
            catch (Exception e)
            {
                cityCount = 100;
            }

            matingPopulationSize = populationSize / 2;
            favoredPopulationSize = matingPopulationSize / 2;
            cutLength = cityCount / 5;

            

            // create a random list of cities
            Cities = new City[cityCount];

            for (int i = 0; i < cityCount; i++)
            {
                Cities[i] = new City(
                (int)(randObj.NextDouble() * 30), (int)(randObj.NextDouble() * 15));
                if (CityCreated != null)
                    CityCreated(this, new NewCityEventArgs() { NewCity = Cities[i] });
            }

            // create the initial chromosomes
            chromosomes = new Chromosome[populationSize];

            for (int i = 0; i < populationSize; i++)
            {
                chromosomes[i] = new Chromosome(Cities);
                chromosomes[i].assignCut(cutLength);
                chromosomes[i].assignMutation(mutationPercent);
            }

            Chromosome.sortChromosomes(chromosomes, populationSize);
       
            if (GenerationCreated != null)
                    GenerationCreated(this, new GenerationCreatedEventArgs() { Generation = chromosomes, GenerationNumber = generation });

            started = true;

            generation = 0;

        }
Esempio n. 2
0
            // chromosomes -- an array of chromosomes which is sorted
            // num -- the number of the chromosome list
            public static void sortChromosomes(Chromosome[] chromosomes, int num)
            {
                bool swapped = true;
                Chromosome dummy;

                while (swapped)
                {
                    swapped = false;

                    for (int i = 0; i < num - 1; i++)
                    {
                        if (chromosomes[i].getCost() > chromosomes[i + 1].getCost())
                        {
                            dummy = chromosomes[i];
                            chromosomes[i] = chromosomes[i + 1];
                            chromosomes[i + 1] = dummy;
                            swapped = true;
                        }
                    }
                }

                

            }
Esempio n. 3
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;

            }