Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Problems.Orienteering/HeuristicLab.Problems.Orienteering/3.3/Creators/GreedyOrienteeringTourCreator.cs @ 11228

Last change on this file since 11228 was 11228, checked in by pfleck, 10 years ago

#2208:

  • Fixed bugs in cost calculation of insertion and replacement
  • Rewritten Cleanup of infeasible tours
  • Small refactoring
File size: 6.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2014 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  [Item("GreedyOrienteeringTourCreator", @"The initial solution for P-VNS is generated by means of a greedy algorithm that takes into
33account all vertices vi that are located within the cost limit Tmax. These points are sorted
34in descending order regarding the sum of their objective values. Afterwards, the algorithm
35starts with a tour only including the starting and ending point and successively inserts the
36points from this list at the first position in which they can feasibly be inserted. (Schilde et. al. 2009)")]
37  [StorableClass]
38  public class GreedyOrienteeringTourCreator : IntegerVectorCreator {
39    public override bool CanChangeName { get { return false; } }
40
41    #region Parameter Properties
42    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
43      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
44    }
45    public ILookupParameter<DoubleArray> ScoresParameter {
46      get { return (ILookupParameter<DoubleArray>)Parameters["Scores"]; }
47    }
48    public ILookupParameter<DoubleValue> MaximumDistanceParameter {
49      get { return (ILookupParameter<DoubleValue>)Parameters["MaximumDistance"]; }
50    }
51    public ILookupParameter<IntValue> StartingPointParameter {
52      get { return (ILookupParameter<IntValue>)Parameters["StartingPoint"]; }
53    }
54    public ILookupParameter<IntValue> TerminusPointParameter {
55      get { return (ILookupParameter<IntValue>)Parameters["TerminusPoint"]; }
56    }
57    public ILookupParameter<DoubleValue> FixedPenaltyParameter {
58      get { return (ILookupParameter<DoubleValue>)Parameters["FixedPenalty"]; }
59    }
60    #endregion
61
62    [StorableConstructor]
63    protected GreedyOrienteeringTourCreator(bool deserializing)
64      : base(deserializing) { }
65    protected GreedyOrienteeringTourCreator(GreedyOrienteeringTourCreator original, Cloner cloner)
66      : base(original, cloner) { }
67
68    public GreedyOrienteeringTourCreator()
69      : base() {
70      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the points."));
71      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
72      Parameters.Add(new LookupParameter<DoubleValue>("MaximumDistance", "The maximum distance constraint for a Orienteering solution."));
73      Parameters.Add(new LookupParameter<IntValue>("StartingPoint", "Index of the starting point."));
74      Parameters.Add(new LookupParameter<IntValue>("TerminusPoint", "Index of the ending point."));
75      Parameters.Add(new LookupParameter<DoubleValue>("FixedPenalty", "The penalty for each visited vertex."));
76    }
77
78    public override IDeepCloneable Clone(Cloner cloner) {
79      return new GreedyOrienteeringTourCreator(this, cloner);
80    }
81
82    protected override IntegerVector Create(IRandom _, IntValue __, IntMatrix ___) {
83      int startPoint = StartingPointParameter.ActualValue.Value;
84      int endPoint = TerminusPointParameter.ActualValue.Value;
85      int numPoints = ScoresParameter.ActualValue.Length;
86      var distances = DistanceMatrixParameter.ActualValue;
87      double fixedPenalty = FixedPenaltyParameter.ActualValue.Value;
88      double maxDistance = MaximumDistanceParameter.ActualValue.Value;
89      var scores = ScoresParameter.ActualValue;
90
91      // Find all points within the maximum distance allowed (ellipse)
92      var feasiblePoints = (
93        from point in Enumerable.Range(0, numPoints)
94        let distance = distances[startPoint, point] + distances[point, endPoint] + fixedPenalty
95        let score = scores[point]
96        where distance <= maxDistance
97        where point != startPoint && point != endPoint
98        orderby score descending
99        select point
100      ).ToList();
101
102      // Add the starting and terminus point
103      var tour = new List<int> {
104        startPoint,
105        endPoint
106      };
107      double length = distances[startPoint, endPoint];
108
109      // Add points in a greedy way
110      bool insertionPerformed = true;
111      while (insertionPerformed) {
112        insertionPerformed = false;
113
114        for (int i = 0; i < feasiblePoints.Count; i++) {
115          for (int insertPosition = 1; insertPosition < tour.Count; insertPosition++) {
116            // Create the candidate tour
117            double detour = distances.CalculateInsertionCosts(tour, insertPosition, feasiblePoints[i], fixedPenalty);
118
119            // If the insertion would be feasible, perform it
120            if (length + detour <= maxDistance) {
121              tour.Insert(insertPosition, feasiblePoints[i]);
122              length += detour;
123              feasiblePoints.RemoveAt(i);
124              insertionPerformed = true;
125              break;
126            }
127          }
128          if (insertionPerformed) break;
129        }
130      }
131
132      return new IntegerVector(tour.ToArray());
133    }
134  }
135}
Note: See TracBrowser for help on using the repository browser.