#region License Information /* HeuristicLab * Copyright (C) 2002-2013 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.Encodings.ConditionActionEncoding; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Selection { /// /// A tournament selection operator which considers a single double quality value for selection. /// [Item("ProportionalTournamentSelector", "A tournament selection operator which considers a single double quality value for selection and uses a proportion of all samples as group.")] [StorableClass] public sealed class ProportionalTournamentSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector, IXCSSelector { public ValueLookupParameter GroupProportionParameter { get { return (ValueLookupParameter)Parameters["GroupProportion"]; } } // separate parameter needed, because in XCS the action set size differs from the number of scopes public ILookupParameter> NumerosityParameter { get { return (ILookupParameter>)Parameters["Numerosity"]; } } [StorableConstructor] private ProportionalTournamentSelector(bool deserializing) : base(deserializing) { } private ProportionalTournamentSelector(ProportionalTournamentSelector original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new ProportionalTournamentSelector(this, cloner); } public ProportionalTournamentSelector() : base() { Parameters.Add(new ValueLookupParameter("GroupProportion", "The proportion of the tournament group.", new PercentValue(0.4))); Parameters.Add(new ScopeTreeLookupParameter("Numerosity", "")); } protected override IScope[] Select(List scopes) { int count = NumberOfSelectedSubScopesParameter.ActualValue.Value; bool copy = CopySelectedParameter.Value.Value; IRandom random = RandomParameter.ActualValue; bool maximization = MaximizationParameter.ActualValue.Value; List qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList(); double groupProportion = GroupProportionParameter.ActualValue.Value; double currentActionSetSize = NumerosityParameter.ActualValue.Sum(x => x.Value); double groupSize = Math.Floor(currentActionSetSize * groupProportion); IScope[] selected = new IScope[count]; //check if list with indexes is as long as the original scope list //otherwise invalid quality values were filtered if (qualities.Count != scopes.Count) { throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate."); } for (int i = 0; i < count; i++) { int best = random.Next(scopes.Count); int index; for (int j = 1; j < groupSize; j++) { index = random.Next(scopes.Count); if (((maximization) && (qualities[index] > qualities[best])) || ((!maximization) && (qualities[index] < qualities[best]))) { best = index; } } if (copy) selected[i] = (IScope)scopes[best].Clone(); else { selected[i] = scopes[best]; scopes.RemoveAt(best); qualities.RemoveAt(best); } } return selected; } } }