Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/IteratedLS.cs @ 15562

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

#1614: added additional algorithms

File size: 5.7 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.Threading;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSearch {
32  [Item("Iterated Local Search (GQAP)", "Iterated local search for the GQAP.")]
33  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
34  [StorableClass]
35  public sealed class IteratedLS : ContextAlgorithm<LocalSearchContext> {
36
37    public override bool SupportsPause {
38      get { return true; }
39    }
40
41    public override Type ProblemType {
42      get { return typeof(GQAP); }
43    }
44
45    public new GQAP Problem {
46      get { return (GQAP)base.Problem; }
47      set { base.Problem = value; }
48    }
49
50    [StorableConstructor]
51    private IteratedLS(bool deserializing) : base(deserializing) { }
52    private IteratedLS(IteratedLS original, Cloner cloner)
53      : base(original, cloner) {
54    }
55    public IteratedLS() {
56
57      Problem = new GQAP();
58    }
59   
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new IteratedLS(this, cloner);
62    }
63
64    protected override void Initialize(CancellationToken token) {
65      base.Initialize(token);
66
67      Context.Problem = Problem;
68      Context.BestQuality = double.NaN;
69      Context.BestSolution = null;
70
71      var assign = GreedyRandomizedSolutionCreator.CreateSolution(Context.Random, Problem.ProblemInstance, 10, true, token);
72      var eval = Problem.ProblemInstance.Evaluate(assign);
73      var fit = Problem.ProblemInstance.ToSingleObjective(eval);
74      Context.EvaluatedSolutions++;
75
76      var candidate = new GQAPSolution(assign, eval);
77      var lsevaluations = 0;
78      OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
79      Context.EvaluatedSolutions += lsevaluations;
80
81      Context.ReplaceIncumbent(Context.ToScope(candidate, fit));
82      Context.BestQuality = fit;
83      Context.BestSolution = (GQAPSolution)candidate.Clone();
84
85      Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
86      Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
87      Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
88      Results.Add(new Result("BestSolution", Context.BestSolution));
89
90      Context.RunOperator(Analyzer, Context.Scope, token);
91    }
92
93    protected override void Run(CancellationToken cancellationToken) {
94      while (!StoppingCriterion()) {
95        var lsevaluations = 0;
96        var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone();
97        RandomWalk(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, candidate.Assignment.Length);
98        candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment);
99        Context.EvaluatedSolutions++;
100        OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
101        Context.EvaluatedSolutions += lsevaluations;
102
103        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
104        if (candidateFit < Context.Incumbent.Fitness) {
105          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
106          Context.BestQuality = candidateFit;
107          Context.BestSolution = (GQAPSolution)candidate.Clone();
108        }
109
110        IResult result;
111        if (Results.TryGetValue("Iterations", out result))
112          ((IntValue)result.Value).Value = Context.Iterations;
113        else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
114        if (Results.TryGetValue("EvaluatedSolutions", out result))
115          ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
116        else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
117        if (Results.TryGetValue("BestQuality", out result))
118          ((DoubleValue)result.Value).Value = Context.BestQuality;
119        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
120        if (Results.TryGetValue("BestSolution", out result))
121          result.Value = Context.BestSolution;
122        else Results.Add(new Result("BestSolution", Context.BestSolution));
123
124        Context.RunOperator(Analyzer, Context.Scope, cancellationToken);
125
126        Context.Iterations++;
127        if (cancellationToken.IsCancellationRequested) break;
128      }
129    }
130
131    private static void RandomWalk(IRandom random, IntegerVector assignment, int locations, int walkLength) {
132      for (int i = 0; i < walkLength; i++) {
133        var equipment = random.Next(assignment.Length);
134        assignment[equipment] = random.Next(locations);
135        if (random.NextDouble() < 1.0 / walkLength) break;
136      }
137    }
138  }
139}
Note: See TracBrowser for help on using the repository browser.