Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment/3.3/Operators/LocalImprovers/ApproximateLocalSearch.cs @ 15553

Last change on this file since 15553 was 15553, checked in by abeham, 6 years ago

#1614:

  • Implementing basic algorithm according to paper (rechecking all operators)
  • Checking implementation with paper
  • Improved speed of move generator
  • Improved speed of randomized solution creator
File size: 9.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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;
33
34namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment {
35  [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.")]
36  [StorableClass]
37  public class ApproximateLocalSearch : SingleSuccessorOperator, IProblemInstanceAwareGQAPOperator,
38    IQualityAwareGQAPOperator, IGQAPLocalImprovementOperator, IAssignmentAwareGQAPOperator, IStochasticOperator {
39    public IProblem Problem { get; set; }
40    public Type ProblemType {
41      get { return typeof(GQAP); }
42    }
43
44    public ILookupParameter<GQAPInstance> ProblemInstanceParameter {
45      get { return (ILookupParameter<GQAPInstance>)Parameters["ProblemInstance"]; }
46    }
47    public ILookupParameter<IntegerVector> AssignmentParameter {
48      get { return (ILookupParameter<IntegerVector>)Parameters["Assignment"]; }
49    }
50    public ILookupParameter<DoubleValue> QualityParameter {
51      get { return (ILookupParameter<DoubleValue>)Parameters["Quality"]; }
52    }
53    public ILookupParameter<Evaluation> EvaluationParameter {
54      get { return (ILookupParameter<Evaluation>)Parameters["Evaluation"]; }
55    }
56    public IValueLookupParameter<IntValue> MaximumIterationsParameter {
57      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumIterations"]; }
58    }
59    public ILookupParameter<IntValue> EvaluatedSolutionsParameter {
60      get { return (ILookupParameter<IntValue>)Parameters["EvaluatedSolutions"]; }
61    }
62    public ILookupParameter<IRandom> RandomParameter {
63      get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
64    }
65    public IValueLookupParameter<IntValue> MaximumCandidateListSizeParameter {
66      get { return (IValueLookupParameter<IntValue>)Parameters["MaximumCandidateListSize"]; }
67    }
68    public IValueLookupParameter<PercentValue> OneMoveProbabilityParameter {
69      get { return (IValueLookupParameter<PercentValue>)Parameters["OneMoveProbability"]; }
70    }
71    public ILookupParameter<ResultCollection> ResultsParameter {
72      get { return (ILookupParameter<ResultCollection>)Parameters["Results"]; }
73    }
74
75    [StorableConstructor]
76    protected ApproximateLocalSearch(bool deserializing) : base(deserializing) { }
77    protected ApproximateLocalSearch(ApproximateLocalSearch original, Cloner cloner) : base(original, cloner) { }
78    public ApproximateLocalSearch()
79      : base() {
80      Parameters.Add(new LookupParameter<GQAPInstance>("ProblemInstance", GQAP.ProblemInstanceDescription));
81      Parameters.Add(new LookupParameter<IntegerVector>("Assignment", GQAPSolutionCreator.AssignmentDescription));
82      Parameters.Add(new LookupParameter<DoubleValue>("Quality", ""));
83      Parameters.Add(new LookupParameter<Evaluation>("Evaluation", GQAP.EvaluationDescription));
84      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumIterations", "The maximum number of iterations that should be performed."));
85      Parameters.Add(new LookupParameter<IntValue>("EvaluatedSolutions", "The number of evaluated solution equivalents."));
86      Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator to use."));
87      Parameters.Add(new ValueLookupParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));
88      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)));
89      Parameters.Add(new LookupParameter<ResultCollection>("Results", "The result collection that stores the results."));
90    }
91
92    public override IDeepCloneable Clone(Cloner cloner) {
93      return new ApproximateLocalSearch(this, cloner);
94    }
95
96    public static void Apply(IRandom random, GQAPSolution sol, int maxCLS,
97      double oneMoveProbability, int maximumIterations,
98      GQAPInstance problemInstance, out int evaluatedSolutions) {
99      var fit = problemInstance.ToSingleObjective(sol.Evaluation);
100      var eval = sol.Evaluation;
101      Apply(random, sol.Assignment, ref fit, ref eval, maxCLS, oneMoveProbability, maximumIterations, problemInstance,
102        out evaluatedSolutions);
103      sol.Evaluation = eval;
104    }
105
106      /// <summary>
107      /// The implementation differs slightly from Mateus et al. in that the maximumIterations parameter defines a cap
108      /// on the number of steps that the local search can perform. While the maxSampleSize parameter corresponds to
109      /// the maxItr parameter defined by Mateus et al.
110      /// </summary>
111      /// <param name="random">The random number generator to use.</param>
112      /// <param name="assignment">The equipment-location assignment vector.</param>
113      /// <param name="quality">The solution quality.</param>
114      /// <param name="evaluation">The evaluation result of the solution.</param>
115      /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
116      /// <param name="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
117      /// <param name="maximumIterations">The maximum number of iterations that should be performed each time the candidate list is generated.</param>
118      /// <param name="problemInstance">The problem instance that contains the data.</param>
119      /// <param name="evaluatedSolutions">The number of evaluated solutions.</param>
120      public static void Apply(IRandom random, IntegerVector assignment,
121      ref double quality, ref Evaluation evaluation, int maxCLS,
122      double oneMoveProbability, int maximumIterations,
123      GQAPInstance problemInstance, out int evaluatedSolutions) {
124      evaluatedSolutions = 0;
125      var capacities = problemInstance.Capacities;
126      var demands = problemInstance.Demands;
127      var evaluations = 0.0;
128      var deltaEvaluationFactor = 1.0 / assignment.Length;
129      while (true) {
130        int count = 0;
131        var CLS = new List<Tuple<NMove, double, Evaluation>>();
132        double sum = 0.0;
133        do {
134          NMove move;
135          if (random.NextDouble() < oneMoveProbability)
136            move = StochasticNMoveSingleMoveGenerator.GenerateOneMove(random, assignment, capacities);
137          else move = StochasticNMoveSingleMoveGenerator.GenerateTwoMove(random, assignment, capacities);
138         
139          var moveEval = GQAPNMoveEvaluator.Evaluate(move, assignment, evaluation, problemInstance);
140          evaluations += move.Indices.Count * deltaEvaluationFactor;
141          double moveQuality = problemInstance.ToSingleObjective(moveEval);
142
143          if (moveEval.ExcessDemand <= 0.0 && moveQuality < quality) {
144            CLS.Add(Tuple.Create(move, moveQuality, moveEval));
145            sum += 1.0 / moveQuality;
146          }
147          count++;
148        } while (CLS.Count < maxCLS && count < maximumIterations);
149
150        if (CLS.Count == 0) {
151          evaluatedSolutions += (int)Math.Ceiling(evaluations);
152          return; // END
153        } else {
154          var ball = random.NextDouble() * sum;
155          var selected = CLS.Last();
156          foreach (var candidate in CLS) {
157            ball -= 1.0 / candidate.Item2;
158            if (ball <= 0.0) {
159              selected = candidate;
160              break;
161            }
162          }
163          NMoveMaker.Apply(assignment, selected.Item1);
164          quality = selected.Item2;
165          evaluation = selected.Item3;
166        }
167      }
168    }
169
170    public override IOperation Apply() {
171      var evaluation = EvaluationParameter.ActualValue;
172      var quality = QualityParameter.ActualValue;
173      var fit = quality.Value;
174      var evaluatedSolutions = 0;
175
176      Apply(RandomParameter.ActualValue,
177        AssignmentParameter.ActualValue,
178        ref fit,
179        ref evaluation,
180        MaximumCandidateListSizeParameter.ActualValue.Value,
181        OneMoveProbabilityParameter.ActualValue.Value,
182        MaximumIterationsParameter.ActualValue.Value,
183        ProblemInstanceParameter.ActualValue,
184        out evaluatedSolutions);
185
186      EvaluationParameter.ActualValue = evaluation;
187      quality.Value = fit;
188      EvaluatedSolutionsParameter.ActualValue.Value += evaluatedSolutions;
189      return base.Apply();
190    }
191  }
192}
Note: See TracBrowser for help on using the repository browser.