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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Optimization;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 |
|
---|
31 | namespace 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 XCSSolution(IConditionActionModel model, IConditionActionProblemData problemData)
|
---|
89 | : base() {
|
---|
90 | name = ItemName;
|
---|
91 | description = ItemDescription;
|
---|
92 | Add(new Result(ModelResultName, "The xcs model.", model));
|
---|
93 | Add(new Result(ProblemDataResultName, "The condition-action problem data.", problemData));
|
---|
94 | Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
|
---|
95 | Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
|
---|
96 |
|
---|
97 | problemData.Changed += new EventHandler(ProblemData_Changed);
|
---|
98 |
|
---|
99 | RecalculateResults();
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void RecalculateResults() {
|
---|
103 | var originalTrainingClassifer = ProblemData.FetchClassifier(ProblemData.TrainingIndices);
|
---|
104 | var originalTestClassifer = ProblemData.FetchClassifier(ProblemData.TestIndices);
|
---|
105 | var estimatedTrainingClassifier = Model.GetAction(originalTrainingClassifer);
|
---|
106 | var estimatedTestClassifier = Model.GetAction(originalTestClassifer);
|
---|
107 |
|
---|
108 | TrainingAccuracy = CalculateAccuracy(originalTrainingClassifer, estimatedTrainingClassifier);
|
---|
109 | TestAccuracy = CalculateAccuracy(originalTestClassifer, estimatedTestClassifier);
|
---|
110 | }
|
---|
111 |
|
---|
112 | private double CalculateAccuracy(IEnumerable<IClassifier> original, IEnumerable<IClassifier> estimated) {
|
---|
113 | double correctClassified = 0;
|
---|
114 |
|
---|
115 | double rows = original.Count();
|
---|
116 | var originalEnumerator = original.GetEnumerator();
|
---|
117 | var estimatedActionEnumerator = estimated.GetEnumerator();
|
---|
118 |
|
---|
119 | while (originalEnumerator.MoveNext() && estimatedActionEnumerator.MoveNext()) {
|
---|
120 | if (originalEnumerator.Current.Action.Equals(estimatedActionEnumerator.Current)) {
|
---|
121 | correctClassified++;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | return correctClassified / rows;
|
---|
126 | }
|
---|
127 |
|
---|
128 | private void ProblemData_Changed(object sender, EventArgs e) {
|
---|
129 | OnProblemDataChanged();
|
---|
130 | }
|
---|
131 |
|
---|
132 | public event EventHandler ModelChanged;
|
---|
133 | protected virtual void OnModelChanged() {
|
---|
134 | RecalculateResults();
|
---|
135 | var listeners = ModelChanged;
|
---|
136 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
137 | }
|
---|
138 |
|
---|
139 | public event EventHandler ProblemDataChanged;
|
---|
140 | protected virtual void OnProblemDataChanged() {
|
---|
141 | RecalculateResults();
|
---|
142 | var listeners = ProblemDataChanged;
|
---|
143 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
144 | }
|
---|
145 |
|
---|
146 | #region INamedItem Members
|
---|
147 | [Storable]
|
---|
148 | protected string name;
|
---|
149 | public string Name {
|
---|
150 | get { return name; }
|
---|
151 | set {
|
---|
152 | if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
|
---|
153 | if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
|
---|
154 | CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
|
---|
155 | OnNameChanging(e);
|
---|
156 | if (!e.Cancel) {
|
---|
157 | name = value == null ? string.Empty : value;
|
---|
158 | OnNameChanged();
|
---|
159 | }
|
---|
160 | }
|
---|
161 | }
|
---|
162 | }
|
---|
163 | public virtual bool CanChangeName {
|
---|
164 | get { return true; }
|
---|
165 | }
|
---|
166 | [Storable]
|
---|
167 | protected string description;
|
---|
168 | public string Description {
|
---|
169 | get { return description; }
|
---|
170 | set {
|
---|
171 | if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
|
---|
172 | if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
|
---|
173 | description = value == null ? string.Empty : value;
|
---|
174 | OnDescriptionChanged();
|
---|
175 | }
|
---|
176 | }
|
---|
177 | }
|
---|
178 | public virtual bool CanChangeDescription {
|
---|
179 | get { return true; }
|
---|
180 | }
|
---|
181 |
|
---|
182 | public override string ToString() {
|
---|
183 | return Name;
|
---|
184 | }
|
---|
185 |
|
---|
186 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
187 | protected virtual void OnNameChanging(CancelEventArgs<string> e) {
|
---|
188 | var handler = NameChanging;
|
---|
189 | if (handler != null) handler(this, e);
|
---|
190 | }
|
---|
191 |
|
---|
192 | public event EventHandler NameChanged;
|
---|
193 | protected virtual void OnNameChanged() {
|
---|
194 | var handler = NameChanged;
|
---|
195 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
196 | OnToStringChanged();
|
---|
197 | }
|
---|
198 |
|
---|
199 | public event EventHandler DescriptionChanged;
|
---|
200 | protected virtual void OnDescriptionChanged() {
|
---|
201 | var handler = DescriptionChanged;
|
---|
202 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
203 | }
|
---|
204 | #endregion
|
---|
205 | }
|
---|
206 | }
|
---|