#region License Information
/* HeuristicLab
* Copyright (C) 2002-2009 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.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using SVM;
namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine {
[StorableClass]
[Item("SupportVectorMachineModelEvaluator", "Represents a operator that evaluates a support vector machine model on a data set.")]
public class SupportVectorMachineModelEvaluator : SingleSuccessorOperator {
private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
private const string ModelParameterName = "SupportVectorMachineModel";
private const string SamplesStartParameterName = "SamplesStart";
private const string SamplesEndParameterName = "SamplesEnd";
private const string ValuesParameterName = "Values";
#region parameter properties
public IValueLookupParameter DataAnalysisProblemDataParameter {
get { return (IValueLookupParameter)Parameters[DataAnalysisProblemDataParameterName]; }
}
public IValueLookupParameter SamplesStartParameter {
get { return (IValueLookupParameter)Parameters[SamplesStartParameterName]; }
}
public IValueLookupParameter SamplesEndParameter {
get { return (IValueLookupParameter)Parameters[SamplesEndParameterName]; }
}
public ILookupParameter SupportVectorMachineModelParameter {
get { return (ILookupParameter)Parameters[ModelParameterName]; }
}
public ILookupParameter ValuesParameter {
get { return (ILookupParameter)Parameters[ValuesParameterName]; }
}
#endregion
#region properties
public DataAnalysisProblemData DataAnalysisProblemData {
get { return DataAnalysisProblemDataParameter.ActualValue; }
}
public SupportVectorMachineModel SupportVectorMachineModel {
get { return SupportVectorMachineModelParameter.ActualValue; }
}
public IntValue SamplesStart {
get { return SamplesStartParameter.ActualValue; }
}
public IntValue SamplesEnd {
get { return SamplesEndParameter.ActualValue; }
}
#endregion
[StorableConstructor]
protected SupportVectorMachineModelEvaluator(bool deserializing) : base(deserializing) { }
protected SupportVectorMachineModelEvaluator(SupportVectorMachineModelEvaluator original, Cloner cloner) : base(original, cloner) { }
public override IDeepCloneable Clone(Cloner cloner) {
return new SupportVectorMachineModelEvaluator(this, cloner);
}
public SupportVectorMachineModelEvaluator()
: base() {
Parameters.Add(new ValueLookupParameter(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
Parameters.Add(new LookupParameter(ModelParameterName, "The result model generated by the SVM."));
Parameters.Add(new ValueLookupParameter(SamplesStartParameterName, "The first index of the data set partition on which the SVM model should be evaluated."));
Parameters.Add(new ValueLookupParameter(SamplesEndParameterName, "The last index of the data set partition on which the SVM model should be evaluated."));
Parameters.Add(new LookupParameter(ValuesParameterName, "A matrix of original values of the target variable and output values of the SVM model."));
}
public override IOperation Apply() {
int start = SamplesStart.Value;
int end = SamplesEnd.Value;
IEnumerable rows =
Enumerable.Range(start, end - start)
.Where(i => i < DataAnalysisProblemData.TestSamplesStart.Value || DataAnalysisProblemData.TestSamplesEnd.Value <= i);
ValuesParameter.ActualValue = new DoubleMatrix(Evaluate(SupportVectorMachineModel, DataAnalysisProblemData, rows));
return base.Apply();
}
public static double[,] Evaluate(SupportVectorMachineModel model, DataAnalysisProblemData problemData, IEnumerable rowIndices) {
SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, rowIndices);
SVM.Problem scaledProblem = model.RangeTransform.Scale(problem);
int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
double[,] values = new double[scaledProblem.Count, 2];
var rowEnumerator = rowIndices.GetEnumerator();
for (int i = 0; i < scaledProblem.Count; i++) {
rowEnumerator.MoveNext();
values[i, 0] = problemData.Dataset[rowEnumerator.Current, targetVariableIndex];
values[i, 1] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]);
}
return values;
}
}
}