Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/10/19 21:55:35 (5 years ago)
Author:
abeham
Message:

#2521: worked on refactoring TSP

Location:
branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPMultipleGuidesPathRelinker.cs

    r17226 r17241  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    2930using HeuristicLab.Optimization.Operators;
    3031using HeuristicLab.Parameters;
    31 using HEAL.Attic;
    3232
    3333namespace HeuristicLab.Problems.TravelingSalesman {
     
    3939  /// </remarks>
    4040  [Item("TSPMultipleGuidesPathRelinker", "An operator that relinks paths between traveling salesman solutions using a multiple guiding strategy. The operator incrementally changes the initiating solution towards the guiding solution by correcting edges as needed. For each city it choses the best edge from all guiding solutions.")]
    41   [StorableType("6B5B2622-AB1D-47E6-8BBC-C6088D393149")]
     41  [StorableType("13e879fd-7621-402e-9345-7dbfdd78e3b6")]
    4242  public sealed class TSPMultipleGuidesPathRelinker : SingleObjectivePathRelinker {
    43     #region Parameter properties
    44     public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
    45       get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
    46     }
    47     #endregion
    48 
    49     #region Properties
    50     public DistanceMatrix DistanceMatrix {
    51       get { return DistanceMatrixParameter.ActualValue; }
    52     }
    53     #endregion
     43    [Storable] public ILookupParameter<ITSPData> TSPDataParameter { get; private set; }
    5444
    5545    [StorableConstructor]
    5646    private TSPMultipleGuidesPathRelinker(StorableConstructorFlag _) : base(_) { }
    57     private TSPMultipleGuidesPathRelinker(TSPMultipleGuidesPathRelinker original, Cloner cloner) : base(original, cloner) { }
     47    private TSPMultipleGuidesPathRelinker(TSPMultipleGuidesPathRelinker original, Cloner cloner)
     48      : base(original, cloner) {
     49      TSPDataParameter = cloner.Clone(original.TSPDataParameter);
     50    }
    5851    public TSPMultipleGuidesPathRelinker()
    5952      : base() {
    60       #region Create parameters
    61       Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
    62       #endregion
     53      Parameters.Add(TSPDataParameter = new LookupParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
    6354    }
    6455
     
    6758    }
    6859
    69     public static ItemArray<IItem> Apply(IItem initiator, IItem[] guides, DistanceMatrix distances, PercentValue n) {
     60    public static ItemArray<IItem> Apply(IItem initiator, IItem[] guides, ITSPData tspData, PercentValue n) {
    7061      if (!(initiator is Permutation) || guides.Any(x => !(x is Permutation)))
    7162        throw new ArgumentException("Cannot relink path because some of the provided solutions have the wrong type.");
     
    8475        int currCityIndex = i;
    8576        int bestCityIndex = (i + 1) % v1.Length;
    86         double currDistance = distances[v1[currCityIndex], v1[bestCityIndex]];
     77        double currDistance = tspData.GetDistance(v1[currCityIndex], v1[bestCityIndex]);
    8778        // check each guiding solution
    8879        targets.ToList().ForEach(solution => {
     
    9283          int succ = solution[(node.Index + 1) % solution.Length];
    9384          // get distances to neighbors
    94           var results = new[] { pred, succ }.Select(x => new { Id = x, Distance = distances[x, node.Id] });
     85          var results = new[] { pred, succ }.Select(x => new { Id = x, Distance = tspData.GetDistance(x, node.Id) });
    9586          var bestCity = results.Where(x => x.Distance < currDistance).OrderBy(x => x.Distance).FirstOrDefault();
    9687          if (bestCity != null) {
     
    129120      if (parents.Length < 2)
    130121        throw new ArgumentException("The number of parents is smaller than 2.");
    131       return Apply(parents[0], parents.Skip(1).ToArray(), DistanceMatrix, n);
     122      return Apply(parents[0], parents.Skip(1).ToArray(), TSPDataParameter.ActualValue, n);
    132123    }
    133124  }
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPPathRelinker.cs

    r17226 r17241  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    2829using HeuristicLab.Encodings.PermutationEncoding;
    2930using HeuristicLab.Optimization.Operators;
    30 using HEAL.Attic;
    3131
    3232namespace HeuristicLab.Problems.TravelingSalesman {
     33
    3334  /// <summary>
    3435  /// An operator that relinks paths between traveling salesman solutions.
     
    3839  /// </remarks>
    3940  [Item("TSPPathRelinker", "An operator that relinks paths between traveling salesman solutions. The operator incrementally assimilates the initiating solution into the guiding solution by correcting edges as needed.")]
    40   [StorableType("0997A24C-0592-4CE4-A681-70C97890D3F7")]
     41  [StorableType("a2c5de50-a050-4269-b80e-1c995eb51626")]
    4142  public sealed class TSPPathRelinker : SingleObjectivePathRelinker {
    4243    [StorableConstructor]
  • branches/2521_ProblemRefactoring/HeuristicLab.Problems.TravelingSalesman/3.3/PathRelinkers/TSPSimultaneousPathRelinker.cs

    r17226 r17241  
    2323using System.Collections.Generic;
    2424using System.Linq;
     25using HEAL.Attic;
    2526using HeuristicLab.Common;
    2627using HeuristicLab.Core;
     
    2829using HeuristicLab.Encodings.PermutationEncoding;
    2930using HeuristicLab.Optimization.Operators;
    30 using HEAL.Attic;
    3131
    3232namespace HeuristicLab.Problems.TravelingSalesman {
     
    3838  /// </remarks>
    3939  [Item("TSPSimultaneousPathRelinker", "An operator that relinks paths between traveling salesman solutions starting from both ends. The operator incrementally assimilates the initiating solution into the guiding solution and vice versa by correcting edges as needed.")]
    40   [StorableType("D44D112E-5139-4848-8A2B-66CFC5784EE4")]
     40  [StorableType("73457ec0-1d71-4d9f-b646-2ba3639317e2")]
    4141  public sealed class TSPSimultaneousPathRelinker : SingleObjectivePathRelinker {
    4242    [StorableConstructor]
Note: See TracChangeset for help on using the changeset viewer.