Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15506 was 15506, checked in by abeham, 7 years ago

#1614: finished wiring

File size: 8.6 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="oneMoveProbability">The probability for performing a 1-move, which is the opposite of performing a 2-move.</param>
109    public static void Apply(IRandom random, IntegerVector assignment,
110      DoubleValue quality, ref Evaluation evaluation, IntValue maxCLS, IntValue maximumIterations,
111      GQAPInstance problemInstance, PercentValue oneMoveProbability) {
112      var capacities = problemInstance.Capacities;
113      var demands = problemInstance.Demands;
114      //var weights = problemInstance.Weights;
115      //var distances = problemInstance.Distances;
116      //var installationCosts = problemInstance.InstallationCosts;
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          double moveQuality = problemInstance.ToSingleObjective(moveEval);
129
130          if (moveEval.ExcessDemand <= 0.0 && moveQuality < 0.0) {
131            CLS.Add(Tuple.Create(move, moveQuality, moveEval));
132            sum += 1.0 / moveQuality;
133          }
134          count++;
135        } while (CLS.Count < maxCLS.Value && count < maximumIterations.Value);
136
137        if (CLS.Count == 0)
138          return; // END
139        else {
140          var ball = random.NextDouble() * sum;
141          var selected = CLS.Last();
142          foreach (var candidate in CLS) {
143            ball -= 1.0 / candidate.Item2;
144            if (ball <= 0.0) {
145              selected = candidate;
146              break;
147            }
148          }
149          NMoveMaker.Apply(assignment, selected.Item1);
150          quality.Value += selected.Item2;
151          evaluation = selected.Item3;
152        }
153      }
154    }
155
156    public override IOperation Apply() {
157      var evaluation = EvaluationParameter.ActualValue;
158      Apply(RandomParameter.ActualValue,
159        AssignmentParameter.ActualValue,
160        QualityParameter.ActualValue,
161        ref evaluation,
162        MaximumCandidateListSizeParameter.ActualValue,
163        MaximumIterationsParameter.ActualValue,
164        ProblemInstanceParameter.ActualValue,
165        OneMoveProbabilityParameter.ActualValue);
166      EvaluationParameter.ActualValue = evaluation;
167      return base.Apply();
168    }
169  }
170}
Note: See TracBrowser for help on using the repository browser.