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 |
|
---|
22 | using System;
|
---|
23 | using System.Linq;
|
---|
24 | using System.Threading;
|
---|
25 | using HeuristicLab.Analysis;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Encodings.IntegerVectorEncoding;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using HeuristicLab.Parameters;
|
---|
32 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
33 | using HeuristicLab.Random;
|
---|
34 |
|
---|
35 | namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.GRASP {
|
---|
36 |
|
---|
37 | /// <summary>
|
---|
38 | /// 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
|
---|
39 | /// </summary>
|
---|
40 | [Item("GRASP+PR (GQAP)", "The algorithm implements the Greedy Randomized Adaptive Search Procedure (GRASP) with Path Relinking as 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.")]
|
---|
41 | [Creatable(CreatableAttribute.Categories.PopulationBasedAlgorithms)]
|
---|
42 | [StorableClass]
|
---|
43 | public class GRASP : BasicAlgorithm {
|
---|
44 |
|
---|
45 | public override bool SupportsPause {
|
---|
46 | get { return true; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override Type ProblemType {
|
---|
50 | get { return typeof(GQAP); }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public new GQAP Problem {
|
---|
54 | get { return (GQAP)base.Problem; }
|
---|
55 | set { base.Problem = value; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [Storable]
|
---|
59 | private ValueParameter<IAnalyzer> analyzerParameter;
|
---|
60 | public IValueParameter<IAnalyzer> AnalyzerParameter {
|
---|
61 | get { return analyzerParameter; }
|
---|
62 | }
|
---|
63 |
|
---|
64 | [Storable]
|
---|
65 | private FixedValueParameter<BoolValue> setSeedRandomlyParameter;
|
---|
66 | private IFixedValueParameter<BoolValue> SetSeedRandomlyParameter {
|
---|
67 | get { return setSeedRandomlyParameter; }
|
---|
68 | }
|
---|
69 | [Storable]
|
---|
70 | private FixedValueParameter<IntValue> seedParameter;
|
---|
71 | private IFixedValueParameter<IntValue> SeedParameter {
|
---|
72 | get { return seedParameter; }
|
---|
73 | }
|
---|
74 | [Storable]
|
---|
75 | private FixedValueParameter<IntValue> eliteSetSizeParameter;
|
---|
76 | private IFixedValueParameter<IntValue> EliteSetSizeParameter {
|
---|
77 | get { return eliteSetSizeParameter; }
|
---|
78 | }
|
---|
79 | [Storable]
|
---|
80 | private FixedValueParameter<IntValue> minimiumEliteSetSizeParameter;
|
---|
81 | public IFixedValueParameter<IntValue> MinimumEliteSetSizeParameter {
|
---|
82 | get { return minimiumEliteSetSizeParameter; }
|
---|
83 | }
|
---|
84 | [Storable]
|
---|
85 | private FixedValueParameter<IntValue> maximumIterationsParameter;
|
---|
86 | public IFixedValueParameter<IntValue> MaximumIterationsParameter {
|
---|
87 | get { return maximumIterationsParameter; }
|
---|
88 | }
|
---|
89 | [Storable]
|
---|
90 | private FixedValueParameter<IntValue> maximumLocalSearchIterationsParameter;
|
---|
91 | public IFixedValueParameter<IntValue> MaximumLocalSearchIterationsParameter {
|
---|
92 | get { return maximumIterationsParameter; }
|
---|
93 | }
|
---|
94 | [Storable]
|
---|
95 | private FixedValueParameter<PercentValue> candidateSizeFactorParameter;
|
---|
96 | public IFixedValueParameter<PercentValue> CandidateSizeFactorParameter {
|
---|
97 | get { return candidateSizeFactorParameter; }
|
---|
98 | }
|
---|
99 | [Storable]
|
---|
100 | private FixedValueParameter<IntValue> maximumCandidateListSizeParameter;
|
---|
101 | public IFixedValueParameter<IntValue> MaximumCandidateListSizeParameter {
|
---|
102 | get { return maximumCandidateListSizeParameter; }
|
---|
103 | }
|
---|
104 | [Storable]
|
---|
105 | private FixedValueParameter<PercentValue> oneMoveProbabilityParameter;
|
---|
106 | public IFixedValueParameter<PercentValue> OneMoveProbabilityParameter {
|
---|
107 | get { return oneMoveProbabilityParameter; }
|
---|
108 | }
|
---|
109 | [Storable]
|
---|
110 | private FixedValueParameter<IntValue> minimumDifferenceParameter;
|
---|
111 | public IFixedValueParameter<IntValue> MinimumDifferenceParameter {
|
---|
112 | get { return minimumDifferenceParameter; }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public bool SetSeedRandomly {
|
---|
116 | get { return setSeedRandomlyParameter.Value.Value; }
|
---|
117 | set { setSeedRandomlyParameter.Value.Value = value; }
|
---|
118 | }
|
---|
119 | public int Seed {
|
---|
120 | get { return seedParameter.Value.Value; }
|
---|
121 | set { seedParameter.Value.Value = value; }
|
---|
122 | }
|
---|
123 | public int EliteSetSize {
|
---|
124 | get { return eliteSetSizeParameter.Value.Value; }
|
---|
125 | set { eliteSetSizeParameter.Value.Value = value; }
|
---|
126 | }
|
---|
127 | public int MinimumEliteSetSize {
|
---|
128 | get { return minimiumEliteSetSizeParameter.Value.Value; }
|
---|
129 | set { minimiumEliteSetSizeParameter.Value.Value = value; }
|
---|
130 | }
|
---|
131 | public int MaximumIterations {
|
---|
132 | get { return maximumIterationsParameter.Value.Value; }
|
---|
133 | set { maximumIterationsParameter.Value.Value = value; }
|
---|
134 | }
|
---|
135 | public int MaximumLocalSearchIterations {
|
---|
136 | get { return maximumLocalSearchIterationsParameter.Value.Value; }
|
---|
137 | set { maximumLocalSearchIterationsParameter.Value.Value = value; }
|
---|
138 | }
|
---|
139 | public double CandidateSizeFactor {
|
---|
140 | get { return candidateSizeFactorParameter.Value.Value; }
|
---|
141 | set { candidateSizeFactorParameter.Value.Value = value; }
|
---|
142 | }
|
---|
143 | public int MaximumCandidateListSize {
|
---|
144 | get { return maximumCandidateListSizeParameter.Value.Value; }
|
---|
145 | set { maximumCandidateListSizeParameter.Value.Value = value; }
|
---|
146 | }
|
---|
147 | public double OneMoveProbability {
|
---|
148 | get { return oneMoveProbabilityParameter.Value.Value; }
|
---|
149 | set { oneMoveProbabilityParameter.Value.Value = value; }
|
---|
150 | }
|
---|
151 | public int MinimumDifference {
|
---|
152 | get { return minimumDifferenceParameter.Value.Value; }
|
---|
153 | set { minimumDifferenceParameter.Value.Value = value; }
|
---|
154 | }
|
---|
155 |
|
---|
156 | [StorableConstructor]
|
---|
157 | protected GRASP(bool deserializing) : base(deserializing) { }
|
---|
158 | protected GRASP(GRASP original, Cloner cloner)
|
---|
159 | : base(original, cloner) {
|
---|
160 | setSeedRandomlyParameter = cloner.Clone(original.setSeedRandomlyParameter);
|
---|
161 | seedParameter = cloner.Clone(original.seedParameter);
|
---|
162 | analyzerParameter = cloner.Clone(original.analyzerParameter);
|
---|
163 | eliteSetSizeParameter = cloner.Clone(original.eliteSetSizeParameter);
|
---|
164 | minimiumEliteSetSizeParameter = cloner.Clone(original.minimiumEliteSetSizeParameter);
|
---|
165 | maximumIterationsParameter = cloner.Clone(original.maximumIterationsParameter);
|
---|
166 | maximumLocalSearchIterationsParameter = cloner.Clone(original.maximumLocalSearchIterationsParameter);
|
---|
167 | candidateSizeFactorParameter = cloner.Clone(original.candidateSizeFactorParameter);
|
---|
168 | maximumCandidateListSizeParameter = cloner.Clone(original.maximumCandidateListSizeParameter);
|
---|
169 | oneMoveProbabilityParameter = cloner.Clone(original.oneMoveProbabilityParameter);
|
---|
170 | minimumDifferenceParameter = cloner.Clone(original.minimumDifferenceParameter);
|
---|
171 | context = cloner.Clone(original.context);
|
---|
172 | }
|
---|
173 | public GRASP() {
|
---|
174 | Parameters.Add(setSeedRandomlyParameter = new FixedValueParameter<BoolValue>("SetSeedRandomly", "Whether to overwrite the seed with a random value each time the algorithm is run.", new BoolValue(true)));
|
---|
175 | Parameters.Add(seedParameter = new FixedValueParameter<IntValue>("Seed", "The random seed that is used in the stochastic algorithm", new IntValue(0)));
|
---|
176 | Parameters.Add(analyzerParameter = new ValueParameter<IAnalyzer>("Analyzer", "The analyzers that are used to perform an analysis of the solutions.", new MultiAnalyzer()));
|
---|
177 | Parameters.Add(eliteSetSizeParameter = new FixedValueParameter<IntValue>("EliteSetSize", "The (maximum) size of the elite set.", new IntValue(10)));
|
---|
178 | Parameters.Add(minimiumEliteSetSizeParameter = new FixedValueParameter<IntValue>("MinimumEliteSetSize", "(ρ) The minimal size of the elite set, before local search and path relinking are applied.", new IntValue(2)));
|
---|
179 | Parameters.Add(maximumIterationsParameter = new FixedValueParameter<IntValue>("MaximumIterations", "The number of iterations that the algorithm should run.", new IntValue(1000)));
|
---|
180 | Parameters.Add(maximumLocalSearchIterationsParameter = new FixedValueParameter<IntValue>("MaximumLocalSearchIteration", "The maximum number of iterations that the approximate local search should run", new IntValue(100)));
|
---|
181 | Parameters.Add(candidateSizeFactorParameter = new FixedValueParameter<PercentValue>("CandidateSizeFactor", "(η) Determines the size of the set of feasible moves in each path - relinking step relative to the maximum size.A value of 50 % means that only half of all possible moves are considered each step.", new PercentValue(0.5)));
|
---|
182 | Parameters.Add(maximumCandidateListSizeParameter = new FixedValueParameter<IntValue>("MaximumCandidateListSize", "The maximum number of candidates that should be found in each step.", new IntValue(10)));
|
---|
183 | Parameters.Add(oneMoveProbabilityParameter = new FixedValueParameter<PercentValue>("OneMoveProbability", "The probability for performing a 1-move, which is the opposite of performing a 2-move.", new PercentValue(.5)));
|
---|
184 | Parameters.Add(minimumDifferenceParameter = new FixedValueParameter<IntValue>("MinimumDifference", "The minimum amount of difference between two solutions so that they are both accepted in the elite set.", new IntValue(4)));
|
---|
185 | Problem = new GQAP();
|
---|
186 | }
|
---|
187 |
|
---|
188 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
189 | return new GRASP(this, cloner);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public override void Prepare() {
|
---|
193 | base.Prepare();
|
---|
194 | Results.Clear();
|
---|
195 | context = null;
|
---|
196 | }
|
---|
197 |
|
---|
198 | [Storable]
|
---|
199 | private GRASPContext context;
|
---|
200 |
|
---|
201 | protected override void Initialize(CancellationToken cancellationToken) {
|
---|
202 | base.Initialize(cancellationToken);
|
---|
203 | context = new GRASPContext();
|
---|
204 | context.Problem = Problem;
|
---|
205 | context.Scope.Variables.Add(new Variable("Results", Results));
|
---|
206 |
|
---|
207 | IExecutionContext ctxt = null;
|
---|
208 | foreach (var item in Problem.ExecutionContextItems)
|
---|
209 | ctxt = new Core.ExecutionContext(ctxt, item, context.Scope);
|
---|
210 | ctxt = new Core.ExecutionContext(ctxt, this, context.Scope);
|
---|
211 | context.Parent = ctxt;
|
---|
212 |
|
---|
213 | if (SetSeedRandomly) {
|
---|
214 | var rnd = new System.Random();
|
---|
215 | Seed = rnd.Next();
|
---|
216 | }
|
---|
217 | context.Random = new MersenneTwister((uint)Seed);
|
---|
218 | context.Iterations = 0;
|
---|
219 | context.EvaluatedSolutions = 0;
|
---|
220 | context.BestQuality = double.NaN;
|
---|
221 | context.BestSolution = null;
|
---|
222 |
|
---|
223 | context.Initialized = true;
|
---|
224 |
|
---|
225 | Results.Add(new Result("Iterations", new IntValue(context.Iterations)));
|
---|
226 | Results.Add(new Result("EvaluatedSolutions", new IntValue(context.EvaluatedSolutions)));
|
---|
227 | Results.Add(new Result("BestQuality", new DoubleValue(context.BestQuality)));
|
---|
228 | Results.Add(new Result("BestSolution", typeof(GQAPSolution)));
|
---|
229 |
|
---|
230 | context.RunOperator(analyzerParameter.Value, context.Scope, cancellationToken);
|
---|
231 | }
|
---|
232 |
|
---|
233 | protected override void Run(CancellationToken cancellationToken) {
|
---|
234 | var eq = new IntegerVectorEqualityComparer();
|
---|
235 | while (!StoppingCriterion()) { // line 2 in Algorithm 1
|
---|
236 | // next: line 3 in Algorithm 1
|
---|
237 | var pi_prime_vec = GreedyRandomizedSolutionCreator.CreateSolution(context.Random, Problem.ProblemInstance, 1000, false, cancellationToken);
|
---|
238 | if (context.PopulationCount >= MinimumEliteSetSize) { // line 4 in Algorithm 1
|
---|
239 | GQAPSolution pi_prime;
|
---|
240 | if (!Problem.ProblemInstance.IsFeasible(pi_prime_vec)) // line 5 in Algorithm 1
|
---|
241 | pi_prime = context.AtPopulation(context.Random.Next(context.PopulationCount)).Solution; // line 6 in Algorithm 1
|
---|
242 | else {
|
---|
243 | // This is necessary, because pi_prime has not been evaluated yet and such details are not covered in Algorithm 1
|
---|
244 | pi_prime = Problem.ProblemInstance.ToEvaluatedSolution(pi_prime_vec);
|
---|
245 | context.EvaluatedSolutions++;
|
---|
246 | }
|
---|
247 |
|
---|
248 | ApproxLocalSearch(pi_prime); // line 8 in Algorithm 1
|
---|
249 | var pi_plus = context.AtPopulation(context.Random.Next(context.PopulationCount)); // line 9 in Algorithm 1
|
---|
250 | pi_prime = PathRelinking(pi_prime, pi_plus.Solution); // line 10 in Algorithm 1
|
---|
251 | ApproxLocalSearch(pi_prime); // line 11 in Algorithm 1
|
---|
252 | var fitness = Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation);
|
---|
253 | // Book-keeping
|
---|
254 | if (context.BestQuality > fitness) {
|
---|
255 | context.BestQuality = fitness;
|
---|
256 | context.BestSolution = (GQAPSolution)pi_prime.Clone();
|
---|
257 | }
|
---|
258 |
|
---|
259 | if (context.PopulationCount == EliteSetSize) { // line 12 in Algorithm 1
|
---|
260 | var fit = Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation);
|
---|
261 | double[] similarities = context.Population.Select(x => HammingSimilarityCalculator.CalculateSimilarity(x.Solution.Assignment, pi_prime.Assignment)).ToArray();
|
---|
262 | if (similarities.Max() <= 1.0 - (MinimumDifference / (double)pi_prime.Assignment.Length)) { // cond. 2 of line 13 in Algorithm 1
|
---|
263 | var replacement = context.Population.Select((v, i) => new { V = v, Index = i })
|
---|
264 | .Where(x => x.V.Fitness >= fit).ToArray();
|
---|
265 | if (replacement.Length > 0) { // cond. 1 of line 13 in Algorithm 1
|
---|
266 | // next two lines: line 14 in Algorithm 1
|
---|
267 | replacement = replacement.OrderBy(x => similarities[x.Index]).ToArray();
|
---|
268 | context.ReplaceAtPopulation(replacement.Last().Index, context.ToScope(pi_prime, fit));
|
---|
269 | }
|
---|
270 | }
|
---|
271 | } else if (IsSufficientlyDifferent(pi_prime.Assignment)) { // line 17 in Algorithm 1
|
---|
272 | context.AddToPopulation(context.ToScope(pi_prime, Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation))); // line 18 in Algorithm 1
|
---|
273 | }
|
---|
274 | } else if (Problem.ProblemInstance.IsFeasible(pi_prime_vec) /* cond. 1 of line 21 in Algorithm 1 */
|
---|
275 | && IsSufficientlyDifferent(pi_prime_vec)) /* cond. 2 of line 21 in Algorithm 1 */ {
|
---|
276 | var pi_prime = Problem.ProblemInstance.ToEvaluatedSolution(pi_prime_vec);
|
---|
277 | context.EvaluatedSolutions++;
|
---|
278 | var fitness = Problem.ProblemInstance.ToSingleObjective(pi_prime.Evaluation);
|
---|
279 | context.AddToPopulation(context.ToScope(pi_prime, fitness)); /* line 22 in Algorithm 1 */
|
---|
280 | // Book-keeping
|
---|
281 | if (context.PopulationCount == 1 || context.BestQuality > fitness) {
|
---|
282 | context.BestQuality = fitness;
|
---|
283 | context.BestSolution = (GQAPSolution)pi_prime.Clone();
|
---|
284 | }
|
---|
285 | }
|
---|
286 |
|
---|
287 | IResult result;
|
---|
288 | if (Results.TryGetValue("Iterations", out result))
|
---|
289 | ((IntValue)result.Value).Value = context.Iterations;
|
---|
290 | else Results.Add(new Result("Iterations", new IntValue(context.Iterations)));
|
---|
291 | if (Results.TryGetValue("EvaluatedSolutions", out result))
|
---|
292 | ((IntValue)result.Value).Value = context.EvaluatedSolutions;
|
---|
293 | else Results.Add(new Result("EvaluatedSolutions", new IntValue(context.EvaluatedSolutions)));
|
---|
294 | if (Results.TryGetValue("BestQuality", out result))
|
---|
295 | ((DoubleValue)result.Value).Value = context.BestQuality;
|
---|
296 | else Results.Add(new Result("BestQuality", new DoubleValue(context.BestQuality)));
|
---|
297 | if (Results.TryGetValue("BestSolution", out result))
|
---|
298 | result.Value = context.BestSolution;
|
---|
299 | else Results.Add(new Result("BestSolution", context.BestSolution));
|
---|
300 |
|
---|
301 | context.RunOperator(analyzerParameter.Value, context.Scope, cancellationToken);
|
---|
302 |
|
---|
303 | context.Iterations++;
|
---|
304 | }
|
---|
305 | }
|
---|
306 |
|
---|
307 | private bool IsSufficientlyDifferent(IntegerVector vec) {
|
---|
308 | return context.Population.All(x =>
|
---|
309 | HammingSimilarityCalculator.CalculateSimilarity(vec, x.Solution.Assignment) <= 1.0 - (MinimumDifference / (double)vec.Length)
|
---|
310 | );
|
---|
311 | }
|
---|
312 |
|
---|
313 | private GQAPSolution PathRelinking(GQAPSolution pi_prime, GQAPSolution pi_plus) {
|
---|
314 | // Following code represents line 1 of Algorithm 4
|
---|
315 | IntegerVector source = pi_prime.Assignment, target = pi_plus.Assignment;
|
---|
316 | Evaluation sourceEval = pi_prime.Evaluation, targetEval = pi_plus.Evaluation;
|
---|
317 | var sourceFit = Problem.ProblemInstance.ToSingleObjective(sourceEval);
|
---|
318 | var targetFit = Problem.ProblemInstance.ToSingleObjective(targetEval);
|
---|
319 | if (targetFit < sourceFit) {
|
---|
320 | var h = source;
|
---|
321 | source = target;
|
---|
322 | target = h;
|
---|
323 | var hh = sourceEval;
|
---|
324 | sourceEval = targetEval;
|
---|
325 | targetEval = hh;
|
---|
326 | }
|
---|
327 | int evaluatedSolutions;
|
---|
328 | // lines 2-36 of Algorithm 4 are implemented in the following call
|
---|
329 | var pi_star = GQAPPathRelinking.Apply(context.Random, source, sourceEval,
|
---|
330 | target, targetEval, Problem.ProblemInstance, CandidateSizeFactor,
|
---|
331 | out evaluatedSolutions);
|
---|
332 | context.EvaluatedSolutions += evaluatedSolutions;
|
---|
333 | return pi_star;
|
---|
334 | }
|
---|
335 |
|
---|
336 | private void ApproxLocalSearch(GQAPSolution pi_prime) {
|
---|
337 | var localSearchEvaluations = 0;
|
---|
338 | ApproximateLocalSearch.Apply(context.Random, pi_prime, MaximumCandidateListSize,
|
---|
339 | OneMoveProbability, MaximumLocalSearchIterations, Problem.ProblemInstance, out localSearchEvaluations);
|
---|
340 | context.EvaluatedSolutions += localSearchEvaluations;
|
---|
341 | }
|
---|
342 |
|
---|
343 | private bool StoppingCriterion() {
|
---|
344 | return context.Iterations > MaximumIterations;
|
---|
345 | }
|
---|
346 | }
|
---|
347 | }
|
---|