Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/Algorithms/DirectedWalk.cs @ 16096

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

#2457:

  • Changed calculation of correlation length (using limit introduced Hordijk 1996)
  • Changed RuggednessCalculator (no more a HL item)
  • Added additional, information-analysis-based features for directed walks
  • Added generic DirectedWalk algorithm (as described in thesis)
  • Made OneSizeInstanceProvider parametrizable
  • Adapted program for analyzing problem instance reidentification
File size: 8.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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.Linq;
25using HeuristicLab.Common;
26
27namespace HeuristicLab.Analysis.FitnessLandscape {
28  /// <summary>
29  /// A generic implementation of a directed walk that can still be used in an efficient manner.
30  /// </summary>
31  /// <typeparam name="T">The type of the solution encoding.</typeparam>
32  public static class DirectedWalk<T> where T : IDeepCloneable {
33    /// <summary>
34    /// Provides regular directed walks between two solutions.
35    /// </summary>
36    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
37    /// <param name="distFunc">The distance function that compares two solutions.</param>
38    /// <param name="neighborFunc">The neighborhood functions that returns _all_ neighbors of a solution.</param>
39    /// <param name="start">The starting solution (to which distance should be increased in every step).</param>
40    /// <param name="startFitness">Fitness value of the starting solution.</param>
41    /// <param name="target">The destination solution (to which distance should be decreased in every step).</param>
42    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
43    /// <returns>The trail of the directed walk including start and target solution.</returns>
44    public static IEnumerable<Tuple<T, double>> Walk(bool maximization, Func<T, T, double> distFunc, Func<Tuple<T, double>, IEnumerable<Tuple<T, double>>> neighborFunc, T start, double startFitness, T target, bool firstImprovement = false) {
45      Func<T, T, bool> restriction = (current, neighbor) => distFunc(neighbor, target) < distFunc(current, target);
46      if (firstImprovement)
47        return DoFirstImprovementWalk(maximization, neighborFunc, start, startFitness, restriction);
48      return DoBestImprovementWalk(maximization, neighborFunc, start, startFitness, restriction);
49    }
50
51    /// <summary>
52    /// Provides regular directed walks between two solutions when already a restricted neighborhood is given.
53    /// </summary>
54    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
55    /// <param name="restrictedNeighborFunc">The neighborhood functions that returns only those neighbors that move closer to some target solution.</param>
56    /// <param name="start">The starting solution.</param>
57    /// <param name="startFitness">Fitness value of the starting solution.</param>
58    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
59    /// <returns>The trail of the directed walk including start solution (and potentially target solution if it was part of the final restricted neighborhood).</returns>
60    public static IEnumerable<Tuple<T, double>> WalkRestricted(bool maximization, Func<Tuple<T, double>, IEnumerable<Tuple<T, double>>> restrictedNeighborFunc, T start, double startFitness, bool firstImprovement = false) {
61      if (firstImprovement)
62        return DoFirstImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
63      return DoBestImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
64    }
65
66    /// <summary>
67    /// Provides inverse directed walks away from a starting solution.
68    /// </summary>
69    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
70    /// <param name="distFunc">The distance function that compares two solutions.</param>
71    /// <param name="neighborFunc">The neighborhood functions that returns _all_ neighbors of a solution.</param>
72    /// <param name="start">The starting solution (to which distance should be increased in every step).</param>
73    /// <param name="startFitness">Fitness value of the starting solution.</param>
74    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
75    /// <returns>The trail of the directed walk including start solution.</returns>
76    public static IEnumerable<Tuple<T, double>> InverseWalk(bool maximization, Func<T, T, double> distFunc, Func<Tuple<T, double>, IEnumerable<Tuple<T, double>>> neighborFunc, T start, double startFitness, bool firstImprovement = false) {
77      Func<T, T, bool> inverseRestriction = (current, neighbor) => distFunc(neighbor, start) > distFunc(current, start);
78      if (firstImprovement)
79        return DoFirstImprovementWalk(maximization, neighborFunc, start, startFitness, inverseRestriction);
80      return DoBestImprovementWalk(maximization, neighborFunc, start, startFitness, inverseRestriction);
81    }
82
83    /// <summary>
84    /// Provides inverse directed walks away from a starting solution when already a restricted neighborhood is given.
85    /// </summary>
86    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
87    /// <param name="restrictedNeighborFunc">The neighborhood functions that returns only those neighbors that move away from the start solution.</param>
88    /// <param name="start">The starting solution.</param>
89    /// <param name="startFitness">Fitness value of the starting solution.</param>
90    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
91    /// <returns>The trail of the directed walk including start solution.</returns>
92    public static IEnumerable<Tuple<T, double>> InverseWalkRestricted(bool maximization, Func<Tuple<T, double>, IEnumerable<Tuple<T, double>>> restrictedNeighborFunc, T start, double startFitness, bool firstImprovement = false) {
93      if (firstImprovement)
94        return DoFirstImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
95      return DoBestImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
96    }
97
98    private static IEnumerable<Tuple<T, double>> DoBestImprovementWalk(bool maximization, Func<Tuple<T, double>, IEnumerable<Tuple<T, double>>> neighborFunc, T start, double startFitness, Func<T, T, bool> restriction) {
99      var current = Tuple.Create(start, startFitness);
100
101      while (true) {
102        yield return current;
103
104        var iterator = neighborFunc(current).Where(n => restriction(current.Item1, n.Item1)).GetEnumerator();
105        if (!iterator.MoveNext()) yield break;
106
107        current = Tuple.Create((T)iterator.Current.Item1.Clone(), iterator.Current.Item2);
108
109        while (iterator.MoveNext()) {
110          var other = iterator.Current;
111          if (maximization && other.Item2 > current.Item2 || !maximization && other.Item2 < current.Item2) {
112            current = Tuple.Create((T)other.Item1.Clone(), other.Item2);
113          }
114        }
115      }
116    }
117
118    private static IEnumerable<Tuple<T, double>> DoFirstImprovementWalk(bool maximization, Func<Tuple<T, double>, IEnumerable<Tuple<T, double>>> neighborFunc, T start, double startFitness, Func<T, T, bool> restriction) {
119      var current = Tuple.Create(start, startFitness);
120
121      while (true) {
122        yield return current;
123
124        var iterator = neighborFunc(current).Where(n => restriction(current.Item1, n.Item1)).GetEnumerator();
125        if (!iterator.MoveNext()) yield break;
126
127        var prevFitness = current.Item2;
128
129        current = Tuple.Create((T)iterator.Current.Item1.Clone(), iterator.Current.Item2);
130
131        if (maximization && current.Item2 > prevFitness || !maximization && current.Item2 < prevFitness)
132          continue;
133
134        while (iterator.MoveNext()) {
135          var other = iterator.Current;
136          if (maximization && other.Item2 > current.Item2 || !maximization && other.Item2 < current.Item2) {
137            current = Tuple.Create((T)other.Item1.Clone(), other.Item2);
138
139            if (maximization && current.Item2 > prevFitness || !maximization && current.Item2 < prevFitness)
140              break;
141          }
142        }
143      }
144    }
145  }
146}
Note: See TracBrowser for help on using the repository browser.