Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.QuadraticAssignment/3.3/LocalImprovement/QAPStochasticScrambleLocalImprovement.cs @ 17362

Last change on this file since 17362 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 7.6 KB
RevLine 
[8338]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) 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
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;
[16565]31using HEAL.Attic;
[8338]32
33namespace 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.")]
[16565]35  [StorableType("045B5151-E5DC-4AF3-8CAD-E160E0EE17FF")]
[11970]36  public class QAPStochasticScrambleLocalImprovement : SingleSuccessorOperator, ILocalImprovementOperator, IStochasticOperator, ISingleObjectiveOperator {
[8338]37
38    public ILookupParameter<IntValue> LocalIterationsParameter {
39      get { return (ILookupParameter<IntValue>)Parameters["LocalIterations"]; }
40    }
41
42    public ILookupParameter<IRandom> RandomParameter {
43      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
44    }
45
46    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
47      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
48    }
49
50    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
51      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
52    }
53
54    public ILookupParameter<ResultCollection> ResultsParameter {
55      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
56    }
57
58    public ILookupParameter<Permutation> AssignmentParameter {
59      get { return (ILookupParameter<Permutation>)Parameters["Assignment"]; }
60    }
61
62    public ILookupParameter<DoubleValue> QualityParameter {
63      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
64    }
65
66    public ILookupParameter<BoolValue> MaximizationParameter {
67      get { return (ILookupParameter<BoolValue>)Parameters["Maximization"]; }
68    }
69
70    public ILookupParameter<DoubleMatrix> WeightsParameter {
71      get { return (ILookupParameter<DoubleMatrix>)Parameters["Weights"]; }
72    }
73
74    public ILookupParameter<DoubleMatrix> DistancesParameter {
75      get { return (ILookupParameter<DoubleMatrix>)Parameters["Distances"]; }
76    }
77
78    public IValueLookupParameter<IntValue> NeighborhoodSizeParameter {
79      get { return (IValueLookupParameter<IntValue>)Parameters["NeighborhoodSize"]; }
80    }
81
82    [StorableConstructor]
[16565]83    protected QAPStochasticScrambleLocalImprovement(StorableConstructorFlag _) : base(_) { }
[8338]84    protected QAPStochasticScrambleLocalImprovement(QAPStochasticScrambleLocalImprovement original, Cloner cloner)
85      : base(original, cloner) {
86    }
87    public QAPStochasticScrambleLocalImprovement()
88      : base() {
89      Parameters.Add(new LookupParameter<IntValue>("LocalIterations", "The number of iterations that have already been performed."));
90      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
91      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)));
92      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)."));
93      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The collection where to store results."));
94      Parameters.Add(new LookupParameter<Permutation>("Assignment", "The permutation that is to be locally optimized."));
95      Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality value of the assignment."));
96      Parameters.Add(new LookupParameter<BoolValue>("Maximization", "True if the problem should be maximized or minimized."));
97      Parameters.Add(new LookupParameter<DoubleMatrix>("Weights", "The weights matrix."));
98      Parameters.Add(new LookupParameter<DoubleMatrix>("Distances", "The distances matrix."));
99      Parameters.Add(new ValueLookupParameter<IntValue>("NeighborhoodSize", "The number of moves to sample from the neighborhood.", new IntValue(100)));
100    }
101
102    public override IDeepCloneable Clone(Cloner cloner) {
103      return new QAPStochasticScrambleLocalImprovement(this, cloner);
104    }
105
106    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) {
107      for (int i = localIterations.Value; i < maxIterations; i++) {
108        ScrambleMove bestMove = null;
109        double bestQuality = 0; // we have to make an improvement, so 0 is the baseline
110        double evaluations = 0.0;
111        for (int j = 0; j < neighborhoodSize; j++) {
112          var move = StochasticScrambleMultiMoveGenerator.GenerateRandomMove(assignment, random);
113          double moveQuality = QAPScrambleMoveEvaluator.Apply(assignment, move, weights, distances);
114          evaluations += 2.0 * move.ScrambledIndices.Length / assignment.Length;
115          if (maximization && moveQuality > bestQuality
116            || !maximization && moveQuality < bestQuality) {
117            bestQuality = moveQuality;
118            bestMove = move;
119          }
120        }
[12811]121        evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
[8338]122        if (bestMove == null) break;
123        ScrambleManipulator.Apply(assignment, bestMove.StartIndex, bestMove.ScrambledIndices);
124        quality.Value += bestQuality;
125        localIterations.Value++;
126        cancellation.ThrowIfCancellationRequested();
127      }
128    }
129
130    public override IOperation Apply() {
131      var random = RandomParameter.ActualValue;
132      var maxIterations = MaximumIterationsParameter.ActualValue.Value;
133      var neighborhoodSize = NeighborhoodSizeParameter.ActualValue.Value;
134      var assignment = AssignmentParameter.ActualValue;
135      var maximization = MaximizationParameter.ActualValue.Value;
136      var weights = WeightsParameter.ActualValue;
137      var distances = DistancesParameter.ActualValue;
138      var quality = QualityParameter.ActualValue;
139      var localIterations = LocalIterationsParameter.ActualValue;
140      var evaluations = EvaluatedSolutionsParameter.ActualValue;
141      if (localIterations == null) {
142        localIterations = new IntValue(0);
143        LocalIterationsParameter.ActualValue = localIterations;
144      }
145
146      Improve(random, assignment, weights, distances, quality, localIterations, evaluations, maximization, maxIterations, neighborhoodSize, CancellationToken);
[8847]147
148      localIterations.Value = 0;
[8338]149      return base.Apply();
150    }
151  }
152}
Note: See TracBrowser for help on using the repository browser.