Free cookie consent management tool by TermsFeed Policy Generator

Changeset 16137


Ignore:
Timestamp:
09/12/18 15:12:29 (6 years ago)
Author:
abeham
Message:

#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
Location:
branches/2457_ExpertSystem
Files:
16 added
3 deleted
10 edited

Legend:

Unmodified
Added
Removed
  • branches/2457_ExpertSystem

    • Property svn:ignore
      •  

        old new  
        55.gitignore
        66.vs
         7packages
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/Algorithms/DirectedWalk.cs

    r16096 r16137  
    2929  /// A generic implementation of a directed walk that can still be used in an efficient manner.
    3030  /// </summary>
     31  /// <remarks>
     32  /// The algorithm is only implemented for use in the API
     33  /// </remarks>
    3134  /// <typeparam name="T">The type of the solution encoding.</typeparam>
    3235  public static class DirectedWalk<T> where T : IDeepCloneable {
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/Analysis/InformationAnalysis.cs

    r14678 r16137  
    3838
    3939    public List<double> InformationContent { get; private set; }
     40    public List<double> SymmetricInformationContent { get; private set; }
    4041    public List<double> PartialInformationContent { get; private set; }
    4142    public List<double> DensityBasinInformation { get; private set; }
     43    public List<double> SymmetricDensityBasinInformation { get; private set; }
    4244    public List<double> TotalEntropy { get; private set; }
     45    public List<double> SymmetricTotalEntropy { get; private set; }
    4346    public List<double> QualityDelta { get; private set; }
    4447    public double InformationStability { get; private set; }
     
    4649    public int Diversity { get; private set; }
    4750    public Peak PeakInformationContent { get; private set; }
    48     public Peak PeakPartialInformationContent { get; private set; }
     51    public Peak PeakSymmetricInformationContent { get; private set; }
    4952    public Peak PeakDensityBasinInformation { get; private set; }
     53    public Peak PeakSymmetricDensityBasinInformation { get; private set; }
    5054    public Peak PeakTotalEntropy { get; private set; }
    51 
    52     public InformationAnalysis(IList<double> qualities, int nQuantiles, int shapeSize) {
    53       InformationContent = new List<double>();
    54       PartialInformationContent = new List<double>();
    55       DensityBasinInformation = new List<double>();
    56       TotalEntropy = new List<double>();
    57       QualityDelta = new List<double>();
    58       if (shapeSize < 1)
    59         throw new ArgumentException("Shape size must be at least 1 (better 2)");
     55    public Peak PeakSymmetricTotalEntropy { get; private set; }
     56
     57    public InformationAnalysis(IList<double> qualities, int nQuantiles = 20) {
     58      InformationContent = new List<double>(nQuantiles);
     59      SymmetricInformationContent = new List<double>(nQuantiles);
     60      PartialInformationContent = new List<double>(nQuantiles);
     61      DensityBasinInformation = new List<double>(nQuantiles);
     62      SymmetricDensityBasinInformation = new List<double>(nQuantiles);
     63      TotalEntropy = new List<double>(nQuantiles);
     64      SymmetricTotalEntropy = new List<double>(nQuantiles);
     65      QualityDelta = new List<double>(nQuantiles);
    6066      if (qualities.Count > 1)
    61         PerformAnalysis(qualities, nQuantiles, shapeSize);
    62     }
    63 
    64     private void PerformAnalysis(IList<double> qualities, int nQuantiles, int shapeSize) {
     67        PerformAnalysis(qualities, nQuantiles);
     68    }
     69
     70    private void PerformAnalysis(IList<double> qualities, int nQuantiles) {
    6571      var differences = Differences(qualities).ToList();
    6672      InformationStability = differences.Select(Math.Abs).Max();
     
    8086          PartialInformationContent.DuplicateLast();
    8187        } else {
    82           var slopes = Slopes(eps, differences).ToList();
    83           var shapes = Shapes(shapeSize, slopes).ToList();
    84           var shapeCounts = CountShapes(shapes);
     88          var slopes = ToSlopes(eps, differences).ToList();
     89          var shapes = ToShapes(slopes).ToList();
     90          var symmetricShapes = shapes.Select(x => Shape.GetSymmetric(x)).ToList();
     91
     92          var shapeFreqs = GetFrequencies(shapes);
     93          var symShapeFreqs = GetFrequencies(symmetricShapes);
     94
     95          foreach (var symShape in Shape.Symmetric.Where(x => x.Sum > 0)) {
     96            if (symShapeFreqs.TryGetValue(symShape, out var freq)) {
     97              symShapeFreqs[symShape] = freq / 2;
     98            }
     99          }
    85100          QualityDelta.Add(eps);
    86           InformationContent.Add(CalculateEntropy(Shape.GetAll(shapeSize, Shape.Form.NonUni), shapeCounts, shapes.Count));
    87           DensityBasinInformation.Add(CalculateEntropy(Shape.GetAll(shapeSize, Shape.Form.Uni), shapeCounts, shapes.Count));
    88           TotalEntropy.Add(CalculateEntropy(Shape.GetAll(shapeSize, Shape.Form.Any), shapeCounts, shapes.Count));
     101
     102          InformationContent.Add(CalculateEntropy(shapeFreqs, Shape.NonUniform.ToList(), shapes.Count));
     103          SymmetricInformationContent.Add(CalculateEntropy(symShapeFreqs, Shape.SymmetricNonUniform.ToList(), symmetricShapes.Count));
     104
     105          DensityBasinInformation.Add(CalculateEntropy(shapeFreqs, Shape.Uniform.ToList(), shapes.Count));
     106          SymmetricDensityBasinInformation.Add(CalculateEntropy(symShapeFreqs, Shape.SymmetricUniform.ToList(), symmetricShapes.Count));
     107
     108          TotalEntropy.Add(CalculateEntropy(shapeFreqs, Shape.All.ToList(), shapes.Count));
     109          SymmetricTotalEntropy.Add(CalculateEntropy(symShapeFreqs, Shape.Symmetric.ToList(), symmetricShapes.Count));
     110
    89111          PartialInformationContent.Add(CalculatePartialInformationContent(eps, differences));
    90112        }
    91113      }
    92114      PeakInformationContent = GetPeak(QualityDelta, InformationContent);
     115      PeakSymmetricInformationContent = GetPeak(QualityDelta, SymmetricInformationContent);
    93116      PeakDensityBasinInformation = GetPeak(QualityDelta, DensityBasinInformation);
    94       PeakPartialInformationContent = GetPeak(QualityDelta, PartialInformationContent);
     117      PeakSymmetricDensityBasinInformation = GetPeak(QualityDelta, SymmetricDensityBasinInformation);
    95118      PeakTotalEntropy = GetPeak(QualityDelta, TotalEntropy);
     119      PeakSymmetricTotalEntropy = GetPeak(QualityDelta, SymmetricTotalEntropy);
    96120    }
    97121
     
    107131    }
    108132
    109     public class Shape : IEnumerable<Slope>, IComparable<Shape> {
    110 
    111       #region types, fields and properties
    112       public enum Form { Uni, NonUni, Any }
    113 
    114       private readonly Slope[] slopes;
    115 
    116       private static readonly Dictionary<Tuple<Form, int>, IList<Shape>> SHAPES =
    117         new Dictionary<Tuple<Form, int>, IList<Shape>>();
    118       #endregion
    119 
    120       public Shape(IEnumerable<Slope> slopes) {
    121         this.slopes = slopes.ToArray();
    122       }
    123 
    124       #region static methods
    125 
    126       public static Shape Get(params Slope[] slopes) {
    127         return new Shape(slopes);
    128       }
    129 
    130       public static IList<Shape> GetAll(int size, Form type) {
    131         var key = Tuple.Create(type, size);
    132         IList<Shape> allShapes;
    133         if (!SHAPES.TryGetValue(key, out allShapes)) {
    134           allShapes = CreateAll(size, type).ToList();
    135           SHAPES[key] = allShapes;
     133    public class Shape {
     134      private static readonly Shape[] s = new Shape[] {
     135        new Shape() { First = Slope.Down, Second = Slope.Down }, // 0
     136        new Shape() { First = Slope.Down, Second = Slope.Flat }, // 1
     137        new Shape() { First = Slope.Down, Second = Slope.Up   }, // 2
     138        new Shape() { First = Slope.Flat, Second = Slope.Down }, // 3
     139        new Shape() { First = Slope.Flat, Second = Slope.Flat }, // 4
     140        new Shape() { First = Slope.Flat, Second = Slope.Up   }, // 5
     141        new Shape() { First = Slope.Up,   Second = Slope.Down }, // 6
     142        new Shape() { First = Slope.Up,   Second = Slope.Flat }, // 7
     143        new Shape() { First = Slope.Up,   Second = Slope.Up   }, // 8
     144      };
     145      public static IEnumerable<Shape> All { get { return s; } }
     146      public static IEnumerable<Shape> Uniform { get { return All.Where(x => x.First == x.Second); } }
     147      public static IEnumerable<Shape> NonUniform { get { return All.Where(x => x.First != x.Second); } }
     148      public static IEnumerable<Shape> Symmetric { get { return All.Where(x => x.Sum >= 0); } }
     149      public static IEnumerable<Shape> SymmetricUniform { get { return Uniform.Where(x => x.First != Slope.Down); } }
     150      public static IEnumerable<Shape> SymmetricNonUniform { get { return NonUniform.Where(x => x.Sum >= 0); } }
     151
     152      public Slope First { get; private set; }
     153      public Slope Second { get; private set; }
     154
     155      internal int Sum { get { return (int)First + (int)Second; } }
     156
     157      public IEnumerable<Slope> Slopes {
     158        get {
     159          yield return First;
     160          yield return Second;
    136161        }
    137         return allShapes;
    138       }
    139 
    140       private static IEnumerable<Shape> CreateAll(int size, Form type) {
    141         if (size == 0) {
    142           yield return Get();
    143         } else {
    144           foreach (var s in CreateAll(size - 1, type)) {
    145             foreach (var s2 in s.ExtendAll(type))
    146               yield return s2;
    147           }
    148         }
    149       }
    150       #endregion
    151 
    152       private Shape Extend(Slope s) {
    153         return new Shape(slopes.Concat(new[] { s }));
    154       }
    155 
    156       private IEnumerable<Shape> ExtendAll(Form t) {
    157         if (Length == 0 || t == Form.Any) {
    158           yield return Extend(Slope.Up);
    159           yield return Extend(Slope.Flat);
    160           yield return Extend(Slope.Down);
    161         } else if (t == Form.Uni) {
    162           yield return Extend(slopes[0]);
    163         } else if (t == Form.NonUni) {
    164           if (slopes.Last() != Slope.Up) yield return Extend(Slope.Up);
    165           if (slopes.Last() != Slope.Flat) yield return Extend(Slope.Flat);
    166           if (slopes.Last() != Slope.Down) yield return Extend(Slope.Down);
    167         }
    168       }
    169 
    170       public int Length {
    171         get { return slopes.Length; }
    172       }
    173 
    174       public Slope this[int i] {
    175         get { return slopes[i]; }
    176       }
    177 
    178       #region IEnumerable Members
    179       public IEnumerator<Slope> GetEnumerator() {
    180         return (IEnumerator<Slope>)slopes.GetEnumerator();
    181       }
    182       System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() {
    183         return GetEnumerator();
    184       }
    185       #endregion
    186 
    187       #region IComparable Members
    188 
    189       public int CompareTo(Shape other) {
    190         if (other.Length < Length)
    191           return -1;
    192         if (other.Length > Length)
    193           return 1;
    194         for (var i = 0; i < Length; i++) {
    195           var d = slopes[i].CompareTo(other.slopes[i]);
    196           if (d != 0)
    197             return d;
    198         }
    199         return 0;
    200       }
    201       public override bool Equals(object obj) {
    202         var s = obj as Shape;
    203         if (s == null) return false;
    204         return CompareTo(s) == 0;
    205       }
    206       public override int GetHashCode() {
    207         return slopes.Aggregate(0, (a, s) => a * 3 + ((int)s + 1)).GetHashCode();
    208       }
    209 
    210       #endregion
    211 
    212       private string asString;
     162      }
     163
     164      private Shape() {
     165
     166      }
     167
     168      public static Shape Get(Slope first, Slope second) {
     169        var a = 1 + (int)first; var b = 1 + (int)second;
     170        return s[a * 3 + b];
     171      }
     172
     173      public static Shape GetSymmetric(Shape shape) {
     174        if (shape.Sum >= 0) return shape;
     175        if (shape.Sum == -2) return s[8]; // both are Slope.Down
     176        if (shape.First == Slope.Down) return s[5];
     177        return s[7]; // shape.Second == Slope.Down
     178      }
     179
    213180      public override string ToString() {
    214         return asString ?? (asString = string.Join("", slopes.Select(s => (s == Slope.Down ? "\\" : (s == Slope.Up ? "/" : "-")))));
    215       }
    216     }
    217 
    218     private static IEnumerable<Slope> Slopes(double eps, IEnumerable<double> differences) {
     181        return string.Join("", Slopes.Select(s => (s == Slope.Down ? "\\" : (s == Slope.Up ? "/" : "-"))));
     182      }
     183    }
     184
     185   
     186
     187    private static IEnumerable<Slope> ToSlopes(double eps, IEnumerable<double> differences) {
    219188      return differences.Select(d => (d > eps ? Slope.Up : (d < -eps ? Slope.Down : Slope.Flat)));
    220189    }
    221190
    222     private static IEnumerable<Shape> Shapes(int size, IEnumerable<Slope> slopes) {
    223       var q = new Queue<Slope>();
    224       foreach (var s in slopes) {
    225         q.Enqueue(s);
    226         if (q.Count < size) continue;
    227         yield return new Shape(q);
    228         q.Dequeue();
    229       }
    230     }
    231 
    232     private static double CalculateEntropy(IList<Shape> shapes, Dictionary<Shape, int> shapeCounts, int totalNShapes) {
    233       return shapes.Aggregate(0.0, (current, s) => current - Entropy(shapeCounts.GetValueOrDefault(s, 0), totalNShapes, shapes.Count));
     191    private static IEnumerable<Shape> ToShapes(IEnumerable<Slope> slopes) {
     192      var iter = slopes.GetEnumerator();
     193      if (!iter.MoveNext()) yield break;
     194      var prev = iter.Current;
     195      while (iter.MoveNext()) {
     196        var cur = iter.Current;
     197        yield return Shape.Get(prev, cur);
     198        prev = cur;
     199      }
     200    }
     201
     202    private static double CalculateEntropy(Dictionary<Shape, int> shapeCounts, ICollection<Shape> shapeTypes, int totalShapes) {
     203      return shapeTypes.Where(x => shapeCounts.ContainsKey(x))
     204        .Aggregate(0.0, (current, s) => current - Entropy(shapeCounts[s], totalShapes, shapeTypes.Count));
    234205    }
    235206
    236207    private static double CalculatePartialInformationContent(double eps, ICollection<double> differences) {
    237       int slope = 0;
    238       int nPeaks = 0;
    239       foreach (var d in differences) {
     208      var iter = differences.GetEnumerator();
     209      if (!iter.MoveNext()) return 0;
     210
     211      var slope = iter.Current < -eps ? -1 : (iter.Current > eps ? 1 : 0);
     212      var nPeaks = Math.Abs(slope);
     213      var count = 1;
     214      while (iter.MoveNext()) {
     215        var d = iter.Current;
    240216        if (d > eps) {
    241217          if (slope < 0) nPeaks++;
     
    245221          slope = -1;
    246222        }
    247       }
    248       return 1.0 * nPeaks / differences.Count;
    249     }
    250 
    251     private static Dictionary<Shape, int> CountShapes(IEnumerable<Shape> shapes) {
    252       var shapeCounts = new Dictionary<Shape, int>();
    253       foreach (var group in shapes.GroupBy(s => s))
    254         shapeCounts[group.Key] = group.Count();
    255       return shapeCounts;
     223        count++;
     224      }
     225      return nPeaks / (double)count;
     226    }
     227
     228    private static Dictionary<Shape, int> GetFrequencies(IEnumerable<Shape> shapes) {
     229      return shapes.GroupBy(x => x).ToDictionary(x => x.Key, x => x.Count());
    256230    }
    257231
    258232    private static double Entropy(int count, int total, int nCases) {
    259       if (count == 0)
    260         return 0;
    261       double freq = 1.0 * count / total;
     233      if (count == 0) return 0;
     234      var freq = 1.0 * count / total;
    262235      return freq * Math.Log(freq, nCases);
    263236    }
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/Analysis/InformationAnalyzer.cs

    r16096 r16137  
    9191
    9292      Parameters.Add(new ValueLookupParameter<IntValue>("NQuantiles", "The number of delta quantiles to display", new IntValue(20)));
    93       Parameters.Add(new ValueLookupParameter<IntValue>("ShapeSize", "The number of slopes to consider as shapes.", new IntValue(2)));
    9493
    9594      Parameters.Add(new LookupParameter<DoubleValue>("InformationContent", "The information content H(0) at eps = 0"));
     
    118117
    119118      var nQuantiles = NQuantilesParameter.ActualValue.Value;
    120       var shapeSize = ShapeSizeParameter.ActualValue.Value;
    121119      var results = ResultsParameter.ActualValue;
    122120
    123       var analysis = new InformationAnalysis(qualities, nQuantiles, shapeSize);
     121      var analysis = new InformationAnalysis(qualities, nQuantiles);
    124122      var ic = new DoubleValue(analysis.InformationContent.FirstOrDefault());
    125123      InformationContentParameter.ActualValue = ic;
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/CharacteristicCalculator/AdaptiveWalkCalculator.cs

    r14776 r16137  
    2020#endregion
    2121
     22using System.Collections.Generic;
     23using System.Linq;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     
    2527using HeuristicLab.Optimization;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using System;
    28 using System.Collections.Generic;
    29 using System.Linq;
    30 using System.Threading;
    3129
    3230namespace HeuristicLab.Analysis.FitnessLandscape {
    3331  [Item("Adaptive Walk Calculator", "Calculates characteristics from an adaptive walk.")]
    3432  [StorableClass]
    35   public class AdaptiveWalkCalculator : NamedItem, ICharacteristicCalculator {
    36 
    37     [Storable]
    38     private IProblem problem;
    39     public IProblem Problem {
    40       get { return problem; }
    41       set {
    42         if (problem == value) return;
    43         problem = value;
    44         var soProblem = problem as ISingleObjectiveHeuristicOptimizationProblem;
    45         walker.Problem = soProblem;
    46       }
    47     }
    48 
    49     [Storable]
    50     private AdaptiveWalk walker;
     33  public sealed class AdaptiveWalkCalculator : AlgorithmCharacteristicCalculator<AdaptiveWalk> {
    5134
    5235    [StorableConstructor]
    5336    private AdaptiveWalkCalculator(bool deserializing) : base(deserializing) { }
    5437    private AdaptiveWalkCalculator(AdaptiveWalkCalculator original, Cloner cloner)
    55       : base(original, cloner) {
    56       problem = cloner.Clone(original.problem);
    57       walker = cloner.Clone(original.walker);
    58       characteristics = cloner.Clone(original.characteristics);
    59     }
     38      : base(original, cloner) { }
    6039    public AdaptiveWalkCalculator() : this(new AdaptiveWalk()) { }
    61     public AdaptiveWalkCalculator(AdaptiveWalk walker) {
    62       Name = ItemName;
    63       Description = ItemDescription;
    64       this.walker = walker;
     40    public AdaptiveWalkCalculator(AdaptiveWalk walker) : base(walker) {
    6541      characteristics = new CheckedItemList<StringValue>(
    6642        new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
     
    7450    }
    7551
    76     private CheckedItemList<StringValue> characteristics;
    77     public ReadOnlyCheckedItemList<StringValue> Characteristics {
    78       get { return characteristics.AsReadOnly(); }
     52    public override bool CanCalculate() {
     53      return base.CanCalculate() && Problem.Operators.Any(x => x is IManipulator);
    7954    }
    8055
    81     public bool CanCalculate() {
    82       return Problem is ISingleObjectiveHeuristicOptimizationProblem
    83         && Problem.Operators.Any(x => x is IManipulator);
    84     }
    85 
    86     public IEnumerable<IResult> Calculate() {
    87       walker.Prepare(true);
    88       using (var waitHandle = new AutoResetEvent(false)) {
    89         EventHandler evHandle = (sender, e) => {
    90           if (walker.ExecutionState == ExecutionState.Paused
    91           || walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
    92         };
    93         walker.ExecutionStateChanged += evHandle;
    94         walker.Start();
    95         waitHandle.WaitOne();
    96         walker.ExecutionStateChanged -= evHandle;
    97       }
    98       foreach (var p in characteristics.CheckedItems) {
    99         yield return new Result("AdaptiveWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value, walker.Results[p.Value.Value].Value);
    100       }
    101       walker.Prepare(true);
    102     }
    103 
    104     public void CollectParameterValues(IDictionary<string, IItem> values) {
    105       walker.CollectParameterValues(values);
    106     }
    107 
    108     public IKeyedItemCollection<string, IParameter> Parameters {
    109       get { return ((IParameterizedItem)walker).Parameters; }
     56    public override IEnumerable<IResult> Calculate() {
     57      foreach (var result in base.Calculate())
     58        yield return new Result("AdaptiveWalk." + Algorithm.MutatorParameter.Value.Name + "." + result.Name, result.Value);
    11059    }
    11160  }
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/CharacteristicCalculator/RandomWalkCalculator.cs

    r14690 r16137  
    2020#endregion
    2121
     22using System.Collections.Generic;
     23using System.Linq;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     
    2527using HeuristicLab.Optimization;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using System;
    28 using System.Collections.Generic;
    29 using System.Linq;
    30 using System.Threading;
    3129
    3230namespace HeuristicLab.Analysis.FitnessLandscape {
    3331  [Item("Random Walk Calculator", "Calculates characteristics from a random walk.")]
    3432  [StorableClass]
    35   public class RandomWalkCalculator : NamedItem, ICharacteristicCalculator {
    36 
    37     [Storable]
    38     private IProblem problem;
    39     public IProblem Problem {
    40       get { return problem; }
    41       set {
    42         if (problem == value) return;
    43         problem = value;
    44         var soProblem = problem as ISingleObjectiveHeuristicOptimizationProblem;
    45         walker.Problem = soProblem;
    46       }
    47     }
    48 
    49     [Storable]
    50     private RandomWalk walker;
     33  public sealed class RandomWalkCalculator : AlgorithmCharacteristicCalculator<RandomWalk> {
    5134
    5235    [StorableConstructor]
    5336    private RandomWalkCalculator(bool deserializing) : base(deserializing) { }
    54     private RandomWalkCalculator(RandomWalkCalculator original, Cloner cloner)
    55       : base(original, cloner) {
    56       problem = cloner.Clone(original.problem);
    57       walker = cloner.Clone(original.walker);
    58       characteristics = cloner.Clone(original.characteristics);
    59     }
     37    private RandomWalkCalculator(RandomWalkCalculator original, Cloner cloner) : base(original, cloner) { }
    6038    public RandomWalkCalculator() : this(new RandomWalk()) { }
    61     public RandomWalkCalculator(RandomWalk walker) {
     39    public RandomWalkCalculator(RandomWalk walker) : base(walker) {
    6240      Name = ItemName;
    6341      Description = ItemDescription;
    64       this.walker = walker;
    6542      characteristics = new CheckedItemList<StringValue>(
    6643        new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
     
    7451    }
    7552
    76     private CheckedItemList<StringValue> characteristics;
    77     public ReadOnlyCheckedItemList<StringValue> Characteristics {
    78       get { return characteristics.AsReadOnly(); }
     53    public override bool CanCalculate() {
     54      return base.CanCalculate() && Problem.Operators.Any(x => x is IManipulator);
    7955    }
    8056
    81     public bool CanCalculate() {
    82       return Problem is ISingleObjectiveHeuristicOptimizationProblem
    83         && Problem.Operators.Any(x => x is IManipulator);
    84     }
    85 
    86     public IEnumerable<IResult> Calculate() {
    87       walker.Prepare(true);
    88       using (var waitHandle = new AutoResetEvent(false)) {
    89         EventHandler evHandle = (sender, e) => {
    90           if (walker.ExecutionState == ExecutionState.Paused
    91           || walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
    92         };
    93         walker.ExecutionStateChanged += evHandle;
    94         try {
    95           walker.Start();
    96           waitHandle.WaitOne();
    97         } finally { walker.ExecutionStateChanged -= evHandle; }
    98       }
    99       foreach (var p in characteristics.CheckedItems) {
    100         var resultName = "RandomWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value;
    101         IResult result;
    102         if (walker.Results.TryGetValue(p.Value.Value, out result)) {
    103           yield return new Result(resultName, result.Value);
    104         } else yield return new Result(resultName, new DoubleValue(0));
    105       }
    106       walker.Prepare(true);
    107     }
    108 
    109     public void CollectParameterValues(IDictionary<string, IItem> values) {
    110       walker.CollectParameterValues(values);
    111     }
    112 
    113     public IKeyedItemCollection<string, IParameter> Parameters {
    114       get { return ((IParameterizedItem)walker).Parameters; }
     57    public override IEnumerable<IResult> Calculate() {
     58      foreach (var result in base.Calculate())
     59        yield return new Result("RandomWalk." + Algorithm.MutatorParameter.Value.Name + "." + result.Name, result.Value);
    11560    }
    11661  }
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/CharacteristicCalculator/UpDownWalkCalculator.cs

    r15031 r16137  
    2020#endregion
    2121
     22using System.Collections.Generic;
     23using System.Linq;
    2224using HeuristicLab.Common;
    2325using HeuristicLab.Core;
     
    2527using HeuristicLab.Optimization;
    2628using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
    27 using System;
    28 using System.Collections.Generic;
    29 using System.Linq;
    30 using System.Threading;
    3129
    3230namespace HeuristicLab.Analysis.FitnessLandscape {
    3331  [Item("Up/Down Walk Calculator", "Calculates characteristics from an up/down walk.")]
    3432  [StorableClass]
    35   public class UpDownWalkCalculator : NamedItem, ICharacteristicCalculator {
    36 
    37     [Storable]
    38     private IProblem problem;
    39     public IProblem Problem {
    40       get { return problem; }
    41       set {
    42         if (problem == value) return;
    43         problem = value;
    44         var soProblem = problem as ISingleObjectiveHeuristicOptimizationProblem;
    45         walker.Problem = soProblem;
    46       }
    47     }
    48 
    49     [Storable]
    50     private UpDownWalk walker;
     33  public sealed class UpDownWalkCalculator : AlgorithmCharacteristicCalculator<UpDownWalk> {
    5134
    5235    [StorableConstructor]
    5336    private UpDownWalkCalculator(bool deserializing) : base(deserializing) { }
    54     private UpDownWalkCalculator(UpDownWalkCalculator original, Cloner cloner)
    55       : base(original, cloner) {
    56       problem = cloner.Clone(original.problem);
    57       walker = cloner.Clone(original.walker);
    58       characteristics = cloner.Clone(original.characteristics);
    59     }
     37    private UpDownWalkCalculator(UpDownWalkCalculator original, Cloner cloner) : base(original, cloner) { }
    6038    public UpDownWalkCalculator() : this(new UpDownWalk()) { }
    61     public UpDownWalkCalculator(UpDownWalk walker) {
     39    public UpDownWalkCalculator(UpDownWalk walker) : base(walker) {
    6240      Name = ItemName;
    6341      Description = ItemDescription;
    64       this.walker = walker;
    6542      characteristics = new CheckedItemList<StringValue>(
    6643        new[] { "AutoCorrelation1", "CorrelationLength", "InformationContent",
     
    7552    }
    7653
    77     private CheckedItemList<StringValue> characteristics;
    78     public ReadOnlyCheckedItemList<StringValue> Characteristics {
    79       get { return characteristics.AsReadOnly(); }
     54    public override bool CanCalculate() {
     55      return base.CanCalculate() && Problem.Operators.Any(x => x is IManipulator);
    8056    }
    8157
    82     public bool CanCalculate() {
    83       return Problem is ISingleObjectiveHeuristicOptimizationProblem
    84         && Problem.Operators.Any(x => x is IManipulator);
    85     }
    86 
    87     public IEnumerable<IResult> Calculate() {
    88       walker.Prepare(true);
    89       using (var waitHandle = new AutoResetEvent(false)) {
    90         EventHandler evHandle = (sender, e) => {
    91           if (walker.ExecutionState == ExecutionState.Paused
    92           || walker.ExecutionState == ExecutionState.Stopped) waitHandle.Set();
    93         };
    94         walker.ExecutionStateChanged += evHandle;
    95         walker.Start();
    96         waitHandle.WaitOne();
    97         walker.ExecutionStateChanged -= evHandle;
    98       }
    99       foreach (var p in characteristics.CheckedItems) {
    100         yield return new Result("UpDownWalk." + walker.MutatorParameter.Value.Name + "." + p.Value.Value, walker.Results[p.Value.Value].Value);
    101       }
    102       walker.Prepare(true);
    103     }
    104 
    105     public void CollectParameterValues(IDictionary<string, IItem> values) {
    106       walker.CollectParameterValues(values);
    107     }
    108 
    109     public IKeyedItemCollection<string, IParameter> Parameters {
    110       get { return ((IParameterizedItem)walker).Parameters; }
     58    public override IEnumerable<IResult> Calculate() {
     59      foreach (var result in base.Calculate())
     60        yield return new Result("UpDownWalk." + Algorithm.MutatorParameter.Value.Name + "." + result.Name, result.Value);
    11161    }
    11262  }
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/HeuristicLab.Analysis.FitnessLandscape-3.3.csproj

    r16096 r16137  
    187187  <ItemGroup>
    188188    <Compile Include="Algorithms\AdaptiveWalk.cs" />
    189     <Compile Include="Algorithms\FLACharacterizer.cs" />
     189    <Compile Include="CharacteristicCalculator\FLACharacterizer.cs" />
    190190    <Compile Include="Algorithms\UpDownWalk.cs" />
    191191    <Compile Include="Algorithms\RandomWalk.cs" />
     
    201201    <Compile Include="Algorithms\LocalAnalysisMainLoop.cs" />
    202202    <Compile Include="Analysis\InformationAnalyzer.cs" />
    203     <Compile Include="Analysis\UniqueThresholdCalculator.cs" />
     203    <Compile Include="CharacteristicCalculator\AlgorithmCharacteristicCalculator.cs" />
     204    <Compile Include="Utilities\UniqueThresholdCalculator.cs" />
    204205    <Compile Include="CharacteristicCalculator\AdaptiveWalkCalculator.cs" />
    205206    <Compile Include="CharacteristicCalculator\UpDownWalkCalculator.cs" />
    206207    <Compile Include="CharacteristicCalculator\RandomWalkCalculator.cs" />
    207     <Compile Include="ProblemCharacteristicAnalysis\CharacteristicCalculator.cs" />
    208     <Compile Include="ProblemCharacteristicAnalysis\CurveAnalysis.cs" />
     208    <Compile Include="CharacteristicCalculator\CharacteristicCalculator.cs" />
     209    <Compile Include="Utilities\CurveAnalysis.cs" />
    209210    <Compile Include="Algorithms\DirectedWalk.cs" />
    210     <Compile Include="ProblemCharacteristicAnalysis\DoubleMatrixCharacteristicCalculator.cs" />
    211     <Compile Include="ProblemCharacteristicAnalysis\PermutationPathAnalysis.cs" />
    212     <Compile Include="ProblemCharacteristicAnalysis\QAP\QAPCharacteristicCalculator.cs" />
    213     <Compile Include="ProblemCharacteristicAnalysis\QAP\QAPDirectedWalk.cs" />
     211    <Compile Include="Utilities\DoubleMatrixCharacteristics.cs" />
     212    <Compile Include="CharacteristicCalculator\QAP\QAPCharacteristicCalculator.cs" />
     213    <Compile Include="CharacteristicCalculator\QAP\QAPDirectedWalk.cs" />
    214214    <Compile Include="ProblemInstanceAnalysis\DirectedPath.cs" />
    215215    <Compile Include="ProblemInstanceAnalysis\ProblemInstanceAnalyzer.cs" />
  • branches/2457_ExpertSystem/HeuristicLab.Analysis.FitnessLandscape/3.3/ProblemInstanceAnalysis/QAPPRProblemInstanceAnalyzer.cs

    r14691 r16137  
    4949      if (trajectories.Count == 0) return null;
    5050
    51       return new DoubleArray(PermutationPathAnalysis.GetCharacteristics(trajectories).GetValues().ToArray());
     51      var result = CurveAnalysis<Permutation>.GetCharacteristics(trajectories, (left, right) => (1.0 - HammingSimilarityCalculator.CalculateSimilarity(left, right)) * left.Length);
     52      return new DoubleArray(result.GetValues().ToArray());
    5253    }
    5354  }
  • branches/2457_ExpertSystem/PerformanceComparison.sln

    r15253 r16137  
    2929EndProject
    3030Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HeuristicLab.Encodings.LinearLinkageEncoding-3.4", "HeuristicLab.Encodings.LinearLinkageEncoding\3.4\HeuristicLab.Encodings.LinearLinkageEncoding-3.4.csproj", "{BE698769-975A-429E-828C-72BB2B6182C8}"
     31EndProject
     32Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTests", "UnitTests\UnitTests.csproj", "{76241AF2-B428-4127-805D-667F79983F0F}"
    3133EndProject
    3234Global
     
    196198    {BE698769-975A-429E-828C-72BB2B6182C8}.Release|x86.ActiveCfg = Release|x86
    197199    {BE698769-975A-429E-828C-72BB2B6182C8}.Release|x86.Build.0 = Release|x86
     200    {76241AF2-B428-4127-805D-667F79983F0F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
     201    {76241AF2-B428-4127-805D-667F79983F0F}.Debug|Any CPU.Build.0 = Debug|Any CPU
     202    {76241AF2-B428-4127-805D-667F79983F0F}.Debug|x64.ActiveCfg = Debug|Any CPU
     203    {76241AF2-B428-4127-805D-667F79983F0F}.Debug|x64.Build.0 = Debug|Any CPU
     204    {76241AF2-B428-4127-805D-667F79983F0F}.Debug|x86.ActiveCfg = Debug|Any CPU
     205    {76241AF2-B428-4127-805D-667F79983F0F}.Debug|x86.Build.0 = Debug|Any CPU
     206    {76241AF2-B428-4127-805D-667F79983F0F}.Release|Any CPU.ActiveCfg = Release|Any CPU
     207    {76241AF2-B428-4127-805D-667F79983F0F}.Release|Any CPU.Build.0 = Release|Any CPU
     208    {76241AF2-B428-4127-805D-667F79983F0F}.Release|x64.ActiveCfg = Release|Any CPU
     209    {76241AF2-B428-4127-805D-667F79983F0F}.Release|x64.Build.0 = Release|Any CPU
     210    {76241AF2-B428-4127-805D-667F79983F0F}.Release|x86.ActiveCfg = Release|Any CPU
     211    {76241AF2-B428-4127-805D-667F79983F0F}.Release|x86.Build.0 = Release|Any CPU
    198212  EndGlobalSection
    199213  GlobalSection(SolutionProperties) = preSolution
    200214    HideSolutionNode = FALSE
    201215  EndGlobalSection
     216  GlobalSection(ExtensibilityGlobals) = postSolution
     217    SolutionGuid = {C8E53BE4-B79D-431E-AFE7-03BB2062436C}
     218  EndGlobalSection
    202219EndGlobal
Note: See TracChangeset for help on using the changeset viewer.