Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980:

  • added Algorithms.GAssist
  • adapted Problems.DecisionListClassification and Encodings.DecisionList
File size: 9.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.DecisionList {
32  [StorableClass]
33  [Item("DecisionListSolution", "Represents a DecisionList solution.")]
34  public class DecisionListSolution : ResultCollection, IDecisionListSolution {
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 TrainingNumberOfAliveRulesName = "Number of alive rules (training)";
40    //private const string TestNumberOfAliveRules = "Number of alive rules (test)";
41    private const string TrainingTheoryLengthName = "Theory Length (training)";
42    private const string TrainingExceptionsLengthName = "Exceptions Length (training)";
43
44    public double TrainingAccuracy {
45      get { return ((PercentValue)this[TrainingAccuracyResultName].Value).Value; }
46      private set { ((PercentValue)this[TrainingAccuracyResultName].Value).Value = value; }
47    }
48    public double TestAccuracy {
49      get { return ((PercentValue)this[TestAccuracyResultName].Value).Value; }
50      private set { ((PercentValue)this[TestAccuracyResultName].Value).Value = value; }
51    }
52    public int TrainingNumberOfAliveRules {
53      get { return ((IntValue)this[TrainingNumberOfAliveRulesName].Value).Value; }
54      private set { ((IntValue)this[TrainingNumberOfAliveRulesName].Value).Value = value; }
55    }
56    public double TrainingTheoryLength {
57      get { return ((DoubleValue)this[TrainingTheoryLengthName].Value).Value; }
58      private set { ((DoubleValue)this[TrainingTheoryLengthName].Value).Value = value; }
59    }
60    public double TrainingExceptionsLength {
61      get { return ((DoubleValue)this[TrainingExceptionsLengthName].Value).Value; }
62      private set { ((DoubleValue)this[TrainingExceptionsLengthName].Value).Value = value; }
63    }
64
65    DecisionList IDecisionListSolution.Model {
66      get { return Model; }
67    }
68
69    public DecisionList Model {
70      get { return (DecisionList)this[ModelResultName].Value; ; }
71      protected set {
72        if (this[ModelResultName].Value != value) {
73          if (value != null) {
74            this[ModelResultName].Value = value;
75            OnModelChanged();
76          }
77        }
78      }
79    }
80
81    public IDecisionListClassificationProblemData ProblemData {
82      get { return (IDecisionListClassificationProblemData)this[ProblemDataResultName].Value; }
83      set {
84        if (this[ProblemDataResultName].Value != value) {
85          if (value != null) {
86            ProblemData.Changed -= new EventHandler(ProblemData_Changed);
87            this[ProblemDataResultName].Value = value;
88            ProblemData.Changed += new EventHandler(ProblemData_Changed);
89            OnProblemDataChanged();
90          }
91        }
92      }
93    }
94
95    [StorableConstructor]
96    protected DecisionListSolution(bool deserializing) : base(deserializing) { }
97    protected DecisionListSolution(DecisionListSolution original, Cloner cloner)
98      : base(original, cloner) {
99      name = original.Name;
100      description = original.Description;
101    }
102    public DecisionListSolution(DecisionList model, IDecisionListClassificationProblemData problemData)
103      : base() {
104      name = ItemName;
105      description = ItemDescription;
106      Add(new Result(ModelResultName, "Decision List.", model));
107      Add(new Result(ProblemDataResultName, "The problem data.", problemData));
108      Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
109      Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
110      Add(new Result(TrainingNumberOfAliveRulesName, "", new IntValue()));
111      Add(new Result(TrainingTheoryLengthName, "", new DoubleValue()));
112      Add(new Result(TrainingExceptionsLengthName, "", new DoubleValue()));
113
114      problemData.Changed += new EventHandler(ProblemData_Changed);
115
116      RecalculateResults();
117    }
118    public override IDeepCloneable Clone(Cloner cloner) {
119      return new DecisionListSolution(this, cloner);
120    }
121
122    private void RecalculateResults() {
123      var originalTrainingCondition = ProblemData.FetchInput(ProblemData.TrainingIndices);
124      var originalTestCondition = ProblemData.FetchInput(ProblemData.TestIndices);
125      int numberOfAliveRules;
126      double theoryLength;
127      var estimatedTraining = Model.Evaluate(originalTrainingCondition, out numberOfAliveRules, out theoryLength);
128      TrainingNumberOfAliveRules = numberOfAliveRules;
129      TrainingTheoryLength = theoryLength;
130      var estimatedTest = Model.Evaluate(originalTestCondition);
131
132      var originalTrainingAction = ProblemData.FetchAction(ProblemData.TrainingIndices);
133      var originalTestAction = ProblemData.FetchAction(ProblemData.TestIndices);
134
135      TrainingAccuracy = CalculateAccuracy(originalTrainingAction, estimatedTraining);
136      TestAccuracy = CalculateAccuracy(originalTestAction, estimatedTest);
137
138      TrainingExceptionsLength = 105.0 - TrainingAccuracy * 100.0;
139    }
140
141    private double CalculateAccuracy(IEnumerable<IAction> original, IEnumerable<IAction> estimated) {
142      double correctClassified = 0;
143
144      double rows = original.Count();
145      var originalEnumerator = original.GetEnumerator();
146      var estimatedActionEnumerator = estimated.GetEnumerator();
147
148      while (originalEnumerator.MoveNext() && estimatedActionEnumerator.MoveNext()) {
149        if (originalEnumerator.Current.Match(estimatedActionEnumerator.Current)) {
150          correctClassified++;
151        }
152      }
153      return correctClassified / rows;
154    }
155
156    private void ProblemData_Changed(object sender, EventArgs e) {
157      OnProblemDataChanged();
158    }
159
160    public event EventHandler ModelChanged;
161    protected virtual void OnModelChanged() {
162      RecalculateResults();
163      var listeners = ModelChanged;
164      if (listeners != null) listeners(this, EventArgs.Empty);
165    }
166
167    public event EventHandler ProblemDataChanged;
168    protected virtual void OnProblemDataChanged() {
169      RecalculateResults();
170      var listeners = ProblemDataChanged;
171      if (listeners != null) listeners(this, EventArgs.Empty);
172    }
173
174    #region INamedItem Members
175    [Storable]
176    protected string name;
177    public string Name {
178      get { return name; }
179      set {
180        if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
181        if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
182          CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
183          OnNameChanging(e);
184          if (!e.Cancel) {
185            name = value == null ? string.Empty : value;
186            OnNameChanged();
187          }
188        }
189      }
190    }
191    public virtual bool CanChangeName {
192      get { return true; }
193    }
194    [Storable]
195    protected string description;
196    public string Description {
197      get { return description; }
198      set {
199        if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
200        if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
201          description = value == null ? string.Empty : value;
202          OnDescriptionChanged();
203        }
204      }
205    }
206    public virtual bool CanChangeDescription {
207      get { return true; }
208    }
209
210    public override string ToString() {
211      return Name;
212    }
213
214    public event EventHandler<CancelEventArgs<string>> NameChanging;
215    protected virtual void OnNameChanging(CancelEventArgs<string> e) {
216      var handler = NameChanging;
217      if (handler != null) handler(this, e);
218    }
219
220    public event EventHandler NameChanged;
221    protected virtual void OnNameChanged() {
222      var handler = NameChanged;
223      if (handler != null) handler(this, EventArgs.Empty);
224      OnToStringChanged();
225    }
226
227    public event EventHandler DescriptionChanged;
228    protected virtual void OnDescriptionChanged() {
229      var handler = DescriptionChanged;
230      if (handler != null) handler(this, EventArgs.Empty);
231    }
232    #endregion
233  }
234}
Note: See TracBrowser for help on using the repository browser.