#region License Information
/* HeuristicLab
* Copyright (C) 2002-2015 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.Linq;
using HEAL.Attic;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.EvolutionTracking;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic {
[Item("SymbolicDataAnalysisGenealogyAnalyzer", "Genealogy analyzer for symbolic data analysis problems")]
[StorableType("A3D2A9C6-D304-47F1-9F02-6ABA9A0F4428")]
public class SymbolicDataAnalysisGenealogyAnalyzer : GenealogyAnalyzer {
private const string EvaluatorParameterName = "Evaluator";
private const string ProblemDataParameterName = "ProblemData";
private const string InterpreterParameterName = "SymbolicExpressionTreeInterpreter";
private const string EstimationLimitsParameterName = "EstimationLimits";
private const string ApplyLinearScalingParameterName = "ApplyLinearScaling";
#region parameters
public ILookupParameter EvaluatorParameter {
get {
return (ILookupParameter)Parameters[EvaluatorParameterName];
}
}
public ILookupParameter ProblemDataParameter {
get {
return (ILookupParameter)Parameters[ProblemDataParameterName];
}
}
public ILookupParameter InterpreterParameter {
get {
return (ILookupParameter)Parameters[InterpreterParameterName];
}
}
public ILookupParameter EstimationLimitsParameter {
get { return (ILookupParameter)Parameters[EstimationLimitsParameterName]; }
}
public ILookupParameter ApplyLinearScalingParameter {
get { return (ILookupParameter)Parameters[ApplyLinearScalingParameterName]; }
}
#endregion
public SymbolicDataAnalysisGenealogyAnalyzer() {
Parameters.Add(new LookupParameter(EvaluatorParameterName));
Parameters.Add(new LookupParameter(ProblemDataParameterName));
Parameters.Add(new LookupParameter(InterpreterParameterName));
Parameters.Add(new LookupParameter(EstimationLimitsParameterName));
Parameters.Add(new LookupParameter(ApplyLinearScalingParameterName));
}
public SymbolicDataAnalysisGenealogyAnalyzer(SymbolicDataAnalysisGenealogyAnalyzer original, Cloner cloner)
: base(original, cloner) {
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicDataAnalysisGenealogyAnalyzer(this, cloner);
}
[StorableConstructor]
protected SymbolicDataAnalysisGenealogyAnalyzer(StorableConstructorFlag _) : base(_) {
}
protected override void EvaluateIntermediateChildren() {
var results = ResultsParameter.ActualValue;
var graph = (IGenealogyGraph)results["PopulationGraph"].Value;
var population = PopulationParameter.ActualValue;
var generation = GenerationsParameter.ActualValue.Value;
var problemData = ProblemDataParameter.ActualValue;
var vertices = population.Select(graph.GetByContent).Where(x => x.InDegree == 1).Select(x => x.Parents.First());
var intermediateVertices = vertices.Where(x => x.Rank.IsAlmost(generation - 0.5));
var classificationProblemData = problemData as IClassificationProblemData;
var regressionProblemData = problemData as IRegressionProblemData;
if (classificationProblemData != null) {
var evaluator = (ISymbolicDataAnalysisSingleObjectiveEvaluator)EvaluatorParameter.ActualValue;
foreach (var v in intermediateVertices) {
var child = v.Data;
v.Quality = evaluator.Evaluate(this.ExecutionContext, child, classificationProblemData, classificationProblemData.TrainingIndices);
}
} else if (regressionProblemData != null) {
var evaluator = (ISymbolicDataAnalysisSingleObjectiveEvaluator)EvaluatorParameter.ActualValue;
foreach (var v in intermediateVertices) {
var child = v.Data;
v.Quality = evaluator.Evaluate(this.ExecutionContext, child, regressionProblemData, problemData.TrainingIndices);
}
}
}
}
}