1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2012 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
34 | [Item("QAPExhaustiveInversionLocalImprovement", "Takes a solution and finds the local optimum with respect to the inversion neighborhood by decending along the steepest gradient.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class QAPExhaustiveInversionLocalImprovement : SingleSuccessorOperator, ILocalImprovementOperator {
|
---|
37 |
|
---|
38 | public Type ProblemType {
|
---|
39 | get { return typeof(QuadraticAssignmentProblem); }
|
---|
40 | }
|
---|
41 |
|
---|
42 | [Storable]
|
---|
43 | private QuadraticAssignmentProblem problem;
|
---|
44 | public IProblem Problem {
|
---|
45 | get { return problem; }
|
---|
46 | set { problem = (QuadraticAssignmentProblem)value; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public ILookupParameter<IntValue> LocalIterationsParameter {
|
---|
50 | get { return (ILookupParameter<IntValue>)Parameters["LocalIterations"]; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
54 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
55 | }
|
---|
56 |
|
---|
57 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
58 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
59 | }
|
---|
60 |
|
---|
61 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
62 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
63 | }
|
---|
64 |
|
---|
65 | public ILookupParameter<Permutation> AssignmentParameter {
|
---|
66 | get { return (ILookupParameter<Permutation>)Parameters["Assignment"]; }
|
---|
67 | }
|
---|
68 |
|
---|
69 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
70 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
71 | }
|
---|
72 |
|
---|
73 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
74 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
75 | }
|
---|
76 |
|
---|
77 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
78 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
79 | }
|
---|
80 |
|
---|
81 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
82 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
83 | }
|
---|
84 |
|
---|
85 | [StorableConstructor]
|
---|
86 | protected QAPExhaustiveInversionLocalImprovement(bool deserializing) : base(deserializing) { }
|
---|
87 | protected QAPExhaustiveInversionLocalImprovement(QAPExhaustiveInversionLocalImprovement original, Cloner cloner)
|
---|
88 | : base(original, cloner) {
|
---|
89 | this.problem = cloner.Clone(original.problem);
|
---|
90 | }
|
---|
91 | public QAPExhaustiveInversionLocalImprovement()
|
---|
92 | : base() {
|
---|
93 | Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
|
---|
94 | 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)));
|
---|
95 | 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)."));
|
---|
96 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
|
---|
97 | Parameters.Add(new LookupParameter<Permutation>("Assignment", "The permutation that is to be locally optimized."));
|
---|
98 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
|
---|
99 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized."));
|
---|
100 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
|
---|
101 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
|
---|
102 | }
|
---|
103 |
|
---|
104 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
105 | return new QAPExhaustiveInversionLocalImprovement(this, cloner);
|
---|
106 | }
|
---|
107 |
|
---|
108 | public static void Improve(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) {
|
---|
109 | for (int i = localIterations.Value; i < maxIterations; i++) {
|
---|
110 | InversionMove bestMove = null;
|
---|
111 | double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
|
---|
112 | double evaluations = 0.0;
|
---|
113 | foreach (var move in ExhaustiveInversionMoveGenerator.Generate(assignment)) {
|
---|
114 | double moveQuality = QAPInversionMoveEvaluator.Apply(assignment, move, weights, distances);
|
---|
115 | evaluations += 2 * (move.Index2 - move.Index1 + 1) / (double)assignment.Length;
|
---|
116 | if (maximization && moveQuality > bestQuality
|
---|
117 | || !maximization && moveQuality < bestQuality) {
|
---|
118 | bestQuality = moveQuality;
|
---|
119 | bestMove = move;
|
---|
120 | }
|
---|
121 | }
|
---|
122 | evaluatedSolutions.Value = (int)Math.Ceiling(evaluations);
|
---|
123 | if (bestMove == null) break;
|
---|
124 | InversionManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
|
---|
125 | quality.Value += bestQuality;
|
---|
126 | localIterations.Value++;
|
---|
127 | cancellation.ThrowIfCancellationRequested();
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | public override IOperation Apply() {
|
---|
132 | var maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
133 | var assignment = AssignmentParameter.ActualValue;
|
---|
134 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
135 | var weights = WeightsParameter.ActualValue;
|
---|
136 | var distances = DistancesParameter.ActualValue;
|
---|
137 | var quality = QualityParameter.ActualValue;
|
---|
138 | var localIterations = LocalIterationsParameter.ActualValue;
|
---|
139 | var evaluations = EvaluatedSolutionsParameter.ActualValue;
|
---|
140 | if (localIterations == null) {
|
---|
141 | localIterations = new IntValue(0);
|
---|
142 | LocalIterationsParameter.ActualValue = localIterations;
|
---|
143 | }
|
---|
144 |
|
---|
145 | Improve(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
|
---|
146 |
|
---|
147 | localIterations.Value = 0;
|
---|
148 | return base.Apply();
|
---|
149 | }
|
---|
150 | }
|
---|
151 | }
|
---|