Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2901_StaticSelectionMethods/HeuristicLab.Selection/3.3/TournamentSelector.cs @ 17712

Last change on this file since 17712 was 15779, checked in by gkronber, 7 years ago

#2901: implemented a static method for tournament selection

File size: 3.9 KB
RevLine 
[2]1#region License Information
2/* HeuristicLab
[15583]3 * Copyright (C) 2002-2018 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]22using System;
[2]23using System.Collections.Generic;
[2818]24using System.Linq;
[4722]25using HeuristicLab.Common;
[2]26using HeuristicLab.Core;
27using HeuristicLab.Data;
[3138]28using HeuristicLab.Optimization;
[2805]29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
[2]31
32namespace HeuristicLab.Selection {
[817]33  /// <summary>
[2805]34  /// A tournament selection operator which considers a single double quality value for selection.
[817]35  /// </summary>
[2805]36  [Item("TournamentSelector", "A tournament selection operator which considers a single double quality value for selection.")]
[3017]37  [StorableClass]
[3138]38  public sealed class TournamentSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
[3048]39    public ValueLookupParameter<IntValue> GroupSizeParameter {
40      get { return (ValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
[2]41    }
42
[4722]43    [StorableConstructor]
44    private TournamentSelector(bool deserializing) : base(deserializing) { }
45    private TournamentSelector(TournamentSelector original, Cloner cloner) : base(original, cloner) { }
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new TournamentSelector(this, cloner);
48    }
49
[4068]50    public TournamentSelector()
51      : base() {
[3048]52      Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the tournament group.", new IntValue(2)));
[2]53    }
54
[2830]55    protected override IScope[] Select(List<IScope> scopes) {
[2805]56      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
57      bool copy = CopySelectedParameter.Value.Value;
58      IRandom random = RandomParameter.ActualValue;
59      bool maximization = MaximizationParameter.ActualValue.Value;
[7995]60      List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
[2805]61      int groupSize = GroupSizeParameter.ActualValue.Value;
[2830]62      IScope[] selected = new IScope[count];
[2]63
[7995]64      //check if list with indexes is as long as the original scope list
65      //otherwise invalid quality values were filtered
66      if (qualities.Count != scopes.Count) {
67        throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
68      }
69
[2805]70      for (int i = 0; i < count; i++) {
[15779]71        var selectedIdx = SelectIdx(random, qualities, maximization, groupSize);
[2]72
[2805]73        if (copy)
[15779]74          selected[i] = (IScope)scopes[selectedIdx].Clone();
[2]75        else {
[15779]76          selected[i] = scopes[selectedIdx];
77          scopes.RemoveAt(selectedIdx);
78          qualities.RemoveAt(selectedIdx);
[2]79        }
80      }
[2817]81      return selected;
[2]82    }
[15779]83
84    public static int SelectIdx(IRandom random, IList<double> qualities, bool maximization, int groupSize) {
85      int best = random.Next(qualities.Count);
86      int index;
87      for (int j = 1; j < groupSize; j++) {
88        index = random.Next(qualities.Count);
89        if (((maximization) && (qualities[index] > qualities[best])) ||
90            ((!maximization) && (qualities[index] < qualities[best]))) {
91          best = index;
92        }
93      }
94
95      return best;
96    }
[2]97  }
98}
Note: See TracBrowser for help on using the repository browser.