#region License Information
/* HeuristicLab
* Copyright (C) 2002-2010 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 System.Drawing;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.PluginInfrastructure;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Problems.DataAnalysis.Regression;
using HeuristicLab.Problems.DataAnalysis.Symbolic;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.ArchitectureAlteringOperators;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Manipulators;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Crossovers;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding.Creators;
namespace HeuristicLab.Problems.DataAnalysis.Regression.Symbolic {
[Item("SymbolicRegressionProblem", "Represents a symbolic regression problem.")]
[Creatable("Problems")]
[StorableClass]
public sealed class SymbolicRegressionProblem : DataAnalysisProblem, ISingleObjectiveProblem {
#region Parameter Properties
public ValueParameter MaximizationParameter {
get { return (ValueParameter)Parameters["Maximization"]; }
}
IParameter ISingleObjectiveProblem.MaximizationParameter {
get { return MaximizationParameter; }
}
public ValueParameter SolutionCreatorParameter {
get { return (ValueParameter)Parameters["SolutionCreator"]; }
}
IParameter IProblem.SolutionCreatorParameter {
get { return SolutionCreatorParameter; }
}
public ValueParameter EvaluatorParameter {
get { return (ValueParameter)Parameters["Evaluator"]; }
}
IParameter IProblem.EvaluatorParameter {
get { return EvaluatorParameter; }
}
public ValueParameter FunctionTreeGrammarParameter {
get { return (ValueParameter)Parameters["FunctionTreeGrammar"]; }
}
public ValueParameter MaxExpressionLengthParameter {
get { return (ValueParameter)Parameters["MaxExpressionLength"]; }
}
public ValueParameter MaxExpressionDepthParameter {
get { return (ValueParameter)Parameters["MaxExpressionDepth"]; }
}
public ValueParameter NumberOfEvaluatedNodesParameter {
get { return (ValueParameter)Parameters["NumberOfEvaluatedNodes"]; }
}
public ValueParameter MaxFunctionDefiningBranchesParameter {
get { return (ValueParameter)Parameters["MaxFunctionDefiningBranches"]; }
}
public ValueParameter MaxFunctionArgumentsParameter {
get { return (ValueParameter)Parameters["MaxFunctionArguments"]; }
}
public OptionalValueParameter VisualizerParameter {
get { return (OptionalValueParameter)Parameters["Visualizer"]; }
}
IParameter IProblem.VisualizerParameter {
get { return VisualizerParameter; }
}
public OptionalValueParameter BestKnownQualityParameter {
get { return (OptionalValueParameter)Parameters["BestKnownQuality"]; }
}
IParameter ISingleObjectiveProblem.BestKnownQualityParameter {
get { return BestKnownQualityParameter; }
}
#endregion
#region Properties
public IntValue MaxExpressionLength {
get { return MaxExpressionLengthParameter.Value; }
set { MaxExpressionLengthParameter.Value = value; }
}
public IntValue MaxExpressionDepth {
get { return MaxExpressionDepthParameter.Value; }
set { MaxExpressionDepthParameter.Value = value; }
}
public SymbolicExpressionTreeCreator SolutionCreator {
get { return SolutionCreatorParameter.Value; }
set { SolutionCreatorParameter.Value = value; }
}
ISolutionCreator IProblem.SolutionCreator {
get { return SolutionCreatorParameter.Value; }
}
public ISymbolicRegressionEvaluator Evaluator {
get { return EvaluatorParameter.Value; }
set { EvaluatorParameter.Value = value; }
}
ISingleObjectiveEvaluator ISingleObjectiveProblem.Evaluator {
get { return EvaluatorParameter.Value; }
}
IEvaluator IProblem.Evaluator {
get { return EvaluatorParameter.Value; }
}
public ISymbolicExpressionGrammar FunctionTreeGrammar {
get { return (ISymbolicExpressionGrammar)FunctionTreeGrammarParameter.Value; }
}
public ISingleObjectiveSolutionsVisualizer Visualizer {
get { return VisualizerParameter.Value; }
set { VisualizerParameter.Value = value; }
}
ISolutionsVisualizer IProblem.Visualizer {
get { return VisualizerParameter.Value; }
}
public DoubleValue BestKnownQuality {
get { return BestKnownQualityParameter.Value; }
}
private List operators;
public IEnumerable Operators {
get { return operators.Cast(); }
}
#endregion
public SymbolicRegressionProblem()
: base() {
SymbolicExpressionTreeCreator creator = new ProbabilisticTreeCreator();
var evaluator = new SymbolicRegressionMeanSquaredErrorEvaluator();
var grammar = new ArithmeticExpressionGrammar();
var globalGrammar = new GlobalSymbolicExpressionGrammar(grammar);
var visualizer = new BestValidationSymbolicRegressionSolutionVisualizer();
var interpreter = new SimpleArithmeticExpressionInterpreter();
Parameters.Add(new ValueParameter("Maximization", "Set to false as the error of the regression model should be minimized.", new BoolValue(false)));
Parameters.Add(new ValueParameter("SolutionCreator", "The operator which should be used to create new symbolic regression solutions.", creator));
Parameters.Add(new ValueParameter("SymbolicExpressionTreeInterpreter", "The interpreter that should be used to evaluate the symbolic expression tree.", interpreter));
Parameters.Add(new ValueParameter("Evaluator", "The operator which should be used to evaluate symbolic regression solutions.", evaluator));
Parameters.Add(new OptionalValueParameter("BestKnownQuality", "The minimal error value that reached by symbolic regression solutions for the problem."));
Parameters.Add(new ValueParameter("FunctionTreeGrammar", "The grammar that should be used for symbolic regression models.", globalGrammar));
Parameters.Add(new ValueParameter("MaxExpressionLength", "Maximal length of the symbolic expression.", new IntValue(100)));
Parameters.Add(new ValueParameter("MaxExpressionDepth", "Maximal depth of the symbolic expression.", new IntValue(10)));
Parameters.Add(new ValueParameter("MaxFunctionDefiningBranches", "Maximal number of automatically defined functions.", new IntValue(3)));
Parameters.Add(new ValueParameter("MaxFunctionArguments", "Maximal number of arguments of automatically defined functions.", new IntValue(3)));
Parameters.Add(new ValueParameter("NumberOfEvaluatedNodes", "The total number of evaluated function tree nodes (for performance measurements.)", new DoubleValue()));
Parameters.Add(new ValueParameter("Visualizer", "The operator which should be used to visualize symbolic regression solutions.", visualizer));
creator.SymbolicExpressionTreeParameter.ActualName = "SymbolicRegressionModel";
creator.MaxFunctionArgumentsParameter.ActualName = "MaxFunctionArguments";
creator.MaxFunctionDefinitionsParameter.ActualName = "MaxFunctionDefiningBranches";
DataAnalysisProblemDataParameter.ValueChanged += new EventHandler(DataAnalysisProblemDataParameter_ValueChanged);
DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
ParameterizeSolutionCreator();
ParameterizeEvaluator();
ParameterizeVisualizer();
Initialize();
}
[StorableConstructor]
private SymbolicRegressionProblem(bool deserializing) : base() { }
public override IDeepCloneable Clone(Cloner cloner) {
SymbolicRegressionProblem clone = (SymbolicRegressionProblem)base.Clone(cloner);
clone.Initialize();
return clone;
}
#region Events
void DataAnalysisProblemDataParameter_ValueChanged(object sender, EventArgs e) {
DataAnalysisProblemData.ProblemDataChanged += new EventHandler(DataAnalysisProblemData_Changed);
}
void DataAnalysisProblemData_Changed(object sender, EventArgs e) {
foreach (var varSymbol in FunctionTreeGrammar.Symbols.OfType()) {
varSymbol.VariableNames = DataAnalysisProblemData.InputVariables.Select(x => x.Value);
}
UpdatePartitioningParameters();
}
private void UpdatePartitioningParameters() {
int trainingStart = DataAnalysisProblemData.TrainingSamplesStart.Value;
int validationEnd = DataAnalysisProblemData.TrainingSamplesEnd.Value;
int trainingEnd = trainingStart + (validationEnd - trainingStart) / 2;
int validationStart = trainingEnd;
var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
if (solutionVisualizer != null) {
solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue(validationStart);
solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(validationEnd);
}
Evaluator.SamplesStartParameter.Value = new IntValue(trainingStart);
Evaluator.SamplesEndParameter.Value = new IntValue(trainingEnd);
}
public event EventHandler SolutionCreatorChanged;
private void OnSolutionCreatorChanged() {
var changed = SolutionCreatorChanged;
if (changed != null)
changed(this, EventArgs.Empty);
}
public event EventHandler EvaluatorChanged;
private void OnEvaluatorChanged() {
var changed = EvaluatorChanged;
if (changed != null)
changed(this, EventArgs.Empty);
}
public event EventHandler VisualizerChanged;
private void OnVisualizerChanged() {
var changed = VisualizerChanged;
if (changed != null)
changed(this, EventArgs.Empty);
}
public event EventHandler OperatorsChanged;
private void OnOperatorsChanged() {
var changed = OperatorsChanged;
if (changed != null)
changed(this, EventArgs.Empty);
}
private void SolutionCreatorParameter_ValueChanged(object sender, EventArgs e) {
SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
ParameterizeSolutionCreator();
ParameterizeEvaluator();
ParameterizeVisualizer();
ParameterizeOperators();
OnSolutionCreatorChanged();
}
private void SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged(object sender, EventArgs e) {
ParameterizeEvaluator();
ParameterizeVisualizer();
ParameterizeOperators();
}
private void EvaluatorParameter_ValueChanged(object sender, EventArgs e) {
Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
ParameterizeEvaluator();
ParameterizeVisualizer();
OnEvaluatorChanged();
}
private void VisualizerParameter_ValueChanged(object sender, EventArgs e) {
ParameterizeVisualizer();
OnVisualizerChanged();
}
private void Evaluator_QualityParameter_ActualNameChanged(object sender, EventArgs e) {
ParameterizeVisualizer();
}
#endregion
#region Helpers
[StorableHook(HookType.AfterDeserialization)]
private void Initialize() {
InitializeOperators();
SolutionCreatorParameter.ValueChanged += new EventHandler(SolutionCreatorParameter_ValueChanged);
SolutionCreator.SymbolicExpressionTreeParameter.ActualNameChanged += new EventHandler(SolutionCreator_SymbolicExpressionTreeParameter_ActualNameChanged);
EvaluatorParameter.ValueChanged += new EventHandler(EvaluatorParameter_ValueChanged);
Evaluator.QualityParameter.ActualNameChanged += new EventHandler(Evaluator_QualityParameter_ActualNameChanged);
VisualizerParameter.ValueChanged += new EventHandler(VisualizerParameter_ValueChanged);
}
private void InitializeOperators() {
operators = new List();
operators.AddRange(ApplicationManager.Manager.GetInstances());
ParameterizeOperators();
}
private void ParameterizeSolutionCreator() {
SolutionCreator.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
SolutionCreator.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
SolutionCreator.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
}
private void ParameterizeEvaluator() {
Evaluator.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
Evaluator.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
Evaluator.QualityParameter.ActualName = "TrainingMeanSquaredError";
Evaluator.SamplesStartParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesStart.Value);
Evaluator.SamplesEndParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
}
private void ParameterizeVisualizer() {
if (Visualizer != null) {
var solutionVisualizer = Visualizer as BestValidationSymbolicRegressionSolutionVisualizer;
if (solutionVisualizer != null) {
solutionVisualizer.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
solutionVisualizer.DataAnalysisProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
solutionVisualizer.ValidationSamplesStartParameter.Value = new IntValue((DataAnalysisProblemData.TrainingSamplesStart.Value + DataAnalysisProblemData.TrainingSamplesEnd.Value) / 2);
solutionVisualizer.ValidationSamplesEndParameter.Value = new IntValue(DataAnalysisProblemData.TrainingSamplesEnd.Value);
}
}
}
private void ParameterizeOperators() {
foreach (ISymbolicExpressionTreeOperator op in Operators.OfType()) {
op.MaxTreeHeightParameter.ActualName = MaxExpressionDepthParameter.Name;
op.MaxTreeSizeParameter.ActualName = MaxExpressionLengthParameter.Name;
op.SymbolicExpressionGrammarParameter.ActualName = FunctionTreeGrammarParameter.Name;
}
foreach (ISymbolicRegressionEvaluator op in Operators.OfType()) {
op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
op.RegressionProblemDataParameter.ActualName = DataAnalysisProblemDataParameter.Name;
op.NumberOfEvaluatedNodesParameter.ActualName = NumberOfEvaluatedNodesParameter.Name;
}
foreach (SymbolicExpressionTreeCrossover op in Operators.OfType()) {
op.ParentsParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
op.ChildParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
}
foreach (SymbolicExpressionTreeManipulator op in Operators.OfType()) {
op.SymbolicExpressionTreeParameter.ActualName = SolutionCreator.SymbolicExpressionTreeParameter.ActualName;
}
foreach (SymbolicExpressionTreeArchitectureAlteringOperator op in Operators.OfType()) {
}
}
#endregion
}
}