Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.QuadraticAssignment/3.3/LocalImprovement/QAPExhaustiveSwap2LocalImprovement.cs @ 8338

Last change on this file since 8338 was 8338, checked in by abeham, 12 years ago

#1904: Added additional local improvement operators

File size: 7.4 KB
Line 
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
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("QAPExhaustiveSwap2LocalImprovement", "Takes a solution and finds the local optimum with respect to the swap2 neighborhood by decending along the steepest gradient.")]
35  [StorableClass]
36  public class QAPExhaustiveSwap2LocalImprovement : 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 QAPExhaustiveSwap2LocalImprovement(bool deserializing) : base(deserializing) { }
87    protected QAPExhaustiveSwap2LocalImprovement(QAPExhaustiveSwap2LocalImprovement original, Cloner cloner)
88      : base(original, cloner) {
89      this.problem = cloner.Clone(original.problem);
90    }
91    public QAPExhaustiveSwap2LocalImprovement()
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 QAPExhaustiveSwap2LocalImprovement(this, cloner);
106    }
107
108    // BackwardsCompatibility3.3
109    #region Backwards compatible code, remove with 3.4
110    [StorableHook(HookType.AfterDeserialization)]
111    private void AfterDeserialization() {
112      if (!Parameters.ContainsKey("LocalIterations"))
113        Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
114    }
115    #endregion
116
117    public static void Improve(Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, CancellationToken cancellation) {
118      double evalSolPerMove = 4.0 / assignment.Length;
119
120      for (int i = localIterations.Value; i < maxIterations; i++) {
121        Swap2Move bestMove = null;
122        double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
123        double evaluations = 0.0;
124        foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) {
125          double moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances);
126          evaluations += evalSolPerMove;
127          if (maximization && moveQuality > bestQuality
128            || !maximization && moveQuality < bestQuality) {
129            bestQuality = moveQuality;
130            bestMove = move;
131          }
132        }
133        evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
134        if (bestMove == null) break;
135        Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
136        quality.Value += bestQuality;
137        localIterations.Value++;
138        cancellation.ThrowIfCancellationRequested();
139      }
140    }
141
142    public override IOperation Apply() {
143      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
144      var assignment = AssignmentParameter.ActualValue;
145      var maximization = MaximizationParameter.ActualValue.Value;
146      var weights = WeightsParameter.ActualValue;
147      var distances = DistancesParameter.ActualValue;
148      var quality = QualityParameter.ActualValue;
149      var localIterations = LocalIterationsParameter.ActualValue;
150      var evaluations = EvaluatedSolutionsParameter.ActualValue;
151      if (localIterations == null) {
152        localIterations = new IntValue(0);
153        LocalIterationsParameter.ActualValue = localIterations;
154      }
155
156      Improve(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
157      return base.Apply();
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.