Example #1
0
            public Chromosome(City[] cities)
            {
                bool[] taken = new bool[cities.Length];
                CityList = new int[cities.Length];
                cost = 0.0;
                for (int i = 0; i < CityList.Length; i++) taken[i] = false;
                for (int i = 0; i < CityList.Length - 1; i++)
                {
                    int icandidate;
                    do
                    {
                        icandidate = (int)(randObj.NextDouble() * (double)CityList.Length);
                    } while (taken[icandidate]);

                    CityList[i] = icandidate;

                    taken[icandidate] = true;

                    if (i == CityList.Length - 2)
                    {
                        icandidate = 0;
                        while (taken[icandidate]) icandidate++;
                        CityList[i + 1] = icandidate;
                    }
                }

                calculateCost(cities);
                crossoverPoint = 1;
            }
Example #2
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;

        }
Example #3
0
            // fitness calculation
            //void calculateCost(myCity cities)
            public void calculateCost(City[] cities)
            {
                cost = 0;

                for (int i = 0; i < CityList.Length - 1; i++)
                {
                    double dist = cities[CityList[i]].proximity(cities[CityList[i + 1]]);
                    cost += dist;
                }
            }
Example #4
0
 public void PrintCity(int i, City[] cities)
 {
     System.Console.WriteLine("City " + i.ToString() + ": (" + cities[CityList[i]].getx().ToString() + ", " + cities[CityList[i]].gety().ToString() + ")");
 }
Example #5
0
 // Returns the distance from the city to another city.
 public int proximity(City cother)
 {
     return proximity(cother.getx(), cother.gety());
 }