Free cookie consent management tool by TermsFeed Policy Generator

source: branches/OaaS/HeuristicLab.Problems.QuadraticAssignment/3.3/LocalImprovement/QAPExhaustiveSwap2LocalImprovement.cs @ 9078

Last change on this file since 9078 was 7878, checked in by abeham, 13 years ago

#1722:

  • In QAPExhaustiveSwap2LocalImprovement moved local search code to a static method
  • Otherwise minor changes to the QAP
File size: 6.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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Encodings.PermutationEncoding;
27using HeuristicLab.Operators;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.QuadraticAssignment {
33  [Item("QAPExhaustiveSwap2LocalImprovement", "Takes a solution and finds the local optimum with respect to the swap2 neighborhood by decending along the steepest gradient.")]
34  [StorableClass]
35  public class QAPExhaustiveSwap2LocalImprovement : SingleSuccessorOperator, ILocalImprovementOperator {
36
37    public Type ProblemType {
38      get { return typeof(QuadraticAssignmentProblem); }
39    }
40
41    [Storable]
42    private QuadraticAssignmentProblem problem;
43    public IProblem Problem {
44      get { return problem; }
45      set { problem = (QuadraticAssignmentProblem)value; }
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> AssignmentParameter {
61      get { return (ILookupParameter<Permutation>)Parameters["Assignment"]; }
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<DoubleMatrix> WeightsParameter {
73      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
74    }
75
76    public ILookupParameter<DoubleMatrix> DistancesParameter {
77      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
78    }
79
80    [StorableConstructor]
81    protected QAPExhaustiveSwap2LocalImprovement(bool deserializing) : base(deserializing) { }
82    protected QAPExhaustiveSwap2LocalImprovement(QAPExhaustiveSwap2LocalImprovement original, Cloner cloner)
83      : base(original, cloner) {
84      this.problem = cloner.Clone(original.problem);
85    }
86    public QAPExhaustiveSwap2LocalImprovement()
87      : base() {
88      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)));
89      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)."));
90      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
91      Parameters.Add(new LookupParameter<Permutation>("Assignment", "The permutation that is to be locally optimized."));
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<DoubleMatrix>("Weights", "The weights matrix."));
95      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
96    }
97
98    public override IDeepCloneable Clone(Cloner cloner) {
99      return new QAPExhaustiveSwap2LocalImprovement(this, cloner);
100    }
101
102    public static double Improve(Permutation assignment, double quality, DoubleMatrix weights, DoubleMatrix distances, bool maximization, int maxIterations, out double evaluatedSolutions) {
103      evaluatedSolutions = 0.0;
104      double evalSolPerMove = 4.0 / assignment.Length;
105
106      for (int i = 0; i < maxIterations; i++) {
107        Swap2Move bestMove = null;
108        double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
109        foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) {
110          double moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances);
111          evaluatedSolutions += evalSolPerMove;
112          if (maximization && moveQuality > bestQuality
113            || !maximization && moveQuality < bestQuality) {
114            bestQuality = moveQuality;
115            bestMove = move;
116          }
117        }
118        if (bestMove == null) break;
119        Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
120        quality += bestQuality;
121      }
122      return quality;
123    }
124
125    public override IOperation Apply() {
126      int maxIterations = MaximumIterationsParameter.ActualValue.Value;
127      Permutation assignment = AssignmentParameter.ActualValue;
128      bool maximization = MaximizationParameter.ActualValue.Value;
129      DoubleMatrix weights = WeightsParameter.ActualValue;
130      DoubleMatrix distances = DistancesParameter.ActualValue;
131      double quality = QualityParameter.ActualValue.Value;
132
133      double evaluations;
134      QualityParameter.ActualValue.Value = Improve(assignment, quality, weights, distances, maximization, maxIterations, out evaluations);
135      EvaluatedSolutionsParameter.ActualValue.Value += (int)Math.Ceiling(evaluations);
136      return base.Apply();
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.