Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3/Selection/NichingTournamentSelector.cs @ 9342

Last change on this file since 9342 was 9342, checked in by sforsten, 11 years ago

#1980:

  • added be project Optimization.Operators.LCS
  • added default rule strategies for GAssist
File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Selection;
31
32namespace HeuristicLab.Optimization.Operators.LCS.Selection {
33  [Item("NichingTournamentSelector", "Description missing")]
34  [StorableClass]
35  public class NichingTournamentSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
36
37    #region Parameter Properties
38    public ValueLookupParameter<IntValue> GroupSizeParameter {
39      get { return (ValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
40    }
41    public LookupParameter<IntValue> NichesParameter {
42      get { return (LookupParameter<IntValue>)Parameters["Niches"]; }
43    }
44    #endregion
45
46    [StorableConstructor]
47    protected NichingTournamentSelector(bool deserializing) : base(deserializing) { }
48    protected NichingTournamentSelector(NichingTournamentSelector original, Cloner cloner)
49      : base(original, cloner) {
50    }
51    public NichingTournamentSelector()
52      : base() {
53      Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the tournament group.", new IntValue(2)));
54      Parameters.Add(new LookupParameter<IntValue>("Niches", ""));
55    }
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new NichingTournamentSelector(this, cloner);
58    }
59
60    protected override IScope[] Select(List<IScope> scopes) {
61      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
62      bool copy = CopySelectedParameter.Value.Value;
63      IRandom random = RandomParameter.ActualValue;
64      bool maximization = MaximizationParameter.ActualValue.Value;
65      List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
66      int groupSize = GroupSizeParameter.ActualValue.Value;
67      IScope[] selected = new IScope[count];
68
69      //check if list with indexes is as long as the original scope list
70      //otherwise invalid quality values were filtered
71      if (qualities.Count != scopes.Count) {
72        throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
73      }
74
75      for (int i = 0; i < count; i++) {
76        int best = random.Next(scopes.Count);
77        int index;
78        for (int j = 1; j < groupSize; j++) {
79          index = random.Next(scopes.Count);
80          if (((maximization) && (qualities[index] > qualities[best])) ||
81              ((!maximization) && (qualities[index] < qualities[best]))) {
82            best = index;
83          }
84        }
85
86        if (copy)
87          selected[i] = (IScope)scopes[best].Clone();
88        else {
89          selected[i] = scopes[best];
90          scopes.RemoveAt(best);
91          qualities.RemoveAt(best);
92        }
93      }
94      return selected;
95    }
96  }
97}
Note: See TracBrowser for help on using the repository browser.