[12228] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 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 |
|
---|
| 22 | using System;
|
---|
[13412] | 23 | using System.Threading;
|
---|
[12228] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
[16565] | 31 | using HEAL.Attic;
|
---|
[12228] | 32 |
|
---|
| 33 | namespace 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>
|
---|
[13470] | 40 | [Item("PTSP Analytical 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.")]
|
---|
[16565] | 41 | [StorableType("C139639F-D6DD-4756-8482-24F12A78C8E9")]
|
---|
[13470] | 42 | public sealed class PTSPAnalyticalTwoPointFiveLocalImprovement : SingleSuccessorOperator, IAnalyticalPTSPOperator, ILocalImprovementOperator {
|
---|
[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 |
|
---|
| 72 | public ILookupParameter<DistanceMatrix> DistanceMatrixParameter {
|
---|
| 73 | get { return (ILookupParameter<DistanceMatrix>)Parameters["DistanceMatrix"]; }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[13470] | 76 | public ILookupParameter<DoubleArray> ProbabilitiesParameter {
|
---|
| 77 | get { return (ILookupParameter<DoubleArray>)Parameters["Probabilities"]; }
|
---|
[12228] | 78 | }
|
---|
| 79 |
|
---|
| 80 | [StorableConstructor]
|
---|
[16565] | 81 | private PTSPAnalyticalTwoPointFiveLocalImprovement(StorableConstructorFlag _) : base(_) { }
|
---|
[13470] | 82 | private PTSPAnalyticalTwoPointFiveLocalImprovement(PTSPAnalyticalTwoPointFiveLocalImprovement original, Cloner cloner) : base(original, cloner) { }
|
---|
| 83 | public PTSPAnalyticalTwoPointFiveLocalImprovement()
|
---|
[12228] | 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."));
|
---|
[13470] | 93 | Parameters.Add(new LookupParameter<DoubleArray>("Probabilities", "The list of probabilities of the cities to appear."));
|
---|
[12228] | 94 | }
|
---|
| 95 |
|
---|
| 96 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[13470] | 97 | return new PTSPAnalyticalTwoPointFiveLocalImprovement(this, cloner);
|
---|
[12228] | 98 | }
|
---|
| 99 |
|
---|
[13470] | 100 | public static void Improve(Permutation assignment, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, DoubleArray probabilities, CancellationToken cancellation) {
|
---|
[13412] | 101 | var distanceM = (DistanceMatrix)distances;
|
---|
[13470] | 102 | Func<int, int, double> distance = (a, b) => distanceM[a, b];
|
---|
[13412] | 103 | for (var i = localIterations.Value; i < maxIterations; i++) {
|
---|
[13470] | 104 | TwoPointFiveMove bestMove = null;
|
---|
| 105 | var bestQuality = quality.Value; // we have to make an improvement, so current quality is the baseline
|
---|
| 106 | var evaluations = 0.0;
|
---|
| 107 | foreach (var move in ExhaustiveTwoPointFiveMoveGenerator.Generate(assignment)) {
|
---|
| 108 | var moveQuality = PTSPAnalyticalTwoPointFiveMoveEvaluator.EvaluateMove(assignment, move, distance, probabilities);
|
---|
| 109 | evaluations++;
|
---|
[12228] | 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;
|
---|
[13470] | 118 | TwoPointFiveMoveMaker.Apply(assignment, bestMove);
|
---|
| 119 | quality.Value = bestQuality;
|
---|
[12228] | 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 distances = DistanceMatrixParameter.ActualValue;
|
---|
| 130 | var quality = QualityParameter.ActualValue;
|
---|
| 131 | var localIterations = LocalIterationsParameter.ActualValue;
|
---|
| 132 | var evaluations = EvaluatedSolutionsParameter.ActualValue;
|
---|
[13470] | 133 | var probabilities = ProbabilitiesParameter.ActualValue;
|
---|
[12228] | 134 | if (localIterations == null) {
|
---|
| 135 | localIterations = new IntValue(0);
|
---|
| 136 | LocalIterationsParameter.ActualValue = localIterations;
|
---|
| 137 | }
|
---|
| 138 |
|
---|
[13470] | 139 | Improve(assignment, distances, quality, localIterations, evaluations, maximization, maxIterations, probabilities, CancellationToken);
|
---|
[12228] | 140 |
|
---|
| 141 | localIterations.Value = 0;
|
---|
| 142 | return base.Apply();
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | }
|
---|