#region License Information
/* HeuristicLab
* Copyright (C) 2002-2011 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.ConditionActionEncoding {
[StorableClass]
[Item("XCSSolution", "Represents a XCS solution.")]
public class XCSSolution : ResultCollection, IXCSSolution {
private const string ModelResultName = "Model";
private const string ProblemDataResultName = "ProblemData";
private const string TrainingAccuracyResultName = "Accuracy (training)";
private const string TestAccuracyResultName = "Accuracy (test)";
private const string NumberOfMacroClassifiersName = "Number of Macroclassifiers";
public string Filename { get; set; }
public double TrainingAccuracy {
get { return ((DoubleValue)this[TrainingAccuracyResultName].Value).Value; }
private set { ((DoubleValue)this[TrainingAccuracyResultName].Value).Value = value; }
}
public double TestAccuracy {
get { return ((DoubleValue)this[TestAccuracyResultName].Value).Value; }
private set { ((DoubleValue)this[TestAccuracyResultName].Value).Value = value; }
}
public int NumberOfMacroClassifiers {
get { return ((IntValue)this[NumberOfMacroClassifiersName].Value).Value; }
private set { ((IntValue)this[NumberOfMacroClassifiersName].Value).Value = value; }
}
IConditionActionModel IConditionActionSolution.Model {
get { return Model; }
}
public IXCSModel Model {
get { return (IXCSModel)this[ModelResultName].Value; ; }
protected set {
if (this[ModelResultName].Value != value) {
if (value != null) {
this[ModelResultName].Value = value;
OnModelChanged();
}
}
}
}
public IConditionActionProblemData ProblemData {
get { return (IConditionActionProblemData)this[ProblemDataResultName].Value; }
set {
if (this[ProblemDataResultName].Value != value) {
if (value != null) {
ProblemData.Changed -= new EventHandler(ProblemData_Changed);
this[ProblemDataResultName].Value = value;
ProblemData.Changed += new EventHandler(ProblemData_Changed);
OnProblemDataChanged();
}
}
}
}
[StorableConstructor]
protected XCSSolution(bool deserializing) : base(deserializing) { }
protected XCSSolution(XCSSolution original, Cloner cloner)
: base(original, cloner) {
name = original.Name;
description = original.Description;
}
public override IDeepCloneable Clone(Cloner cloner) {
return new XCSSolution(this, cloner);
}
public XCSSolution(IConditionActionModel model, IConditionActionProblemData problemData)
: base() {
name = ItemName;
description = ItemDescription;
Add(new Result(ModelResultName, "The xcs model.", model));
Add(new Result(ProblemDataResultName, "The condition-action problem data.", problemData));
Add(new Result(TrainingAccuracyResultName, "Accuracy of the model on the training partition (percentage of correctly classified instances).", new PercentValue()));
Add(new Result(TestAccuracyResultName, "Accuracy of the model on the test partition (percentage of correctly classified instances).", new PercentValue()));
Add(new Result(NumberOfMacroClassifiersName, new IntValue(model.ClassifierCount)));
problemData.Changed += new EventHandler(ProblemData_Changed);
RecalculateResults();
}
private void RecalculateResults() {
NumberOfMacroClassifiers = Model.ClassifierCount;
var originalTrainingCondition = ProblemData.FetchInput(ProblemData.TrainingIndices);
var originalTestCondition = ProblemData.FetchInput(ProblemData.TestIndices);
var estimatedTrainingClassifier = Model.GetAction(originalTrainingCondition);
var estimatedTestClassifier = Model.GetAction(originalTestCondition);
var originalTrainingAction = ProblemData.FetchAction(ProblemData.TrainingIndices);
var originalTestAction = ProblemData.FetchAction(ProblemData.TestIndices);
TrainingAccuracy = CalculateAccuracy(originalTrainingAction, estimatedTrainingClassifier);
TestAccuracy = CalculateAccuracy(originalTestAction, estimatedTestClassifier);
}
private double CalculateAccuracy(IEnumerable original, IEnumerable estimated) {
double correctClassified = 0;
double rows = original.Count();
var originalEnumerator = original.GetEnumerator();
var estimatedActionEnumerator = estimated.GetEnumerator();
while (originalEnumerator.MoveNext() && estimatedActionEnumerator.MoveNext()) {
if (originalEnumerator.Current.Match(estimatedActionEnumerator.Current)) {
correctClassified++;
}
}
return correctClassified / rows;
}
private void ProblemData_Changed(object sender, EventArgs e) {
OnProblemDataChanged();
}
public event EventHandler ModelChanged;
protected virtual void OnModelChanged() {
RecalculateResults();
var listeners = ModelChanged;
if (listeners != null) listeners(this, EventArgs.Empty);
}
public event EventHandler ProblemDataChanged;
protected virtual void OnProblemDataChanged() {
RecalculateResults();
var listeners = ProblemDataChanged;
if (listeners != null) listeners(this, EventArgs.Empty);
}
#region INamedItem Members
[Storable]
protected string name;
public string Name {
get { return name; }
set {
if (!CanChangeName) throw new NotSupportedException("Name cannot be changed.");
if (!(name.Equals(value) || (value == null) && (name == string.Empty))) {
CancelEventArgs e = value == null ? new CancelEventArgs(string.Empty) : new CancelEventArgs(value);
OnNameChanging(e);
if (!e.Cancel) {
name = value == null ? string.Empty : value;
OnNameChanged();
}
}
}
}
public virtual bool CanChangeName {
get { return true; }
}
[Storable]
protected string description;
public string Description {
get { return description; }
set {
if (!CanChangeDescription) throw new NotSupportedException("Description cannot be changed.");
if (!(description.Equals(value) || (value == null) && (description == string.Empty))) {
description = value == null ? string.Empty : value;
OnDescriptionChanged();
}
}
}
public virtual bool CanChangeDescription {
get { return true; }
}
public override string ToString() {
return Name;
}
public event EventHandler> NameChanging;
protected virtual void OnNameChanging(CancelEventArgs e) {
var handler = NameChanging;
if (handler != null) handler(this, e);
}
public event EventHandler NameChanged;
protected virtual void OnNameChanged() {
var handler = NameChanged;
if (handler != null) handler(this, EventArgs.Empty);
OnToStringChanged();
}
public event EventHandler DescriptionChanged;
protected virtual void OnDescriptionChanged() {
var handler = DescriptionChanged;
if (handler != null) handler(this, EventArgs.Empty);
}
#endregion
}
}