Exemplo n.º 1
0
 /// <summary>
 /// Applies the result to the genome.
 /// </summary>
 /// <param name="genomes"></param>
 /// <param name="result"></param>
 public void Do(List<int> genomes, BestPlacementResult result)
 {
     if (genomes.Count == 0)
     {
         genomes.Add(result.City);
     }
     else if (genomes.Count == result.CityIdx)
     {
         genomes.Add(result.City);
     }
     else
     {
         genomes.Insert(result.CityIdx, result.City);
     }
 }
Exemplo n.º 2
0
        /// <summary>
        /// Searches for the best place to insert the given city.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="calculator"></param>
        /// <param name="genomes"></param>
        /// <param name="city_to_place"></param>
        /// <returns></returns>
        public BestPlacementResult CalculateBestPlacement(
            GeneticProblem problem,
            FitnessCalculator calculator,
            List<int> genomes,
            int city_to_place)
        {
            // initialize the best placement result.
            BestPlacementResult result
                = new BestPlacementResult();
            result.City = city_to_place;

            // initialize the best increase.
            double increase = 0;

            if (genomes.Count > 0)
            {
                // calculate from first.
                double new_weights =
                    problem.Weight(problem.First, city_to_place)
                        + (problem.Weight(city_to_place, genomes[0]));

                // calculate the old weights.
                double old_weight =
                    problem.Weight(problem.First, genomes[0]);

                // calculate the difference to know the increase.
                double new_increase =
                    new_weights - (old_weight);

                // set the new increase.
                increase = new_increase;

                // set the result city.
                result.CityIdx = 0;

                for (int idx = 0; idx < genomes.Count - 1; idx++)
                {
                    // calculate the new weights.
                    new_weights =
                        problem.Weight(genomes[idx], city_to_place)
                            + (problem.Weight(city_to_place, genomes[idx + 1]));

                    // calculate the old weights.
                    old_weight =
                        problem.Weight(genomes[idx], genomes[idx + 1]);

                    // calculate the difference to know the increase.
                    new_increase =
                        new_weights - (old_weight);
                    //if (increase == null || new_increase < increase)
                    if (new_increase < increase)
                    {
                        // set the new increase.
                        increase = new_increase;

                        // set the result city.
                        result.CityIdx = idx + 1;
                    }
                }

                // test to the last.
                // calculate the new weights.
                new_weights =
                    problem.Weight(genomes[genomes.Count - 1], city_to_place)
                        + (problem.Weight(city_to_place, problem.Last));

                // calculate the old weights.
                old_weight =
                    problem.Weight(genomes[genomes.Count - 1], problem.Last);

                // calculate the difference to know the increase.
                new_increase =
                    new_weights - (old_weight);
                //if (increase == null || new_increase < increase)
                if (new_increase < increase)
                {
                    // set the new increase.
                    increase = new_increase;

                    // set the result city.
                    result.CityIdx = genomes.Count;
                }
            }
            else
            {
                // calculate the new weights.
                double new_weights =
                    problem.Weight(problem.First, city_to_place)
                        + (problem.Weight(city_to_place, problem.Last));

                // calculate the old weights.
                double old_weight =
                    problem.Weight(problem.First, problem.Last);

                // calculate the difference to know the increase.
                double new_increase =
                    new_weights - (old_weight);

                // set the new increase.
                increase = new_increase;

                // set the result city.
                result.CityIdx = 0;
            }

            // calculate the fitness.
            result.Increase = increase;

            // return result.
            return result;
        }
Exemplo n.º 3
0
        /// <summary>
        /// Searches for the city that can be placed best with the least increase in cost.
        /// </summary>
        /// <param name="problem"></param>
        /// <param name="calculator"></param>
        /// <param name="genomes"></param>
        /// <param name="cities_to_place"></param>
        /// <returns></returns>
        public BestPlacementResult CalculateBestPlacement(
            GeneticProblem problem,
            FitnessCalculator calculator,
            List<int> genomes,
            List<int> cities_to_place)
        {
            // initialize the best placement result.
            BestPlacementResult result
                = new BestPlacementResult();

            // try and place all cities.
            for (int city_idx = 0; city_idx < cities_to_place.Count; city_idx++)
            {
                // try to place first city.
                int city = cities_to_place[city_idx];

                // place the city and check the result.
                BestPlacementResult current_result =
                    this.CalculateBestPlacement(
                        problem,
                        calculator,
                        genomes,
                        city);

                // check current result
                if (current_result.Increase < result.Increase)
                {
                    result = current_result;
                }
            }

            return result;
        }