Пример #1
0
        public static List<Problem> Divide(Problem problem, List<int> frac=null)
        {
            if(frac == null) frac = DefaultFraction;

            var subproblems = new List<Problem>(frac.Count);
            var rnd = new Random();
            var customers = new List<Customer>(problem.Customers);

            foreach (var count in frac)
            {
                var p = new Problem
                {
                    Abbr = problem.Abbr,
                    VehicleCap = problem.VehicleCap,
                    Depot = problem.Depot,
                    Customers = new List<Customer>(count)
                };

                for(var i = 0; i < count; ++i)
                {
                    var j = rnd.Next(0, customers.Count);
                    p.Customers.Add(customers[j]);
                    customers.RemoveAt(j);
                }
                p.SetAllNodes();
                subproblems.Add(p);
            }

            return subproblems;
        }
Пример #2
0
 public Route(Problem problem)
 {
     Depot depot = problem.Depot;
     Problem = problem;
     Solution = null;
     RouteList = new List<AbsNode>();
     ServiceBeginingTimes = new List<double>();
     AddNode(depot);
     AddNode(depot);
 }
Пример #3
0
        private void Button1Click(object sender, EventArgs e)
        {
            var reader = new CsvProblemReader();
            _problem = reader.Read(@"TestData/c1_2_1_v50_c200.csv");
            var sol87Solver = new Solomon87();

            var t = ShapleyDivider.Divide(_problem);

            _p1 = sol87Solver.Solve(t[0]);
            _p2 = sol87Solver.Solve(t[1]);
            _p3 = sol87Solver.Solve(t[2]);

            _p12 = sol87Solver.Solve(ShapleyDivider.Merge(t[0], t[1]));
            _p23 = sol87Solver.Solve(ShapleyDivider.Merge(t[1], t[2]));
            _p31 = sol87Solver.Solve(ShapleyDivider.Merge(t[2], t[0]));

            _p123 = sol87Solver.Solve(_problem);

            textBox1.Text = _p123.PrintToString();
            Draw();
        }
Пример #4
0
 public Problem Read(string csvFilePath)
 {
     var nodes = new List<NodeInfo>();
     var p = new Problem();
     using (var reader = new CsvReader(csvFilePath))
     {
         reader.ReadHeaderRecord();
         foreach (var record in reader.DataRecords)
         {
             nodes.Add(new NodeInfo
                               {
                                   Id = int.Parse(record["Id"]),
                                   X = float.Parse(record["X"], CultureInfo.InvariantCulture),
                                   Y = float.Parse(record["Y"], CultureInfo.InvariantCulture),
                                   Demand = float.Parse(record["Demand"], CultureInfo.InvariantCulture),
                                   ReadyTime = float.Parse(record["ReadyTime"], CultureInfo.InvariantCulture),
                                   DueDate = float.Parse(record["DueDate"], CultureInfo.InvariantCulture),
                                   ServiceTime = float.Parse(record["ServiceTime"], CultureInfo.InvariantCulture)
                               });
         }
     }
     p.SetNodes(nodes);
     return p;
 }
Пример #5
0
 public static Problem Merge(Problem p1, Problem p2)
 {
     var newP = new Problem()
                    {
                        Depot = p1.Depot,
                        VehicleCap = p1.VehicleCap,
                        Abbr = p1.Abbr,
                        Customers = new List<Customer>()
                    };
     foreach (var c in p1.Customers)
         newP.Customers.Add(c);
     foreach (var c in p2.Customers)
         newP.Customers.Add(c);
     newP.SetAllNodes();
     return newP;
 }
Пример #6
0
        public override Solution Solve(Problem problem, Solution initialSolution)
        {
            var cNumber = (int) (problem.Customers.Count*CNumCoef);

            /* 1. */
            var selected = SelectionStage(initialSolution, cNumber);

            /* 2. */
            var p = .75;
            var probablyOrdered = OrderRoutesWithProb(initialSolution, p);

            /* 3. */
            var newSolution = ImproveSolution(initialSolution, selected, probablyOrdered);

            return newSolution;
        }
Пример #7
0
        public override Solution Solve(Problem problem, Solution initialSolution = null)
        {
            var solution = new Solution(problem);
            var unroutedCustomers = new List<Customer>(problem.Customers);
            int seedId = SelectSeedCustomer(unroutedCustomers, problem.Depot);
            Customer seed = unroutedCustomers[seedId];
            unroutedCustomers.RemoveAt(seedId);
            var partialRoute = new Route(problem, seed);

            while (unroutedCustomers.Count > 0)
            {
                Route newRoute = partialRoute.Copy();
                var bestU = new List<int>();
                var c1Vals = new List<double>();
                foreach (Customer cust in unroutedCustomers)
                {
                    double minC1 = double.MaxValue;
                    int optimalU = 1;
                    for (int i = 1; i < newRoute.RouteList.Count - 1; ++i)
                    {
                        double c1 = CriterionC1(i, cust, i + 1, newRoute);
                        if (c1 < minC1)
                        {
                            minC1 = c1;
                            optimalU = i;
                        }
                    }
                    bestU.Add(optimalU);
                    c1Vals.Add(minC1);
                }

                int bestCust = 0;
                double minC2 = double.MaxValue;
                for (int i = 0; i < unroutedCustomers.Count; ++i)
                {
                    double c2 = CriterionC2(unroutedCustomers[i], c1Vals[i], newRoute);
                    if (c2 < minC2)
                    {
                        minC2 = c2;
                        bestCust = i;
                    }
                }

                Customer newCustomer = unroutedCustomers[bestCust];
                newRoute.InsertCustomer(newCustomer, bestU[bestCust]);

                if (newRoute.IsFeasible())
                {
                    unroutedCustomers.RemoveAt(bestCust);
                    partialRoute = newRoute;
                }
                else
                {
                    solution.AddRoute(partialRoute);
                    seedId = SelectSeedCustomer(unroutedCustomers, problem.Depot);
                    seed = unroutedCustomers[seedId];
                    unroutedCustomers.RemoveAt(seedId);
                    partialRoute = new Route(problem, seed);
                }

                if (unroutedCustomers.Count == 0)
                    solution.AddRoute(partialRoute);
            }

            return solution;
        }
Пример #8
0
 public Solution(Problem problem)
 {
     Routes = new List<Route>();
     Problem = problem;
     Phi = 0.0;
 }