Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1980: several small bug fixes

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