Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.PTSP/3.3/Improvers/PTSPEstimatedTwoPointFiveLocalImprovement.cs @ 14000

Last change on this file since 14000 was 13470, checked in by abeham, 9 years ago

#2221:

  • implemented review comments
    • hid rng as private class, implemented djb2 hash function (hash function implementation may also change)
    • added missing probabilities
    • base class for instance providers
    • prebuild event events
    • build platforms
    • unit test will be removed on trunk integration
    • corrected assembly file version
    • distance calculator parameter was not hidden, can be changed by user, updates distance matrix
    • fixed performance problems (ouch!) also for estimated ptsp (inlined GetDistance method)
  • added moves (full evaluation) for analytical tsp
  • added local improvement operators for analytical ptsp
  • added recalculation of distance matrix when parameters change
  • still lots of other changes
File size: 7.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.PermutationEncoding;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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>
40  [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.")]
41  [StorableClass]
42  public sealed class PTSPEstimatedTwoPointFiveLocalImprovement : SingleSuccessorOperator, IEstimatedPTSPOperator, ILocalImprovementOperator {
43
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
72    public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
73      get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
74    }
75
76    public ILookupParameter<ItemList<BoolArray>> RealizationsParameter {
77      get { return (ILookupParameter<ItemList<BoolArray>>)Parameters["Realizations"]; }
78    }
79
80    [StorableConstructor]
81    private PTSPEstimatedTwoPointFiveLocalImprovement(bool deserializing) : base(deserializing) { }
82    private PTSPEstimatedTwoPointFiveLocalImprovement(PTSPEstimatedTwoPointFiveLocalImprovement original, Cloner cloner) : base(original, cloner) { }
83    public PTSPEstimatedTwoPointFiveLocalImprovement()
84      : base() {
85      Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
86      Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
87      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)));
88      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)."));
89      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
90      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
91      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized."));
92      Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
93      Parameters.Add(new LookupParameter<ItemList<BoolArray>>("Realizations", "The list of samples drawn from all possible stochastic instances."));
94    }
95
96    public override IDeepCloneable Clone(Cloner cloner) {
97      return new PTSPEstimatedTwoPointFiveLocalImprovement(this, cloner);
98    }
99
100    public static void Improve(Permutation assignment, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, ItemList<BoolArray> realizations, CancellationToken cancellation) {
101      var distanceM = (DistanceMatrix)distances;
102      Func<int, int, double> distance = (a, b) => distanceM[a, b];
103      for (var i = localIterations.Value; i < maxIterations; i++) {
104        TwoPointFiveMove bestMove = null;
105        var bestQuality = 0.0; // we have to make an improvement, so 0 is the baseline
106        var evaluations = 0.0;
107        foreach (var move in ExhaustiveTwoPointFiveMoveGenerator.Generate(assignment)) {
108          var moveQuality = PTSPEstimatedTwoPointFiveMoveEvaluator.EvaluateMove(assignment, move, distance, realizations);
109          if (move.IsInvert) evaluations += realizations.Count * 4.0 / (assignment.Length * assignment.Length);
110          else evaluations += realizations.Count * 6.0 / (assignment.Length * assignment.Length);
111          if (maximization && moveQuality > bestQuality
112            || !maximization && moveQuality < bestQuality) {
113            bestQuality = moveQuality;
114            bestMove = move;
115          }
116        }
117        evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
118        if (bestMove == null) break;
119        TwoPointFiveMoveMaker.Apply(assignment, bestMove);
120        quality.Value += bestQuality;
121        localIterations.Value++;
122        cancellation.ThrowIfCancellationRequested();
123      }
124    }
125
126    public override IOperation Apply() {
127      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
128      var assignment = PermutationParameter.ActualValue;
129      var maximization = MaximizationParameter.ActualValue.Value;
130      var distances = DistanceMatrixParameter.ActualValue;
131      var quality = QualityParameter.ActualValue;
132      var localIterations = LocalIterationsParameter.ActualValue;
133      var evaluations = EvaluatedSolutionsParameter.ActualValue;
134      var realizations = RealizationsParameter.ActualValue;
135      if (localIterations == null) {
136        localIterations = new IntValue(0);
137        LocalIterationsParameter.ActualValue = localIterations;
138      }
139
140      Improve(assignment, distances, quality, localIterations, evaluations, maximization, maxIterations, realizations, CancellationToken);
141
142      localIterations.Value = 0;
143      return base.Apply();
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.