Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Selection/3.3/TournamentSelector.cs @ 2817

Last change on this file since 2817 was 2817, checked in by swagner, 14 years ago

Operator architecture refactoring (#95)

  • worked on selection
File size: 3.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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.Collections.Generic;
23using HeuristicLab.Core;
24using HeuristicLab.Data;
25using HeuristicLab.Parameters;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27
28namespace HeuristicLab.Selection {
29  /// <summary>
30  /// A tournament selection operator which considers a single double quality value for selection.
31  /// </summary>
32  [Item("TournamentSelector", "A tournament selection operator which considers a single double quality value for selection.")]
33  [EmptyStorableClass]
34  [Creatable("Test")]
35  public sealed class TournamentSelector : StochasticSingleObjectiveSelector {
36    public ValueLookupParameter<IntData> GroupSizeParameter {
37      get { return (ValueLookupParameter<IntData>)Parameters["GroupSize"]; }
38    }
39
40    public TournamentSelector() : base() {
41      Parameters.Add(new ValueLookupParameter<IntData>("GroupSize", "The size of the tournament group.", new IntData(2)));
42      CopySelected.Value = true;
43    }
44
45    protected override ScopeList Select(ScopeList scopes) {
46      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
47      bool copy = CopySelectedParameter.Value.Value;
48      IRandom random = RandomParameter.ActualValue;
49      bool maximization = MaximizationParameter.ActualValue.Value;
50      List<DoubleData> qualities = new List<DoubleData>(QualityParameter.ActualValue);
51      int groupSize = GroupSizeParameter.ActualValue.Value;
52      ScopeList selected = new ScopeList();
53
54      for (int i = 0; i < count; i++) {
55        int best = random.Next(scopes.Count);
56        int index;
57        for (int j = 1; j < groupSize; j++) {
58          index = random.Next(scopes.Count);
59          if (((maximization) && (qualities[index].Value > qualities[best].Value)) ||
60              ((!maximization) && (qualities[index].Value < qualities[best].Value))) {
61            best = index;
62          }
63        }
64
65        if (copy)
66          selected.Add((IScope)scopes[best].Clone());
67        else {
68          selected.Add(scopes[best]);
69          scopes.RemoveAt(best);
70          qualities.RemoveAt(best);
71        }
72      }
73      return selected;
74    }
75  }
76}
Note: See TracBrowser for help on using the repository browser.