[6086] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 HeuristicLab.Common;
|
---|
| 24 | using HeuristicLab.Core;
|
---|
| 25 | using HeuristicLab.Data;
|
---|
| 26 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace 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 override IOperation Apply() {
|
---|
| 103 | int maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
| 104 | Permutation assignment = AssignmentParameter.ActualValue;
|
---|
| 105 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 106 | DoubleMatrix weights = WeightsParameter.ActualValue;
|
---|
| 107 | DoubleMatrix distances = DistancesParameter.ActualValue;
|
---|
| 108 |
|
---|
| 109 | double evaluatedSolutions = 0.0;
|
---|
| 110 | double evalSolPerMove = 4.0 / assignment.Length;
|
---|
| 111 |
|
---|
| 112 | for (int i = 0; i < maxIterations; i++) {
|
---|
| 113 | Swap2Move bestMove = null;
|
---|
| 114 | double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
|
---|
| 115 | foreach (Swap2Move move in ExhaustiveSwap2MoveGenerator.Generate(assignment)) {
|
---|
| 116 | double moveQuality = QAPSwap2MoveEvaluator.Apply(assignment, move, weights, distances);
|
---|
| 117 | evaluatedSolutions += evalSolPerMove;
|
---|
| 118 | if (maximization && moveQuality > bestQuality
|
---|
| 119 | || !maximization && moveQuality < bestQuality) {
|
---|
| 120 | bestQuality = moveQuality;
|
---|
| 121 | bestMove = move;
|
---|
| 122 | }
|
---|
| 123 | }
|
---|
| 124 | if (bestMove == null) break;
|
---|
| 125 | Swap2Manipulator.Apply(assignment, bestMove.Index1, bestMove.Index2);
|
---|
| 126 | QualityParameter.ActualValue.Value += bestQuality;
|
---|
| 127 | }
|
---|
| 128 | EvaluatedSolutionsParameter.ActualValue.Value += (int)Math.Ceiling(evaluatedSolutions);
|
---|
| 129 | return base.Apply();
|
---|
| 130 | }
|
---|
| 131 | }
|
---|
| 132 | }
|
---|