source: branches/2936_GQAPIntegration/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/LocalImprovers/ApproximateLocalSearch.cs @ 16077

Last change on this file since 16077 was 16077, checked in by abeham, 12 months ago

#2936: Added integration branch

File size: 10.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Operators;
30using HeuristicLab.Optimization;
31using HeuristicLab.Parameters;
32using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
33using HeuristicLab.Random;
34
35namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
36  /// <summary>
37  /// This is an implementation of the algorithm described in Mateus, G.R., Resende, M.G.C. & Silva, R.M.A. J Heuristics (2011) 17: 527. https://doi.org/10.1007/s10732-010-9144-0
38  /// </summary>
39  [Item("ApproximateLocalSearch", "The approximate local search is described in Mateus, G., Resende, M., and Silva, R. 2011. GRASP with path-relinking for the generalized quadratic assignment problem. Journal of Heuristics 17, Springer Netherlands, pp. 527-565.")]
40  [StorableClass]
41  public class ApproximateLocalSearch : SingleSuccessorOperator, IProblemInstanceAwareGQAPOperator,
42    IQualityAwareGQAPOperator, IGQAPLocalImprovementOperator, IAssignmentAwareGQAPOperator, IStochasticOperator {
43    public IProblem Problem { get; set; }
44    public Type ProblemType {
45      get { return typeof(GQAP); }
46    }
47
48    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
49      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
50    }
51    public ILookupParameter<IntegerVector> AssignmentParameter {
52      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
53    }
54    public ILookupParameter<DoubleValue> QualityParameter {
55      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
56    }
57    public ILookupParameter<Evaluation> EvaluationParameter {
58      get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; }
59    }
60    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
61      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
62    }
63    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
64      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
65    }
66    public ILookupParameter<IRandom> RandomParameter {
67      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
68    }
69    public IValueLookupParameter<IntValue> MaximumCandidateListSizeParameter {
70      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumCandidateListSize"]; }
71    }
72    public IValueLookupParameter<PercentValue> OneMoveProbabilityParameter {
73      get { return (IValueLookupParameter<PercentValue>)Parameters["OneMoveProbability"]; }
74    }
75    public ILookupParameter<ResultCollection> ResultsParameter {
76      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
77    }
78    public IValueLookupParameter<BoolValue> GreedyParameter {
79      get { return (IValueLookupParameter<BoolValue>)Parameters["Greedy"]; }
80    }
81
82    [StorableConstructor]
83    protected ApproximateLocalSearch(bool deserializing) : base(deserializing) { }
84    protected ApproximateLocalSearch(ApproximateLocalSearch original, Cloner cloner) : base(original, cloner) { }
85    public ApproximateLocalSearch()
86      : base() {
87      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
88      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
89      Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
90      Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription));
91      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations that should be performed."));
92      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents."));
93      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
94      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));
95      Parameters.Add(new ValueLookupParameter<PercentValue>("OneMoveProbability", "The probability for performing a 1-move, which is the opposite of performing a 2-move.", new PercentValue(.5)));
96      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results."));
97      Parameters.Add(new ValueLookupParameter<BoolValue>("Greedy", "Whether to use a greedy selection strategy or a probabilistic one.", new BoolValue(true)));
98    }
99
100    public override IDeepCloneable Clone(Cloner cloner) {
101      return new ApproximateLocalSearch(this, cloner);
102    }
103
104    public static void Apply(IRandom random, GQAPSolution sol, int maxCLS,
105      double oneMoveProbability, int maximumIterations,
106      GQAPInstance problemInstance, out int evaluatedSolutions, bool greedy = true) {
107      var fit = problemInstance.ToSingleObjective(sol.Evaluation);
108      var eval = sol.Evaluation;
109      Apply(random, sol.Assignment, ref fit, ref eval, maxCLS, oneMoveProbability, maximumIterations, problemInstance,
110        out evaluatedSolutions, greedy);
111      sol.Evaluation = eval;
112    }
113
114    /// <summary>
115    /// </summary>
116    /// <param name="random">The random number generator to use.</param>
117    /// <param name="assignment">The equipment-location assignment vector.</param>
118    /// <param name="quality">The solution quality.</param>
119    /// <param name="evaluation">The evaluation result of the solution.</param>
120    /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
121    /// <param name="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
122    /// <param name="maximumIterations">The maximum number of iterations that should be performed each time the candidate list is generated.</param>
123    /// <param name="problemInstance">The problem instance that contains the data.</param>
124    /// <param name="evaluatedSolutions">The number of evaluated solutions.</param>
125    /// <param name="greedy">Greedy selection performed better in 5 of 8 instances according to the paper</param>
126    public static void Apply(IRandom random, IntegerVector assignment,
127      ref double quality, ref Evaluation evaluation, int maxCLS,
128      double oneMoveProbability, int maximumIterations,
129      GQAPInstance problemInstance, out int evaluatedSolutions, bool greedy = true) {
130      evaluatedSolutions = 0;
131      var capacities = problemInstance.Capacities;
132      var demands = problemInstance.Demands;
133      var evaluations = 0.0;
134      var deltaEvaluationFactor = 1.0 / assignment.Length;
135      while (true) { // line 1 of Algorithm 3
136        var count = 0; // line 2 of Algorithm 3
137        var CLS = new List<Tuple<NMove, double, Evaluation>>(); // line 3 of Algorithm 3
138        do {
139          var move = Move(random, assignment, oneMoveProbability, capacities); // line 4 of Algorithm 3
140
141          var moveEval = GQAPNMoveEvaluator.Evaluate(move, assignment, evaluation, problemInstance);
142          evaluations += move.Indices.Count * deltaEvaluationFactor;
143          double moveQuality = problemInstance.ToSingleObjective(moveEval);
144
145          if (moveEval.ExcessDemand <= 0.0 && moveQuality < quality) { // line 5 of Algorithm 3
146            CLS.Add(Tuple.Create(move, moveQuality, moveEval)); // line 6 of Algorithm 3
147          }
148          count++; // line 8 of Algorithm 3
149        } while (CLS.Count < maxCLS && count < maximumIterations); // line 9 of Algorithm 3
150
151        if (CLS.Count == 0) { // line 10 of Algorithm 3
152          evaluatedSolutions += (int)Math.Ceiling(evaluations);
153          return; // END
154        } else {
155          // line 11 of Algorithm 3
156          Tuple<NMove, double, Evaluation> selected;
157          if (greedy) {
158            selected = CLS.MinItems(x => x.Item2).Shuffle(random).First();
159          } else {
160            selected = CLS.SampleProportional(random, 1, CLS.Select(x => 1.0 / x.Item2), false, false).Single();
161          }
162          NMoveMaker.Apply(assignment, selected.Item1);
163          quality = selected.Item2;
164          evaluation = selected.Item3;
165        }
166      }
167    }
168
169    private static NMove Move(IRandom random, IntegerVector assignment, double oneMoveProbability, DoubleArray capacities) {
170      if (random.NextDouble() < oneMoveProbability)
171        return StochasticNMoveSingleMoveGenerator.GenerateOneMove(random, assignment, capacities);
172      return StochasticNMoveSingleMoveGenerator.GenerateTwoMove(random, assignment, capacities);
173    }
174
175    public override IOperation Apply() {
176      var evaluation = EvaluationParameter.ActualValue;
177      var quality = QualityParameter.ActualValue;
178      var fit = quality.Value;
179      var evaluatedSolutions = 0;
180
181      Apply(RandomParameter.ActualValue,
182        AssignmentParameter.ActualValue,
183        ref fit,
184        ref evaluation,
185        MaximumCandidateListSizeParameter.ActualValue.Value,
186        OneMoveProbabilityParameter.ActualValue.Value,
187        MaximumIterationsParameter.ActualValue.Value,
188        ProblemInstanceParameter.ActualValue,
189        out evaluatedSolutions,
190        GreedyParameter.ActualValue.Value);
191
192      EvaluationParameter.ActualValue = evaluation;
193      quality.Value = fit;
194      EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
195      return base.Apply();
196    }
197  }
198}
Note: See TracBrowser for help on using the repository browser.