Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/IteratedLS.cs @ 17751

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

#1614: updated to new persistence and .NET 4.6.1

File size: 6.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.Threading;
24using HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31
32namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.LocalSearch {
33  [Item("Iterated Local Search (GQAP)", "Iterated local search for the GQAP.")]
34  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
35  [StorableType("2C8EE433-B133-4382-8B4F-4EFFA536D759")]
36  public sealed class IteratedLS : StochasticAlgorithm<LocalSearchContext, IntegerVectorEncoding> {
37
38    public override bool SupportsPause {
39      get { return true; }
40    }
41
42    public override Type ProblemType {
43      get { return typeof(GQAP); }
44    }
45
46    public new GQAP Problem {
47      get { return (GQAP)base.Problem; }
48      set { base.Problem = value; }
49    }
50
51    [Storable]
52    private IFixedValueParameter<PercentValue> perturbationStrengthParameter;
53    public IFixedValueParameter<PercentValue> PerturbationStrengthParameter {
54      get { return perturbationStrengthParameter; }
55    }
56
57    public double PerturbationStrength {
58      get { return perturbationStrengthParameter.Value.Value; }
59      set { perturbationStrengthParameter.Value.Value = value; }
60    }
61
62    [StorableConstructor]
63    private IteratedLS(StorableConstructorFlag _) : base(_) { }
64    private IteratedLS(IteratedLS original, Cloner cloner)
65      : base(original, cloner) {
66      perturbationStrengthParameter = cloner.Clone(original.perturbationStrengthParameter);
67    }
68    public IteratedLS() {
69      Parameters.Add(perturbationStrengthParameter = new FixedValueParameter<PercentValue>("PerturbationStrength", "The expected length of the random walk relative to the size of the solution vector.", new PercentValue(0.5)));
70
71      Problem = new GQAP();
72    }
73   
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new IteratedLS(this, cloner);
76    }
77
78    protected override void Initialize(CancellationToken token) {
79      base.Initialize(token);
80
81      Context.Problem = Problem;
82      Context.BestSolution = null;
83
84      var assign = GreedyRandomizedSolutionCreator.CreateSolution(Context.Random, Problem.ProblemInstance, 10, true, token);
85      var eval = Problem.ProblemInstance.Evaluate(assign);
86      var fit = Problem.ProblemInstance.ToSingleObjective(eval);
87      Context.EvaluatedSolutions++;
88
89      var candidate = new GQAPSolution(assign, eval);
90      var lsevaluations = 0;
91      OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
92      Context.EvaluatedSolutions += lsevaluations;
93
94      Context.ReplaceIncumbent(Context.ToScope(candidate, fit));
95      Context.BestQuality = fit;
96      Context.BestSolution = (GQAPSolution)candidate.Clone();
97
98      Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
99      Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
100      Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
101      Results.Add(new Result("BestSolution", Context.BestSolution));
102
103      try {
104        Context.RunOperator(Analyzer, token);
105      } catch (OperationCanceledException) { }
106    }
107
108    protected override void Run(CancellationToken cancellationToken) {
109      base.Run(cancellationToken);
110      var lastUpdate = ExecutionTime;
111
112      while (!StoppingCriterion()) {
113        var lsevaluations = 0;
114        var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone();
115        RelocateEquipmentManipluator.Apply(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, 1.0 / (PerturbationStrength * candidate.Assignment.Length));
116        candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment);
117        Context.EvaluatedSolutions++;
118        OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
119        Context.EvaluatedSolutions += lsevaluations;
120
121        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
122        if (candidateFit < Context.Incumbent.Fitness) {
123          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
124          Context.BestQuality = candidateFit;
125          Context.BestSolution = (GQAPSolution)candidate.Clone();
126        }
127
128        IResult result;
129        if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
130          if (Results.TryGetValue("Iterations", out result))
131            ((IntValue)result.Value).Value = Context.Iterations;
132          else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
133          if (Results.TryGetValue("EvaluatedSolutions", out result))
134            ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
135          else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
136          lastUpdate = ExecutionTime;
137        }
138        if (Results.TryGetValue("BestQuality", out result))
139          ((DoubleValue)result.Value).Value = Context.BestQuality;
140        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
141        if (Results.TryGetValue("BestSolution", out result))
142          result.Value = Context.BestSolution;
143        else Results.Add(new Result("BestSolution", Context.BestSolution));
144
145        try {
146          Context.RunOperator(Analyzer, cancellationToken);
147        } catch (OperationCanceledException) { }
148
149        Context.Iterations++;
150        if (cancellationToken.IsCancellationRequested) break;
151      }
152      IResult result2;
153      if (Results.TryGetValue("Iterations", out result2))
154        ((IntValue)result2.Value).Value = Context.Iterations;
155      else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
156      if (Results.TryGetValue("EvaluatedSolutions", out result2))
157        ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
158      else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
159    }
160  }
161}
Note: See TracBrowser for help on using the repository browser.