Free cookie consent management tool by TermsFeed Policy Generator

source: branches/FitnessLandscapeAnalysis/HeuristicLab.Analysis.FitnessLandscape/BestWorstSelector.cs @ 14710

Last change on this file since 14710 was 7128, checked in by epitzer, 13 years ago

#1696 Integrate fitness landscape analysis plugins from Heureka! repository.

File size: 4.3 KB
Line 
1using System.Collections.Generic;
2using System.Linq;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Data;
6using HeuristicLab.Optimization;
7using HeuristicLab.Parameters;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Selection;
10
11namespace HeuristicLab.Analysis.FitnessLandscape {
12  [Item("BestOrWorstSelector", "A selection operator that moves towards the next local optimum, when found reverses direction and so on.")]
13  [StorableClass]
14  public class BestWorstSelector : SingleObjectiveSelector, ISingleObjectiveSelector {
15
16    #region Parameters
17    public ScopePathLookupParameter<BoolValue> MoveTowardsOptimumParameter {
18      get { return (ScopePathLookupParameter<BoolValue>)Parameters["MoveTowardsOptimum"]; }
19    }
20    public ScopePathLookupParameter<DoubleValue> BaseQualityParameter {
21      get { return (ScopePathLookupParameter<DoubleValue>)Parameters["BaseQuality"]; }
22    }
23    public LookupParameter<IRandom> RandomParameter {
24      get { return (LookupParameter<IRandom>)Parameters["Random"]; }
25    }
26    #endregion
27
28    #region Construction & Cloning
29
30    [StorableConstructor]
31    protected BestWorstSelector(bool deserializing) : base(deserializing) { }
32    protected BestWorstSelector(BestWorstSelector original, Cloner cloner) : base(original, cloner) { }
33    public BestWorstSelector() {
34      Parameters.Add(new ScopePathLookupParameter<BoolValue>("MoveTowardsOptimum", "Specifies whether the selector should optimize towards or away from the optimum."));
35      Parameters.Add(new ScopePathLookupParameter<DoubleValue>("BaseQuality", "The base quality value to compare to. This is required to determine wheter a local optimum has been found and the direction should be reversed."));
36      Parameters.Add(new LookupParameter<IRandom>("Random", "Random number generator"));
37      MoveTowardsOptimumParameter.Hidden = false;
38      BaseQualityParameter.Hidden = false;
39      MoveTowardsOptimumParameter.Path = "..";
40      BaseQualityParameter.Path = "../Remaining/{0}";
41      BaseQualityParameter.ActualName = "Quality";
42      QualityParameter.Hidden = false;
43      MaximizationParameter.Hidden = true;
44      NumberOfSelectedSubScopesParameter.Value = new IntValue(1);
45      NumberOfSelectedSubScopesParameter.Hidden = true;
46    }
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new BestWorstSelector(this, cloner);
49    }
50    #endregion
51
52    protected override IScope[] Select(List<IScope> scopes) {
53      bool optimize = true;
54      if (MoveTowardsOptimumParameter.ActualValue == null) {
55        MoveTowardsOptimumParameter.ActualValue = new BoolValue(true);
56      } else {
57        optimize = MoveTowardsOptimumParameter.ActualValue.Value;
58      }
59      double baseQuality = BaseQualityParameter.ActualValue.Value;
60      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
61      bool copy = CopySelectedParameter.Value.Value;
62      bool maximization = MaximizationParameter.ActualValue.Value;
63      ItemArray<DoubleValue> qualities = QualityParameter.ActualValue;
64      IScope[] selected = new IScope[count];
65      IRandom random = RandomParameter.ActualValue;
66
67      var list = qualities.Select((x, i) => new { Index=i, Quality=x.Value }).ToList();
68      if (random != null)
69        list.Shuffle(random);
70      if (maximization && optimize || !maximization && !optimize) {
71        list = list.OrderByDescending(x => x.Quality).ToList();
72        if (list.Count > 0 && list[0].Quality <= baseQuality)
73          MoveTowardsOptimumParameter.ActualValue = new BoolValue(!optimize);
74      } else {
75        list = list.OrderBy(x => x.Quality).ToList();
76        if (list.Count > 0 && list[0].Quality >= baseQuality)
77          MoveTowardsOptimumParameter.ActualValue = new BoolValue(!optimize);
78      }
79
80      if (copy) {
81        int j = 0;
82        for (int i = 0; i < count; i++) {
83          selected[i] = (IScope)scopes[list[j].Index].Clone();
84          j++;
85          if (j >= list.Count) j = 0;
86        }
87      } else {
88        for (int i = 0; i < count; i++) {
89          selected[i] = scopes[list[0].Index];
90          scopes[list[0].Index] = null;
91          list.RemoveAt(0);
92        }
93        scopes.RemoveAll(x => x == null);
94      }
95
96      return selected;
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.