Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 8782 was 8670, checked in by svonolfe, 12 years ago

Added first version of the dynamic vehicle routing addon (#1955)

File size: 5.6 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        if(tours < solution.VehicleAssignment.Length) {
78          Tour newTour = new Tour();
79          solution.Tours.Add(newTour);
80        }
81
82        double minCosts = double.MaxValue;
83        Tour insertTour = null;
84        int sourceLocation = -1;
85        int targetLocation = -1; 
86
87        foreach (Tour tour in solution.Tours) {
88          VRPEvaluation eval = instance.Evaluate(solution);
89
90          for (int i = 0; i <= tour.Stops.Count; i++) {
91            tour.Stops.Insert(i, source);
92            VRPEvaluation tourEval = instance.Evaluate(solution);
93            double sourceCosts = tourEval.Quality - eval.Quality;
94
95            if (tour.Stops.Count == 1) {
96              int vehicle = solution.VehicleAssignment[solution.Tours.IndexOf(tour)];
97              if (!pdp.VehicleUsed[vehicle])
98                sourceCosts += instance.FleetUsageFactor.Value;
99            }
100
101
102            if (source != target) {
103              for (int j = i + 1; j <= tour.Stops.Count; j++) {
104                bool feasible;
105                double targetCosts = instance.GetInsertionCosts(tourEval, solution, target, solution.Tours.IndexOf(tour), j, out feasible);
106
107                double costs = sourceCosts + targetCosts;
108                if (costs < minCosts) {
109                  minCosts = costs;
110                  insertTour = tour;
111                  sourceLocation = i;
112                  targetLocation = j;
113                }
114              }
115            } else {
116              double costs = sourceCosts;
117              if (costs < minCosts) {
118                minCosts = costs;
119                insertTour = tour;
120                sourceLocation = i;
121                targetLocation = -1;
122              }
123            }
124            tour.Stops.Remove(source);
125          }
126        }
127
128        insertTour.Stops.Insert(sourceLocation, source);
129        if (source != target) {
130          insertTour.Stops.Insert(targetLocation, target);
131        }
132
133        solution.Unrouted.Remove(source);
134        if (source != target) {
135          solution.Unrouted.Remove(target);
136        }
137
138        solution.Repair();
139      }
140    }
141
142    protected override void PrepareInstance(DynamicPDProblemInstance instance, ChangeInformation changeInformation) {
143      instance.StaticInstance.Vehicles.Value = changeInformation.CreatedVehicles.Count;
144      instance.SetCurrentPlan(new PotvinEncoding(instance.StaticInstance));
145      instance.InitializeInstance(changeInformation.CreatedVehicles, changeInformation.CreatedOrders);
146    }
147
148    protected override bool PerformOptimization(DynamicPDProblemInstance instance, ChangeInformation changeInformation) {
149     PotvinEncoding solution = instance.Solutions.First();
150
151     RouteUnrouted(instance.StaticInstance, solution);
152
153     PerformPlan(solution);
154
155     return true;
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.