Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614:

  • Added LAHC and pLAHC-s
  • Changed all algorithms to update high frequency results only every second
File size: 6.4 KB
RevLine 
[15562]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]
[15563]35  public sealed class IteratedLS : StochasticAlgorithm<LocalSearchContext> {
[15562]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) {
[15563]94      var lastUpdate = ExecutionTime;
95
[15562]96      while (!StoppingCriterion()) {
97        var lsevaluations = 0;
98        var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone();
99        RandomWalk(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, candidate.Assignment.Length);
100        candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment);
101        Context.EvaluatedSolutions++;
102        OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
103        Context.EvaluatedSolutions += lsevaluations;
104
105        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
106        if (candidateFit < Context.Incumbent.Fitness) {
107          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
108          Context.BestQuality = candidateFit;
109          Context.BestSolution = (GQAPSolution)candidate.Clone();
110        }
111
112        IResult result;
[15563]113        if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
114          if (Results.TryGetValue("Iterations", out result))
115            ((IntValue)result.Value).Value = Context.Iterations;
116          else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
117          if (Results.TryGetValue("EvaluatedSolutions", out result))
118            ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
119          else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
120          lastUpdate = ExecutionTime;
121        }
[15562]122        if (Results.TryGetValue("BestQuality", out result))
123          ((DoubleValue)result.Value).Value = Context.BestQuality;
124        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
125        if (Results.TryGetValue("BestSolution", out result))
126          result.Value = Context.BestSolution;
127        else Results.Add(new Result("BestSolution", Context.BestSolution));
128
129        Context.RunOperator(Analyzer, Context.Scope, cancellationToken);
130
131        Context.Iterations++;
132        if (cancellationToken.IsCancellationRequested) break;
133      }
[15563]134      IResult result2;
135      if (Results.TryGetValue("Iterations", out result2))
136        ((IntValue)result2.Value).Value = Context.Iterations;
137      else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
138      if (Results.TryGetValue("EvaluatedSolutions", out result2))
139        ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
140      else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
[15562]141    }
142
143    private static void RandomWalk(IRandom random, IntegerVector assignment, int locations, int walkLength) {
144      for (int i = 0; i < walkLength; i++) {
145        var equipment = random.Next(assignment.Length);
146        assignment[equipment] = random.Next(locations);
147        if (random.NextDouble() < 1.0 / walkLength) break;
148      }
149    }
150  }
151}
Note: See TracBrowser for help on using the repository browser.