コード例 #1
0
        public override Phenotype Develop(Genotype genotype)
        {
            IntGenotype   intGenotype   = (IntGenotype)genotype;
            TourPhenotype tourPhenotype = new TourPhenotype();

            int max = intGenotype.Max;

            int[] tour = new int[max];

            tourPhenotype.Tour = tour;

            // Fill in possible cities
            List <int> possibleCities = new List <int>(max);

            for (int i = 0; i < max; i++)
            {
                possibleCities.Add(i);
            }


            // Choose cities based on genotype
            int c;

            for (int i = 0; i < max; i++)
            {
                //c = intGenotype.List[i] % possibleCities.Count;
                //tour[i] = possibleCities[c];
                //possibleCities.RemoveAt(c);
                tour[i] = intGenotype.List[i];
            }

            return(tourPhenotype);
        }
コード例 #2
0
ファイル: TourEvaluator.cs プロジェクト: oyvind-kjerland/MTSP
        public override double Evaluate(Individual individual)
        {
            TourPhenotype phenotype = (TourPhenotype)individual.Phenotype;

            // Fitness 2 is cost
            int totalCost = 0;

            // Fitness 1 is distance
            int totalDistance = 0;

            int[] tour = phenotype.Tour;

            // Want to minimize both
            for (int i = 0; i < tour.Length - 1; i++)
            {
                totalCost     += cityData.CostArr[tour[i], tour[i + 1]];
                totalDistance += cityData.DistanceArr[tour[i], tour[i + 1]];
            }
            // Add the last edge
            totalCost     += cityData.CostArr[tour.Last(), tour[0]];
            totalDistance += cityData.DistanceArr[tour.Last(), tour[0]];

            individual.Cost     = totalCost;
            individual.Distance = totalDistance;

            return(0);
        }