Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2457: working on identification of problem instances

File size: 3.7 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 Subspace Hillclimber (permutation)", "", ExcludeGenericTypeInfo = true)]
34  [StorableClass]
35  public class ExhaustiveHillClimbSubspace<TContext> : NamedItem, ILocalSearch<TContext>
36      where TContext : ISingleSolutionHeuristicAlgorithmContext<ISingleObjectiveHeuristicOptimizationProblem, Encodings.PermutationEncoding.Permutation>,
37                       IPermutationSubspaceContext, IEvaluationServiceContext<Encodings.PermutationEncoding.Permutation>,
38                       ILocalSearchPathContext<Encodings.PermutationEncoding.Permutation> {
39
40    [StorableConstructor]
41    protected ExhaustiveHillClimbSubspace(bool deserializing) : base(deserializing) { }
42    protected ExhaustiveHillClimbSubspace(ExhaustiveHillClimbSubspace<TContext> original, Cloner cloner) : base(original, cloner) { }
43    public ExhaustiveHillClimbSubspace() {
44      Name = ItemName;
45      Description = ItemDescription;
46    }
47
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new ExhaustiveHillClimbSubspace<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        var min = 0.0;
58        var max = 1.0;
59        var qap = context.Problem as QuadraticAssignmentProblem;
60        if (qap != null) {
61          min = qap.LowerBound.Value;
62          max = qap.AverageQuality.Value;
63        }
64
65        var path = new List<Tuple<Encodings.PermutationEncoding.Permutation, double>>();
66        path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)context.Solution.Solution.Clone(), (quality - min) / (max - min)));
67
68        var result = Exhaustive.HillClimb(context.Random, context.Solution.Solution, quality,
69          context.Maximization, context.Evaluate, CancellationToken.None, context.Subspace.Subspace);
70
71        var evaluations = 0;
72        foreach (var step in result) {
73          path.Add(Tuple.Create((Encodings.PermutationEncoding.Permutation)step.Item1.Clone(), (step.Item2 - min) / (max - min)));
74          evaluations = step.Item3; // last one will be actual evaluations
75        }
76        context.LocalSearchPaths.AddPath(path);
77        context.IncrementEvaluatedSolutions(evaluations);
78        context.Iterations = path.Count - 2;
79      } finally {
80        context.Solution.Fitness = quality;
81      }
82    }
83  }
84}
Note: See TracBrowser for help on using the repository browser.