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