Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1614: Added CPLEX algorithms

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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Encodings.IntegerVectorEncoding;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
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  [StorableClass]
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(bool deserializing) : base(deserializing) { }
64    private IteratedLS(IteratedLS original, Cloner cloner)
65      : base(original, cloner) {
66    }
67    public IteratedLS() {
68      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)));
69
70      Problem = new GQAP();
71    }
72   
73    public override IDeepCloneable Clone(Cloner cloner) {
74      return new IteratedLS(this, cloner);
75    }
76
77    protected override void Initialize(CancellationToken token) {
78      base.Initialize(token);
79
80      Context.Problem = Problem;
81      Context.BestQuality = double.NaN;
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      var lastUpdate = ExecutionTime;
110
111      while (!StoppingCriterion()) {
112        var lsevaluations = 0;
113        var candidate = (GQAPSolution)Context.Incumbent.Solution.Clone();
114        RelocateEquipmentManipluator.Apply(Context.Random, candidate.Assignment, Problem.ProblemInstance.Capacities.Length, 1.0 / (PerturbationStrength * candidate.Assignment.Length));
115        candidate.Evaluation = Problem.ProblemInstance.Evaluate(candidate.Assignment);
116        Context.EvaluatedSolutions++;
117        OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
118        Context.EvaluatedSolutions += lsevaluations;
119
120        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
121        if (candidateFit < Context.Incumbent.Fitness) {
122          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
123          Context.BestQuality = candidateFit;
124          Context.BestSolution = (GQAPSolution)candidate.Clone();
125        }
126
127        IResult result;
128        if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
129          if (Results.TryGetValue("Iterations", out result))
130            ((IntValue)result.Value).Value = Context.Iterations;
131          else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
132          if (Results.TryGetValue("EvaluatedSolutions", out result))
133            ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
134          else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
135          lastUpdate = ExecutionTime;
136        }
137        if (Results.TryGetValue("BestQuality", out result))
138          ((DoubleValue)result.Value).Value = Context.BestQuality;
139        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
140        if (Results.TryGetValue("BestSolution", out result))
141          result.Value = Context.BestSolution;
142        else Results.Add(new Result("BestSolution", Context.BestSolution));
143
144        try {
145          Context.RunOperator(Analyzer, cancellationToken);
146        } catch (OperationCanceledException) { }
147
148        Context.Iterations++;
149        if (cancellationToken.IsCancellationRequested) break;
150      }
151      IResult result2;
152      if (Results.TryGetValue("Iterations", out result2))
153        ((IntValue)result2.Value).Value = Context.Iterations;
154      else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
155      if (Results.TryGetValue("EvaluatedSolutions", out result2))
156        ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
157      else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
158    }
159  }
160}
Note: See TracBrowser for help on using the repository browser.