Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2434_crossvalidation/HeuristicLab.Selection/3.3/TournamentSelector.cs @ 16654

Last change on this file since 16654 was 12012, checked in by ascheibe, 10 years ago

#2212 merged r12008, r12009, r12010 back into trunk

File size: 3.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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        int best = random.Next(scopes.Count);
72        int index;
73        for (int j = 1; j < groupSize; j++) {
74          index = random.Next(scopes.Count);
75          if (((maximization) && (qualities[index] > qualities[best])) ||
76              ((!maximization) && (qualities[index] < qualities[best]))) {
77            best = index;
78          }
79        }
80
81        if (copy)
82          selected[i] = (IScope)scopes[best].Clone();
83        else {
84          selected[i] = scopes[best];
85          scopes.RemoveAt(best);
86          qualities.RemoveAt(best);
87        }
88      }
89      return selected;
90    }
91  }
92}
Note: See TracBrowser for help on using the repository browser.