Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.Orienteering/3.3/Creators/GreedyOrienteeringTourCreator.cs @ 17533

Last change on this file since 17533 was 17533, checked in by abeham, 4 years ago

#2521: Unified architecture

File size: 4.8 KB
RevLine 
[11191]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[11191]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;
[17525]24using HEAL.Attic;
[11191]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Parameters;
30
31namespace HeuristicLab.Problems.Orienteering {
[12721]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.")]
[16723]41  [StorableType("FB68525D-DD53-4BE7-A6B4-EC54E6FD0E64")]
[11321]42  public sealed class GreedyOrienteeringTourCreator : IntegerVectorCreator, IOrienteeringSolutionCreator {
[11191]43    public override bool CanChangeName { get { return false; } }
44
[17525]45    public ILookupParameter<IOrienteeringProblemData> OrienteeringProblemDataParameter {
46      get { return (ILookupParameter<IOrienteeringProblemData>)Parameters["OrienteeringProblemData"]; }
[11191]47    }
48
49    [StorableConstructor]
[16723]50    private GreedyOrienteeringTourCreator(StorableConstructorFlag _) : base(_) { }
[11307]51    private GreedyOrienteeringTourCreator(GreedyOrienteeringTourCreator original, Cloner cloner)
[11191]52      : base(original, cloner) { }
53
54    public GreedyOrienteeringTourCreator()
55      : base() {
[17525]56      Parameters.Add(new LookupParameter<IOrienteeringProblemData>("OrienteeringProblemData", "The main data that comprises the orienteering problem."));
[11191]57    }
58
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new GreedyOrienteeringTourCreator(this, cloner);
61    }
62
[12721]63    protected override IntegerVector Create(IRandom random, IntValue length, IntMatrix bounds) {
[17525]64      var data = OrienteeringProblemDataParameter.ActualValue;
[11191]65
66      // Find all points within the maximum distance allowed (ellipse)
[11228]67      var feasiblePoints = (
[17533]68        from point in Enumerable.Range(0, data.Cities)
69        let travelCosts = data.GetDistance(data.StartingPoint, point)
70        + data.GetDistance(point, data.TerminalPoint) + data.PointVisitingCosts
[17525]71        let score = data.GetScore(point)
72        where travelCosts <= data.MaximumTravelCosts
73        where point != data.StartingPoint && point != data.TerminalPoint
[11228]74        orderby score descending
75        select point
76      ).ToList();
[11191]77
78      // Add the starting and terminus point
79      var tour = new List<int> {
[17525]80        data.StartingPoint,
81        data.TerminalPoint
[11191]82      };
[17533]83      double tourLength = data.GetDistance(data.StartingPoint, data.TerminalPoint);
[11191]84
85      // Add points in a greedy way
86      bool insertionPerformed = true;
87      while (insertionPerformed) {
88        insertionPerformed = false;
89
[11228]90        for (int i = 0; i < feasiblePoints.Count; i++) {
[11192]91          for (int insertPosition = 1; insertPosition < tour.Count; insertPosition++) {
[11191]92            // Create the candidate tour
[17525]93            double detour = OrienteeringProblem.CalculateInsertionCosts(data, tour, insertPosition, feasiblePoints[i]);
[11191]94
95            // If the insertion would be feasible, perform it
[17525]96            if (tourLength + detour <= data.MaximumTravelCosts) {
[11228]97              tour.Insert(insertPosition, feasiblePoints[i]);
[12721]98              tourLength += detour;
[11228]99              feasiblePoints.RemoveAt(i);
[11191]100              insertionPerformed = true;
101              break;
102            }
103          }
104          if (insertionPerformed) break;
105        }
106      }
107
108      return new IntegerVector(tour.ToArray());
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.