[8338] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[11170] | 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Threading;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Encodings.PermutationEncoding;
|
---|
| 28 | using HeuristicLab.Operators;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Problems.QuadraticAssignment {
|
---|
| 34 | [Item("QAPStochasticScrambleLocalImprovement", "Takes a solution and finds the local optimum with respect to the scramble neighborhood by decending along the steepest gradient.")]
|
---|
| 35 | [StorableClass]
|
---|
| 36 | public class QAPStochasticScrambleLocalImprovement : SingleSuccessorOperator, ILocalImprovementOperator, IStochasticOperator {
|
---|
| 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 ILookupParameter<IRandom> RandomParameter {
|
---|
| 54 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | public IValueLookupParameter<IntValue> MaximumIterationsParameter {
|
---|
| 58 | get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
|
---|
| 62 | get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | public ILookupParameter<ResultCollection> ResultsParameter {
|
---|
| 66 | get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
| 69 | public ILookupParameter<Permutation> AssignmentParameter {
|
---|
| 70 | get { return (ILookupParameter<Permutation>)Parameters["Assignment"]; }
|
---|
| 71 | }
|
---|
| 72 |
|
---|
| 73 | public ILookupParameter<DoubleValue> QualityParameter {
|
---|
| 74 | get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | public ILookupParameter<BoolValue> MaximizationParameter {
|
---|
| 78 | get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | public ILookupParameter<DoubleMatrix> WeightsParameter {
|
---|
| 82 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
|
---|
| 83 | }
|
---|
| 84 |
|
---|
| 85 | public ILookupParameter<DoubleMatrix> DistancesParameter {
|
---|
| 86 | get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | public IValueLookupParameter<IntValue> NeighborhoodSizeParameter {
|
---|
| 90 | get { return (IValueLookupParameter<IntValue>)Parameters["NeighborhoodSize"]; }
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | [StorableConstructor]
|
---|
| 94 | protected QAPStochasticScrambleLocalImprovement(bool deserializing) : base(deserializing) { }
|
---|
| 95 | protected QAPStochasticScrambleLocalImprovement(QAPStochasticScrambleLocalImprovement original, Cloner cloner)
|
---|
| 96 | : base(original, cloner) {
|
---|
| 97 | this.problem = cloner.Clone(original.problem);
|
---|
| 98 | }
|
---|
| 99 | public QAPStochasticScrambleLocalImprovement()
|
---|
| 100 | : base() {
|
---|
| 101 | Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
|
---|
| 102 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
|
---|
| 103 | 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)));
|
---|
| 104 | 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)."));
|
---|
| 105 | Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
|
---|
| 106 | Parameters.Add(new LookupParameter<Permutation>("Assignment", "The permutation that is to be locally optimized."));
|
---|
| 107 | Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
|
---|
| 108 | Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized."));
|
---|
| 109 | Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
|
---|
| 110 | Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
|
---|
| 111 | Parameters.Add(new ValueLookupParameter<IntValue>("NeighborhoodSize", "The number of moves to sample from the neighborhood.", new IntValue(100)));
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 115 | return new QAPStochasticScrambleLocalImprovement(this, cloner);
|
---|
| 116 | }
|
---|
| 117 |
|
---|
| 118 | public static void Improve(IRandom random, Permutation assignment, DoubleMatrix weights, DoubleMatrix distances, DoubleValue quality, IntValue localIterations, IntValue evaluatedSolutions, bool maximization, int maxIterations, int neighborhoodSize, CancellationToken cancellation) {
|
---|
| 119 | for (int i = localIterations.Value; i < maxIterations; i++) {
|
---|
| 120 | ScrambleMove bestMove = null;
|
---|
| 121 | double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
|
---|
| 122 | double evaluations = 0.0;
|
---|
| 123 | for (int j = 0; j < neighborhoodSize; j++) {
|
---|
| 124 | var move = StochasticScrambleMultiMoveGenerator.GenerateRandomMove(assignment, random);
|
---|
| 125 | double moveQuality = QAPScrambleMoveEvaluator.Apply(assignment, move, weights, distances);
|
---|
| 126 | evaluations += 2.0 * move.ScrambledIndices.Length / assignment.Length;
|
---|
| 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 | ScrambleManipulator.Apply(assignment, bestMove.StartIndex, bestMove.ScrambledIndices);
|
---|
| 136 | quality.Value += bestQuality;
|
---|
| 137 | localIterations.Value++;
|
---|
| 138 | cancellation.ThrowIfCancellationRequested();
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
| 141 |
|
---|
| 142 | public override IOperation Apply() {
|
---|
| 143 | var random = RandomParameter.ActualValue;
|
---|
| 144 | var maxIterations = MaximumIterationsParameter.ActualValue.Value;
|
---|
| 145 | var neighborhoodSize = NeighborhoodSizeParameter.ActualValue.Value;
|
---|
| 146 | var assignment = AssignmentParameter.ActualValue;
|
---|
| 147 | var maximization = MaximizationParameter.ActualValue.Value;
|
---|
| 148 | var weights = WeightsParameter.ActualValue;
|
---|
| 149 | var distances = DistancesParameter.ActualValue;
|
---|
| 150 | var quality = QualityParameter.ActualValue;
|
---|
| 151 | var localIterations = LocalIterationsParameter.ActualValue;
|
---|
| 152 | var evaluations = EvaluatedSolutionsParameter.ActualValue;
|
---|
| 153 | if (localIterations == null) {
|
---|
| 154 | localIterations = new IntValue(0);
|
---|
| 155 | LocalIterationsParameter.ActualValue = localIterations;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
| 158 | Improve(random, assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, neighborhoodSize, CancellationToken);
|
---|
[8847] | 159 |
|
---|
| 160 | localIterations.Value = 0;
|
---|
[8338] | 161 | return base.Apply();
|
---|
| 162 | }
|
---|
| 163 | }
|
---|
| 164 | }
|
---|