Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/LocalImprovement/QAPExhaustiveInversionLocalImprovement.cs @ 11704

Last change on this file since 11704 was 11300, checked in by pfleck, 10 years ago

#2232
Introduced ILocalImprovementAlgorithmOperator to separate single operators for local improvement and operator graphs/algorithms for local improvement.
This way the ILocalImprovementOperator does not have to specify a problem type and does not store a problem any more.

The LocalSearchImprovementOperator and SimulatedAnnealingImprovementOperator implement the new ILocalImprovementAlgorithmOperator as they represent an operator graph for local improvement.
The QAP and VRP local improvement operators implement the ILocalImprovementOperator which does not store a problem anymore.

File size: 6.8 KB
RevLine 
[8338]1#region License Information
2/* HeuristicLab
[11171]3 * Copyright (C) 2002-2014 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8338]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.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 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 QAPExhaustiveInversionLocalImprovement(bool deserializing) : base(deserializing) { }
76    protected QAPExhaustiveInversionLocalImprovement(QAPExhaustiveInversionLocalImprovement original, Cloner cloner)
77      : base(original, cloner) {
78    }
79    public QAPExhaustiveInversionLocalImprovement()
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 QAPExhaustiveInversionLocalImprovement(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        InversionMove 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 ExhaustiveInversionMoveGenerator.Generate(assignment)) {
102          double moveQuality = QAPInversionMoveEvaluator.Apply(assignment, move, weights, distances);
103          evaluations += 2 * (move.Index2 - move.Index1 + 1) / (double)assignment.Length;
104          if (maximization && moveQuality > bestQuality
105            || !maximization && moveQuality < bestQuality) {
106            bestQuality = moveQuality;
107            bestMove = move;
108          }
109        }
110        evaluatedSolutions.Value = (int)Math.Ceiling(evaluations);
111        if (bestMove == null) break;
112        InversionManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
113        quality.Value += bestQuality;
114        localIterations.Value++;
115        cancellation.ThrowIfCancellationRequested();
116      }
117    }
118
119    public override IOperation Apply() {
120      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
121      var assignment = AssignmentParameter.ActualValue;
122      var maximization = MaximizationParameter.ActualValue.Value;
123      var weights = WeightsParameter.ActualValue;
124      var distances = DistancesParameter.ActualValue;
125      var quality = QualityParameter.ActualValue;
[8847]126      var localIterations = LocalIterationsParameter.ActualValue;
[8338]127      var evaluations = EvaluatedSolutionsParameter.ActualValue;
[8847]128      if (localIterations == null) {
129        localIterations = new IntValue(0);
130        LocalIterationsParameter.ActualValue = localIterations;
[8338]131      }
132
[8847]133      Improve(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
134
135      localIterations.Value = 0;
[8338]136      return base.Apply();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.