Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9352 was 9352, checked in by sforsten, 11 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: 5.1 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    // default rule (action) is part of the rule set size
47    public int RuleSetSize {
48      get { return defaultAction == null ? rules.Count : rules.Count + 1; }
49    }
50
51    [StorableConstructor]
52    protected DecisionList(bool deserializing) : base(deserializing) { }
53    protected DecisionList(DecisionList original, Cloner cloner)
54      : base(original, cloner) {
55      defaultAction = original.DefaultAction;
56      rules = new List<Rule>(original.rules.Count);
57      foreach (var rule in original.Rules) {
58        rules.Add(cloner.Clone(rule));
59      }
60    }
61    public DecisionList()
62      : base() {
63      rules = new List<Rule>();
64      defaultAction = null;
65    }
66    public DecisionList(List<Rule> rules)
67      : base() {
68      this.rules = rules;
69      this.defaultAction = null;
70    }
71    public DecisionList(List<Rule> rules, IAction defaultAction)
72      : base() {
73      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."); }
74      this.rules = rules;
75      this.defaultAction = defaultAction;
76    }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new DecisionList(this, cloner);
79    }
80
81    // for convenience
82    public IEnumerable<IAction> Evaluate(IEnumerable<DecisionListInput> input) {
83      int numberOfAliveRules;
84      return Evaluate(input, out numberOfAliveRules);
85    }
86    // for convenience
87    public IEnumerable<IAction> Evaluate(IEnumerable<DecisionListInput> input, out int numberOfAliveRules) {
88      double theoryLengtgh;
89      return Evaluate(input, out numberOfAliveRules, out theoryLengtgh);
90    }
91    public IEnumerable<IAction> Evaluate(IEnumerable<DecisionListInput> input, out int numberOfAliveRules, out double theoryLengtgh) {
92      var estimated = new List<IAction>();
93      var activatedRules = new HashSet<Rule>();
94      foreach (var dli in input) {
95        foreach (var rule in rules) {
96          if (rule.MatchInput(dli)) {
97            estimated.Add(rule.Action);
98            activatedRules.Add(rule);
99            break;
100          }
101        }
102      }
103      numberOfAliveRules = activatedRules.Count;
104      theoryLengtgh = activatedRules.Sum(x => x.ComputeTheoryLength());
105
106      return estimated;
107    }
108
109    #region IGAssistIndividual Members
110
111    public IGAssistNiche Niche {
112      get { return defaultAction; }
113    }
114
115    public void SetNiche(IRandom random, IGAssistNiche niche) {
116      var action = niche as IAction;
117      if (action == null) {
118        throw new ArgumentException("Niche has to be an action");
119      }
120      if (Niche != null) {
121        throw new ArgumentException("Niche has already been set. It cannot be set again.");
122      }
123      defaultAction = action;
124      var except = new List<IAction>() { action };
125      foreach (var rule in rules.Where(x => x.Action.SameNiche(niche))) {
126        rule.Action.Randomize(random, except);
127      }
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    #endregion
148  }
149}
Note: See TracBrowser for help on using the repository browser.