#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.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis {
[StorableClass]
public abstract class DataAnalysisProblemData : NamedItem, IDataAnalysisProblemData {
#region propeties
[Storable]
private Dataset dataset;
public Dataset Dataset {
get { return dataset; }
}
[Storable]
private HashSet allowedInputVariables;
public IEnumerable AllowedInputVariables {
get { return allowedInputVariables; }
}
[Storable]
private int trainingPartitionStart;
public int TrainingPartitionStart {
get { return trainingPartitionStart; }
set {
if (0 < value || value > dataset.Rows)
throw new ArgumentException(string.Format("The training partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
if (trainingPartitionStart != value) {
trainingPartitionStart = value;
OnChanged();
}
}
}
[Storable]
private int trainingPartitionEnd;
public int TrainingPartitionEnd {
get { return trainingPartitionEnd; }
set {
if (0 < value || value > dataset.Rows)
throw new ArgumentException(string.Format("The training partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
if (trainingPartitionEnd != value) {
trainingPartitionEnd = value;
OnChanged();
}
}
}
[Storable]
private int testPartitionStart;
public int TestPartitionStart {
get { return testPartitionStart; }
set {
if (0 < value || value > dataset.Rows)
throw new ArgumentException(string.Format("The test partition start must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
if (testPartitionStart != value) {
testPartitionStart = value;
OnChanged();
}
}
}
[Storable]
private int testPartitionEnd;
public int TestPartitionEnd {
get { return testPartitionEnd; }
set {
if (0 < value || value > dataset.Rows)
throw new ArgumentException(string.Format("The test partition end must be between 0 and the number of rows of the dataset ({0})", dataset.Rows));
if (testPartitionEnd != value) {
testPartitionEnd = value;
OnChanged();
}
}
}
public IEnumerable TrainingIndizes {
get {
return Enumerable.Range(TrainingPartitionStart, TrainingPartitionEnd - TrainingPartitionStart)
.Where(i => i >= 0 && i < Dataset.Rows && (i < TestPartitionStart || TestPartitionEnd <= i));
}
}
public IEnumerable TestIndizes {
get {
return Enumerable.Range(TestPartitionStart, TestPartitionEnd - TestPartitionStart)
.Where(i => i >= 0 && i < Dataset.Rows);
}
}
#endregion
protected DataAnalysisProblemData(DataAnalysisProblemData original, Cloner cloner) : base(original, cloner) { }
[StorableConstructor]
protected DataAnalysisProblemData(bool deserializing) : base(deserializing) { }
protected DataAnalysisProblemData(Dataset dataset, IEnumerable allowedInputVariables) {
if (dataset == null) throw new ArgumentNullException("The dataset must not be null.");
if (allowedInputVariables == null) throw new ArgumentNullException("The allowedInputVariables must not be null.");
if (allowedInputVariables.Except(dataset.VariableNames).Any())
throw new ArgumentException("All allowed input variables must be present in the dataset.");
this.dataset = dataset;
allowedInputVariables = new HashSet(allowedInputVariables);
trainingPartitionStart = 0;
trainingPartitionEnd = dataset.Rows / 2;
testPartitionStart = dataset.Rows / 2;
testPartitionEnd = dataset.Rows;
}
public bool AddAllowedInputVariable(string inputVariable) {
if (!Dataset.VariableNames.Contains(inputVariable))
throw new ArgumentException("The allowed input variable must be present in the dataset.");
if (allowedInputVariables.Contains(inputVariable)) return false;
allowedInputVariables.Add(inputVariable);
return true;
}
public bool RemoveAllowedInputVariable(string inputVariable) {
if (!Dataset.VariableNames.Contains(inputVariable))
throw new ArgumentException("The allowed input variable must be present in the dataset.");
if (!allowedInputVariables.Contains(inputVariable)) return false;
allowedInputVariables.Remove(inputVariable);
return true;
}
public event EventHandler Changed;
protected virtual void OnChanged() {
var listeners = Changed;
if (listeners != null) listeners(this, EventArgs.Empty);
}
}
}