Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9928 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

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