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

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

#1614: added additional algorithms

File size: 4.3 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 : 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 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.BestQuality = double.NaN;
69      Context.BestSolution = null;
70    }
71
72    protected override void Run(CancellationToken cancellationToken) {
73      while (!StoppingCriterion()) {
74        var lsevaluations = 0;
75        var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
76        var eval = Problem.ProblemInstance.Evaluate(assign);
77        Context.EvaluatedSolutions++;
78
79        var candidate = new GQAPSolution(assign, eval);
80        OneOptLocalSearch.Apply(Context.Random, candidate, Problem.ProblemInstance, out lsevaluations);
81        Context.EvaluatedSolutions += lsevaluations;
82
83        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
84        if (Context.Incumbent == null || candidateFit < Context.Incumbent.Fitness) {
85          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
86          Context.BestQuality = candidateFit;
87          Context.BestSolution = (GQAPSolution)candidate.Clone();
88        }
89
90        IResult result;
91        if (Results.TryGetValue("Iterations", out result))
92          ((IntValue)result.Value).Value = Context.Iterations;
93        else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
94        if (Results.TryGetValue("EvaluatedSolutions", out result))
95          ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
96        else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
97        if (Results.TryGetValue("BestQuality", out result))
98          ((DoubleValue)result.Value).Value = Context.BestQuality;
99        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
100        if (Results.TryGetValue("BestSolution", out result))
101          result.Value = Context.BestSolution;
102        else Results.Add(new Result("BestSolution", Context.BestSolution));
103
104        Context.RunOperator(Analyzer, Context.Scope, cancellationToken);
105
106        Context.Iterations++;
107        if (cancellationToken.IsCancellationRequested) break;
108      }
109    }
110  }
111}
Note: See TracBrowser for help on using the repository browser.