Пример #1
0
        public static Genome CreateFrom(DynamicAsymmetricMultiRoute route)
        {
            Genome     genome    = new Genome();
            List <int> customers = new List <int>();
            List <int> sizes     = new List <int>();

            for (int idx = 0; idx < route.Count; idx++)
            {
                int customer_idx = 0;
                foreach (int customer in route.Route(idx))
                {
                    customer_idx++;
                    customers.Add(customer);
                }

                sizes.Add(customer_idx);
            }

            genome.Customers = customers.ToArray();
            genome.Sizes     = sizes.ToArray();

            return(genome);
        }
Пример #2
0
        /// <summary>
        /// Generates individuals based on a random first customer for each route.
        /// </summary>
        /// <param name="solver"></param>
        /// <returns></returns>
        public Individual <List <Genome>, Problem, Fitness> Generate(
            Solver <List <Genome>, Problem, Fitness> solver)
        {
            Problem problem = solver.Problem;

            DynamicAsymmetricMultiRoute multi_route = new DynamicAsymmetricMultiRoute(problem.Size, true);

            // create the problem for the genetic algorithm.
            List <int> customers = new List <int>();

            for (int customer = problem.Depots.Count; customer < problem.Size; customer++)
            {
                customers.Add(customer);
            }
            CheapestInsertionHelper helper = new CheapestInsertionHelper();

            List <double> weights = new List <double>();

            for (int i = 0; i < problem.Depots.Count; i++)
            {
                multi_route.Add(i);
                weights.Add(0);
            }
            int k = OsmSharp.Math.Random.StaticRandomGenerator.Get().Generate(problem.Depots.Count);

            // keep placing customer until none are left.
            while (customers.Count > 0)
            {
                k = (k + 1) % problem.Depots.Count;

                // use best placement to generate a route.
                IRoute current_route = multi_route.Route(k);


                //Console.WriteLine("Starting new route with {0}", customer);
                while (customers.Count > 0)
                {
                    // calculate the best placement.
                    CheapestInsertionResult result = CheapestInsertionHelper.CalculateBestPlacement(problem, current_route, customers);

                    if (result.CustomerAfter == -1 || result.CustomerBefore == -1)
                    {
                        customers.Remove(result.Customer);
                        continue;
                    }
                    // calculate the new weight.
                    customers.Remove(result.Customer);
                    //current_route.InsertAfterAndRemove(result.CustomerBefore, result.Customer, result.CustomerAfter);
                    current_route.InsertAfter(result.CustomerBefore, result.Customer);
                    weights[k] += result.Increase + 15 * 60;

                    if (weights[k] == weights.Max())
                    {
                        break;
                    }
                }
            }

            for (int i = 0; i < problem.Depots.Count; i++)
            {
                multi_route.RemoveCustomer(i);
            }


            List <Genome> genomes = new List <Genome>();

            genomes.Add(Genome.CreateFrom(multi_route));
            Individual <List <Genome>, Problem, Fitness> individual = new Individual <List <Genome>, Problem, Fitness>(genomes);

            //individual.Initialize();
            return(individual);
        }