[2] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[2] | 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 |
|
---|
[7995] | 22 | using System;
|
---|
[2] | 23 | using System.Collections.Generic;
|
---|
[2818] | 24 | using System.Linq;
|
---|
[4722] | 25 | using HeuristicLab.Common;
|
---|
[2] | 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
[3138] | 28 | using HeuristicLab.Optimization;
|
---|
[14429] | 29 | using HeuristicLab.Optimization.Selection;
|
---|
[2805] | 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
[2] | 32 |
|
---|
| 33 | namespace HeuristicLab.Selection {
|
---|
[817] | 34 | /// <summary>
|
---|
[2805] | 35 | /// A tournament selection operator which considers a single double quality value for selection.
|
---|
[817] | 36 | /// </summary>
|
---|
[2805] | 37 | [Item("TournamentSelector", "A tournament selection operator which considers a single double quality value for selection.")]
|
---|
[3017] | 38 | [StorableClass]
|
---|
[3138] | 39 | public sealed class TournamentSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
|
---|
[3048] | 40 | public ValueLookupParameter<IntValue> GroupSizeParameter {
|
---|
| 41 | get { return (ValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
|
---|
[2] | 42 | }
|
---|
| 43 |
|
---|
[4722] | 44 | [StorableConstructor]
|
---|
| 45 | private TournamentSelector(bool deserializing) : base(deserializing) { }
|
---|
| 46 | private TournamentSelector(TournamentSelector original, Cloner cloner) : base(original, cloner) { }
|
---|
| 47 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 48 | return new TournamentSelector(this, cloner);
|
---|
| 49 | }
|
---|
| 50 |
|
---|
[4068] | 51 | public TournamentSelector()
|
---|
| 52 | : base() {
|
---|
[3048] | 53 | Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the tournament group.", new IntValue(2)));
|
---|
[2] | 54 | }
|
---|
| 55 |
|
---|
[2830] | 56 | protected override IScope[] Select(List<IScope> scopes) {
|
---|
[2805] | 57 | int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
|
---|
| 58 | bool copy = CopySelectedParameter.Value.Value;
|
---|
| 59 | IRandom random = RandomParameter.ActualValue;
|
---|
| 60 | bool maximization = MaximizationParameter.ActualValue.Value;
|
---|
[7995] | 61 | List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
|
---|
[2805] | 62 | int groupSize = GroupSizeParameter.ActualValue.Value;
|
---|
[2830] | 63 | IScope[] selected = new IScope[count];
|
---|
[2] | 64 |
|
---|
[7995] | 65 | //check if list with indexes is as long as the original scope list
|
---|
| 66 | //otherwise invalid quality values were filtered
|
---|
| 67 | if (qualities.Count != scopes.Count) {
|
---|
| 68 | throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
|
---|
| 69 | }
|
---|
| 70 |
|
---|
[2805] | 71 | for (int i = 0; i < count; i++) {
|
---|
[2817] | 72 | int best = random.Next(scopes.Count);
|
---|
[2805] | 73 | int index;
|
---|
[1063] | 74 | for (int j = 1; j < groupSize; j++) {
|
---|
[2817] | 75 | index = random.Next(scopes.Count);
|
---|
[2818] | 76 | if (((maximization) && (qualities[index] > qualities[best])) ||
|
---|
| 77 | ((!maximization) && (qualities[index] < qualities[best]))) {
|
---|
[2805] | 78 | best = index;
|
---|
[2] | 79 | }
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[2805] | 82 | if (copy)
|
---|
[2830] | 83 | selected[i] = (IScope)scopes[best].Clone();
|
---|
[2] | 84 | else {
|
---|
[2830] | 85 | selected[i] = scopes[best];
|
---|
[2817] | 86 | scopes.RemoveAt(best);
|
---|
[2805] | 87 | qualities.RemoveAt(best);
|
---|
[2] | 88 | }
|
---|
| 89 | }
|
---|
[2817] | 90 | return selected;
|
---|
[2] | 91 | }
|
---|
| 92 | }
|
---|
[14429] | 93 |
|
---|
| 94 | [Item("Tournament Selector", "", ExcludeGenericTypeInfo = true)]
|
---|
| 95 | [StorableClass]
|
---|
| 96 | public sealed class TournamentSelector<TContext, TProblem, TEncoding, TSolution> : ParameterizedNamedItem, ISelector<TContext>
|
---|
| 97 | where TContext : ISingleObjectivePopulationContext<TSolution>, IMatingpoolContext<TSolution>, IStochasticContext,
|
---|
| 98 | IProblemContext<TProblem, TEncoding, TSolution>
|
---|
| 99 | where TProblem : class, ISingleObjectiveProblem<TEncoding, TSolution>, ISingleObjectiveProblemDefinition<TEncoding, TSolution>
|
---|
| 100 | where TEncoding : class, IEncoding<TSolution>
|
---|
| 101 | where TSolution : class, ISolution {
|
---|
| 102 |
|
---|
| 103 | [Storable]
|
---|
| 104 | private IValueParameter<IntValue> groupSizeParameter;
|
---|
| 105 | public int GroupSize {
|
---|
| 106 | get { return groupSizeParameter.Value.Value; }
|
---|
| 107 | set {
|
---|
| 108 | if (value < 1) throw new ArgumentException("Cannot use a group size less than 1 in tournament selection.");
|
---|
| 109 | groupSizeParameter.Value.Value = value;
|
---|
| 110 | }
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | [StorableConstructor]
|
---|
| 114 | private TournamentSelector(bool deserializing) : base(deserializing) { }
|
---|
| 115 | private TournamentSelector(TournamentSelector<TContext, TProblem, TEncoding, TSolution> original, Cloner cloner)
|
---|
| 116 | : base(original, cloner) {
|
---|
| 117 | groupSizeParameter = cloner.Clone(groupSizeParameter);
|
---|
| 118 | }
|
---|
| 119 | public TournamentSelector() {
|
---|
| 120 | Parameters.Add(groupSizeParameter = new ValueParameter<IntValue>("GroupSize", "The group size that competes in the tournament.", new IntValue(2)));
|
---|
| 121 | }
|
---|
| 122 |
|
---|
| 123 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 124 | return new TournamentSelector<TContext, TProblem, TEncoding, TSolution>(this, cloner);
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | public void Select(TContext context, int n, bool withRepetition) {
|
---|
| 128 | context.MatingPool = Select(context.Random, context.Problem.IsBetter, context.Population, GroupSize, n, withRepetition);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
| 131 | public static IEnumerable<ISingleObjectiveSolutionScope<TSolution>> Select(IRandom random, Func<double, double, bool> isBetterFunc, IEnumerable<ISingleObjectiveSolutionScope<TSolution>> population, int groupSize, int n, bool withRepetition) {
|
---|
| 132 | var pop = population.Where(x => !double.IsNaN(x.Fitness)).ToList();
|
---|
| 133 |
|
---|
| 134 | var i = n;
|
---|
| 135 | while (i > 0 && pop.Count > 0) {
|
---|
| 136 | var best = random.Next(pop.Count);
|
---|
| 137 | for (var j = 1; j < groupSize; j++) {
|
---|
| 138 | var index = random.Next(pop.Count);
|
---|
| 139 | if (isBetterFunc(pop[index].Fitness, pop[best].Fitness)) {
|
---|
| 140 | best = index;
|
---|
| 141 | }
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | yield return pop[best];
|
---|
| 145 | i--;
|
---|
| 146 | if (!withRepetition) {
|
---|
| 147 | pop.RemoveAt(i);
|
---|
| 148 | }
|
---|
| 149 | }
|
---|
| 150 | }
|
---|
| 151 | }
|
---|
[2] | 152 | }
|
---|