Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
04/10/13 15:15:13 (11 years ago)
Author:
sforsten
Message:

#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
Location:
branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3

    • Property svn:ignore set to
      obj
      Plugin.cs
  • branches/LearningClassifierSystems/HeuristicLab.Optimization.Operators.LCS/3.3/Selection/NichingTournamentSelector.cs

    r9342 r9352  
    3030using HeuristicLab.Selection;
    3131
    32 namespace HeuristicLab.Optimization.Operators.LCS.Selection {
     32namespace HeuristicLab.Optimization.Operators.LCS {
    3333  [Item("NichingTournamentSelector", "Description missing")]
    3434  [StorableClass]
    35   public class NichingTournamentSelector : StochasticSingleObjectiveSelector, ISingleObjectiveSelector {
     35  public class NichingTournamentSelector : StochasticSingleObjectiveSelector, INichingSingleObjectiveSelector {
    3636
    3737    #region Parameter Properties
     
    3939      get { return (ValueLookupParameter<IntValue>)Parameters["GroupSize"]; }
    4040    }
    41     public LookupParameter<IntValue> NichesParameter {
    42       get { return (LookupParameter<IntValue>)Parameters["Niches"]; }
     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"]; }
    4352    }
    4453    #endregion
     
    5261      : base() {
    5362      Parameters.Add(new ValueLookupParameter<IntValue>("GroupSize", "The size of the tournament group.", new IntValue(2)));
    54       Parameters.Add(new LookupParameter<IntValue>("Niches", ""));
     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", ""));
    5567    }
    5668    public override IDeepCloneable Clone(Cloner cloner) {
     
    6476      bool maximization = MaximizationParameter.ActualValue.Value;
    6577      List<double> qualities = QualityParameter.ActualValue.Where(x => IsValidQuality(x.Value)).Select(x => x.Value).ToList();
     78      List<IGAssistIndividual> individuals = IndividualParameter.ActualValue.ToList();
    6679      int groupSize = GroupSizeParameter.ActualValue.Value;
    6780      IScope[] selected = new IScope[count];
     81      bool doNiching = NichingParameter.ActualValue.Value;
    6882
    6983      //check if list with indexes is as long as the original scope list
    7084      //otherwise invalid quality values were filtered
    71       if (qualities.Count != scopes.Count) {
     85      if (qualities.Count != scopes.Count && individuals.Count != scopes.Count) {
    7286        throw new ArgumentException("The scopes contain invalid quality values (either infinity or double.NaN) on which the selector cannot operate.");
    7387      }
    7488
    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;
     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)];
    83116          }
    84         }
     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          }
    85128
    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);
     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++;
    92140        }
    93141      }
     142
    94143      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");
    95160    }
    96161  }
Note: See TracChangeset for help on using the changeset viewer.