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 |
|
---|
22 | using System;
|
---|
23 | using System.Threading;
|
---|
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;
|
---|
31 | using HEAL.Attic;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
34 | [Item("QAPExhaustiveInsertionLocalImprovement", "Takes a solution and finds the local optimum with respect to the insertion neighborhood by decending along the steepest gradient.")]
|
---|
35 | [StorableType("9E024205-A638-4671-B212-511E941B5992")]
|
---|
36 | public class QAPExhaustiveInsertionLocalImprovement : SingleSuccessorOperator, ILocalImprovementOperator, ISingleObjectiveOperator {
|
---|
37 |
|
---|
38 | public ILookupParameter<IntValue> LocalIterationsParameter {
|
---|
39 | get { return (ILookupParameter<IntValue>)Parameters["LocalIterations"]; }
|
---|
40 | }
|
---|
41 |
|
---|
42 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
43 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
44 | }
|
---|
45 |
|
---|
46 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
47 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
48 | }
|
---|
49 |
|
---|
50 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
51 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
52 | }
|
---|
53 |
|
---|
54 | public ILookupParameter<Permutation> AssignmentParameter {
|
---|
55 | get { return (ILookupParameter<Permutation>)Parameters["Assignment"]; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
59 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
60 | }
|
---|
61 |
|
---|
62 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
63 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
64 | }
|
---|
65 |
|
---|
66 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
67 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
68 | }
|
---|
69 |
|
---|
70 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
71 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
72 | }
|
---|
73 |
|
---|
74 | [StorableConstructor]
|
---|
75 | protected QAPExhaustiveInsertionLocalImprovement(StorableConstructorFlag _) : base(_) { }
|
---|
76 | protected QAPExhaustiveInsertionLocalImprovement(QAPExhaustiveInsertionLocalImprovement original, Cloner cloner)
|
---|
77 | : base(original, cloner) {
|
---|
78 | }
|
---|
79 | public QAPExhaustiveInsertionLocalImprovement()
|
---|
80 | : base() {
|
---|
81 | Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
|
---|
82 | 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)));
|
---|
83 | 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)."));
|
---|
84 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
|
---|
85 | Parameters.Add(new LookupParameter<Permutation>("Assignment", "The permutation that is to be locally optimized."));
|
---|
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."));
|
---|
88 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
|
---|
89 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
|
---|
90 | }
|
---|
91 |
|
---|
92 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
93 | return new QAPExhaustiveInsertionLocalImprovement(this, cloner);
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static void Improve(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) {
|
---|
97 | for (int i = localIterations.Value; i < maxIterations; i++) {
|
---|
98 | TranslocationMove bestMove = null;
|
---|
99 | double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
|
---|
100 | double evaluations = 0.0;
|
---|
101 | foreach (var move in ExhaustiveInsertionMoveGenerator.Generate(assignment)) {
|
---|
102 | double moveQuality = QAPTranslocationMoveEvaluator.Apply(assignment, move, weights, distances);
|
---|
103 | int min = Math.Min(move.Index1, move.Index3);
|
---|
104 | int max = Math.Max(move.Index2, move.Index3 + (move.Index2 - move.Index1));
|
---|
105 | evaluations += 2.0 * (max - min + 1) / assignment.Length
|
---|
106 | + 4.0 * (assignment.Length - (max - min + 1)) / assignment.Length;
|
---|
107 | if (maximization && moveQuality > bestQuality
|
---|
108 | || !maximization && moveQuality < bestQuality) {
|
---|
109 | bestQuality = moveQuality;
|
---|
110 | bestMove = move;
|
---|
111 | }
|
---|
112 | }
|
---|
113 | evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
|
---|
114 | if (bestMove == null) break;
|
---|
115 | TranslocationManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2, bestMove.Index3);
|
---|
116 | quality.Value += bestQuality;
|
---|
117 | localIterations.Value++;
|
---|
118 | cancellation.ThrowIfCancellationRequested();
|
---|
119 | }
|
---|
120 | }
|
---|
121 |
|
---|
122 | public override IOperation Apply() {
|
---|
123 | var maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
124 | var assignment = AssignmentParameter.ActualValue;
|
---|
125 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
126 | var weights = WeightsParameter.ActualValue;
|
---|
127 | var distances = DistancesParameter.ActualValue;
|
---|
128 | var quality = QualityParameter.ActualValue;
|
---|
129 | var localIterations = LocalIterationsParameter.ActualValue;
|
---|
130 | var evaluations = EvaluatedSolutionsParameter.ActualValue;
|
---|
131 | if (localIterations == null) {
|
---|
132 | localIterations = new IntValue(0);
|
---|
133 | LocalIterationsParameter.ActualValue = localIterations;
|
---|
134 | }
|
---|
135 |
|
---|
136 | Improve(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
|
---|
137 |
|
---|
138 | localIterations.Value = 0;
|
---|
139 | return base.Apply();
|
---|
140 | }
|
---|
141 | }
|
---|
142 | }
|
---|