Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms/3.3/RandomSearch/RandomSearch.cs @ 18242

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

#1614: updated to new persistence and .NET 4.6.1

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 HEAL.Attic;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Encodings.IntegerVectorEncoding;
29using HeuristicLab.Optimization;
30
31namespace HeuristicLab.Problems.GeneralizedQuadraticAssignment.Algorithms.RandomSearch {
32  [Item("Random Search (GQAP)", "Random search for the GQAP.")]
33  [Creatable(CreatableAttribute.Categories.SingleSolutionAlgorithms)]
34  [StorableType("D6ED1A84-BBCD-4163-96A7-47F3DCACBE49")]
35  public sealed class RandomSearch : StochasticAlgorithm<RandomSearchContext, 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(StorableConstructorFlag _) : base(_) { }
52    private RandomSearch(RandomSearch original, Cloner cloner)
53      : base(original, cloner) {
54    }
55    public RandomSearch() {
56      Problem = new GQAP();
57    }
58   
59    public override IDeepCloneable Clone(Cloner cloner) {
60      return new RandomSearch(this, cloner);
61    }
62
63    protected override void Initialize(CancellationToken token) {
64      base.Initialize(token);
65
66      Context.Problem = Problem;
67      Context.BestSolution = null;
68    }
69
70    protected override void Run(CancellationToken cancellationToken) {
71      base.Run(cancellationToken);
72      var lastUpdate = ExecutionTime;
73
74      while (!StoppingCriterion()) {
75        var assign = new IntegerVector(Problem.ProblemInstance.Demands.Length, Context.Random, 0, Problem.ProblemInstance.Capacities.Length);
76        var eval = Problem.ProblemInstance.Evaluate(assign);
77        var candidate = new GQAPSolution(assign, eval);
78        Context.EvaluatedSolutions++;
79
80        var candidateFit = Problem.ProblemInstance.ToSingleObjective(candidate.Evaluation);
81        if (Context.Incumbent == null || candidateFit < Context.Incumbent.Fitness) {
82          Context.ReplaceIncumbent(Context.ToScope(candidate, candidateFit));
83          Context.BestQuality = candidateFit;
84          Context.BestSolution = (GQAPSolution)candidate.Clone();
85        }
86
87        IResult result;
88        if (ExecutionTime - lastUpdate > TimeSpan.FromSeconds(1)) {
89          if (Results.TryGetValue("Iterations", out result))
90            ((IntValue)result.Value).Value = Context.Iterations;
91          else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
92          if (Results.TryGetValue("EvaluatedSolutions", out result))
93            ((IntValue)result.Value).Value = Context.EvaluatedSolutions;
94          else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
95          lastUpdate = ExecutionTime;
96        }
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        try {
105          Context.RunOperator(Analyzer, cancellationToken);
106        } catch (OperationCanceledException) { }
107
108        Context.Iterations++;
109        if (cancellationToken.IsCancellationRequested) break;
110      }
111
112      IResult result2;
113      if (Results.TryGetValue("Iterations", out result2))
114        ((IntValue)result2.Value).Value = Context.Iterations;
115      else Results.Add(new Result("Iterations", new IntValue(Context.Iterations)));
116      if (Results.TryGetValue("EvaluatedSolutions", out result2))
117        ((IntValue)result2.Value).Value = Context.EvaluatedSolutions;
118      else Results.Add(new Result("EvaluatedSolutions", new IntValue(Context.EvaluatedSolutions)));
119    }
120  }
121}
Note: See TracBrowser for help on using the repository browser.