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 |
|
---|
22 | using System;
|
---|
23 | using HeuristicLab.Common;
|
---|
24 | using HeuristicLab.Core;
|
---|
25 | using HeuristicLab.Data;
|
---|
26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using System.Threading;
|
---|
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>
|
---|
40 | [Item("PTSPExhaustiveInsertionLocalImprovement", "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 PTSPExhaustiveInsertionLocalImprovement : SingleSuccessorOperator, 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 IValueParameter<ItemList<ItemList<IntValue>>> RealizationsParameter {
|
---|
77 | get { return (IValueParameter<ItemList<ItemList<IntValue>>>)Parameters["Realizations"]; }
|
---|
78 | }
|
---|
79 |
|
---|
80 | [StorableConstructor]
|
---|
81 | protected PTSPExhaustiveInsertionLocalImprovement(bool deserializing) : base(deserializing) { }
|
---|
82 | protected PTSPExhaustiveInsertionLocalImprovement(PTSPExhaustiveInsertionLocalImprovement original, Cloner cloner)
|
---|
83 | : base(original, cloner) {
|
---|
84 | }
|
---|
85 | public PTSPExhaustiveInsertionLocalImprovement()
|
---|
86 | : base() {
|
---|
87 | Parameters.Add(new LookupParameter<Permutation>("Permutation", "The solution as permutation."));
|
---|
88 | Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
|
---|
89 | 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)));
|
---|
90 | 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)."));
|
---|
91 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
|
---|
92 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
|
---|
93 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized."));
|
---|
94 | Parameters.Add(new LookupParameter<DistanceMatrix>("DistanceMatrix", "The matrix which contains the distances between the cities."));
|
---|
95 | Parameters.Add(new ValueParameter<ItemList<ItemList<IntValue>>>("Realizations", "The concrete..."));
|
---|
96 | }
|
---|
97 |
|
---|
98 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
99 | return new PTSPExhaustiveInsertionLocalImprovement(this, cloner);
|
---|
100 | }
|
---|
101 |
|
---|
102 | public static void Improve(Permutation assignment, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation, ItemList<ItemList<IntValue>> realizations) {
|
---|
103 | DistanceMatrix distanceM = (DistanceMatrix)distances;
|
---|
104 | for (int i = localIterations.Value; i < maxIterations; i++) {
|
---|
105 | TranslocationMove bestMove = null;
|
---|
106 | double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
|
---|
107 | double evaluations = 0.0;
|
---|
108 | foreach (var move in ExhaustiveInsertionMoveGenerator.Generate(assignment)) {
|
---|
109 | double moveQuality = PTSPEstimatedInsertionEvaluator.EvaluateByDistanceMatrix(assignment, move, distanceM, realizations);
|
---|
110 | int min = Math.Min(move.Index1, move.Index3);
|
---|
111 | int max = Math.Max(move.Index2, move.Index3 + (move.Index2 - move.Index1));
|
---|
112 | evaluations += 2.0 * (max - min + 1) / assignment.Length
|
---|
113 | + 4.0 * (assignment.Length - (max - min + 1)) / assignment.Length;
|
---|
114 | if (maximization && moveQuality > bestQuality
|
---|
115 | || !maximization && moveQuality < bestQuality) {
|
---|
116 | bestQuality = moveQuality;
|
---|
117 | bestMove = move;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
|
---|
121 | if (bestMove == null) break;
|
---|
122 | TranslocationManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2, bestMove.Index3);
|
---|
123 | quality.Value += bestQuality;
|
---|
124 | localIterations.Value++;
|
---|
125 | cancellation.ThrowIfCancellationRequested();
|
---|
126 | }
|
---|
127 | }
|
---|
128 |
|
---|
129 | public override IOperation Apply() {
|
---|
130 | var maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
131 | var assignment = PermutationParameter.ActualValue;
|
---|
132 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
133 | var distances = DistanceMatrixParameter.ActualValue;
|
---|
134 | var quality = QualityParameter.ActualValue;
|
---|
135 | var localIterations = LocalIterationsParameter.ActualValue;
|
---|
136 | var evaluations = EvaluatedSolutionsParameter.ActualValue;
|
---|
137 | var realizations = RealizationsParameter.Value;
|
---|
138 | if (localIterations == null) {
|
---|
139 | localIterations = new IntValue(0);
|
---|
140 | LocalIterationsParameter.ActualValue = localIterations;
|
---|
141 | }
|
---|
142 |
|
---|
143 | Improve(assignment, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken, realizations);
|
---|
144 |
|
---|
145 | localIterations.Value = 0;
|
---|
146 | return base.Apply();
|
---|
147 | }
|
---|
148 | }
|
---|
149 | }
|
---|