Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2457:

  • Restructured FLA plugin (moved files between folders, added common base classes)
  • Fixed AC1 in QAPDirectedWalk (ouch!)
  • Changed PartialInformationContent to be in range [0;1]
  • Added unit test for information analysis
  • Refactored information analysis and discard ability to use more symbols than 2 as shapes
File size: 9.0 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  /// <remarks>
32  /// The algorithm is only implemented for use in the API
33  /// </remarks>
34  /// <typeparam name="T">The type of the solution encoding.</typeparam>
35  public static class DirectedWalk<T> where T : IDeepCloneable {
36    /// <summary>
37    /// Provides regular directed walks between two solutions.
38    /// </summary>
39    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
40    /// <param name="distFunc">The distance function that compares two solutions.</param>
41    /// <param name="neighborFunc">The neighborhood functions that returns _all_ neighbors of a solution.</param>
42    /// <param name="start">The starting solution (to which distance should be increased in every step).</param>
43    /// <param name="startFitness">Fitness value of the starting solution.</param>
44    /// <param name="target">The destination solution (to which distance should be decreased in every step).</param>
45    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
46    /// <returns>The trail of the directed walk including start and target solution.</returns>
47    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) {
48      Func<T, T, bool> restriction = (current, neighbor) => distFunc(neighbor, target) < distFunc(current, target);
49      if (firstImprovement)
50        return DoFirstImprovementWalk(maximization, neighborFunc, start, startFitness, restriction);
51      return DoBestImprovementWalk(maximization, neighborFunc, start, startFitness, restriction);
52    }
53
54    /// <summary>
55    /// Provides regular directed walks between two solutions when already a restricted neighborhood is given.
56    /// </summary>
57    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
58    /// <param name="restrictedNeighborFunc">The neighborhood functions that returns only those neighbors that move closer to some target solution.</param>
59    /// <param name="start">The starting solution.</param>
60    /// <param name="startFitness">Fitness value of the starting solution.</param>
61    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
62    /// <returns>The trail of the directed walk including start solution (and potentially target solution if it was part of the final restricted neighborhood).</returns>
63    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) {
64      if (firstImprovement)
65        return DoFirstImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
66      return DoBestImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
67    }
68
69    /// <summary>
70    /// Provides inverse directed walks away from a starting solution.
71    /// </summary>
72    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
73    /// <param name="distFunc">The distance function that compares two solutions.</param>
74    /// <param name="neighborFunc">The neighborhood functions that returns _all_ neighbors of a solution.</param>
75    /// <param name="start">The starting solution (to which distance should be increased in every step).</param>
76    /// <param name="startFitness">Fitness value of the starting solution.</param>
77    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
78    /// <returns>The trail of the directed walk including start solution.</returns>
79    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) {
80      Func<T, T, bool> inverseRestriction = (current, neighbor) => distFunc(neighbor, start) > distFunc(current, start);
81      if (firstImprovement)
82        return DoFirstImprovementWalk(maximization, neighborFunc, start, startFitness, inverseRestriction);
83      return DoBestImprovementWalk(maximization, neighborFunc, start, startFitness, inverseRestriction);
84    }
85
86    /// <summary>
87    /// Provides inverse directed walks away from a starting solution when already a restricted neighborhood is given.
88    /// </summary>
89    /// <param name="maximization">Whether the problem is a maximization or minimization problem.</param>
90    /// <param name="restrictedNeighborFunc">The neighborhood functions that returns only those neighbors that move away from the start solution.</param>
91    /// <param name="start">The starting solution.</param>
92    /// <param name="startFitness">Fitness value of the starting solution.</param>
93    /// <param name="firstImprovement">Whether the walk should choose the first improving neighbor instead of scanning the whole neighborhood.</param>
94    /// <returns>The trail of the directed walk including start solution.</returns>
95    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) {
96      if (firstImprovement)
97        return DoFirstImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
98      return DoBestImprovementWalk(maximization, restrictedNeighborFunc, start, startFitness, (_, __) => true);
99    }
100
101    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) {
102      var current = Tuple.Create(start, startFitness);
103
104      while (true) {
105        yield return current;
106
107        var iterator = neighborFunc(current).Where(n => restriction(current.Item1, n.Item1)).GetEnumerator();
108        if (!iterator.MoveNext()) yield break;
109
110        current = Tuple.Create((T)iterator.Current.Item1.Clone(), iterator.Current.Item2);
111
112        while (iterator.MoveNext()) {
113          var other = iterator.Current;
114          if (maximization && other.Item2 > current.Item2 || !maximization && other.Item2 < current.Item2) {
115            current = Tuple.Create((T)other.Item1.Clone(), other.Item2);
116          }
117        }
118      }
119    }
120
121    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) {
122      var current = Tuple.Create(start, startFitness);
123
124      while (true) {
125        yield return current;
126
127        var iterator = neighborFunc(current).Where(n => restriction(current.Item1, n.Item1)).GetEnumerator();
128        if (!iterator.MoveNext()) yield break;
129
130        var prevFitness = current.Item2;
131
132        current = Tuple.Create((T)iterator.Current.Item1.Clone(), iterator.Current.Item2);
133
134        if (maximization && current.Item2 > prevFitness || !maximization && current.Item2 < prevFitness)
135          continue;
136
137        while (iterator.MoveNext()) {
138          var other = iterator.Current;
139          if (maximization && other.Item2 > current.Item2 || !maximization && other.Item2 < current.Item2) {
140            current = Tuple.Create((T)other.Item1.Clone(), other.Item2);
141
142            if (maximization && current.Item2 > prevFitness || !maximization && current.Item2 < prevFitness)
143              break;
144          }
145        }
146      }
147    }
148  }
149}
Note: See TracBrowser for help on using the repository browser.