Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added XCSSolution, XCSModel, XCSClassifier to represent the xcs classifier
  • XCSSolution also shows the current accuracy (training and test partition has to be added)
  • added XCSSolutionAnalyzer to create a XCSSolution during the run of the algorithm
  • added XCSModelView to show the xcs model
  • fixed a bug in XCSDeletionOperator (sometimes it deleted less classifiers than it should)
  • moved some parameter from ConditionActionClassificationProblem to ConditionActionClassificationProblemData
File size: 6.9 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29
30namespace HeuristicLab.Encodings.ConditionActionEncoding {
31  [StorableClass]
32  [Item("XCSSolution", "Represents a XCS solution.")]
33  public class XCSSolution : ResultCollection, IXCSSolution {
34    private const string ModelResultName = "Model";
35    private const string ProblemDataResultName = "ProblemData";
36    private const string AccuracyResultName = "Accuracy";
37
38    public string Filename { get; set; }
39
40    public double Accuracy {
41      get { return ((DoubleValue)this[AccuracyResultName].Value).Value; }
42      private set { ((DoubleValue)this[AccuracyResultName].Value).Value = value; }
43    }
44
45    IConditionActionModel IConditionActionSolution.Model {
46      get { return Model; }
47    }
48
49    public IXCSModel Model {
50      get { return (IXCSModel)this[ModelResultName].Value; ; }
51      protected set {
52        if (this[ModelResultName].Value != value) {
53          if (value != null) {
54            this[ModelResultName].Value = value;
55            OnModelChanged();
56          }
57        }
58      }
59    }
60
61    public IConditionActionProblemData ProblemData {
62      get { return (IConditionActionProblemData)this[ProblemDataResultName].Value; }
63      set {
64        if (this[ProblemDataResultName].Value != value) {
65          if (value != null) {
66            ProblemData.Changed -= new EventHandler(ProblemData_Changed);
67            this[ProblemDataResultName].Value = value;
68            ProblemData.Changed += new EventHandler(ProblemData_Changed);
69            OnProblemDataChanged();
70          }
71        }
72      }
73    }
74
75    [StorableConstructor]
76    protected XCSSolution(bool deserializing) : base(deserializing) { }
77    protected XCSSolution(XCSSolution original, Cloner cloner)
78      : base(original, cloner) {
79      name = original.Name;
80      description = original.Description;
81    }
82    public XCSSolution(IConditionActionModel model, IConditionActionProblemData problemData)
83      : base() {
84      name = ItemName;
85      description = ItemDescription;
86      Add(new Result(ModelResultName, "The xcs model.", model));
87      Add(new Result(ProblemDataResultName, "The condition-action problem data.", problemData));
88      Add(new Result(AccuracyResultName, "Accuracy of the model (percentage of correctly classified instances).", new PercentValue()));
89
90      problemData.Changed += new EventHandler(ProblemData_Changed);
91
92      RecalculateResults();
93    }
94
95    private void RecalculateResults() {
96      var originalClassifiers = new List<IClassifier>();
97      for (int i = 0; i < ProblemData.Dataset.Rows; i++) {
98        originalClassifiers.Add(ProblemData.FetchClassifier(i));
99      }
100
101      var estimatedAction = new List<IClassifier>();
102      foreach (var original in originalClassifiers) {
103        estimatedAction.Add(Model.GetAction(original));
104      }
105
106      double correctClassified = 0;
107      double rows = ProblemData.Dataset.Rows;
108      var originalEnumerator = originalClassifiers.GetEnumerator();
109      var estimatedActionEnumerator = estimatedAction.GetEnumerator();
110
111      while (originalEnumerator.MoveNext() && estimatedActionEnumerator.MoveNext()) {
112        if (originalEnumerator.Current.Action.Equals(estimatedActionEnumerator.Current)) {
113          correctClassified++;
114        }
115      }
116
117      Accuracy = correctClassified / rows;
118    }
119
120    private void ProblemData_Changed(object sender, EventArgs e) {
121      OnProblemDataChanged();
122    }
123
124    public event EventHandler ModelChanged;
125    protected virtual void OnModelChanged() {
126      RecalculateResults();
127      var listeners = ModelChanged;
128      if (listeners != null) listeners(this, EventArgs.Empty);
129    }
130
131    public event EventHandler ProblemDataChanged;
132    protected virtual void OnProblemDataChanged() {
133      RecalculateResults();
134      var listeners = ProblemDataChanged;
135      if (listeners != null) listeners(this, EventArgs.Empty);
136    }
137
138    #region INamedItem Members
139    [Storable]
140    protected string name;
141    public string Name {
142      get { return name; }
143      set {
144        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
145        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
146          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
147          OnNameChanging(e);
148          if (!e.Cancel) {
149            name = value == null ? string.Empty : value;
150            OnNameChanged();
151          }
152        }
153      }
154    }
155    public virtual bool CanChangeName {
156      get { return true; }
157    }
158    [Storable]
159    protected string description;
160    public string Description {
161      get { return description; }
162      set {
163        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
164        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
165          description = value == null ? string.Empty : value;
166          OnDescriptionChanged();
167        }
168      }
169    }
170    public virtual bool CanChangeDescription {
171      get { return true; }
172    }
173
174    public override string ToString() {
175      return Name;
176    }
177
178    public event EventHandler<CancelEventArgs<string>> NameChanging;
179    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
180      var handler = NameChanging;
181      if (handler != null) handler(this, e);
182    }
183
184    public event EventHandler NameChanged;
185    protected virtual void OnNameChanged() {
186      var handler = NameChanged;
187      if (handler != null) handler(this, EventArgs.Empty);
188      OnToStringChanged();
189    }
190
191    public event EventHandler DescriptionChanged;
192    protected virtual void OnDescriptionChanged() {
193      var handler = DescriptionChanged;
194      if (handler != null) handler(this, EventArgs.Empty);
195    }
196    #endregion
197  }
198}
Note: See TracBrowser for help on using the repository browser.