Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PerformanceComparison/HeuristicLab.Algorithms.MemPR/3.3/Permutation/LocalSearch/ExhaustiveHillClimb.cs @ 14691

Last change on this file since 14691 was 14691, checked in by abeham, 8 years ago

#2457: working on identification of problem instances

File size: 3.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Collections.Generic;
24using System.Threading;
25using HeuristicLab.Algorithms.MemPR.Interfaces;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.QuadraticAssignment;
31
32namespace HeuristicLab.Algorithms.MemPR.Permutation.LocalSearch {
33  [Item("Exhaustive Hillclimber (permutation)", "", ExcludeGenericTypeInfo = true)]
34  [StorableClass]
35  public class ExhaustiveHillClimb<TContext> : NamedItem, ILocalSearch<TContext>
36      where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, Encodings.PermutationEncoding.Permutation>,
37                       IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation>,
38                       ILocalSearchPathContext<Encodings.PermutationEncoding.Permutation> {
39
40    [StorableConstructor]
41    protected ExhaustiveHillClimb(bool deserializing) : base(deserializing) { }
42    protected ExhaustiveHillClimb(ExhaustiveHillClimb<TContext> original, Cloner cloner) : base(original, cloner) { }
43    public ExhaustiveHillClimb() {
44      Name = ItemName;
45      Description = ItemDescription;
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new ExhaustiveHillClimb<TContext>(this, cloner);
50    }
51
52    public void Optimize(TContext context) {
53      if (double.IsNaN(context.Solution.Fitness))
54        context.Evaluate(context.Solution, CancellationToken.None);
55      var quality = context.Solution.Fitness;
56      try {
57
58        var min = 0.0;
59        var max = 1.0;
60        var qap = context.Problem as QuadraticAssignmentProblem;
61        if (qap != null) {
62          min = qap.LowerBound.Value;
63          max = qap.AverageQuality.Value;
64        }
65
66        var path = new List<Tuple<Encodings.PermutationEncoding.Permutation, double>>();
67        path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)context.Solution.Solution.Clone(), (quality - min) / (max - min)));
68
69        var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, quality,
70          context.Maximization, context.Evaluate, CancellationToken.None);
71       
72        var evaluations = 0;
73        foreach (var step in result) {
74          path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)step.Item1.Clone(), (step.Item2 - min) / (max - min)));
75          evaluations = step.Item3; // last one will be actual evaluations
76        }
77        context.LocalSearchPaths.AddPath(path);
78        context.IncrementEvaluatedSolutions(evaluations);
79        context.Iterations = path.Count - 2;
80      } finally {
81        context.Solution.Fitness = quality;
82      }
83    }
84  }
85}
Note: See TracBrowser for help on using the repository browser.