Free cookie consent management tool by TermsFeed Policy Generator

source: branches/PushGP/HeuristicLab.PushGP/HeuristicLab.Problems.ProgramSynthesis/Push/Selector/LexicaseSelector.cs @ 14908

Last change on this file since 14908 was 14908, checked in by pkimmesw, 7 years ago

#2665 Removed "this" qualifier

File size: 5.3 KB
Line 
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
22namespace HeuristicLab.Problems.ProgramSynthesis.Push.Selector {
23  using System;
24  using System.Collections.Generic;
25  using System.Linq;
26
27  using HeuristicLab.Common;
28  using HeuristicLab.Core;
29  using HeuristicLab.Data;
30  using HeuristicLab.Parameters;
31  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32  using HeuristicLab.Problems.ProgramSynthesis.Push.Extensions;
33  using HeuristicLab.Problems.ProgramSynthesis.Push.Problem;
34  using HeuristicLab.Selection;
35  using Random;
36
37  /// <summary>
38  /// A lexicase selection operator which considers all successful evaluated training cases for selection.
39  ///
40  /// ToDo: LexicaseSelector and ICaseSingleObjectiveSelector are ISingleObjectiveOperator, which contains Maximization and Qualities which is not needed
41  /// </summary>
42  [Item("LexicaseSelector", "A lexicase selection operator which considers all successful evaluated training cases for selection.")]
43  [StorableClass]
44  public sealed class LexicaseSelector : StochasticSingleObjectiveSelector, ICaseSingleObjectiveSelector {
45    public ILookupParameter<ItemArray<DoubleArray>> CaseQualitiesParameter
46    {
47      get { return (ILookupParameter<ItemArray<DoubleArray>>)Parameters[PushProblem.CaseQualitiesScopeParameterName]; }
48    }
49
50    [StorableConstructor]
51    private LexicaseSelector(bool deserializing) : base(deserializing) { }
52    private LexicaseSelector(LexicaseSelector original, Cloner cloner) : base(original, cloner) { }
53    public override IDeepCloneable Clone(Cloner cloner) {
54      return new LexicaseSelector(this, cloner);
55    }
56
57    public LexicaseSelector() {
58      Parameters.Add(new ScopeTreeLookupParameter<DoubleArray>(
59        PushProblem.CaseQualitiesScopeParameterName,
60        "The quality of every single training case for each individual."));
61    }
62
63    protected override IScope[] Select(List<IScope> population) {
64      var count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
65      var copy = CopySelectedParameter.Value.Value;
66      var maximization = MaximizationParameter.ActualValue.Value;
67      var random = RandomParameter.ActualValue;
68      var selected = new IScope[count];
69      var caseQualities = CaseQualitiesParameter.ActualValue;
70      var repeats = Math.Ceiling(count / (double)population.Count);
71      var caseCount = caseQualities[0].Length;
72      var source = Enumerable.Range(0, population.Count).ToList();
73
74      for (var k = 0; k < repeats; k++) {
75        // The fitness cases are shuffled.
76        var fitnessCaseIndexes = Enumerable.Range(0, caseCount).Shuffle(random).ToArray();
77
78        // copy list if required
79        var pool = k == repeats - 1 ? source : new List<int>(source);
80        var countLimit = Math.Min(count - k * population.Count, population.Count);
81
82        for (var i = 0; i < countLimit; i++) {
83          var bestIndividuals = pool;
84
85          for (var j = 0; j < fitnessCaseIndexes.Length && bestIndividuals.Count > 1; j++)
86            bestIndividuals = GetBestIndividuals(maximization, caseQualities, bestIndividuals, fitnessCaseIndexes[j]);
87
88          /*  If only one individual remains, it is the chosen parent. If no more fitness cases are left, a parent is
89              chosen randomly from the remaining individuals */
90          var bestIndividualIndex = bestIndividuals.Count == 1 ? bestIndividuals[0] : bestIndividuals.Random(random);
91          var bestIndividual = population[bestIndividualIndex];
92
93          selected[k * population.Count + i] = copy ? (IScope)bestIndividual.Clone() : bestIndividual;
94
95          pool.Remove(bestIndividualIndex);
96        }
97      }
98
99      return selected;
100    }
101
102    private static List<int> GetBestIndividuals(bool maximization, ItemArray<DoubleArray> caseQualities, List<int> bestIndividuals, int index) {
103      var bestFitness = maximization ? double.NegativeInfinity : double.PositiveInfinity;
104      var result = new List<int>();
105
106      for (var l = 0; l < bestIndividuals.Count; l++) {
107        var individual = bestIndividuals[l];
108        var caseQuality = caseQualities[individual][index];
109
110        if (bestFitness.IsAlmost(caseQuality)) {
111          result.Add(individual);
112        } else if (maximization && bestFitness < caseQuality ||
113                  !maximization && bestFitness > caseQuality) {
114          bestFitness = caseQuality;
115          result.Clear();
116          result.Add(individual);
117        }
118
119        bestIndividuals = result;
120      }
121
122      return bestIndividuals;
123    }
124  }
125}
Note: See TracBrowser for help on using the repository browser.