Free cookie consent management tool by TermsFeed Policy Generator

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

#2521: working on TSP refactoring

File:
1 edited

Legend:

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

    r17241 r17248  
    1 using System;
     1#region License Information
     2/* HeuristicLab
     3 * Copyright (C) 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;
     23using System.Collections.Generic;
    224using System.Linq;
    325using HEAL.Attic;
     
    2547
    2648    [Storable] public IValueParameter<ITSPData> TSPDataParameter { get; private set; }
    27     [Storable] public IValueParameter<Permutation> BestKnownSolutionParameter { get; private set; }
     49    [Storable] public IValueParameter<ITSPSolution> BestKnownSolutionParameter { get; private set; }
    2850
    2951    public ITSPData TSPData {
     
    3153      set { TSPDataParameter.Value = value; }
    3254    }
    33     public Permutation BestKnownSolution {
     55    public ITSPSolution BestKnownSolution {
    3456      get { return BestKnownSolutionParameter.Value; }
    3557      set { BestKnownSolutionParameter.Value = value; }
     
    4769    public TSP() : base(new PermutationEncoding("Tour", 16, PermutationTypes.RelativeUndirected)) {
    4870      Parameters.Add(TSPDataParameter = new ValueParameter<ITSPData>("TSPData", "The main parameters of the TSP."));
    49       Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<Permutation>("BestKnownSolution", "The best known solution."));
     71      Parameters.Add(BestKnownSolutionParameter = new OptionalValueParameter<ITSPSolution>("BestKnownSolution", "The best known solution."));
    5072     
    5173      TSPData = new EuclideanTSPData() {
     
    85107        i = qualities.Select((x, index) => new { index, Fitness = x }).OrderBy(x => x.Fitness).First().index;
    86108      else i = qualities.Select((x, index) => new { index, Fitness = x }).OrderByDescending(x => x.Fitness).First().index;
     109      var solution = TSPData.GetSolution(solutions[i], qualities[i]);
    87110
    88111      if (double.IsNaN(BestKnownQuality) ||
    89112          Maximization && qualities[i] > BestKnownQuality ||
    90113          !Maximization && qualities[i] < BestKnownQuality) {
    91         BestKnownQualityParameter.ActualValue = new DoubleValue(qualities[i]);
    92         BestKnownSolutionParameter.ActualValue = (Permutation)solutions[i].Clone();
    93       }
    94 
    95       var solution = TSPData.GetSolution(solutions[i], qualities[i]);
     114        BestKnownQualityParameter.Value = new DoubleValue(qualities[i]);
     115        BestKnownSolutionParameter.Value = solution;
     116      }
    96117      results.AddOrUpdateResult("Best TSP Solution", solution);
     118    }
     119
     120    public override IEnumerable<Permutation> GetNeighbors(Permutation solution, IRandom random) {
     121      foreach (var move in ExhaustiveInversionMoveGenerator.Generate(solution)) {
     122        var clone = (Permutation)solution.Clone();
     123        InversionManipulator.Apply(clone, move.Index1, move.Index2);
     124        yield return clone;
     125      }
    97126    }
    98127
     
    141170      if (data.BestKnownTour != null) {
    142171        try {
    143           BestKnownSolution = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
    144           BestKnownQuality = Evaluate(BestKnownSolution);
     172          var tour = new Permutation(PermutationTypes.RelativeUndirected, data.BestKnownTour);
     173          var tourLength = Evaluate(tour);
     174          BestKnownSolution = new TSPSolution(data.Coordinates != null ? new DoubleMatrix(data.Coordinates) : null, tour, new DoubleValue(tourLength));
     175          BestKnownQuality = tourLength;
    145176        } catch (InvalidOperationException) {
    146177          if (data.BestKnownQuality.HasValue)
     
    170201
    171202      Operators.Add(new TSPAlleleFrequencyAnalyzer());
    172       foreach (var op in ApplicationManager.Manager.GetInstances<ITSPMoveEvaluator>())
     203      foreach (var op in ApplicationManager.Manager.GetInstances<ITSPMoveEvaluator>()) {
     204        Encoding.ConfigureOperator(op);
    173205        Operators.Add(op);
    174 
     206      }
    175207      ParameterizeOperators();
    176208    }
Note: See TracChangeset for help on using the changeset viewer.