1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Random;
|
---|
31 | using HeuristicLab.Selection;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Misc {
|
---|
34 | using HeuristicLab.Problems.ProgramSynthesis.Push.Selector;
|
---|
35 |
|
---|
36 | /// <summary>
|
---|
37 | /// A lexicase selection operator which considers all successful evaluated training cases for selection.
|
---|
38 | ///
|
---|
39 | /// ToDo: LexicaseSelector and ICaseSingleObjectiveSelector are ISingleObjectiveOperator, which contains Maximization and Qualities which is not needed
|
---|
40 | /// </summary>
|
---|
41 | [Item("LexicaseSelector", "A lexicase selection operator which considers all successful evaluated training cases for selection.")]
|
---|
42 | [StorableClass]
|
---|
43 | public sealed class LexicaseSelector : StochasticSingleObjectiveSelector, ICaseSingleObjectiveSelector {
|
---|
44 | public ILookupParameter<ItemArray<DoubleArray>> CaseQualitiesParameter
|
---|
45 | {
|
---|
46 | get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters["CaseQualities"]; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | [StorableConstructor]
|
---|
50 | private LexicaseSelector(bool deserializing) : base(deserializing) { }
|
---|
51 | private LexicaseSelector(LexicaseSelector original, Cloner cloner) : base(original, cloner) { }
|
---|
52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
53 | return new LexicaseSelector(this, cloner);
|
---|
54 | }
|
---|
55 |
|
---|
56 | public LexicaseSelector()
|
---|
57 | : base() {
|
---|
58 | Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>("CaseQualities", "The quality of every single training case for each individual."));
|
---|
59 | }
|
---|
60 |
|
---|
61 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
62 | int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
|
---|
63 | bool copy = CopySelectedParameter.Value.Value;
|
---|
64 | IRandom random = RandomParameter.ActualValue;
|
---|
65 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
66 | List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
|
---|
67 | List<DoubleArray> caseQualities = CaseQualitiesParameter.ActualValue.ToList();
|
---|
68 |
|
---|
69 | // remove scopes, qualities and case qualities, if the case qualities are empty
|
---|
70 | var removeindices = Enumerable.Range(0, caseQualities.Count)
|
---|
71 | .Zip(caseQualities, (i, c) => new { Index = i, CaseQuality = c })
|
---|
72 | .Where(c => c.CaseQuality.Count() == 0)
|
---|
73 | .Select(c => c.Index)
|
---|
74 | .Reverse();
|
---|
75 | foreach (var i in removeindices) {
|
---|
76 | scopes.RemoveAt(i);
|
---|
77 | qualities.RemoveAt(i);
|
---|
78 | caseQualities.RemoveAt(i);
|
---|
79 | }
|
---|
80 |
|
---|
81 | if (caseQualities.Any(x => x.Count() != caseQualities[0].Length)) { throw new ArgumentException("Not all case qualities have the same length"); }
|
---|
82 |
|
---|
83 | IScope[] selected = new IScope[count];
|
---|
84 |
|
---|
85 | for (int i = 0; i < count; i++) {
|
---|
86 | int index = LexicaseSelect(caseQualities, RandomParameter.ActualValue, maximization);
|
---|
87 |
|
---|
88 | if (copy)
|
---|
89 | selected[i] = (IScope)scopes[index].Clone();
|
---|
90 | else {
|
---|
91 | selected[i] = scopes[index];
|
---|
92 | scopes.RemoveAt(index);
|
---|
93 | qualities.RemoveAt(index);
|
---|
94 | caseQualities.RemoveAt(index);
|
---|
95 | }
|
---|
96 | }
|
---|
97 | return selected;
|
---|
98 | }
|
---|
99 |
|
---|
100 | private int LexicaseSelect(List<DoubleArray> caseQualities, IRandom random, bool maximization) {
|
---|
101 | IList<int> candidates = Enumerable.Range(0, caseQualities.Count()).ToList();
|
---|
102 | IEnumerable<int> order = Enumerable.Range(0, caseQualities[0].Count()).Shuffle(random);
|
---|
103 |
|
---|
104 | foreach (int curCase in order) {
|
---|
105 | List<int> nextCandidates = new List<int>();
|
---|
106 | double best = maximization ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
107 | foreach (int candidate in candidates) {
|
---|
108 | if (caseQualities[candidate][curCase].IsAlmost(best)) {
|
---|
109 | // if the individuals is as good as the best one, add it
|
---|
110 | nextCandidates.Add(candidate);
|
---|
111 | } else if (((maximization) && (caseQualities[candidate][curCase] > best)) ||
|
---|
112 | ((!maximization) && (caseQualities[candidate][curCase] < best))) {
|
---|
113 | // if the individuals is better than the best one, remove all previous candidates and add the new one
|
---|
114 | nextCandidates.Clear();
|
---|
115 | nextCandidates.Add(candidate);
|
---|
116 | // also set the nes best quality value
|
---|
117 | best = caseQualities[candidate][curCase];
|
---|
118 | }
|
---|
119 | // else {do nothing}
|
---|
120 | }
|
---|
121 |
|
---|
122 | if (nextCandidates.Count == 1) {
|
---|
123 | return nextCandidates.First();
|
---|
124 | } else if (nextCandidates.Count < 1) {
|
---|
125 | return candidates.SampleRandom(random);
|
---|
126 | }
|
---|
127 | candidates = nextCandidates;
|
---|
128 | }
|
---|
129 |
|
---|
130 |
|
---|
131 | if (candidates.Count == 1) {
|
---|
132 | return candidates.First();
|
---|
133 | }
|
---|
134 | return candidates.SampleRandom(random);
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|