#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.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.ConditionActionEncoding;
using HeuristicLab.Encodings.VariableVector;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.ConditionActionClassification;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Problems.VariableVectorClassification {
[StorableClass]
[Item("CombinedIntegerVectorClassificationProblemData", "A problem data for LCS.")]
public class VariableVectorClassificationProblemData : ConditionActionClassificationProblemData, IVariableVectorClassificationProblemData {
#region parameter properites
public IValueParameter SampleVariableVectorParameter {
get { return (IValueParameter)Parameters["SampleVariableVector"]; }
}
public IFixedValueParameter SpreadPercentageParameter {
get { return (IFixedValueParameter)Parameters["SpreadPercentage"]; }
}
#endregion
#region properties
public VariableVector SampleVariableVector {
get { return SampleVariableVectorParameter.Value; }
}
public PercentValue SpreadPercentage {
get { return SpreadPercentageParameter.Value; }
}
public VariableVectorActionComparer ConcreteClassifierComparer {
get { return new VariableVectorActionComparer(); }
}
public override IClassifierComparer ClassifierComparer {
get { return ConcreteClassifierComparer; }
}
#endregion
[StorableConstructor]
protected VariableVectorClassificationProblemData(bool deserializing) : base(deserializing) { }
protected VariableVectorClassificationProblemData(VariableVectorClassificationProblemData original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new VariableVectorClassificationProblemData(this, cloner);
}
public VariableVectorClassificationProblemData(Dataset dataset, IEnumerable allowedConditionVariables, IEnumerable allowedActionVariables) :
base(dataset, allowedConditionVariables, allowedActionVariables) {
Parameters.Add(new ValueParameter("SampleVariableVector", "", GenerateSampleVariableVector(dataset, allowedConditionVariables, allowedActionVariables)));
Parameters.Add(new FixedValueParameter("SpreadPercentage", "", new PercentValue(0.5)));
}
private VariableVector GenerateSampleVariableVector(Dataset dataset, IEnumerable allowedConditionVariables, IEnumerable allowedActionVariables) {
var conditionVariables = GetVariablesOfDataSet(dataset, allowedConditionVariables);
var actionVariables = GetVariablesOfDataSet(dataset, allowedActionVariables);
if (actionVariables.Count() == 0 || !actionVariables.All(x => x is IActionVariable)) {
throw new ArgumentException("Action variable can not be empty and all action variables have to be of type int or string.");
}
return new VariableVector(conditionVariables, actionVariables);
}
private IEnumerable GetVariablesOfDataSet(DataAnalysis.Dataset dataset, IEnumerable allowedVariables) {
var variables = new List();
foreach (var variableName in allowedVariables) {
var variableValues = dataset.GetValues(variableName);
HeuristicLab.Encodings.VariableVector.IVariable variable;
if (variableValues is List) {
variable = new StringVariable(variableName, (variableValues as List).Distinct());
} else if (variableValues is List) {
var doubleValues = (variableValues as List).Distinct();
if (doubleValues.All(x => x % 1 == 0 || Double.IsNaN(x))) {
// ToList call is necessary, because otherwise it wouldn't be possible to serialize it
variable = new IntVariable(variableName, doubleValues.Select(x => Convert.ToInt32(x)).ToList());
} else {
variable = new DoubleVariable(variableName, doubleValues);
}
} else {
throw new ArgumentException("There is no matching variable type for the values in the dataset");
}
variables.Add(variable);
}
return variables;
}
public override IInput FetchInput(int rowNumber) {
if (!fetchInputCache.ContainsKey(rowNumber)) {
VariableVectorInput input = new VariableVectorInput();
IEnumerable variableNames = SampleVariableVector.Condition.VariableDictionary.Keys.Union(SampleVariableVector.Action.VariableDictionary.Keys);
foreach (var variableName in variableNames) {
input.InputDictionary.Add(variableName, Dataset.GetValue(rowNumber, variableName));
}
fetchInputCache.Add(rowNumber, input);
}
return fetchInputCache[rowNumber];
}
protected IDictionary fetchActionCache = new Dictionary();
public override IAction FetchAction(int rowNumber) {
if (!fetchActionCache.ContainsKey(rowNumber)) {
var action = SampleVariableVector.Action.GetEmptyCopy();
foreach (var variableName in action.VariableDictionary.Keys) {
action.VariableDictionary[variableName].SetTo(Dataset.GetValue(rowNumber, variableName));
}
fetchActionCache.Add(rowNumber, action);
}
return fetchActionCache[rowNumber];
}
protected override void ActionConditionVariablesChanged() {
SampleVariableVectorParameter.Value = GenerateSampleVariableVector(Dataset, AllowedConditionVariables, AllowedActionVariables);
}
}
}