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 | namespace 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 selected = Apply(
|
---|
63 | population,
|
---|
64 | NumberOfSelectedSubScopesParameter.ActualValue.Value,
|
---|
65 | CopySelectedParameter.Value.Value,
|
---|
66 | MaximizationParameter.ActualValue.Value,
|
---|
67 | RandomParameter.ActualValue,
|
---|
68 | CaseQualitiesParameter.ActualValue);
|
---|
69 |
|
---|
70 | return selected;
|
---|
71 | }
|
---|
72 |
|
---|
73 | public static IScope[] Apply(
|
---|
74 | List<IScope> population,
|
---|
75 | int count,
|
---|
76 | bool copy,
|
---|
77 | bool maximization,
|
---|
78 | IRandom random,
|
---|
79 | ItemArray<DoubleArray> caseQualities) {
|
---|
80 | var selected = new IScope[count];
|
---|
81 | var repeats = (int)Math.Ceiling(count / (double)population.Count);
|
---|
82 | var caseCount = caseQualities[0].Length;
|
---|
83 | var source = Enumerable.Range(0, population.Count).ToList();
|
---|
84 |
|
---|
85 | for (var k = 0; k < repeats; k++) {
|
---|
86 | // The fitness cases are shuffled.
|
---|
87 | var fitnessCaseIndexes = Enumerable.Range(0, caseCount).Shuffle(random).ToList();
|
---|
88 |
|
---|
89 | // copy list if required
|
---|
90 | var pool = repeats == 1 ? source : new List<int>(source);
|
---|
91 | var countLimit = Math.Min(count - k * population.Count, population.Count);
|
---|
92 |
|
---|
93 | for (var i = 0; i < countLimit; i++) {
|
---|
94 | var candidates = pool;
|
---|
95 |
|
---|
96 | for (var j = 0; j < fitnessCaseIndexes.Count && candidates.Count > 1; j++) {
|
---|
97 | candidates = GetBestIndividuals(maximization, caseQualities, candidates, fitnessCaseIndexes[j]);
|
---|
98 | }
|
---|
99 |
|
---|
100 | /* If only one individual remains, it is the chosen parent. If no more fitness cases are left, a parent is
|
---|
101 | chosen randomly from the remaining individuals */
|
---|
102 | var bestIndividualIndex = candidates.Count == 1 ? candidates[0] : candidates.Random(random);
|
---|
103 | var bestIndividual = population[bestIndividualIndex];
|
---|
104 |
|
---|
105 | selected[k * population.Count + i] = copy ? (IScope)bestIndividual.Clone() : bestIndividual;
|
---|
106 |
|
---|
107 | pool.Remove(bestIndividualIndex);
|
---|
108 | }
|
---|
109 | }
|
---|
110 |
|
---|
111 | return selected;
|
---|
112 | }
|
---|
113 |
|
---|
114 | private static List<int> GetBestIndividuals(bool maximization, ItemArray<DoubleArray> caseQualities, List<int> bestIndividuals, int index) {
|
---|
115 | var bestFitness = maximization ? double.NegativeInfinity : double.PositiveInfinity;
|
---|
116 | var result = new List<int>();
|
---|
117 |
|
---|
118 | for (var l = 0; l < bestIndividuals.Count; l++) {
|
---|
119 | var individual = bestIndividuals[l];
|
---|
120 | var caseQuality = caseQualities[individual][index];
|
---|
121 |
|
---|
122 | if (bestFitness.IsAlmost(caseQuality)) {
|
---|
123 | result.Add(individual);
|
---|
124 | } else if (maximization && bestFitness < caseQuality ||
|
---|
125 | !maximization && bestFitness > caseQuality) {
|
---|
126 | bestFitness = caseQuality;
|
---|
127 | result.Clear();
|
---|
128 | result.Add(individual);
|
---|
129 | }
|
---|
130 |
|
---|
131 | bestIndividuals = result;
|
---|
132 | }
|
---|
133 |
|
---|
134 | return bestIndividuals;
|
---|
135 | }
|
---|
136 | }
|
---|
137 | }
|
---|