#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.Collections.Generic;
using System.Linq;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[Item("SymbolicRegressionMoveEvaluator", "")]
[StorableClass]
public class SymbolicRegressionMoveEvaluator : SingleSuccessorOperator, ISymbolicRegressionMoveEvaluator {
public override bool CanChangeName {
get { return false; }
}
public ILookupParameter MoveParameter {
get { return (ILookupParameter)Parameters["ChangeNodeTypeMove"]; }
}
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters["Quality"]; }
}
public ILookupParameter MoveQualityParameter {
get { return (ILookupParameter)Parameters["MoveQuality"]; }
}
public ILookupParameter EvaluatorParameter {
get { return (ILookupParameter)Parameters["Evaluator"]; }
}
public ILookupParameter ProblemDataParameter {
get { return (ILookupParameter)Parameters["ProblemData"]; }
}
public ILookupParameter FitnessCalculationPartitionParameter {
get { return (ILookupParameter)Parameters["FitnessCalculationPartition"]; }
}
public ILookupParameter SymbolicExpressionTreeParameter {
get { return (ILookupParameter)Parameters["SymbolicExpressionTree"]; }
}
[StorableConstructor]
protected SymbolicRegressionMoveEvaluator(bool deserializing) : base(deserializing) { }
protected SymbolicRegressionMoveEvaluator(SymbolicRegressionMoveEvaluator original, Cloner cloner) : base(original, cloner) { }
public SymbolicRegressionMoveEvaluator()
: base() {
Parameters.Add(new LookupParameter("Quality", "The quality of a symbolic regression solution."));
Parameters.Add(new LookupParameter("MoveQuality", "The evaluated quality of a move on a symbolic regression solution."));
Parameters.Add(new LookupParameter("Evaluator", ""));
Parameters.Add(new LookupParameter("ChangeNodeTypeMove", ""));
Parameters.Add(new LookupParameter("ProblemData", ""));
Parameters.Add(new LookupParameter("FitnessCalculationPartition", ""));
Parameters.Add(new LookupParameter("SymbolicExpressionTree", ""));
}
public override IOperation Apply() {
var evaluator = EvaluatorParameter.ActualValue;
// clone the move and all contained objects to make sure that the items in the scope are untouched
var move = (ChangeNodeTypeMove)MoveParameter.ActualValue.Clone();
var oldNode = move.Parent.GetSubtree(move.SubtreeIndex);
var children = new List(oldNode.Subtrees);
while (oldNode.SubtreeCount > 0) oldNode.RemoveSubtree(0);
var newNode = move.NewChild;
foreach (var c in children)
newNode.AddSubtree(c);
move.Parent.RemoveSubtree(move.SubtreeIndex);
move.Parent.InsertSubtree(move.SubtreeIndex, newNode);
var problemData = ProblemDataParameter.ActualValue;
IExecutionContext childContext = (IExecutionContext)ExecutionContext.CreateChildOperation(evaluator);
var fitnessPartition = FitnessCalculationPartitionParameter.ActualValue;
var rows = Enumerable.Range(fitnessPartition.Start, fitnessPartition.End - fitnessPartition.Start);
var quality = evaluator.Evaluate(childContext, move.Tree, problemData, rows);
MoveQualityParameter.ActualValue = new DoubleValue(quality);
return base.Apply();
}
public override IDeepCloneable Clone(Cloner cloner) {
return new SymbolicRegressionMoveEvaluator(this, cloner);
}
}
}