Exemple #1
0
        public static void Main()
        {
            Chromosome chromosome = GetFromTxt("chromosome.txt");
            Customer   customer   = new Customer(101010, 50, 50, 50)
            {
                IsNull = false
            };

            Insert(customer, chromosome);
            int ii = 0;
            int jj = 0;

            for (int i = 0; i < chromosome.Count; i++)
            {
                Depot depot = chromosome[i];
                for (int j = 0; j < depot.Count; j++)
                {
                    if (depot[j].Id == 101010)
                    {
                        ii = i;
                        jj = j;
                    }
                }
            }
            Console.WriteLine(ii.ToString() + " " + jj.ToString());
        }
Exemple #2
0
        /// <summary>
        /// For building initial population, we build random chromosomes.
        /// </summary>
        /// <param name="depot">Depot</param>
        public static void RandomList(this Depot depot)
        {
            var random = new Random();

            for (int i = 0; i < depot.Count; i++)
            {
                int      ran  = random.Next(i, depot.Count);
                Customer temp = depot[i];
                depot[i]   = depot[ran];
                depot[ran] = temp;
            }
        }
Exemple #3
0
        /// <summary>
        /// For Reversal mutation we need to get a path to cut it by two points , then replace reversed. (Part of intra-depot mutation)
        /// </summary>
        /// <param name="depot">The depot we want to find random route</param>
        /// <returns>A route for reverse</returns>
        private static List <Customer> MutationRoute(Depot depot)
        {
            List <Customer> route = new List <Customer>();

            Random   randomGenerator      = new Random();
            int      customerInRouteIndex = randomGenerator.Next(0, depot.DepotCustomers.Count);
            Customer customer             = depot[customerInRouteIndex];

            while (customer.IsNull)
            {
                customerInRouteIndex = randomGenerator.Next(0, depot.DepotCustomers.Count);
                customer             = depot[customerInRouteIndex];
            }

            int routeStartIndex = customerInRouteIndex;

            for (int i = customerInRouteIndex - 1; i >= 0; i--)
            {
                Customer temp = depot[i];
                if (!temp.IsNull)
                {
                    routeStartIndex = i;
                }
                else
                {
                    break;
                }
            }

            int routeEndIndex = customerInRouteIndex;

            for (int i = customerInRouteIndex + 1; i < depot.DepotCustomers.Count; i++)
            {
                Customer temp = depot[i];
                if (!temp.IsNull)
                {
                    routeEndIndex = i;
                }
                else
                {
                    break;
                }
            }

            route = depot.DepotCustomers.GetRange(routeStartIndex, routeEndIndex);

            mutationRouteStartIndex = routeStartIndex;
            mutationRouteEndIndex   = routeEndIndex;

            return(route);
        }
Exemple #4
0
        // Intra-depot mutation methods


        /// <summary>
        /// Reversal mutation mutate a chromosome with reversing a cut in chromosome in place.
        /// </summary>
        /// <param name="chromosome">Chromosome to Mutate</param>
        /// <returns></returns>
        public static void ReversalMutation(this Chromosome chromosome)
        {
            Random          randomGenerator = new Random();
            List <Customer> mutatedRoute    = new List <Customer>();

            int   randomDepot = randomGenerator.Next(0, chromosome.ChromosomeList.Count);
            Depot depot       = new Depot();

            depot = chromosome[randomDepot];

            List <Customer> route = new List <Customer>();

            route        = MutationRoute(depot);
            mutatedRoute = route;

            int firstCut  = randomGenerator.Next(0, route.Count);
            int secondCut = randomGenerator.Next(0, route.Count);

            while (secondCut < firstCut)
            {
                secondCut = randomGenerator.Next(0, route.Count);
            }


            if (firstCut == secondCut)
            {
            }
            else
            {
                for (int i = secondCut, k = firstCut; i >= firstCut; i--, k++)
                {
                    mutatedRoute[k] = route[i];
                }
            }

            for (int i = mutationRouteStartIndex; i < mutationRouteEndIndex; i++)
            {
                chromosome[randomDepot][i] = mutatedRoute[i];
            }
        }
Exemple #5
0
        private static Chromosome GetFromTxt(string path)
        {
            string[]     lines          = System.IO.File.ReadAllLines(@path);
            string       chromosomeInfo = lines[0];
            int          capacity       = int.Parse(chromosomeInfo.Split(' ')[2]);
            List <Depot> depot          = new List <Depot>();
            int          k = -1;

            for (int i = 1; i < lines.Length; i++)
            {
                string[] line = lines[i].Split(' ');

                if (line.Length == 3)  // depot
                {
                    k += 1;
                    Depot temp = new Depot(0, int.Parse(line[1]), int.Parse(line[2]), capacity);
                    depot.Add(temp);
                    i += 1;
                }
                string[] args     = lines[i].Split(' ');
                int      id       = int.Parse(args[0]);
                int      x        = int.Parse(args[1]);
                int      y        = int.Parse(args[2]);
                int      cost     = int.Parse(args[3]);
                bool     Null     = bool.Parse(args[4]);
                Customer customer = new Customer(id, x, y, cost)
                {
                    IsNull = Null
                };
                depot[k].Add(customer);
            }

            Chromosome chromosome = new Chromosome(depot.Count, capacity)
            {
                ChromosomeList = depot
            };

            return(chromosome);
        }
Exemple #6
0
        /// <summary>
        /// Sequentially insert customers to routes until we reach weight limitation.
        /// </summary>
        /// <param name="depot"></param>
        /// <returns></returns>
        public static void Routing(this Depot depot)
        {
            int      weight       = 0;
            Customer nullCustomer = new Customer
            {
                Id     = 0,
                X      = depot.X,
                Y      = depot.Y,
                IsNull = true
            };

            for (int i = 0; i < depot.Count; i++)
            {
                if (weight + depot[i].Cost > depot.Capacity)
                {
                    depot.Insert(i, nullCustomer);
                    weight = 0;
                    i++;
                }
                weight += depot[i].Cost;
            }
            depot.Add(nullCustomer);
        }
Exemple #7
0
        /// <summary>
        /// Compute the Euclidean distance between a customers and a depot to group them to the depots.
        /// </summary>
        /// <param name="customer"></param>
        /// <param name="depot"></param>
        /// <returns></returns>
        public static double EuclideanDistance(Customer customer, Depot depot)
        {
            double distance = Math.Sqrt(Math.Pow((customer.X - depot.X), 2) + Math.Pow((customer.Y - depot.Y), 2));

            return(distance);
        }
Exemple #8
0
        static void Main(string[] args)
        {
            int populaitionSize = 10; //input
            int capacity        = 80; //input
            int n = 1800;

            //Functions.SaveDataAsExcel("test",1,1,"Shit down");

            Customer c1  = new Customer(1, 37, 52, 7);
            Customer c2  = new Customer(2, 49, 49, 30);
            Customer c3  = new Customer(3, 52, 64, 16);
            Customer c4  = new Customer(4, 20, 26, 9);
            Customer c5  = new Customer(5, 40, 30, 21);
            Customer c6  = new Customer(6, 21, 47, 15);
            Customer c7  = new Customer(7, 17, 63, 19);
            Customer c8  = new Customer(8, 31, 62, 23);
            Customer c9  = new Customer(9, 52, 33, 11);
            Customer c10 = new Customer(10, 51, 21, 5);
            Customer c11 = new Customer(11, 42, 41, 19);
            Customer c12 = new Customer(12, 31, 32, 29);
            Customer c13 = new Customer(13, 5, 25, 23);
            Customer c14 = new Customer(14, 12, 42, 21);
            Customer c15 = new Customer(15, 36, 16, 10);
            Customer c16 = new Customer(16, 52, 41, 15);
            Customer c17 = new Customer(17, 27, 23, 3);
            Customer c18 = new Customer(18, 17, 33, 41);
            Customer c19 = new Customer(19, 13, 13, 9);
            Customer c20 = new Customer(20, 57, 58, 28);
            Customer c21 = new Customer(21, 62, 42, 8);
            Customer c22 = new Customer(22, 42, 57, 8);
            Customer c23 = new Customer(23, 16, 52, 16);
            Customer c24 = new Customer(24, 8, 52, 10);
            Customer c25 = new Customer(25, 7, 38, 28);
            Customer c26 = new Customer(26, 27, 68, 7);
            Customer c27 = new Customer(27, 30, 48, 15);
            Customer c28 = new Customer(28, 43, 67, 15);
            Customer c29 = new Customer(29, 58, 48, 6);
            Customer c30 = new Customer(30, 58, 27, 19);
            Customer c31 = new Customer(31, 37, 69, 11);
            Customer c32 = new Customer(32, 38, 46, 12);
            Customer c33 = new Customer(33, 46, 10, 23);
            Customer c34 = new Customer(34, 61, 33, 26);
            Customer c35 = new Customer(35, 62, 63, 17);
            Customer c36 = new Customer(36, 63, 69, 6);
            Customer c37 = new Customer(37, 32, 22, 9);
            Customer c38 = new Customer(38, 45, 35, 15);
            Customer c39 = new Customer(39, 59, 15, 14);
            Customer c40 = new Customer(40, 10, 17, 27);
            Customer c41 = new Customer(41, 5, 6, 7);
            Customer c42 = new Customer(42, 21, 10, 13);
            Customer c43 = new Customer(43, 15, 64, 11);
            Customer c44 = new Customer(44, 30, 15, 16);
            Customer c45 = new Customer(45, 39, 10, 10);
            Customer c46 = new Customer(46, 32, 39, 5);
            Customer c47 = new Customer(47, 25, 32, 25);
            Customer c48 = new Customer(48, 25, 55, 17);
            Customer c49 = new Customer(49, 48, 28, 18);
            Customer c50 = new Customer(50, 56, 37, 10);

            Depot d1 = new Depot(1, 20, 20, capacity);
            Depot d2 = new Depot(2, 30, 40, capacity);
            Depot d3 = new Depot(3, 50, 30, capacity);
            Depot d4 = new Depot(4, 60, 50, capacity);

            List <Customer> customer = new List <Customer>()
            {
                c1, c2, c3, c4, c5, c6, c7, c8, c9, c10, c11, c12, c13, c14, c15,
                c16, c17, c18, c19, c20, c21, c22, c23, c24, c25, c26, c27, c28, c29, c30, c31, c32, c33, c34, c35,
                c36, c37, c38, c39, c40, c41, c42, c43, c44, c45, c46, c47, c48, c49, c50
            };                                                                              //input
            List <Depot> depot = new List <Depot>()
            {
                d1, d2, d3, d4
            };                                                      //input

            //------------------------
            for (int i = 0; i < customer.Count; i++)
            {
                for (int j = 0; j < depot.Count; j++)
                {
                    Console.WriteLine(customer[i].Id + " - " + depot[j].Id + " = " + Functions.EuclideanDistance(customer[i], depot[j]));
                }
            }
            Console.WriteLine("  -------   ");
            for (int i = 0; i < customer.Count; i++)
            {
                for (int j = 0; j < customer.Count; j++)
                {
                    Console.WriteLine(customer[i].Id + " - " + customer[j].Id + " = " + Functions.EuclideanDistance(customer[i], customer[j]));
                }
            }
            Console.WriteLine("-----");
            //------------------------

            Chromosome chromosomeSample = GenerateChromosomeSample(depot, customer, capacity);

            for (int j = 0; j < chromosomeSample.Count; j++)
            {
                for (int k = 0; k < chromosomeSample[j].Count; k++)
                {
                    Console.Write(chromosomeSample[j][k].Id + "  ");
                }
                Console.Write(" }{ ");
            }
            Console.WriteLine();

            List <Chromosome> population = GeneratePopulation(populaitionSize, chromosomeSample);

            CalculationFitness(population);

            for (int i = 0; i < population.Count; i++)
            {
                for (int j = 0; j < population[i].Count; j++)
                {
                    for (int k = 0; k < population[i][j].Count; k++)
                    {
                        Console.Write(population[i][j][k].Id + "   ");
                    }
                    Console.Write(" }{  ");
                }
                Console.Write(population[i].Fitness + "   ");
                Console.WriteLine();
            }

            Console.WriteLine("  ");
            Console.WriteLine("-----");

            for (int i = 0; i < n; i++)
            {
                population = GenerateNewPopulation(population);
                //jahesh
                CalculationFitness(population);

                for (int o = 0; o < population.Count; o++)
                {
                    for (int j = 0; j < population[o].Count; j++)
                    {
                        for (int k = 0; k < population[o][j].Count; k++)
                        {
                            Console.Write(population[o][j][k].Id + "   ");
                        }
                        Console.Write(" }{  ");
                    }
                    Console.Write(population[o].Fitness + "   ");
                    Console.WriteLine();
                }

                Console.WriteLine("  ");
                Console.WriteLine("-----");
            }


            Console.ReadLine();
        }