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 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 originalTrainingCondition = ProblemData.FetchInput(ProblemData.TrainingIndices);
|
---|
107 | var originalTestCondition = ProblemData.FetchInput(ProblemData.TestIndices);
|
---|
108 | var estimatedTrainingClassifier = Model.GetAction(originalTrainingCondition);
|
---|
109 | var estimatedTestClassifier = Model.GetAction(originalTestCondition);
|
---|
110 |
|
---|
111 | var originalTrainingAction = ProblemData.FetchAction(ProblemData.TrainingIndices);
|
---|
112 | var originalTestAction = ProblemData.FetchAction(ProblemData.TestIndices);
|
---|
113 |
|
---|
114 | TrainingAccuracy = CalculateAccuracy(originalTrainingAction, estimatedTrainingClassifier);
|
---|
115 | TestAccuracy = CalculateAccuracy(originalTestAction, estimatedTestClassifier);
|
---|
116 | }
|
---|
117 |
|
---|
118 | private double CalculateAccuracy(IEnumerable<IAction> original, IEnumerable<IAction> estimated) {
|
---|
119 | double correctClassified = 0;
|
---|
120 |
|
---|
121 | double rows = original.Count();
|
---|
122 | var originalEnumerator = original.GetEnumerator();
|
---|
123 | var estimatedActionEnumerator = estimated.GetEnumerator();
|
---|
124 |
|
---|
125 | while (originalEnumerator.MoveNext() && estimatedActionEnumerator.MoveNext()) {
|
---|
126 | if (originalEnumerator.Current.Match(estimatedActionEnumerator.Current)) {
|
---|
127 | correctClassified++;
|
---|
128 | }
|
---|
129 | }
|
---|
130 |
|
---|
131 | return correctClassified / rows;
|
---|
132 | }
|
---|
133 |
|
---|
134 | private void ProblemData_Changed(object sender, EventArgs e) {
|
---|
135 | OnProblemDataChanged();
|
---|
136 | }
|
---|
137 |
|
---|
138 | public event EventHandler ModelChanged;
|
---|
139 | protected virtual void OnModelChanged() {
|
---|
140 | RecalculateResults();
|
---|
141 | var listeners = ModelChanged;
|
---|
142 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
143 | }
|
---|
144 |
|
---|
145 | public event EventHandler ProblemDataChanged;
|
---|
146 | protected virtual void OnProblemDataChanged() {
|
---|
147 | RecalculateResults();
|
---|
148 | var listeners = ProblemDataChanged;
|
---|
149 | if (listeners != null) listeners(this, EventArgs.Empty);
|
---|
150 | }
|
---|
151 |
|
---|
152 | #region INamedItem Members
|
---|
153 | [Storable]
|
---|
154 | protected string name;
|
---|
155 | public string Name {
|
---|
156 | get { return name; }
|
---|
157 | set {
|
---|
158 | if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
|
---|
159 | if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
|
---|
160 | CancelEventArgs<string> e = value == null ? new CancelEventArgs<string>(string.Empty) : new CancelEventArgs<string>(value);
|
---|
161 | OnNameChanging(e);
|
---|
162 | if (!e.Cancel) {
|
---|
163 | name = value == null ? string.Empty : value;
|
---|
164 | OnNameChanged();
|
---|
165 | }
|
---|
166 | }
|
---|
167 | }
|
---|
168 | }
|
---|
169 | public virtual bool CanChangeName {
|
---|
170 | get { return true; }
|
---|
171 | }
|
---|
172 | [Storable]
|
---|
173 | protected string description;
|
---|
174 | public string Description {
|
---|
175 | get { return description; }
|
---|
176 | set {
|
---|
177 | if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
|
---|
178 | if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
|
---|
179 | description = value == null ? string.Empty : value;
|
---|
180 | OnDescriptionChanged();
|
---|
181 | }
|
---|
182 | }
|
---|
183 | }
|
---|
184 | public virtual bool CanChangeDescription {
|
---|
185 | get { return true; }
|
---|
186 | }
|
---|
187 |
|
---|
188 | public override string ToString() {
|
---|
189 | return Name;
|
---|
190 | }
|
---|
191 |
|
---|
192 | public event EventHandler<CancelEventArgs<string>> NameChanging;
|
---|
193 | protected virtual void OnNameChanging(CancelEventArgs<string> e) {
|
---|
194 | var handler = NameChanging;
|
---|
195 | if (handler != null) handler(this, e);
|
---|
196 | }
|
---|
197 |
|
---|
198 | public event EventHandler NameChanged;
|
---|
199 | protected virtual void OnNameChanged() {
|
---|
200 | var handler = NameChanged;
|
---|
201 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
202 | OnToStringChanged();
|
---|
203 | }
|
---|
204 |
|
---|
205 | public event EventHandler DescriptionChanged;
|
---|
206 | protected virtual void OnDescriptionChanged() {
|
---|
207 | var handler = DescriptionChanged;
|
---|
208 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
209 | }
|
---|
210 | #endregion
|
---|
211 | }
|
---|
212 | }
|
---|