#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 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.Collections;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.ConditionActionEncoding;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Problems.ConditionActionClassification {
[StorableClass]
[Item("ConditionActionClassificationProblemData", "A problem data for LCS.")]
public abstract class ConditionActionClassificationProblemData : ParameterizedNamedItem, IConditionActionProblemData {
#region default data
public static string[] defaultVariableNames = new string[] { "a", "b", "c", "d", "e", "f", "g" };
public static double[,] defaultData = new double[,]{
{0,0,1,1,0,0,0},
{0,1,1,1,0,0,0},
{0,0,1,0,0,0,1},
{1,0,1,0,1,1,0}
};
#endregion
#region parameter properites
public IFixedValueParameter DatasetParameter {
get { return (IFixedValueParameter)Parameters["Dataset"]; }
}
public IFixedValueParameter> ConditionVariablesParameter {
get { return (IFixedValueParameter>)Parameters["ConditionVariables"]; }
}
public IFixedValueParameter> ActionVariablesParameter {
get { return (IFixedValueParameter>)Parameters["ActionVariables"]; }
}
public IFixedValueParameter TrainingPartitionParameter {
get { return (IFixedValueParameter)Parameters["TrainingPartition"]; }
}
public IFixedValueParameter TestPartitionParameter {
get { return (IFixedValueParameter)Parameters["TestPartition"]; }
}
#endregion
#region properties
public Dataset Dataset {
get { return DatasetParameter.Value; }
}
public ICheckedItemList ConditionVariables {
get { return ConditionVariablesParameter.Value; }
}
public ICheckedItemList ActionVariables {
get { return ActionVariablesParameter.Value; }
}
public IEnumerable AllowedConditionVariables {
get { return ConditionVariables.CheckedItems.Select(x => x.Value.Value); }
}
public IEnumerable AllowedActionVariables {
get { return ActionVariables.CheckedItems.Select(x => x.Value.Value); }
}
public IntRange TrainingPartition {
get { return TrainingPartitionParameter.Value; }
}
public IntRange TestPartition {
get { return TestPartitionParameter.Value; }
}
public IEnumerable TrainingIndices {
get {
return Enumerable.Range(TrainingPartition.Start, Math.Max(0, TrainingPartition.End - TrainingPartition.Start))
.Where(IsTrainingSample);
}
}
public IEnumerable TestIndices {
get {
return Enumerable.Range(TestPartition.Start, Math.Max(0, TestPartition.End - TestPartition.Start))
.Where(IsTestSample);
}
}
public bool IsTrainingSample(int index) {
return index >= 0 && index < Dataset.Rows &&
TrainingPartition.Start <= index && index < TrainingPartition.End &&
(index < TestPartition.Start || TestPartition.End <= index);
}
public bool IsTestSample(int index) {
return index >= 0 && index < Dataset.Rows &&
TestPartition.Start <= index && index < TestPartition.End;
}
public abstract IClassifierComparer ClassifierComparer { get; }
#endregion
[StorableHook(HookType.AfterDeserialization)]
private void AfterDeserialization() {
RegisterParameterEvents();
}
[StorableConstructor]
protected ConditionActionClassificationProblemData(bool deserializing) : base(deserializing) { }
protected ConditionActionClassificationProblemData(ConditionActionClassificationProblemData original, Cloner cloner)
: base(original, cloner) {
RegisterParameterEvents();
}
public ConditionActionClassificationProblemData(Dataset dataset, IEnumerable allowedConditionVariables, IEnumerable allowedActionVariables) {
if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
if (allowedActionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
if (allowedConditionVariables == null) throw new ArgumentNullException("The allowedActionVariables must not be null.");
var actionVariables = CheckVariablesForPossibleTargetVariables(dataset);
foreach (StringValue x in actionVariables) {
actionVariables.SetItemCheckedState(x, allowedActionVariables.Contains(x.Value));
}
var conditionVariables = new CheckedItemList(dataset.VariableNames.Select(x => new StringValue(x)));
foreach (StringValue x in conditionVariables) {
conditionVariables.SetItemCheckedState(x, allowedConditionVariables.Contains(x.Value));
}
int trainingPartitionStart = 0;
int trainingPartitionEnd = dataset.Rows / 2;
int testPartitionStart = dataset.Rows / 2;
int testPartitionEnd = dataset.Rows;
Parameters.Add(new FixedValueParameter("Dataset", "", dataset));
Parameters.Add(new FixedValueParameter>("ActionVariables", "", actionVariables.AsReadOnly()));
Parameters.Add(new FixedValueParameter>("ConditionVariables", "", conditionVariables.AsReadOnly()));
Parameters.Add(new FixedValueParameter("TrainingPartition", "", new IntRange(trainingPartitionStart, trainingPartitionEnd)));
Parameters.Add(new FixedValueParameter("TestPartition", "", new IntRange(testPartitionStart, testPartitionEnd)));
((ValueParameter)DatasetParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
RegisterParameterEvents();
}
protected virtual CheckedItemList CheckVariablesForPossibleTargetVariables(DataAnalysis.Dataset dataset) {
return new CheckedItemList(dataset.VariableNames.Select(x => new StringValue(x)));
}
public event EventHandler Changed;
protected virtual void OnChanged() {
var listeners = Changed;
if (listeners != null) listeners(this, EventArgs.Empty);
}
public IEnumerable FetchInput(IEnumerable rows) {
foreach (var row in rows) {
yield return FetchInput(row);
}
}
public abstract IInput FetchInput(int rowNumber);
public IEnumerable FetchAction(IEnumerable rows) {
foreach (var row in rows) {
yield return FetchAction(row);
}
}
public abstract IAction FetchAction(int rowNumber);
protected IDictionary fetchInputCache = new Dictionary();
#region events
private void RegisterParameterEvents() {
ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler>(VariablesChanged);
ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler>(VariablesChanged);
}
private void DeregisterParameterEvents() {
ActionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler>(VariablesChanged);
ActionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
ConditionVariablesParameter.Value.CheckedItemsChanged += new CollectionItemsChangedEventHandler>(VariablesChanged);
ConditionVariablesParameter.ValueChanged += new EventHandler(VariablesChanged);
}
private void Value_CheckedItemsChanged(object sender, CollectionItemsChangedEventArgs> e) {
ActionConditionVariablesChanged();
}
private void VariablesChanged(object sender, EventArgs e) {
ActionConditionVariablesChanged();
}
protected abstract void ActionConditionVariablesChanged();
#endregion
#region IDataAnalysisProblemData Members
public bool IsEmpty {
get { return true; }
}
public ICheckedItemList InputVariables {
get { return ConditionVariables; }
}
public IEnumerable AllowedInputVariables {
get { return AllowedConditionVariables; }
}
#endregion
}
}