Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.VRPEnhancements/HeuristicLab.Problems.VehicleRouting/3.4/Encodings/Potvin/Manipulators/PotvinGreedyTourCreationManipulator.cs @ 14559

Last change on this file since 14559 was 14417, checked in by jzenisek, 8 years ago

#2707 added some new vrp operators (1 creator, 1 crossover, 1 manipulator)

File size: 4.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Linq.Expressions;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Parameters;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.VehicleRouting.Interfaces;
29using HeuristicLab.Problems.VehicleRouting.ProblemInstances;
30
31namespace HeuristicLab.Problems.VehicleRouting.Encodings.Potvin {
32  [Item("PotvinGreedyTourCreationManipulator", "This Manipulator reorders the stops within all tours of a solution candidate by following the greedy heuristic: gradually adding those stops to a tour with the least effect on the overall costs.")]
33  [StorableClass]
34  public sealed class PotvinGreedyTourCreationManipulator : PotvinManipulator {
35    public IValueParameter<BoolValue> IncludeCosts {
36      get { return (IValueParameter<BoolValue>) Parameters["IncludeCosts"]; }
37    }
38
39    [StorableConstructor]
40    private PotvinGreedyTourCreationManipulator(bool deserializing) : base(deserializing) { }
41
42    public PotvinGreedyTourCreationManipulator() : base() {
43      Parameters.Add(new ValueParameter<BoolValue>("IncludeCosts", "Indicates if the actual costs should be taken into account when evaluating a tour. Otherwise only the distances between stops are counted.", new BoolValue(true)));
44    }
45    public override IDeepCloneable Clone(Cloner cloner) {
46      return new PotvinGreedyTourCreationManipulator(this, cloner);
47    }
48
49    private PotvinGreedyTourCreationManipulator(PotvinGreedyTourCreationManipulator original, Cloner cloner)
50      : base(original, cloner) {
51    }
52
53    public static void ApplyManipulation(IRandom random, PotvinEncoding individual, IVRPProblemInstance instance,
54      bool allowInfeasible, bool includeCosts) {
55
56      for (int i = 0; i < individual.Tours.Count; i++) {
57        Tour tour = individual.Tours[i];
58        Tour unrouted = individual.Tours[i].Clone() as Tour;
59        tour.Stops.Clear();
60        VRPEvaluation evaluation = instance.EvaluateTour(tour, individual);
61        double currentQuality = (includeCosts) ? evaluation.Quality : evaluation.Distance;
62
63        while (unrouted.Stops.Count > 0) {
64          int insertionPosition = tour.Stops.Count;
65          int nextCityIdxToAdd = 0;
66
67          tour.Stops.Add(unrouted.Stops[nextCityIdxToAdd]);
68          evaluation = instance.EvaluateTour(tour, individual);
69          double minQualityIncrease = ((includeCosts) ? evaluation.Quality : evaluation.Distance) - currentQuality;
70          tour.Stops.RemoveAt(insertionPosition);
71
72          for (int j = 1; j < unrouted.Stops.Count; j++) {
73            tour.Stops.Insert(insertionPosition, unrouted.Stops[j]);
74            evaluation = instance.EvaluateTour(tour, individual);
75            double qualityIncrease = ((includeCosts) ? evaluation.Quality : evaluation.Distance) - currentQuality;
76            tour.Stops.RemoveAt(insertionPosition);
77
78            if (qualityIncrease < minQualityIncrease) {
79              nextCityIdxToAdd = j;
80              minQualityIncrease = qualityIncrease;
81            }
82          }
83
84          currentQuality += minQualityIncrease;
85          tour.Stops.Insert(insertionPosition, unrouted.Stops[nextCityIdxToAdd]);
86          unrouted.Stops.RemoveAt(nextCityIdxToAdd);
87        }
88      }
89    }
90
91    protected override void Manipulate(IRandom random, PotvinEncoding individual) {
92      bool allowInfeasible = AllowInfeasibleSolutions.Value.Value;
93      bool includeCosts = IncludeCosts.Value.Value;
94      ApplyManipulation(random, individual, ProblemInstance, allowInfeasible, includeCosts);
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.