Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Optimizers/LocalUpdate/BestInsertion.cs @ 13042

Last change on this file since 13042 was 8815, checked in by svonolfe, 12 years ago

Improved best insertion heuristic (#1955)

File size: 5.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Common;
29using HeuristicLab.PDPSimulation.DomainModel;
30using System.Threading;
31using HeuristicLab.Problems.VehicleRouting.Interfaces;
32using HeuristicLab.Problems.VehicleRouting;
33using HeuristicLab.Problems.VehicleRouting.Variants;
34using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
35using HeuristicLab.PDPSimulation.Operators;
36using HeuristicLab.Parameters;
37using HeuristicLab.Data;
38using System.Threading.Tasks;
39using HeuristicLab.Problems.VehicleRouting.Encodings.Potvin;
40
41namespace HeuristicLab.PDPSimulation {
42  [Item("BestInsertion", "A best insertion local update optimization.")]
43  [StorableClass]
44  public class BestInsertion : DynamicPDPOptimization {   
45    public BestInsertion(): base() {
46    }
47    [StorableConstructor]
48    private BestInsertion(bool deserializing) : base(deserializing) {
49    }
50    private BestInsertion(BestInsertion original, Cloner cloner)
51      : base(original, cloner) {
52    }
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new BestInsertion(this, cloner);
55    }
56
57    public static void RouteUnrouted(IVRPProblemInstance instance, PotvinEncoding solution) {
58      DynPDPProblemInstance pdp = instance as DynPDPProblemInstance;
59     
60      while (solution.Unrouted.Count > 0) {
61        int city = solution.Unrouted[0];
62        int dest = pdp.GetPickupDeliveryLocation(city);
63
64        int source, target;
65        if (instance.GetDemand(city) >= 0) {
66          source = city;
67          target = dest;
68        } else {
69          source = dest;
70          target = city;
71        }
72
73        //consider creating new tours
74        int tours = solution.Tours.Count;
75       
76        for (int i = tours; i < solution.VehicleAssignment.Length; i++) {
77          Tour newTour = new Tour();
78          solution.Tours.Add(newTour);
79        }
80
81        double minCosts = double.MaxValue;
82        Tour insertTour = null;
83        int sourceLocation = -1;
84        int targetLocation = -1; 
85
86        foreach (Tour tour in solution.Tours) {
87          VRPEvaluation eval = instance.Evaluate(solution);
88
89          for (int i = 0; i <= tour.Stops.Count; i++) {
90            tour.Stops.Insert(i, source);
91            VRPEvaluation tourEval = instance.Evaluate(solution);
92            double sourceCosts = tourEval.Quality - eval.Quality;
93
94            if (tour.Stops.Count == 1) {
95              int vehicle = solution.VehicleAssignment[solution.Tours.IndexOf(tour)];
96              if (!pdp.VehicleUsed[vehicle])
97                sourceCosts += instance.FleetUsageFactor.Value;
98            }
99
100
101            if (source != target) {
102              for (int j = i + 1; j <= tour.Stops.Count; j++) {
103                bool feasible;
104                double targetCosts = instance.GetInsertionCosts(tourEval, solution, target, solution.Tours.IndexOf(tour), j, out feasible);
105
106                double costs = sourceCosts + targetCosts;
107                if (costs < minCosts) {
108                  minCosts = costs;
109                  insertTour = tour;
110                  sourceLocation = i;
111                  targetLocation = j;
112                }
113              }
114            } else {
115              double costs = sourceCosts;
116              if (costs < minCosts) {
117                minCosts = costs;
118                insertTour = tour;
119                sourceLocation = i;
120                targetLocation = -1;
121              }
122            }
123            tour.Stops.Remove(source);
124          }
125        }
126
127        insertTour.Stops.Insert(sourceLocation, source);
128        if (source != target) {
129          insertTour.Stops.Insert(targetLocation, target);
130        }
131
132        solution.Unrouted.Remove(source);
133        if (source != target) {
134          solution.Unrouted.Remove(target);
135        }
136
137        solution.Repair();
138      }
139    }
140
141    protected override void PrepareInstance(DynamicPDProblemInstance instance, ChangeInformation changeInformation) {
142      instance.StaticInstance.Vehicles.Value = changeInformation.CreatedVehicles.Count;
143      instance.SetCurrentPlan(new PotvinEncoding(instance.StaticInstance));
144      instance.InitializeInstance(changeInformation.CreatedVehicles, changeInformation.CreatedOrders);
145    }
146
147    protected override bool PerformOptimization(DynamicPDProblemInstance instance, ChangeInformation changeInformation) {
148     PotvinEncoding solution = instance.Solutions.First();
149
150     RouteUnrouted(instance.StaticInstance, solution);
151
152     PerformPlan(solution);
153
154     return true;
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.