Free cookie consent management tool by TermsFeed Policy Generator

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

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

#2665 Added PlushEncoding, ZeroErrorDistributionAnalzer

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