Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/LocalSearch/MultistartLS.cs @ 15775

Last change on this file since 15775 was 15700, checked in by abeham, 7 years ago

#1614:

  • Changed performance measure to stopwatch instead of datetime for precision reasons
File size: 5.0 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("Multi-start Local Search (GQAP)", "Multi-start local search for the GQAP.")]
33  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
34  [StorableClass]
35  public sealed class MultistartLS : StochasticAlgorithm<LocalSearchContext, IntegerVectorEncoding> {
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 MultistartLS(bool deserializing) : base(deserializing) { }
52    private MultistartLS(MultistartLS original, Cloner cloner)
53      : base(original, cloner) {
54    }
55    public MultistartLS() {
56
57      Problem = new GQAP();
58    }
59   
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new MultistartLS(this, cloner);
62    }
63
64    protected override void Initialize(CancellationToken token) {
65      base.Initialize(token);
66
67      Context.Problem = Problem;
68      Context.BestSolution = null;
69    }
70
71    protected override void Run(CancellationToken cancellationToken) {
72      base.Run(cancellationToken);
73      var lastUpdate = ExecutionTime;
74
75      while (!StoppingCriterion()) {
76        var lsevaluations = 0;
77        var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
78        var eval = Problem.ProblemInstance.Evaluate(assign);
79        Context.EvaluatedSolutions++;
80
81        var candidate = new GQAPSolution(assign, eval);
82        OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
83        Context.EvaluatedSolutions += lsevaluations;
84
85        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
86        if (Context.Incumbent == null || candidateFit < Context.Incumbent.Fitness) {
87          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
88          Context.BestQuality = candidateFit;
89          Context.BestSolution = (GQAPSolution)candidate.Clone();
90        }
91
92        IResult result;
93        if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
94          if (Results.TryGetValue("Iterations", out result))
95            ((IntValue)result.Value).Value = Context.Iterations;
96          else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
97          if (Results.TryGetValue("EvaluatedSolutions", out result))
98            ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
99          else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
100          lastUpdate = ExecutionTime;
101        }
102        if (Results.TryGetValue("BestQuality", out result))
103          ((DoubleValue)result.Value).Value = Context.BestQuality;
104        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
105        if (Results.TryGetValue("BestSolution", out result))
106          result.Value = Context.BestSolution;
107        else Results.Add(new Result("BestSolution", Context.BestSolution));
108
109        try {
110          Context.RunOperator(Analyzer, cancellationToken);
111        } catch (OperationCanceledException) { }
112
113        Context.Iterations++;
114        if (cancellationToken.IsCancellationRequested) break;
115      }
116
117      IResult result2;
118      if (Results.TryGetValue("Iterations", out result2))
119        ((IntValue)result2.Value).Value = Context.Iterations;
120      else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
121      if (Results.TryGetValue("EvaluatedSolutions", out result2))
122        ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
123      else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.