Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15511 was 15511, checked in by abeham, 5 years ago

#1614: Improved performance by switching from Dictionary to Array

File size: 8.9 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.Operators {
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    /// <summary>
97    /// The implementation differs slightly from Mateus et al. in that the maximumIterations parameter defines a cap
98    /// on the number of steps that the local search can perform. While the maxSampleSize parameter corresponds to
99    /// the maxItr parameter defined by Mateus et al.
100    /// </summary>
101    /// <param name="random">The random number generator to use.</param>
102    /// <param name="assignment">The equipment-location assignment vector.</param>
103    /// <param name="quality">The solution quality.</param>
104    /// <param name="evaluation">The evaluation result of the solution.</param>
105    /// <param name="maxCLS">The maximum number of candidates that should be found in each step.</param>
106    /// <param name="maximumIterations">The maximum number of iterations that should be performed each time the candidate list is generated.</param>
107    /// <param name="problemInstance">The problem instance that contains the data.</param>
108    /// <param name="evaluatedSolutions">The number of evaluated solutions.</param>
109    /// <param name="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
110    public static void Apply(IRandom random, IntegerVector assignment,
111      DoubleValue quality, ref Evaluation evaluation, IntValue maxCLS, IntValue maximumIterations,
112      GQAPInstance problemInstance, IntValue evaluatedSolutions, PercentValue oneMoveProbability) {
113      var capacities = problemInstance.Capacities;
114      var demands = problemInstance.Demands;
115      var evaluations = 0.0;
116      var deltaEvaluationFactor = 1.0 / assignment.Length;
117      while (true) {
118        int count = 0;
119        var CLS = new List<Tuple<NMove, double, Evaluation>>();
120        double sum = 0.0;
121        do {
122          NMove move;
123          if (random.NextDouble() < oneMoveProbability.Value)
124            move = StochasticNMoveSingleMoveGenerator.GenerateExactlyN(random, assignment, 1, capacities);
125          else move = StochasticNMoveSingleMoveGenerator.GenerateExactlyN(random, assignment, 2, capacities);
126         
127          var moveEval = GQAPNMoveEvaluator.Evaluate(move, assignment, evaluation, problemInstance);
128          evaluations += move.Indices.Count * deltaEvaluationFactor;
129          double moveQuality = problemInstance.ToSingleObjective(moveEval);
130
131          if (moveEval.ExcessDemand <= 0.0 && moveQuality < quality.Value) {
132            CLS.Add(Tuple.Create(move, moveQuality, moveEval));
133            sum += 1.0 / moveQuality;
134          }
135          count++;
136        } while (CLS.Count < maxCLS.Value && count < maximumIterations.Value);
137
138        if (CLS.Count == 0) {
139          evaluatedSolutions.Value += (int)Math.Ceiling(evaluations);
140          return; // END
141        } else {
142          var ball = random.NextDouble() * sum;
143          var selected = CLS.Last();
144          foreach (var candidate in CLS) {
145            ball -= 1.0 / candidate.Item2;
146            if (ball <= 0.0) {
147              selected = candidate;
148              break;
149            }
150          }
151          NMoveMaker.Apply(assignment, selected.Item1);
152          quality.Value = selected.Item2;
153          evaluation = selected.Item3;
154        }
155      }
156    }
157
158    public override IOperation Apply() {
159      var evaluation = EvaluationParameter.ActualValue;
160      Apply(RandomParameter.ActualValue,
161        AssignmentParameter.ActualValue,
162        QualityParameter.ActualValue,
163        ref evaluation,
164        MaximumCandidateListSizeParameter.ActualValue,
165        MaximumIterationsParameter.ActualValue,
166        ProblemInstanceParameter.ActualValue,
167        EvaluatedSolutionsParameter.ActualValue,
168        OneMoveProbabilityParameter.ActualValue);
169      EvaluationParameter.ActualValue = evaluation;
170      return base.Apply();
171    }
172  }
173}
Note: See TracBrowser for help on using the repository browser.