Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1955_DynamicVehicleRouting/HeuristicLab.PDPSimulation/3.3/Optimizers/Reoptimization/PFIHReoptimization.cs @ 17578

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

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

File size: 7.0 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("PFIHReoptimization", "A pickup and delivery PFIH optimization.")]
43  [StorableClass]
44  public class PFIHReoptimization : DynamicPDPOptimization {
45    public IValueParameter<DoubleValue> Alpha {
46      get { return (IValueParameter<DoubleValue>)Parameters["Alpha"]; }
47    }
48    public IValueParameter<DoubleValue> AlphaVariance {
49      get { return (IValueParameter<DoubleValue>)Parameters["AlphaVariance"]; }
50    }
51    public IValueParameter<DoubleValue> Beta {
52      get { return (IValueParameter<DoubleValue>)Parameters["Beta"]; }
53    }
54    public IValueParameter<DoubleValue> BetaVariance {
55      get { return (IValueParameter<DoubleValue>)Parameters["BetaVariance"]; }
56    }
57    public IValueParameter<DoubleValue> Gamma {
58      get { return (IValueParameter<DoubleValue>)Parameters["Gamma"]; }
59    }
60    public IValueParameter<DoubleValue> GammaVariance {
61      get { return (IValueParameter<DoubleValue>)Parameters["GammaVariance"]; }
62    }
63
64    public ValueParameter<IntValue> SampleSizeParameter {
65      get { return (ValueParameter<IntValue>)Parameters["SampleSize"]; }
66    }
67
68    public ValueParameter<BoolValue> ComputeInParallelParameter {
69      get { return (ValueParameter<BoolValue>)Parameters["ComputeInParallel"]; }
70    }
71   
72    public PFIHReoptimization(): base() {
73      Parameters.Add(new ValueParameter<DoubleValue>("Alpha", "The alpha value.", new DoubleValue(0.7)));
74      Parameters.Add(new ValueParameter<DoubleValue>("AlphaVariance", "The alpha variance.", new DoubleValue(0.5)));
75      Parameters.Add(new ValueParameter<DoubleValue>("Beta", "The beta value.", new DoubleValue(0.1)));
76      Parameters.Add(new ValueParameter<DoubleValue>("BetaVariance", "The beta variance.", new DoubleValue(0.07)));
77      Parameters.Add(new ValueParameter<DoubleValue>("Gamma", "The gamma value.", new DoubleValue(0.2)));
78      Parameters.Add(new ValueParameter<DoubleValue>("GammaVariance", "The gamma variance.", new DoubleValue(0.14)));
79
80      Parameters.Add(new ValueParameter<IntValue>("SampleSize", "The sample size.", new IntValue(10)));
81      Parameters.Add(new ValueParameter<BoolValue>("ComputeInParallel", "Indicates if the samples should be computed in parallel.", new BoolValue(true)));
82    }
83    [StorableConstructor]
84    private PFIHReoptimization(bool deserializing) : base(deserializing) {
85    }
86    private PFIHReoptimization(PFIHReoptimization original, Cloner cloner)
87      : base(original, cloner) {
88    }
89    public override IDeepCloneable Clone(Cloner cloner) {
90      return new PFIHReoptimization(this, cloner);
91    }
92
93    class PFIHResult {
94      public PFIHResult() {
95        bestFeasible = false;
96        bestQuality = double.MaxValue;
97        bestSolution = null;
98      }
99
100      public bool bestFeasible;
101      public double bestQuality;
102      public IVRPEncoding bestSolution;
103    }
104
105    private IVRPEncoding Optimize(DynamicPDProblemInstance instance) {
106      List<bool> vehicleUsed = new List<bool>();
107      for (int i = 0; i < instance.StaticInstance.Vehicles.Value; i++) {
108        Vehicle vehicle = GetVehicle(instance.GetVehicle(i));
109        vehicleUsed.Add(vehicle.Distance > 0);
110      }
111
112      int sampleSize = SampleSizeParameter.Value.Value;
113      PFIHResult bestResult = new PFIHResult();
114
115      var options = new ParallelOptions();
116      if (ComputeInParallelParameter.Value.Value)
117        options.MaxDegreeOfParallelism = Math.Max(Environment.ProcessorCount - 1, 1);
118      else
119        options.MaxDegreeOfParallelism = 1;
120
121      IRandom random = new HeuristicLab.Random.MersenneTwister(1234);
122
123      Parallel.For<PFIHResult>(0, sampleSize, options,
124        () => new PFIHResult(),
125        (i, loop, result) => {
126          IVRPEncoding current = DynPushForwardInsertionCreator.CreateSolution(instance.StaticInstance,
127                  random, vehicleUsed,
128                  Alpha.Value.Value, Beta.Value.Value, Gamma.Value.Value,
129                  AlphaVariance.Value.Value, BetaVariance.Value.Value, GammaVariance.Value.Value);
130
131          VRPEvaluation eval = instance.StaticInstance.Evaluate(current);
132          double quality = eval.Quality;
133          bool feasible = instance.StaticInstance.Feasible(eval);
134
135          if ((feasible && !result.bestFeasible) || (feasible == result.bestFeasible && quality < result.bestQuality)) {
136            result.bestQuality = quality;
137            result.bestSolution = current;
138            result.bestFeasible = feasible;
139          }
140
141          return result;
142        },
143        (result) => {
144          lock (bestResult) {
145            if ((result.bestFeasible && !bestResult.bestFeasible) || (result.bestFeasible == bestResult.bestFeasible && result.bestQuality < bestResult.bestQuality)) {
146              bestResult.bestQuality = result.bestQuality;
147              bestResult.bestSolution = result.bestSolution;
148              bestResult.bestFeasible = result.bestFeasible;
149            }
150          }
151        }
152        );
153
154      //Recourse
155      if (!bestResult.bestFeasible) {
156        PotvinEncoding currentPlan = (instance.StaticInstance as DynPDPProblemInstance).CurrentPlan;
157        BestInsertion.RouteUnrouted(instance.StaticInstance, currentPlan);
158        bestResult.bestSolution = currentPlan;
159      }
160
161      return bestResult.bestSolution;
162    }
163
164    protected override bool PerformOptimization(DynamicPDProblemInstance instance, ChangeInformation changeInformation) {
165
166      IVRPEncoding solution = Optimize(instance);
167      PerformPlan(solution);
168
169      return true;
170    }
171  }
172}
Note: See TracBrowser for help on using the repository browser.