Free cookie consent management tool by TermsFeed Policy Generator

source: branches/LearningClassifierSystems/HeuristicLab.Encodings.ConditionActionEncoding/3.3/XCSSolution.cs @ 13745

Last change on this file since 13745 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: 8.4 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.Persistence.Default.CompositeSerializers.Storable;
30
31namespace HeuristicLab.Encodings.ConditionActionEncoding {
32  [StorableClass]
33  [Item("XCSSolution", "Represents a XCS solution.")]
34  public class XCSSolution : ResultCollection, IXCSSolution {
35    private const string ModelResultName = "Model";
36    private const string ProblemDataResultName = "ProblemData";
37    private const string TrainingAccuracyResultName = "Accuracy (training)";
38    private const string TestAccuracyResultName = "Accuracy (test)";
39    private const string NumberOfMacroClassifiersName = "Number of Macroclassifiers";
40
41    public string Filename { get; set; }
42
43    public double TrainingAccuracy {
44      get { return ((DoubleValue)this[TrainingAccuracyResultName].Value).Value; }
45      private set { ((DoubleValue)this[TrainingAccuracyResultName].Value).Value = value; }
46    }
47    public double TestAccuracy {
48      get { return ((DoubleValue)this[TestAccuracyResultName].Value).Value; }
49      private set { ((DoubleValue)this[TestAccuracyResultName].Value).Value = value; }
50    }
51    public int NumberOfMacroClassifiers {
52      get { return ((IntValue)this[NumberOfMacroClassifiersName].Value).Value; }
53      private set { ((IntValue)this[NumberOfMacroClassifiersName].Value).Value = value; }
54    }
55
56    IConditionActionModel IConditionActionSolution.Model {
57      get { return Model; }
58    }
59
60    public IXCSModel Model {
61      get { return (IXCSModel)this[ModelResultName].Value; ; }
62      protected set {
63        if (this[ModelResultName].Value != value) {
64          if (value != null) {
65            this[ModelResultName].Value = value;
66            OnModelChanged();
67          }
68        }
69      }
70    }
71
72    public IConditionActionProblemData ProblemData {
73      get { return (IConditionActionProblemData)this[ProblemDataResultName].Value; }
74      set {
75        if (this[ProblemDataResultName].Value != value) {
76          if (value != null) {
77            ProblemData.Changed -= new EventHandler(ProblemData_Changed);
78            this[ProblemDataResultName].Value = value;
79            ProblemData.Changed += new EventHandler(ProblemData_Changed);
80            OnProblemDataChanged();
81          }
82        }
83      }
84    }
85
86    [StorableConstructor]
87    protected XCSSolution(bool deserializing) : base(deserializing) { }
88    protected XCSSolution(XCSSolution original, Cloner cloner)
89      : base(original, cloner) {
90      name = original.Name;
91      description = original.Description;
92    }
93    public override IDeepCloneable Clone(Cloner cloner) {
94      return new XCSSolution(this, cloner);
95    }
96    public XCSSolution(IConditionActionModel model, IConditionActionProblemData problemData)
97      : base() {
98      name = ItemName;
99      description = ItemDescription;
100      Add(new Result(ModelResultName, "The xcs model.", model));
101      Add(new Result(ProblemDataResultName, "The condition-action problem data.", problemData));
102      Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
103      Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
104      Add(new Result(NumberOfMacroClassifiersName, new IntValue(model.ClassifierCount)));
105
106      problemData.Changed += new EventHandler(ProblemData_Changed);
107
108      RecalculateResults();
109    }
110
111    private void RecalculateResults() {
112      NumberOfMacroClassifiers = Model.ClassifierCount;
113      var originalTrainingCondition = ProblemData.FetchInput(ProblemData.TrainingIndices);
114      var originalTestCondition = ProblemData.FetchInput(ProblemData.TestIndices);
115      var estimatedTrainingClassifier = Model.GetAction(originalTrainingCondition);
116      var estimatedTestClassifier = Model.GetAction(originalTestCondition);
117
118      var originalTrainingAction = ProblemData.FetchAction(ProblemData.TrainingIndices);
119      var originalTestAction = ProblemData.FetchAction(ProblemData.TestIndices);
120
121      TrainingAccuracy = CalculateAccuracy(originalTrainingAction, estimatedTrainingClassifier);
122      TestAccuracy = CalculateAccuracy(originalTestAction, estimatedTestClassifier);
123    }
124
125    private double CalculateAccuracy(IEnumerable<IAction> original, IEnumerable<IAction> estimated) {
126      double correctClassified = 0;
127
128      double rows = original.Count();
129      var originalEnumerator = original.GetEnumerator();
130      var estimatedActionEnumerator = estimated.GetEnumerator();
131
132      while (originalEnumerator.MoveNext() && estimatedActionEnumerator.MoveNext()) {
133        if (originalEnumerator.Current.Match(estimatedActionEnumerator.Current)) {
134          correctClassified++;
135        }
136      }
137
138      return correctClassified / rows;
139    }
140
141    private void ProblemData_Changed(object sender, EventArgs e) {
142      OnProblemDataChanged();
143    }
144
145    public event EventHandler ModelChanged;
146    protected virtual void OnModelChanged() {
147      RecalculateResults();
148      var listeners = ModelChanged;
149      if (listeners != null) listeners(this, EventArgs.Empty);
150    }
151
152    public event EventHandler ProblemDataChanged;
153    protected virtual void OnProblemDataChanged() {
154      RecalculateResults();
155      var listeners = ProblemDataChanged;
156      if (listeners != null) listeners(this, EventArgs.Empty);
157    }
158
159    #region INamedItem Members
160    [Storable]
161    protected string name;
162    public string Name {
163      get { return name; }
164      set {
165        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
166        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
167          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
168          OnNameChanging(e);
169          if (!e.Cancel) {
170            name = value == null ? string.Empty : value;
171            OnNameChanged();
172          }
173        }
174      }
175    }
176    public virtual bool CanChangeName {
177      get { return true; }
178    }
179    [Storable]
180    protected string description;
181    public string Description {
182      get { return description; }
183      set {
184        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
185        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
186          description = value == null ? string.Empty : value;
187          OnDescriptionChanged();
188        }
189      }
190    }
191    public virtual bool CanChangeDescription {
192      get { return true; }
193    }
194
195    public override string ToString() {
196      return Name;
197    }
198
199    public event EventHandler<CancelEventArgs<string>> NameChanging;
200    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
201      var handler = NameChanging;
202      if (handler != null) handler(this, e);
203    }
204
205    public event EventHandler NameChanged;
206    protected virtual void OnNameChanged() {
207      var handler = NameChanged;
208      if (handler != null) handler(this, EventArgs.Empty);
209      OnToStringChanged();
210    }
211
212    public event EventHandler DescriptionChanged;
213    protected virtual void OnDescriptionChanged() {
214      var handler = DescriptionChanged;
215      if (handler != null) handler(this, EventArgs.Empty);
216    }
217    #endregion
218  }
219}
Note: See TracBrowser for help on using the repository browser.