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

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

#2901: implemented a static method for tournament selection

File size: 3.9 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Selection {
33  /// <summary>
34  /// A tournament selection operator which considers a single double quality value for selection.
35  /// </summary>
36  [Item("TournamentSelector", "A tournament selection operator which considers a single double quality value for selection.")]
37  [StorableClass]
38  public sealed class TournamentSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
39    public ValueLookupParameter<IntValue> GroupSizeParameter {
40      get { return (ValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
41    }
42
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
50    public TournamentSelector()
51      : base() {
52      Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the tournament group.", new IntValue(2)));
53    }
54
55    protected override IScope[] Select(List<IScope> scopes) {
56      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
57      bool copy = CopySelectedParameter.Value.Value;
58      IRandom random = RandomParameter.ActualValue;
59      bool maximization = MaximizationParameter.ActualValue.Value;
60      List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
61      int groupSize = GroupSizeParameter.ActualValue.Value;
62      IScope[] selected = new IScope[count];
63
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
70      for (int i = 0; i < count; i++) {
71        var selectedIdx = SelectIdx(random, qualities, maximization, groupSize);
72
73        if (copy)
74          selected[i] = (IScope)scopes[selectedIdx].Clone();
75        else {
76          selected[i] = scopes[selectedIdx];
77          scopes.RemoveAt(selectedIdx);
78          qualities.RemoveAt(selectedIdx);
79        }
80      }
81      return selected;
82    }
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    }
97  }
98}
Note: See TracBrowser for help on using the repository browser.