Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2521_ProblemRefactoring/HeuristicLab.Problems.PTSP/3.3/Improvers/PTSPEstimatedTwoPointFiveLocalImprovement.cs @ 17253

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

#2521: worked on refactoring PTSP

File size: 7.5 KB
Line 
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;
24using System.Linq;
25using System.Threading;
26using HEAL.Attic;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Data;
30using HeuristicLab.Encodings.PermutationEncoding;
31using HeuristicLab.Operators;
32using HeuristicLab.Optimization;
33using HeuristicLab.Parameters;
34
35namespace HeuristicLab.Problems.PTSP {
36  /// <summary>
37  /// An operator that improves probabilistic traveling salesman solutions.
38  /// </summary>
39  /// <remarks>
40  /// The operator tries to improve the probabilistic traveling salesman solution by swapping two randomly chosen edges for a certain number of times.
41  /// </remarks>
42  [Item("PTSP Estimated 2.5 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.")]
43  [StorableType("8B04265A-50AD-4FAD-99F8-2357D6F10CC3")]
44  public sealed class PTSPEstimatedTwoPointFiveLocalImprovement : SingleSuccessorOperator, IEstimatedPTSPOperator, ILocalImprovementOperator {
45
46    public ILookupParameter<IntValue> LocalIterationsParameter {
47      get { return (ILookupParameter<IntValue>)Parameters["LocalIterations"]; }
48    }
49
50    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
51      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
52    }
53
54    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
55      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
56    }
57
58    public ILookupParameter<ResultCollection> ResultsParameter {
59      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
60    }
61
62    public ILookupParameter<Permutation> PermutationParameter {
63      get { return (ILookupParameter<Permutation>)Parameters["Permutation"]; }
64    }
65
66    public ILookupParameter<DoubleValue> QualityParameter {
67      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
68    }
69
70    public ILookupParameter<BoolValue> MaximizationParameter {
71      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
72    }
73
74    public ILookupParameter<IProbabilisticTSPData> ProbabilisticTSPDataParameter {
75      get { return (ILookupParameter<IProbabilisticTSPData>)Parameters["PTSP Data"]; }
76    }
77    public ILookupParameter<ReadOnlyItemList<BoolArray>> RealizationsParameter {
78      get { return (ILookupParameter<ReadOnlyItemList<BoolArray>>)Parameters["Realizations"]; }
79    }
80
81    [StorableConstructor]
82    private PTSPEstimatedTwoPointFiveLocalImprovement(StorableConstructorFlag _) : base(_) { }
83    private PTSPEstimatedTwoPointFiveLocalImprovement(PTSPEstimatedTwoPointFiveLocalImprovement original, Cloner cloner) : base(original, cloner) { }
84    public PTSPEstimatedTwoPointFiveLocalImprovement()
85      : base() {
86      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
87      Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
88      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)));
89      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)."));
90      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
91      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
92      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized."));
93      Parameters.Add(new LookupParameter<IProbabilisticTSPData>("PTSP Data", "The main parameters of the p-TSP."));
94      Parameters.Add(new LookupParameter<ReadOnlyItemList<BoolArray>>("Realizations", "The list of samples drawn from all possible stochastic instances."));
95    }
96
97    public override IDeepCloneable Clone(Cloner cloner) {
98      return new PTSPEstimatedTwoPointFiveLocalImprovement(this, cloner);
99    }
100
101    public static void Improve(Permutation assignment, IProbabilisticTSPData data, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, IEnumerable<BoolArray> realizations, CancellationToken cancellation) {
102      for (var i = localIterations.Value; i < maxIterations; i++) {
103        TwoPointFiveMove bestMove = null;
104        var bestQuality = 0.0; // we have to make an improvement, so 0 is the baseline
105        var evaluations = 0.0;
106        foreach (var move in ExhaustiveTwoPointFiveMoveGenerator.Generate(assignment)) {
107          var moveQuality = PTSPEstimatedTwoPointFiveMoveEvaluator.EvaluateMove(assignment, move, data, realizations);
108          if (move.IsInvert) evaluations += realizations.Count() * 4.0 / (assignment.Length * assignment.Length);
109          else evaluations += realizations.Count() * 6.0 / (assignment.Length * assignment.Length);
110          if (maximization && moveQuality > bestQuality
111            || !maximization && moveQuality < bestQuality) {
112            bestQuality = moveQuality;
113            bestMove = move;
114          }
115        }
116        evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
117        if (bestMove == null) break;
118        TwoPointFiveMoveMaker.Apply(assignment, bestMove);
119        quality.Value += bestQuality;
120        localIterations.Value++;
121        cancellation.ThrowIfCancellationRequested();
122      }
123    }
124
125    public override IOperation Apply() {
126      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
127      var assignment = PermutationParameter.ActualValue;
128      var maximization = MaximizationParameter.ActualValue.Value;
129      var quality = QualityParameter.ActualValue;
130      var localIterations = LocalIterationsParameter.ActualValue;
131      var evaluations = EvaluatedSolutionsParameter.ActualValue;
132      var data = ProbabilisticTSPDataParameter.ActualValue;
133      var realizations = RealizationsParameter.ActualValue;
134      if (localIterations == null) {
135        localIterations = new IntValue(0);
136        LocalIterationsParameter.ActualValue = localIterations;
137      }
138
139      Improve(assignment, data, quality, localIterations, evaluations, maximization, maxIterations, realizations, CancellationToken);
140
141      localIterations.Value = 0;
142      return base.Apply();
143    }
144  }
145}
Note: See TracBrowser for help on using the repository browser.