Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2520_PersistenceReintegration/HeuristicLab.Problems.Orienteering/3.3/Creators/GreedyOrienteeringTourCreator.cs @ 16462

Last change on this file since 16462 was 16462, checked in by jkarder, 5 years ago

#2520: worked on reintegration of new persistence

  • added nuget references to HEAL.Fossil
  • added StorableType attributes to many classes
  • changed signature of StorableConstructors
  • removed some classes in old persistence
  • removed some unnecessary usings
File size: 6.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HEAL.Fossil;
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  [StorableType("FB68525D-DD53-4BE7-A6B4-EC54E6FD0E64")]
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(StorableConstructorFlag _) : base(_) { }
68    private GreedyOrienteeringTourCreator(GreedyOrienteeringTourCreator original, Cloner cloner)
69      : base(original, cloner) { }
70
71    public GreedyOrienteeringTourCreator()
72      : base() {
73      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the points."));
74      Parameters.Add(new LookupParameter<DoubleArray>("Scores", "The scores of the points."));
75      Parameters.Add(new LookupParameter<DoubleValue>("MaximumDistance", "The maximum distance constraint for a Orienteering solution."));
76      Parameters.Add(new LookupParameter<IntValue>("StartingPoint", "Index of the starting point."));
77      Parameters.Add(new LookupParameter<IntValue>("TerminalPoint", "Index of the ending point."));
78      Parameters.Add(new LookupParameter<DoubleValue>("PointVisitingCosts", "The costs for visiting a point."));
79    }
80
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new GreedyOrienteeringTourCreator(this, cloner);
83    }
84
85    protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) {
86      int startPoint = StartingPointParameter.ActualValue.Value;
87      int endPoint = TerminalPointParameter.ActualValue.Value;
88      int numPoints = ScoresParameter.ActualValue.Length;
89      var distances = DistanceMatrixParameter.ActualValue;
90      double pointVisitingCosts = PointVisitingCostsParameter.ActualValue.Value;
91      double maxDistance = MaximumDistanceParameter.ActualValue.Value;
92      var scores = ScoresParameter.ActualValue;
93
94      // Find all points within the maximum distance allowed (ellipse)
95      var feasiblePoints = (
96        from point in Enumerable.Range(0, numPoints)
97        let distance = distances[startPoint, point] + distances[point, endPoint] + pointVisitingCosts
98        let score = scores[point]
99        where distance <= maxDistance
100        where point != startPoint && point != endPoint
101        orderby score descending
102        select point
103      ).ToList();
104
105      // Add the starting and terminus point
106      var tour = new List<int> {
107        startPoint,
108        endPoint
109      };
110      double tourLength = distances[startPoint, endPoint];
111
112      // Add points in a greedy way
113      bool insertionPerformed = true;
114      while (insertionPerformed) {
115        insertionPerformed = false;
116
117        for (int i = 0; i < feasiblePoints.Count; i++) {
118          for (int insertPosition = 1; insertPosition < tour.Count; insertPosition++) {
119            // Create the candidate tour
120            double detour = distances.CalculateInsertionCosts(tour, insertPosition, feasiblePoints[i], pointVisitingCosts);
121
122            // If the insertion would be feasible, perform it
123            if (tourLength + detour <= maxDistance) {
124              tour.Insert(insertPosition, feasiblePoints[i]);
125              tourLength += detour;
126              feasiblePoints.RemoveAt(i);
127              insertionPerformed = true;
128              break;
129            }
130          }
131          if (insertionPerformed) break;
132        }
133      }
134
135      return new IntegerVector(tour.ToArray());
136    }
137  }
138}
Note: See TracBrowser for help on using the repository browser.