Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/Improvers/PTSPAnalyticalInversionLocalImprovement.cs

Last change on this file was 17264, checked in by abeham, 5 years ago

#2521: Finished refactoring pTSP

File size: 6.8 KB
RevLine 
[12228]1#region License Information
2/* HeuristicLab
[17226]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12228]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;
[13412]23using System.Threading;
[17253]24using HEAL.Attic;
[12228]25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.PermutationEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32
33namespace HeuristicLab.Problems.PTSP {
34  /// <summary>
35  /// An operator that improves probabilistic traveling salesman solutions.
36  /// </summary>
37  /// <remarks>
38  /// The operator tries to improve the probabilistic traveling salesman solution by swapping two randomly chosen edges for a certain number of times.
39  /// </remarks>
[17260]40  [Item("pTSP Analytical Inversion Local Improvement", "An operator that improves probabilistic traveling salesman solutions. The operator tries to improve the probabilistic traveling salesman solution by swapping two randomly chosen edges for a certain number of times.")]
[17253]41  [StorableType("88147d6a-e843-4f65-8058-421453b5206a")]
[17264]42  public sealed class PTSPAnalyticalInversionLocalImprovement : SingleSuccessorOperator, IAnalyticalPTSPOperator, IPermutationLocalImprovementOperator {
[13412]43
[12228]44    public ILookupParameter<IntValue> LocalIterationsParameter {
45      get { return (ILookupParameter<IntValue>)Parameters["LocalIterations"]; }
46    }
47
48    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
49      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
50    }
51
52    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
53      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
54    }
55
56    public ILookupParameter<ResultCollection> ResultsParameter {
57      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
58    }
59
60    public ILookupParameter<Permutation> PermutationParameter {
61      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
62    }
63
64    public ILookupParameter<DoubleValue> QualityParameter {
65      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
66    }
67
68    public ILookupParameter<BoolValue> MaximizationParameter {
69      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
70    }
71
[17253]72    public ILookupParameter<IProbabilisticTSPData> ProbabilisticTSPDataParameter {
73      get { return (ILookupParameter<IProbabilisticTSPData>)Parameters["PTSP Data"]; }
[12228]74    }
75
76    [StorableConstructor]
[16723]77    private PTSPAnalyticalInversionLocalImprovement(StorableConstructorFlag _) : base(_) { }
[13470]78    private PTSPAnalyticalInversionLocalImprovement(PTSPAnalyticalInversionLocalImprovement original, Cloner cloner) : base(original, cloner) { }
79    public PTSPAnalyticalInversionLocalImprovement()
[12228]80      : base() {
81      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
82      Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
83      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum amount of iterations that should be performed (note that this operator will abort earlier when a local optimum is reached).", new IntValue(10000)));
84      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The amount of evaluated solutions (here a move is counted only as 4/n evaluated solutions with n being the length of the permutation)."));
85      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
86      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
87      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized."));
[17260]88      Parameters.Add(new LookupParameter<IProbabilisticTSPData>("PTSP Data", "The main parameters of the pTSP."));
[12228]89    }
90
91    public override IDeepCloneable Clone(Cloner cloner) {
[13470]92      return new PTSPAnalyticalInversionLocalImprovement(this, cloner);
[12228]93    }
94
[17253]95    public static void Improve(Permutation assignment, IProbabilisticTSPData data, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) {
[13412]96      for (var i = localIterations.Value; i < maxIterations; i++) {
[12228]97        InversionMove bestMove = null;
[13470]98        var bestQuality = quality.Value; // we have to make an improvement, so current quality is the baseline
99        var evaluations = 0.0;
[12228]100        foreach (var move in ExhaustiveInversionMoveGenerator.Generate(assignment)) {
[17253]101          var moveQuality = PTSPAnalyticalInversionMoveEvaluator.EvaluateMove(assignment, move, data);
[13470]102          evaluations++;
[12228]103          if (maximization && moveQuality > bestQuality
104            || !maximization && moveQuality < bestQuality) {
105            bestQuality = moveQuality;
106            bestMove = move;
107          }
108        }
109        evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
110        if (bestMove == null) break;
111        InversionManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
[13470]112        quality.Value = bestQuality;
[12228]113        localIterations.Value++;
114        cancellation.ThrowIfCancellationRequested();
115      }
116    }
117
118    public override IOperation Apply() {
119      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
120      var assignment = PermutationParameter.ActualValue;
121      var maximization = MaximizationParameter.ActualValue.Value;
122      var quality = QualityParameter.ActualValue;
123      var localIterations = LocalIterationsParameter.ActualValue;
124      var evaluations = EvaluatedSolutionsParameter.ActualValue;
[17253]125      var data = ProbabilisticTSPDataParameter.ActualValue;
[12228]126      if (localIterations == null) {
127        localIterations = new IntValue(0);
128        LocalIterationsParameter.ActualValue = localIterations;
129      }
130
[17253]131      Improve(assignment, data, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
[12228]132
133      localIterations.Value = 0;
134      return base.Apply();
135    }
136  }
137}
Note: See TracBrowser for help on using the repository browser.