Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.DecisionList/3.3/DecisionList.cs @ 15512

Last change on this file since 15512 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: 5.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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Optimization.Operators.LCS;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.DecisionList {
31  [StorableClass]
32  [Item("DecisionList", "")]
33  public class DecisionList : Item, IGAssistIndividual {
34    [Storable]
35    private IList<Rule> rules;
36    public IList<Rule> Rules {
37      get { return rules; }
38    }
39
40    [Storable]
41    private IAction defaultAction;
42    public IAction DefaultAction {
43      get { return defaultAction; }
44    }
45
46    public double Length {
47      get { return 1 + rules.Sum(r => r.Length); }
48    }
49
50    // default rule (action) is part of the rule set size
51    public int RuleSetSize {
52      get { return defaultAction == null ? rules.Count : rules.Count + 1; }
53    }
54
55    [StorableConstructor]
56    protected DecisionList(bool deserializing) : base(deserializing) { }
57    protected DecisionList(DecisionList original, Cloner cloner)
58      : base(original, cloner) {
59      defaultAction = cloner.Clone(original.DefaultAction);
60      rules = new List<Rule>(original.rules.Count);
61      foreach (var rule in original.Rules) {
62        rules.Add(cloner.Clone(rule));
63      }
64    }
65    public DecisionList()
66      : base() {
67      rules = new List<Rule>();
68      defaultAction = null;
69    }
70    public DecisionList(List<Rule> rules)
71      : base() {
72      this.rules = rules;
73      this.defaultAction = null;
74    }
75    public DecisionList(List<Rule> rules, IAction defaultAction)
76      : base() {
77      if (rules.Any(x => x.Action.Match(defaultAction))) { throw new ArgumentException("If a default action is used. The default action is not allowed be used in any rule."); }
78      this.rules = rules;
79      this.defaultAction = defaultAction;
80    }
81    public override IDeepCloneable Clone(Cloner cloner) {
82      return new DecisionList(this, cloner);
83    }
84
85    // for convenience
86    public IEnumerable<IAction> Evaluate(IEnumerable<IGAssistInput> input) {
87      ItemSet<Rule> aliveRules;
88      return Evaluate(input, out aliveRules);
89    }
90    // for convenience
91    public IEnumerable<IAction> Evaluate(IEnumerable<IGAssistInput> input, out ItemSet<Rule> aliveRules) {
92      double theoryLengtgh;
93      return Evaluate(input, out aliveRules, out theoryLengtgh);
94    }
95    public IEnumerable<IAction> Evaluate(IEnumerable<IGAssistInput> input, out ItemSet<Rule> aliveRules, out double theoryLength) {
96      var estimated = new List<IAction>();
97      var activatedRules = new ItemSet<Rule>();
98      int count = 0;
99      foreach (var dli in input) {
100        count++;
101        foreach (var rule in rules) {
102          if (rule.MatchInput(dli)) {
103            estimated.Add(rule.Action);
104            activatedRules.Add(rule);
105            break;
106          }
107        }
108        if (count > estimated.Count) {
109          estimated.Add(defaultAction);
110        }
111      }
112      aliveRules = activatedRules;
113      theoryLength = activatedRules.Sum(x => x.ComputeTheoryLength());
114
115      return estimated;
116    }
117
118    public void RemoveRules(IEnumerable<Rule> deadRules) {
119      foreach (var deadRule in deadRules) {
120        Rules.Remove(deadRule);
121      }
122    }
123
124    #region IGAssistIndividual Members
125
126    public IGAssistNiche Niche {
127      get { return defaultAction; }
128    }
129
130    public void ApplySplit(IRandom random, double probability) {
131      foreach (var rule in rules) {
132        rule.ApplySplit(random, probability);
133      }
134    }
135
136    public void ApplyMerge(IRandom random, double probability) {
137      foreach (var rule in rules) {
138        rule.ApplyMerge(random, probability);
139      }
140    }
141
142    public void ApplyReinitialize(IRandom random, double probability, double oneProbability, IEnumerable<IDiscretizer> discretizers) {
143      foreach (var rule in rules) {
144        rule.ApplyReinitialize(random, probability, oneProbability, discretizers);
145      }
146    }
147
148    public IGAssistSolution CreateGAssistSolution(IGAssistProblemData problemData) {
149      return new DecisionListSolution(this, problemData);
150    }
151    #endregion
152
153    #region IGAssistModel Members
154    IEnumerable<IGAssistNiche> IGAssistModel.Evaluate(IEnumerable<IGAssistInput> input) {
155      return Evaluate(input);
156    }
157    public IEnumerable<IGAssistNiche> Evaluate(IEnumerable<IGAssistInput> input, out ItemSet<IItem> aliveRules, out double theoryLength) {
158      return Evaluate(input, out aliveRules, out theoryLength);
159    }
160    #endregion
161  }
162}
Note: See TracBrowser for help on using the repository browser.