Free cookie consent management tool by TermsFeed Policy Generator

source: branches/1614_GeneralizedQAP/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/RandomSearch/RandomSearch.cs @ 15698

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

#1614: Added random search and fixed execution time counting for commercial solvers

File size: 4.8 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("Random Search (GQAP)", "Random search for the GQAP.")]
33  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
34  [StorableClass]
35  public sealed class RandomSearch : 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 RandomSearch(bool deserializing) : base(deserializing) { }
52    private RandomSearch(RandomSearch original, Cloner cloner)
53      : base(original, cloner) {
54    }
55    public RandomSearch() {
56
57      Problem = new GQAP();
58    }
59   
60    public override IDeepCloneable Clone(Cloner cloner) {
61      return new RandomSearch(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      var lastUpdate = ExecutionTime;
74
75      while (!StoppingCriterion()) {
76        var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
77        var eval = Problem.ProblemInstance.Evaluate(assign);
78        var candidate = new GQAPSolution(assign, eval);
79        Context.EvaluatedSolutions++;
80
81        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
82        if (Context.Incumbent == null || candidateFit < Context.Incumbent.Fitness) {
83          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
84          Context.BestQuality = candidateFit;
85          Context.BestSolution = (GQAPSolution)candidate.Clone();
86        }
87
88        IResult result;
89        if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
90          if (Results.TryGetValue("Iterations", out result))
91            ((IntValue)result.Value).Value = Context.Iterations;
92          else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
93          if (Results.TryGetValue("EvaluatedSolutions", out result))
94            ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
95          else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
96          lastUpdate = ExecutionTime;
97        }
98        if (Results.TryGetValue("BestQuality", out result))
99          ((DoubleValue)result.Value).Value = Context.BestQuality;
100        else Results.Add(new Result("BestQuality", new DoubleValue(Context.BestQuality)));
101        if (Results.TryGetValue("BestSolution", out result))
102          result.Value = Context.BestSolution;
103        else Results.Add(new Result("BestSolution", Context.BestSolution));
104
105        try {
106          Context.RunOperator(Analyzer, cancellationToken);
107        } catch (OperationCanceledException) { }
108
109        Context.Iterations++;
110        if (cancellationToken.IsCancellationRequested) break;
111      }
112
113      IResult result2;
114      if (Results.TryGetValue("Iterations", out result2))
115        ((IntValue)result2.Value).Value = Context.Iterations;
116      else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
117      if (Results.TryGetValue("EvaluatedSolutions", out result2))
118        ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
119      else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
120    }
121  }
122}
Note: See TracBrowser for help on using the repository browser.