Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9352 was 9352, checked in by sforsten, 12 years ago

#1980:

  • added DecisionListView
  • added event handlers in *ProblemData
  • renamed project Problems.XCS.Views to Problems.lCS.Views and Problems.Instances.ConditionActionClassification to Problems.Instances.LCS
  • integrated niching in GAssist and added NichingTournamentSelector
  • minor code improvements and property changes
File size: 6.7 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 {
33  [Item("NichingTournamentSelector", "Description missing")]
34  [StorableClass]
35  public class NichingTournamentSelector : StochasticSingleObjectiveSelector, INichingSingleObjectiveSelector {
36
37    #region Parameter Properties
38    public ValueLookupParameter<IntValue> GroupSizeParameter {
39      get { return (ValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
40    }
41    public ILookupParameter<IGAssistNichesProblemData> GAssistNichesProblemDataParameter {
42      get { return (LookupParameter<IGAssistNichesProblemData>)Parameters["GAssistNichesProblemData"]; }
43    }
44    public ILookupParameter<BoolValue> NichingParameter {
45      get { return (LookupParameter<BoolValue>)Parameters["Niching"]; }
46    }
47    public IValueLookupParameter<IntValue> ParentsPerChildParameter {
48      get { return (IValueLookupParameter<IntValue>)Parameters["ParentsPerChild"]; }
49    }
50    public ILookupParameter<ItemArray<IGAssistIndividual>> IndividualParameter {
51      get { return (ILookupParameter<ItemArray<IGAssistIndividual>>)Parameters["Individual"]; }
52    }
53    #endregion
54
55    [StorableConstructor]
56    protected NichingTournamentSelector(bool deserializing) : base(deserializing) { }
57    protected NichingTournamentSelector(NichingTournamentSelector original, Cloner cloner)
58      : base(original, cloner) {
59    }
60    public NichingTournamentSelector()
61      : base() {
62      Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the tournament group.", new IntValue(2)));
63      Parameters.Add(new LookupParameter<IGAssistNichesProblemData>("GAssistNichesProblemData", ""));
64      Parameters.Add(new LookupParameter<BoolValue>("Niching", ""));
65      Parameters.Add(new ValueLookupParameter<IntValue>("ParentsPerChild", ""));
66      Parameters.Add(new ScopeTreeLookupParameter<IGAssistIndividual>("Individual", ""));
67    }
68    public override IDeepCloneable Clone(Cloner cloner) {
69      return new NichingTournamentSelector(this, cloner);
70    }
71
72    protected override IScope[] Select(List<IScope> scopes) {
73      int count = NumberOfSelectedSubScopesParameter.ActualValue.Value;
74      bool copy = CopySelectedParameter.Value.Value;
75      IRandom random = RandomParameter.ActualValue;
76      bool maximization = MaximizationParameter.ActualValue.Value;
77      List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
78      List<IGAssistIndividual> individuals = IndividualParameter.ActualValue.ToList();
79      int groupSize = GroupSizeParameter.ActualValue.Value;
80      IScope[] selected = new IScope[count];
81      bool doNiching = NichingParameter.ActualValue.Value;
82
83      //check if list with indexes is as long as the original scope list
84      //otherwise invalid quality values were filtered
85      if (qualities.Count != scopes.Count && individuals.Count != scopes.Count) {
86        throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
87      }
88
89      int parentsPerChild = ParentsPerChildParameter.ActualValue.Value;
90      var possibleNiches = GAssistNichesProblemDataParameter.ActualValue.GetPossibleNiches().ToList();
91      var selectPerNiche = new Dictionary<IGAssistNiche, int>(possibleNiches.First().Comparer);
92
93      var nicheScope = new Dictionary<IGAssistNiche, List<int>>(possibleNiches.First().Comparer);
94      foreach (var niche in possibleNiches) {
95        nicheScope.Add(niche, new List<int>());
96        selectPerNiche.Add(niche, count / possibleNiches.Count);
97      }
98
99      for (int i = 0; i < individuals.Count; i++) {
100        nicheScope[individuals[i].Niche].Add(i);
101      }
102
103      int curCount = 0;
104      while (curCount < count) {
105        IGAssistNiche niche = null;
106        int best = -1;
107        if (doNiching) {
108          niche = GetNiche(random, selectPerNiche, possibleNiches);
109        } else {
110          best = random.Next(scopes.Count);
111        }
112        for (int i = 0; i < parentsPerChild; i++) {
113          int index;
114          if (doNiching) {
115            best = nicheScope[niche][random.Next(nicheScope[niche].Count)];
116          }
117          for (int j = 1; j < groupSize; j++) {
118            if (niche != null) {
119              index = nicheScope[niche][random.Next(nicheScope[niche].Count)];
120            } else {
121              index = random.Next(scopes.Count);
122            }
123            if (((maximization) && (qualities[index] > qualities[best])) ||
124                ((!maximization) && (qualities[index] < qualities[best]))) {
125              best = index;
126            }
127          }
128
129          niche = individuals[best].Niche;
130
131          if (copy)
132            selected[curCount] = (IScope)scopes[best].Clone();
133          else {
134            selected[curCount] = scopes[best];
135            scopes.RemoveAt(best);
136            qualities.RemoveAt(best);
137          }
138          selectPerNiche[niche]--;
139          curCount++;
140        }
141      }
142
143      return selected;
144    }
145
146    private IGAssistNiche GetNiche(IRandom random, Dictionary<IGAssistNiche, int> nicheScope, List<IGAssistNiche> possibleNiches) {
147      int sum = nicheScope.Values.Sum();
148      if (sum <= 0) { return possibleNiches[random.Next(possibleNiches.Count)]; }
149      int pos = random.Next(sum);
150      int total = 0;
151      IGAssistNiche niche = nicheScope.Keys.First();
152      foreach (var item in nicheScope) {
153        total += item.Value;
154        niche = item.Key;
155        if (pos < total) {
156          return niche;
157        }
158      }
159      throw new ArgumentException("error in code");
160    }
161  }
162}
Note: See TracBrowser for help on using the repository browser.