[9467] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2013 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.Encodings.ConditionActionEncoding;
|
---|
| 29 | using HeuristicLab.Optimization;
|
---|
| 30 | using HeuristicLab.Parameters;
|
---|
| 31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Selection {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// A tournament selection operator which considers a single double quality value for selection.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("ProportionalTournamentSelector", "A tournament selection operator which considers a single double quality value for selection and uses a proportion of all samples as group.")]
|
---|
| 38 | [StorableClass]
|
---|
| 39 | public sealed class ProportionalTournamentSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector, IXCSSelector {
|
---|
| 40 | public ValueLookupParameter<PercentValue> GroupProportionParameter {
|
---|
| 41 | get { return (ValueLookupParameter<PercentValue>)Parameters["GroupProportion"]; }
|
---|
| 42 | }
|
---|
| 43 | // separate parameter needed, because in XCS the action set size differs from the number of scopes
|
---|
| 44 | public ILookupParameter<ItemArray<IntValue>> NumerosityParameter {
|
---|
| 45 | get { return (ILookupParameter<ItemArray<IntValue>>)Parameters["Numerosity"]; }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | [StorableConstructor]
|
---|
| 49 | private ProportionalTournamentSelector(bool deserializing) : base(deserializing) { }
|
---|
| 50 | private ProportionalTournamentSelector(ProportionalTournamentSelector original, Cloner cloner) : base(original, cloner) { }
|
---|
| 51 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 52 | return new ProportionalTournamentSelector(this, cloner);
|
---|
| 53 | }
|
---|
| 54 |
|
---|
| 55 | public ProportionalTournamentSelector()
|
---|
| 56 | : base() {
|
---|
| 57 | Parameters.Add(new ValueLookupParameter<PercentValue>("GroupProportion", "The proportion of the tournament group.", new PercentValue(0.4)));
|
---|
| 58 | Parameters.Add(new ScopeTreeLookupParameter<IntValue>("Numerosity", ""));
|
---|
| 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 | double groupProportion = GroupProportionParameter.ActualValue.Value;
|
---|
| 68 | double currentActionSetSize = NumerosityParameter.ActualValue.Sum(x => x.Value);
|
---|
| 69 | double groupSize = Math.Floor(currentActionSetSize * groupProportion);
|
---|
| 70 | IScope[] selected = new IScope[count];
|
---|
| 71 |
|
---|
| 72 | //check if list with indexes is as long as the original scope list
|
---|
| 73 | //otherwise invalid quality values were filtered
|
---|
| 74 | if (qualities.Count != scopes.Count) {
|
---|
| 75 | throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
|
---|
| 76 | }
|
---|
| 77 |
|
---|
| 78 | for (int i = 0; i < count; i++) {
|
---|
| 79 | int best = random.Next(scopes.Count);
|
---|
| 80 | int index;
|
---|
| 81 | for (int j = 1; j < groupSize; j++) {
|
---|
| 82 | index = random.Next(scopes.Count);
|
---|
| 83 | if (((maximization) && (qualities[index] > qualities[best])) ||
|
---|
| 84 | ((!maximization) && (qualities[index] < qualities[best]))) {
|
---|
| 85 | best = index;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | if (copy)
|
---|
| 90 | selected[i] = (IScope)scopes[best].Clone();
|
---|
| 91 | else {
|
---|
| 92 | selected[i] = scopes[best];
|
---|
| 93 | scopes.RemoveAt(best);
|
---|
| 94 | qualities.RemoveAt(best);
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
| 97 | return selected;
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 | }
|
---|