Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • renamed algorithm Learning Classifier System to XCS
  • DecisionListSolution and XCSSolution show more information
  • VariableVectorClassificationProblemData can now also import datasets where the last variable is not the target variable
File size: 11.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2011 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.Optimization;
29using HeuristicLab.Optimization.Operators.LCS;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Encodings.DecisionList {
33  [StorableClass]
34  [Item("DecisionListSolution", "Represents a DecisionList solution.")]
35  public class DecisionListSolution : ResultCollection, IDecisionListSolution {
36    private const string ModelResultName = "Model";
37    private const string ProblemDataResultName = "ProblemData";
38    private const string TrainingAccuracyResultName = "Accuracy (training)";
39    private const string TestAccuracyResultName = "Accuracy (test)";
40    private const string TrainingNumberOfAliveRulesName = "Number of alive rules (training)";
41    private const string TrainingAliveRulesName = "Alive Rules (training)";
42    private const string TestNumberOfAliveRulesName = "Number of alive rules (test)";
43    private const string TestAliveRulesName = "Alive Rules (test)";
44    private const string TrainingTheoryLengthName = "Theory Length (training)";
45    private const string TrainingExceptionsLengthName = "Exceptions Length (training)";
46    private const string DefaultRuleName = "Default Rule Action";
47    private const string RulesName = "Rules";
48    private const string NumberOfRulesName = "Number of Rules";
49
50    public double TrainingAccuracy {
51      get { return ((PercentValue)this[TrainingAccuracyResultName].Value).Value; }
52      private set { ((PercentValue)this[TrainingAccuracyResultName].Value).Value = value; }
53    }
54    public double TestAccuracy {
55      get { return ((PercentValue)this[TestAccuracyResultName].Value).Value; }
56      private set { ((PercentValue)this[TestAccuracyResultName].Value).Value = value; }
57    }
58    public int TrainingNumberOfAliveRules {
59      get { return ((IntValue)this[TrainingNumberOfAliveRulesName].Value).Value; }
60      private set { ((IntValue)this[TrainingNumberOfAliveRulesName].Value).Value = value; }
61    }
62    public ItemSet<Rule> TrainingAliveRules {
63      get { return (ItemSet<Rule>)this[TrainingAliveRulesName].Value; }
64      private set { this[TrainingAliveRulesName].Value = value; }
65    }
66    public double TrainingTheoryLength {
67      get { return ((DoubleValue)this[TrainingTheoryLengthName].Value).Value; }
68      private set { ((DoubleValue)this[TrainingTheoryLengthName].Value).Value = value; }
69    }
70    public double TrainingExceptionsLength {
71      get { return ((DoubleValue)this[TrainingExceptionsLengthName].Value).Value; }
72      private set { ((DoubleValue)this[TrainingExceptionsLengthName].Value).Value = value; }
73    }
74    public int TestNumberOfAliveRules {
75      get { return ((IntValue)this[TestNumberOfAliveRulesName].Value).Value; }
76      private set { ((IntValue)this[TestNumberOfAliveRulesName].Value).Value = value; }
77    }
78    public ItemSet<Rule> TestAliveRules {
79      get { return (ItemSet<Rule>)this[TestAliveRulesName].Value; }
80      private set { this[TestAliveRulesName].Value = value; }
81    }
82    public IAction DefaultRule {
83      get { return (IAction)this[DefaultRuleName].Value; }
84      private set { this[DefaultRuleName].Value = value; }
85    }
86    public ItemList<Rule> Rules {
87      get { return (ItemList<Rule>)this[RulesName].Value; }
88      private set { this[RulesName].Value = value; }
89    }
90    public int NumberOfRules {
91      get { return ((IntValue)this[NumberOfRulesName].Value).Value; }
92      private set { ((IntValue)this[NumberOfRulesName].Value).Value = value; }
93    }
94
95    public int Classes {
96      get { return ProblemData.Classes; }
97    }
98
99    IGAssistModel IGAssistSolution.Model {
100      get { return Model; }
101    }
102
103    public DecisionList Model {
104      get { return (DecisionList)this[ModelResultName].Value; ; }
105      protected set {
106        if (this[ModelResultName].Value != value) {
107          if (value != null) {
108            this[ModelResultName].Value = value;
109            OnModelChanged();
110          }
111        }
112      }
113    }
114
115    public IGAssistProblemData ProblemData {
116      get { return (IGAssistProblemData)this[ProblemDataResultName].Value; }
117      set {
118        if (this[ProblemDataResultName].Value != value) {
119          if (value != null) {
120            ProblemData.Changed -= new EventHandler(ProblemData_Changed);
121            this[ProblemDataResultName].Value = value;
122            ProblemData.Changed += new EventHandler(ProblemData_Changed);
123            OnProblemDataChanged();
124          }
125        }
126      }
127    }
128
129    [StorableConstructor]
130    protected DecisionListSolution(bool deserializing) : base(deserializing) { }
131    protected DecisionListSolution(DecisionListSolution original, Cloner cloner)
132      : base(original, cloner) {
133      name = original.Name;
134      description = original.Description;
135    }
136    public DecisionListSolution(DecisionList model, IGAssistProblemData problemData)
137      : base() {
138      name = ItemName;
139      description = ItemDescription;
140      Add(new Result(ModelResultName, "Decision List.", model));
141      Add(new Result(ProblemDataResultName, "The problem data.", problemData));
142      Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
143      Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
144      Add(new Result(TrainingNumberOfAliveRulesName, "", new IntValue()));
145      Add(new Result(TrainingAliveRulesName, "", new ItemSet<Rule>()));
146      Add(new Result(TrainingTheoryLengthName, "", new DoubleValue()));
147      Add(new Result(TrainingExceptionsLengthName, "", new DoubleValue()));
148      Add(new Result(TestNumberOfAliveRulesName, "", new IntValue()));
149      Add(new Result(TestAliveRulesName, "", new ItemSet<Rule>()));
150      Add(new Result(DefaultRuleName, model.DefaultAction));
151      Add(new Result(RulesName, new ItemList<Rule>(model.Rules)));
152      Add(new Result(NumberOfRulesName, new IntValue(model.RuleSetSize)));
153
154      problemData.Changed += new EventHandler(ProblemData_Changed);
155
156      RecalculateResults();
157    }
158    public override IDeepCloneable Clone(Cloner cloner) {
159      return new DecisionListSolution(this, cloner);
160    }
161
162    private void RecalculateResults() {
163      DefaultRule = Model.DefaultAction;
164      Rules = new ItemList<Rule>(Model.Rules);
165      NumberOfRules = Model.RuleSetSize;
166      var originalTrainingCondition = ProblemData.FetchInput(ProblemData.TrainingIndices);
167      var originalTestCondition = ProblemData.FetchInput(ProblemData.TestIndices);
168      ItemSet<Rule> trainingAliveRules;
169      double trainingTheoryLength;
170      var estimatedTraining = Model.Evaluate(originalTrainingCondition, out trainingAliveRules, out trainingTheoryLength);
171      TrainingNumberOfAliveRules = trainingAliveRules.Count + (Model.DefaultAction != null ? 1 : 0);
172      TrainingAliveRules = trainingAliveRules;
173      TrainingTheoryLength = trainingTheoryLength;
174      ItemSet<Rule> testAliveRules;
175      var estimatedTest = Model.Evaluate(originalTestCondition, out testAliveRules);
176      TestNumberOfAliveRules = testAliveRules.Count + (Model.DefaultAction != null ? 1 : 0);
177      TestAliveRules = testAliveRules;
178
179      var originalTrainingAction = ProblemData.FetchAction(ProblemData.TrainingIndices);
180      var originalTestAction = ProblemData.FetchAction(ProblemData.TestIndices);
181
182      TrainingAccuracy = CalculateAccuracy(originalTrainingAction, estimatedTraining);
183      TestAccuracy = CalculateAccuracy(originalTestAction, estimatedTest);
184
185      TrainingExceptionsLength = 105.0 - TrainingAccuracy * 100.0;
186    }
187
188    public static double CalculateAccuracy(IEnumerable<IGAssistNiche> original, IEnumerable<IGAssistNiche> estimated) {
189      double correctClassified = 0;
190
191      double rows = original.Count();
192      var originalEnumerator = original.GetEnumerator();
193      var estimatedActionEnumerator = estimated.GetEnumerator();
194
195      while (originalEnumerator.MoveNext() && estimatedActionEnumerator.MoveNext()) {
196        if (originalEnumerator.Current != null && estimatedActionEnumerator.Current != null
197          && originalEnumerator.Current.SameNiche(estimatedActionEnumerator.Current)) {
198          correctClassified++;
199        }
200      }
201      return correctClassified / rows;
202    }
203
204    private void ProblemData_Changed(object sender, EventArgs e) {
205      OnProblemDataChanged();
206    }
207
208    public event EventHandler ModelChanged;
209    protected virtual void OnModelChanged() {
210      RecalculateResults();
211      var listeners = ModelChanged;
212      if (listeners != null) listeners(this, EventArgs.Empty);
213    }
214
215    public event EventHandler ProblemDataChanged;
216    protected virtual void OnProblemDataChanged() {
217      RecalculateResults();
218      var listeners = ProblemDataChanged;
219      if (listeners != null) listeners(this, EventArgs.Empty);
220    }
221
222    #region INamedItem Members
223    [Storable]
224    protected string name;
225    public string Name {
226      get { return name; }
227      set {
228        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
229        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
230          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
231          OnNameChanging(e);
232          if (!e.Cancel) {
233            name = value == null ? string.Empty : value;
234            OnNameChanged();
235          }
236        }
237      }
238    }
239    public virtual bool CanChangeName {
240      get { return true; }
241    }
242    [Storable]
243    protected string description;
244    public string Description {
245      get { return description; }
246      set {
247        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
248        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
249          description = value == null ? string.Empty : value;
250          OnDescriptionChanged();
251        }
252      }
253    }
254    public virtual bool CanChangeDescription {
255      get { return true; }
256    }
257
258    public override string ToString() {
259      return Name;
260    }
261
262    public event EventHandler<CancelEventArgs<string>> NameChanging;
263    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
264      var handler = NameChanging;
265      if (handler != null) handler(this, e);
266    }
267
268    public event EventHandler NameChanged;
269    protected virtual void OnNameChanged() {
270      var handler = NameChanged;
271      if (handler != null) handler(this, EventArgs.Empty);
272      OnToStringChanged();
273    }
274
275    public event EventHandler DescriptionChanged;
276    protected virtual void OnDescriptionChanged() {
277      var handler = DescriptionChanged;
278      if (handler != null) handler(this, EventArgs.Empty);
279    }
280    #endregion
281  }
282}
Note: See TracBrowser for help on using the repository browser.