Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

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