Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Breadcrumbs/HeuristicLab.Problems.QuadraticAssignment/3.3/LocalImprovement/QAPExhaustiveInsertionLocalImprovement.cs @ 10204

Last change on this file since 10204 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 7.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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("QAPExhaustiveInsertionLocalImprovement", "Takes a solution and finds the local optimum with respect to the insertion neighborhood by decending along the steepest gradient.")]
35  [StorableClass]
36  public class QAPExhaustiveInsertionLocalImprovement : 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 QAPExhaustiveInsertionLocalImprovement(bool deserializing) : base(deserializing) { }
87    protected QAPExhaustiveInsertionLocalImprovement(QAPExhaustiveInsertionLocalImprovement original, Cloner cloner)
88      : base(original, cloner) {
89      this.problem = cloner.Clone(original.problem);
90    }
91    public QAPExhaustiveInsertionLocalImprovement()
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 QAPExhaustiveInsertionLocalImprovement(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        TranslocationMove 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 ExhaustiveInsertionMoveGenerator.Generate(assignment)) {
114          double moveQuality = QAPTranslocationMoveEvaluator.Apply(assignment, move, weights, distances);
115          int min = Math.Min(move.Index1, move.Index3);
116          int max = Math.Max(move.Index2, move.Index3 + (move.Index2 - move.Index1));
117          evaluations += 2.0 * (max - min + 1) / assignment.Length
118            + 4.0 * (assignment.Length - (max - min + 1)) / assignment.Length;
119          if (maximization && moveQuality > bestQuality
120            || !maximization && moveQuality < bestQuality) {
121            bestQuality = moveQuality;
122            bestMove = move;
123          }
124        }
125        evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
126        if (bestMove == null) break;
127        TranslocationManipulator.Apply(assignment, bestMove.Index1, bestMove.Index2, bestMove.Index3);
128        quality.Value += bestQuality;
129        localIterations.Value++;
130        cancellation.ThrowIfCancellationRequested();
131      }
132    }
133
134    public override IOperation Apply() {
135      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
136      var assignment = AssignmentParameter.ActualValue;
137      var maximization = MaximizationParameter.ActualValue.Value;
138      var weights = WeightsParameter.ActualValue;
139      var distances = DistancesParameter.ActualValue;
140      var quality = QualityParameter.ActualValue;
141      var localIterations = LocalIterationsParameter.ActualValue;
142      var evaluations = EvaluatedSolutionsParameter.ActualValue;
143      if (localIterations == null) {
144        localIterations = new IntValue(0);
145        LocalIterationsParameter.ActualValue = localIterations;
146      }
147
148      Improve(assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, CancellationToken);
149
150      localIterations.Value = 0;
151      return base.Apply();
152    }
153  }
154}
Note: See TracBrowser for help on using the repository browser.