Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.VehicleRouting/3.4/VehicleRoutingProblem.cs @ 15069

Last change on this file since 15069 was 15069, checked in by abeham, 7 years ago

#2706:

  • Added or updated similarity calculators and population similarity analysis for several problems (BinPacking, LAP, Orienteering, Parameter optimization, PTSP, QAP, TF, TSP, VRP)
  • Made TSPSimilarityCalculator obsolete since it's essentially the same as the one in the permutation plugin
  • Made QAPPopulationDiversityAnalyzer obsolete as it is replaced by the newer PopulationSimilarityAnalyzer
  • Removed genotype specific similarity code in QAPPermutationProximityCalculator (again identical to the permutation plugin)
  • Changed QAPSimilarityCalculator to perform phenotype similarity instead of genotype similarity (has not been previously used)
File size: 15.9 KB
RevLine 
[4360]1#region License Information
2/* HeuristicLab
[14185]3 * Copyright (C) 2002-2016 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[4360]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.Drawing;
25using System.Linq;
[8720]26using HeuristicLab.Analysis;
[4360]27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Optimization;
[12102]31using HeuristicLab.Optimization.Operators;
[4360]32using HeuristicLab.Parameters;
33using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
34using HeuristicLab.PluginInfrastructure;
[7934]35using HeuristicLab.Problems.Instances;
[11302]36using HeuristicLab.Problems.VehicleRouting.Encodings.Alba;
[4362]37using HeuristicLab.Problems.VehicleRouting.Interfaces;
[7934]38using HeuristicLab.Problems.VehicleRouting.Interpreters;
[4362]39using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
[4365]40using HeuristicLab.Problems.VehicleRouting.Variants;
[4360]41
42namespace HeuristicLab.Problems.VehicleRouting {
[13173]43  [Item("Vehicle Routing Problem (VRP)", "Represents a Vehicle Routing Problem.")]
[12504]44  [Creatable(CreatableAttribute.Categories.CombinatorialProblems, Priority = 110)]
[4360]45  [StorableClass]
[11285]46  public sealed class VehicleRoutingProblem : Problem, ISingleObjectiveHeuristicOptimizationProblem, IStorableContent, IProblemInstanceConsumer<IVRPData> {
[4444]47    public string Filename { get; set; }
[7203]48
49    public static new Image StaticItemImage {
[5867]50      get { return HeuristicLab.Common.Resources.VSImageLibrary.Type; }
[4360]51    }
52
[4362]53    #region Parameter Properties
54    public ValueParameter<BoolValue> MaximizationParameter {
55      get { return (ValueParameter<BoolValue>)Parameters["Maximization"]; }
[4360]56    }
[5867]57    IParameter ISingleObjectiveHeuristicOptimizationProblem.MaximizationParameter {
[4362]58      get { return MaximizationParameter; }
[4360]59    }
[4362]60    public ValueParameter<IVRPProblemInstance> ProblemInstanceParameter {
61      get { return (ValueParameter<IVRPProblemInstance>)Parameters["ProblemInstance"]; }
62    }
63    public OptionalValueParameter<DoubleValue> BestKnownQualityParameter {
64      get { return (OptionalValueParameter<DoubleValue>)Parameters["BestKnownQuality"]; }
65    }
[5867]66    IParameter ISingleObjectiveHeuristicOptimizationProblem.BestKnownQualityParameter {
[4362]67      get { return BestKnownQualityParameter; }
68    }
[4860]69    public OptionalValueParameter<VRPSolution> BestKnownSolutionParameter {
70      get { return (OptionalValueParameter<VRPSolution>)Parameters["BestKnownSolution"]; }
71    }
[8121]72    public IConstrainedValueParameter<IVRPCreator> SolutionCreatorParameter {
73      get { return (IConstrainedValueParameter<IVRPCreator>)Parameters["SolutionCreator"]; }
[4360]74    }
[5867]75    IParameter IHeuristicOptimizationProblem.SolutionCreatorParameter {
[4365]76      get { return SolutionCreatorParameter; }
[4362]77    }
[4365]78    public IValueParameter<IVRPEvaluator> EvaluatorParameter {
79      get { return (IValueParameter<IVRPEvaluator>)Parameters["Evaluator"]; }
80    }
[5867]81    IParameter IHeuristicOptimizationProblem.EvaluatorParameter {
[4365]82      get { return EvaluatorParameter; }
83    }
[4362]84    #endregion
[4360]85
[4362]86    #region Properties
87    public IVRPProblemInstance ProblemInstance {
88      get { return ProblemInstanceParameter.Value; }
89      set { ProblemInstanceParameter.Value = value; }
[4360]90    }
91
[4860]92    public VRPSolution BestKnownSolution {
93      get { return BestKnownSolutionParameter.Value; }
94      set { BestKnownSolutionParameter.Value = value; }
95    }
96
[7852]97    public DoubleValue BestKnownQuality {
98      get { return BestKnownQualityParameter.Value; }
99      set { BestKnownQualityParameter.Value = value; }
100    }
101
[4362]102    public ISingleObjectiveEvaluator Evaluator {
[7852]103      get { return EvaluatorParameter.Value; }
[4360]104    }
105
[5867]106    IEvaluator IHeuristicOptimizationProblem.Evaluator {
[4362]107      get { return this.Evaluator; }
[4360]108    }
109
[8121]110    ISolutionCreator IHeuristicOptimizationProblem.SolutionCreator {
[7852]111      get { return SolutionCreatorParameter.Value; }
[4360]112    }
[8121]113    public IVRPCreator SolutionCreator {
114      get { return SolutionCreatorParameter.Value; }
115      set { SolutionCreatorParameter.Value = value; }
116    }
[4360]117    #endregion
118
119    [StorableConstructor]
120    private VehicleRoutingProblem(bool deserializing) : base(deserializing) { }
121    public VehicleRoutingProblem()
122      : base() {
123      Parameters.Add(new ValueParameter<BoolValue>("Maximization", "Set to false as the Vehicle Routing Problem is a minimization problem.", new BoolValue(false)));
[4362]124      Parameters.Add(new ValueParameter<IVRPProblemInstance>("ProblemInstance", "The VRP problem instance"));
125      Parameters.Add(new OptionalValueParameter<DoubleValue>("BestKnownQuality", "The quality of the best known solution of this VRP instance."));
[4860]126      Parameters.Add(new OptionalValueParameter<VRPSolution>("BestKnownSolution", "The best known solution of this VRP instance."));
[4365]127
[7852]128      Parameters.Add(new ConstrainedValueParameter<IVRPCreator>("SolutionCreator", "The operator which should be used to create new VRP solutions."));
129      Parameters.Add(new ValueParameter<IVRPEvaluator>("Evaluator", "The operator which should be used to evaluate VRP solutions."));
130
131      EvaluatorParameter.Hidden = true;
132
[4360]133      InitializeRandomVRPInstance();
[4365]134      InitializeOperators();
[4360]135
136      AttachEventHandlers();
[4362]137      AttachProblemInstanceEventHandlers();
[10363]138
139      EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
[4360]140    }
141
142    public override IDeepCloneable Clone(Cloner cloner) {
[7906]143      cloner.Clone(ProblemInstance);
[4752]144      return new VehicleRoutingProblem(this, cloner);
[4360]145    }
146
[4752]147    private VehicleRoutingProblem(VehicleRoutingProblem original, Cloner cloner)
148      : base(original, cloner) {
149      this.AttachEventHandlers();
[10360]150      this.AttachProblemInstanceEventHandlers();
[10363]151
152      ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
[4752]153    }
154
[4360]155    #region Events
156    public event EventHandler SolutionCreatorChanged;
157    private void OnSolutionCreatorChanged() {
158      EventHandler handler = SolutionCreatorChanged;
159      if (handler != null) handler(this, EventArgs.Empty);
160    }
161    public event EventHandler EvaluatorChanged;
162    private void OnEvaluatorChanged() {
163      EventHandler handler = EvaluatorChanged;
164      if (handler != null) handler(this, EventArgs.Empty);
165    }
166    #endregion
167
168    #region Helpers
169    [StorableHook(HookType.AfterDeserialization)]
[7934]170    private void AfterDeserialization() {
[4360]171      AttachEventHandlers();
[4362]172      AttachProblemInstanceEventHandlers();
[10363]173
174      ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
[4360]175    }
176
[8006]177    [Storable(Name = "operators", AllowOneWay = true)]
178    private List<IOperator> StorableOperators {
179      set { Operators.AddRange(value); }
180    }
181
[4360]182    private void AttachEventHandlers() {
[4362]183      ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
[7861]184      BestKnownSolutionParameter.ValueChanged += new EventHandler(BestKnownSolutionParameter_ValueChanged);
[8006]185      EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
186      SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
[4360]187    }
[4362]188
189    private void AttachProblemInstanceEventHandlers() {
190      if (ProblemInstance != null) {
[7852]191        ProblemInstance.EvaluationChanged += new EventHandler(ProblemInstance_EvaluationChanged);
[7934]192      }
[7852]193    }
[4860]194
[7861]195    private void EvalBestKnownSolution() {
[7852]196      if (BestKnownSolution != null) {
197        //call evaluator
198        BestKnownQuality = new DoubleValue(ProblemInstance.Evaluate(BestKnownSolution.Solution).Quality);
199        BestKnownSolution.Quality = BestKnownQuality;
[4362]200      }
[4360]201    }
[4362]202
[7861]203    void BestKnownSolutionParameter_ValueChanged(object sender, EventArgs e) {
204      EvalBestKnownSolution();
205    }
206
207    void ProblemInstance_EvaluationChanged(object sender, EventArgs e) {
[14481]208      BestKnownQuality = null;
[7861]209      EvalBestKnownSolution();
210    }
211
[4362]212    void ProblemInstanceParameter_ValueChanged(object sender, EventArgs e) {
[7853]213      InitializeOperators();
[4362]214      AttachProblemInstanceEventHandlers();
[4365]215
[10363]216      EvaluatorParameter.Value = ProblemInstance.SolutionEvaluator;
217
[4365]218      OnSolutionCreatorChanged();
219      OnEvaluatorChanged();
220      OnOperatorsChanged();
[4360]221    }
[6907]222
223    public void SetProblemInstance(IVRPProblemInstance instance) {
224      ProblemInstanceParameter.ValueChanged -= new EventHandler(ProblemInstanceParameter_ValueChanged);
225
226      ProblemInstance = instance;
227      AttachProblemInstanceEventHandlers();
228
229      OnSolutionCreatorChanged();
230      OnEvaluatorChanged();
231
232      ProblemInstanceParameter.ValueChanged += new EventHandler(ProblemInstanceParameter_ValueChanged);
233    }
234
[4362]235    private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
236      OnSolutionCreatorChanged();
[4360]237    }
[4362]238    private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
[8006]239      if (ProblemInstance != null)
240        ProblemInstance.SolutionEvaluator = EvaluatorParameter.Value;
[4362]241      OnEvaluatorChanged();
[4360]242    }
[4365]243
244    private void InitializeOperators() {
[8346]245      var solutionCreatorParameter = SolutionCreatorParameter as ConstrainedValueParameter<IVRPCreator>;
246      solutionCreatorParameter.ValidValues.Clear();
247
[8006]248      Operators.Clear();
[4365]249
250      if (ProblemInstance != null) {
[8006]251        Operators.AddRange(
[4365]252        ProblemInstance.Operators.Concat(
253          ApplicationManager.Manager.GetInstances<IGeneralVRPOperator>().Cast<IOperator>()).OrderBy(op => op.Name));
[8346]254        Operators.Add(new VRPSimilarityCalculator());
[12102]255        Operators.Add(new QualitySimilarityCalculator());
[12069]256        Operators.Add(new PopulationSimilarityAnalyzer(Operators.OfType<ISolutionSimilarityCalculator>()));
[8346]257
258        IVRPCreator defaultCreator = null;
259        foreach (IVRPCreator creator in Operators.Where(o => o is IVRPCreator)) {
260          solutionCreatorParameter.ValidValues.Add(creator);
261          if (creator is Encodings.Alba.RandomCreator)
262            defaultCreator = creator;
263        }
[11302]264        Operators.Add(new AlbaLambdaInterchangeLocalImprovementOperator());
[8346]265        if (defaultCreator != null)
266          solutionCreatorParameter.Value = defaultCreator;
[4365]267      }
268
269      ParameterizeOperators();
270    }
271
272    private void ParameterizeOperators() {
[7999]273      foreach (IOperator op in Operators.OfType<IOperator>()) {
[4365]274        if (op is IMultiVRPOperator) {
[7999]275          (op as IMultiVRPOperator).SetOperators(Operators.OfType<IOperator>());
[4365]276        }
277      }
[8346]278      if (ProblemInstance != null) {
279        foreach (ISingleObjectiveImprovementOperator op in Operators.OfType<ISingleObjectiveImprovementOperator>()) {
280          op.SolutionParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
281          op.SolutionParameter.Hidden = true;
282        }
283        foreach (ISingleObjectivePathRelinker op in Operators.OfType<ISingleObjectivePathRelinker>()) {
284          op.ParentsParameter.ActualName = SolutionCreator.VRPToursParameter.ActualName;
285          op.ParentsParameter.Hidden = true;
286        }
[12102]287        foreach (ISolutionSimilarityCalculator op in Operators.OfType<ISolutionSimilarityCalculator>()) {
[8346]288          op.SolutionVariableName = SolutionCreator.VRPToursParameter.ActualName;
289          op.QualityVariableName = ProblemInstance.SolutionEvaluator.QualityParameter.ActualName;
[12102]290          var calc = op as VRPSimilarityCalculator;
291          if (calc != null) calc.ProblemInstance = ProblemInstance;
[8346]292        }
293      }
[4365]294    }
[8346]295
[4360]296    #endregion
297
298    private void InitializeRandomVRPInstance() {
299      System.Random rand = new System.Random();
300
[4362]301      CVRPTWProblemInstance problem = new CVRPTWProblemInstance();
[4360]302      int cities = 100;
[4362]303
304      problem.Coordinates = new DoubleMatrix(cities + 1, 2);
305      problem.Demand = new DoubleArray(cities + 1);
306      problem.DueTime = new DoubleArray(cities + 1);
307      problem.ReadyTime = new DoubleArray(cities + 1);
308      problem.ServiceTime = new DoubleArray(cities + 1);
309
310      problem.Vehicles.Value = 100;
311      problem.Capacity.Value = 200;
312
313      for (int i = 0; i <= cities; i++) {
314        problem.Coordinates[i, 0] = rand.Next(0, 100);
315        problem.Coordinates[i, 1] = rand.Next(0, 100);
316
317        if (i == 0) {
318          problem.Demand[i] = 0;
319          problem.DueTime[i] = Int16.MaxValue;
320          problem.ReadyTime[i] = 0;
321          problem.ServiceTime[i] = 0;
322        } else {
323          problem.Demand[i] = rand.Next(10, 50);
[6851]324          problem.DueTime[i] = rand.Next((int)Math.Ceiling(problem.GetDistance(0, i, null)), 1200);
[4362]325          problem.ReadyTime[i] = problem.DueTime[i] - rand.Next(0, 100);
326          problem.ServiceTime[i] = 90;
327        }
328      }
329
330      this.ProblemInstance = problem;
[4360]331    }
[4860]332
333    public void ImportSolution(string solutionFileName) {
334      SolutionParser parser = new SolutionParser(solutionFileName);
335      parser.Parse();
336
337      HeuristicLab.Problems.VehicleRouting.Encodings.Potvin.PotvinEncoding encoding = new Encodings.Potvin.PotvinEncoding(ProblemInstance);
338
339      int cities = 0;
340      foreach (List<int> route in parser.Routes) {
341        Tour tour = new Tour();
342        tour.Stops.AddRange(route);
343        cities += tour.Stops.Count;
344
345        encoding.Tours.Add(tour);
346      }
347
348      if (cities != ProblemInstance.Coordinates.Rows - 1)
349        ErrorHandling.ShowErrorDialog(new Exception("The optimal solution does not seem to correspond with the problem data"));
350      else {
351        VRPSolution solution = new VRPSolution(ProblemInstance, encoding, new DoubleValue(0));
[7852]352        BestKnownSolutionParameter.Value = solution;
[4860]353      }
354    }
[7871]355
[8905]356    #region Instance Consuming
357    public void Load(IVRPData data, IVRPDataInterpreter interpreter) {
358      VRPInstanceDescription instance = interpreter.Interpret(data);
[7871]359
[8905]360      Name = instance.Name;
361      Description = instance.Description;
[10860]362
363      BestKnownQuality = null;
364      BestKnownSolution = null;
365
[8905]366      if (ProblemInstance != null && instance.ProblemInstance != null &&
367        instance.ProblemInstance.GetType() == ProblemInstance.GetType())
368        SetProblemInstance(instance.ProblemInstance);
369      else
370        ProblemInstance = instance.ProblemInstance;
[7871]371
[8905]372      OnReset();
[7871]373
[8905]374      if (instance.BestKnownQuality != null) {
375        BestKnownQuality = new DoubleValue((double)instance.BestKnownQuality);
376      }
[7871]377
[8905]378      if (instance.BestKnownSolution != null) {
379        VRPSolution solution = new VRPSolution(ProblemInstance, instance.BestKnownSolution, new DoubleValue(0));
380        BestKnownSolution = solution;
[7934]381      }
[7871]382    }
[10435]383    #endregion
[8905]384
[10435]385    #region IProblemInstanceConsumer<VRPData> Members
[8905]386
[11285]387    public void Load(IVRPData data) {
[10651]388      var interpreterDataType = data.GetType();
389      var interpreterType = typeof(IVRPDataInterpreter<>).MakeGenericType(interpreterDataType);
[8905]390
[10651]391      var interpreters = ApplicationManager.Manager.GetTypes(interpreterType);
392
393      var concreteInterpreter = interpreters.Single(t => GetInterpreterDataType(t) == interpreterDataType);
394
395      Load(data, (IVRPDataInterpreter)Activator.CreateInstance(concreteInterpreter));
[8905]396    }
397
[10651]398    private Type GetInterpreterDataType(Type type) {
399      var parentInterfaces = type.BaseType.GetInterfaces();
400      var interfaces = type.GetInterfaces().Except(parentInterfaces);
401
402      var interpreterInterface = interfaces.Single(i => typeof(IVRPDataInterpreter).IsAssignableFrom(i));
403      return interpreterInterface.GetGenericArguments()[0];
404    }
405
[8905]406    #endregion
[4360]407  }
408}
Note: See TracBrowser for help on using the repository browser.