Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Problems.Orienteering/3.3/Creators/GreedyOrienteeringTourCreator.cs @ 15559

Last change on this file since 15559 was 14186, checked in by swagner, 8 years ago

#2526: Updated year of copyrights in license headers

File size: 6.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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.Orienteering {
32  /// <summary>
33  /// The initial solution for P-VNS is generated by means of a greedy algorithm that takes into
34  /// account all vertices vi that are located within the cost limit Tmax. These points are sorted
35  /// in descending order regarding the sum of their objective values. Afterwards, the algorithm
36  /// starts with a tour only including the starting and ending point and successively inserts the
37  /// points from this list at the first position in which they can feasibly be inserted.
38  /// (Schilde et. al. 2009)
39  /// </summary>
40  [Item("GreedyOrienteeringTourCreator", @"Implements the solution creation procedure described in Schilde M., Doerner K.F., Hartl R.F., Kiechle G. 2009. Metaheuristics for the bi-objective orienteering problem. Swarm Intelligence, Volume 3, Issue 3, pp 179-201.")]
41  [StorableClass]
42  public sealed class GreedyOrienteeringTourCreator : IntegerVectorCreator, IOrienteeringSolutionCreator {
43    public override bool CanChangeName { get { return false; } }
44
45    #region Parameter Properties
46    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
47      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
48    }
49    public ILookupParameter<DoubleArray> ScoresParameter {
50      get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
51    }
52    public ILookupParameter<DoubleValue> MaximumDistanceParameter {
53      get { return (ILookupParameter<DoubleValue>)Parameters["MaximumDistance"]; }
54    }
55    public ILookupParameter<IntValue> StartingPointParameter {
56      get { return (ILookupParameter<IntValue>)Parameters["StartingPoint"]; }
57    }
58    public ILookupParameter<IntValue> TerminalPointParameter {
59      get { return (ILookupParameter<IntValue>)Parameters["TerminalPoint"]; }
60    }
61    public ILookupParameter<DoubleValue> PointVisitingCostsParameter {
62      get { return (ILookupParameter<DoubleValue>)Parameters["PointVisitingCosts"]; }
63    }
64    #endregion
65
66    [StorableConstructor]
67    private GreedyOrienteeringTourCreator(bool deserializing)
68      : base(deserializing) { }
69    private GreedyOrienteeringTourCreator(GreedyOrienteeringTourCreator original, Cloner cloner)
70      : base(original, cloner) { }
71
72    public GreedyOrienteeringTourCreator()
73      : base() {
74      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the points."));
75      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
76      Parameters.Add(new LookupParameter<DoubleValue>("MaximumDistance", "The maximum distance constraint for a Orienteering solution."));
77      Parameters.Add(new LookupParameter<IntValue>("StartingPoint", "Index of the starting point."));
78      Parameters.Add(new LookupParameter<IntValue>("TerminalPoint", "Index of the ending point."));
79      Parameters.Add(new LookupParameter<DoubleValue>("PointVisitingCosts", "The costs for visiting a point."));
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new GreedyOrienteeringTourCreator(this, cloner);
84    }
85
86    protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) {
87      int startPoint = StartingPointParameter.ActualValue.Value;
88      int endPoint = TerminalPointParameter.ActualValue.Value;
89      int numPoints = ScoresParameter.ActualValue.Length;
90      var distances = DistanceMatrixParameter.ActualValue;
91      double pointVisitingCosts = PointVisitingCostsParameter.ActualValue.Value;
92      double maxDistance = MaximumDistanceParameter.ActualValue.Value;
93      var scores = ScoresParameter.ActualValue;
94
95      // Find all points within the maximum distance allowed (ellipse)
96      var feasiblePoints = (
97        from point in Enumerable.Range(0, numPoints)
98        let distance = distances[startPoint, point] + distances[point, endPoint] + pointVisitingCosts
99        let score = scores[point]
100        where distance <= maxDistance
101        where point != startPoint && point != endPoint
102        orderby score descending
103        select point
104      ).ToList();
105
106      // Add the starting and terminus point
107      var tour = new List<int> {
108        startPoint,
109        endPoint
110      };
111      double tourLength = distances[startPoint, endPoint];
112
113      // Add points in a greedy way
114      bool insertionPerformed = true;
115      while (insertionPerformed) {
116        insertionPerformed = false;
117
118        for (int i = 0; i < feasiblePoints.Count; i++) {
119          for (int insertPosition = 1; insertPosition < tour.Count; insertPosition++) {
120            // Create the candidate tour
121            double detour = distances.CalculateInsertionCosts(tour, insertPosition, feasiblePoints[i], pointVisitingCosts);
122
123            // If the insertion would be feasible, perform it
124            if (tourLength + detour <= maxDistance) {
125              tour.Insert(insertPosition, feasiblePoints[i]);
126              tourLength += detour;
127              feasiblePoints.RemoveAt(i);
128              insertionPerformed = true;
129              break;
130            }
131          }
132          if (insertionPerformed) break;
133        }
134      }
135
136      return new IntegerVector(tour.ToArray());
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.