Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/Creators/SmartDecisionListCreator.cs @ 9605

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

#1980:

  • set plugin dependencies
  • added smart initialization
  • added hierarchical selection
  • fixed major and minor default rule
  • fixed several smaller bugs
  • some refactoring has been done
File size: 3.4 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Optimization.Operators.LCS;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28
29namespace HeuristicLab.Encodings.DecisionList {
30  [Item("SmartDecisionListCreator", "Description missing")]
31  [StorableClass]
32  public class SmartDecisionListCreator : DecisionListCreator {
33
34    [StorableConstructor]
35    protected SmartDecisionListCreator(bool deserializing) : base(deserializing) { }
36    protected SmartDecisionListCreator(SmartDecisionListCreator original, Cloner cloner)
37      : base(original, cloner) {
38    }
39    public SmartDecisionListCreator()
40      : base() {
41    }
42    public override IDeepCloneable Clone(Cloner cloner) {
43      return new SmartDecisionListCreator(this, cloner);
44    }
45
46    protected override DecisionList Create(IRandom random, Rule sampleRule, int initialNumberOfRules, double oneProbability, ItemCollection<IDiscretizer> discretizers, IAction niche) {
47      var problemData = ProblemDataParameter.ActualValue;
48
49      List<Rule> rules = new List<Rule>();
50      for (int i = 0; i < initialNumberOfRules; i++) {
51        rules.Add(SmartRuleInitialization(problemData, random, sampleRule, initialNumberOfRules, oneProbability, discretizers, niche));
52      }
53      if (niche != null) {
54        return new DecisionList(rules, niche);
55      } else {
56        return new DecisionList(rules);
57      }
58    }
59
60    public static Rule SmartRuleInitialization(IDecisionListClassificationProblemData problemData, IRandom random,
61                                               Rule sampleRule, int initialNumberOfRules, double oneProbability,
62                                               ItemCollection<IDiscretizer> discretizers, IAction niche = null) {
63      IList<int> trainingIndicesOfOtherNiches = null;
64      if (niche != null) {
65        trainingIndicesOfOtherNiches =
66          problemData.TrainingIndices.Where(x => !problemData.FetchAction(x).SameNiche(niche)).ToList();
67      } else {
68        trainingIndicesOfOtherNiches = problemData.TrainingIndices.ToList();
69      }
70
71      var newRule = (Rule)sampleRule.Clone();
72      newRule.Randomize(random, oneProbability, discretizers);
73
74      int sample = random.Next(trainingIndicesOfOtherNiches.Count);
75      var input = problemData.FetchInput(trainingIndicesOfOtherNiches[sample]);
76      var action = problemData.FetchAction(trainingIndicesOfOtherNiches[sample]);
77
78      newRule.SetToMatchInput(input);
79      newRule.Action.SetTo(action);
80      return newRule;
81    }
82  }
83}
Note: See TracBrowser for help on using the repository browser.