#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.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("ReplaceBranchMoveEvaluator", "")] [StorableClass] public class ReplaceBranchMoveEvaluator : SingleSuccessorOperator, ISymbolicRegressionMoveEvaluator { public override bool CanChangeName { get { return false; } } public ILookupParameter MoveParameter { get { return (ILookupParameter)Parameters["ReplaceBranchMove"]; } } 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 ReplaceBranchMoveEvaluator(bool deserializing) : base(deserializing) { } protected ReplaceBranchMoveEvaluator(ReplaceBranchMoveEvaluator original, Cloner cloner) : base(original, cloner) { } public ReplaceBranchMoveEvaluator() : 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("ReplaceBranchMove", "")); Parameters.Add(new LookupParameter("ProblemData", "")); Parameters.Add(new LookupParameter("FitnessCalculationPartition", "")); Parameters.Add(new LookupParameter("SymbolicExpressionTree", "")); } public override IOperation Apply() { var evaluator = EvaluatorParameter.ActualValue; var move = MoveParameter.ActualValue; // calculate linear scaling OnlineCalculatorError errorState; double alpha, beta; OnlineLinearScalingParameterCalculator.Calculate(move.NewOutput, move.OriginalOutput, out alpha, out beta, out errorState); // prevent scaling to a constant value if (beta.IsAlmost(0.0)) beta = 1.0; move.Alpha = alpha; move.Beta = beta; var oldBranch = move.Parent.GetSubtree(move.SubtreeIndex); move.Parent.RemoveSubtree(move.SubtreeIndex); move.Parent.InsertSubtree(move.SubtreeIndex, Scale(move.NewBranch, alpha, beta)); 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); // revert changes to tree move.Parent.RemoveSubtree(move.SubtreeIndex); move.Parent.InsertSubtree(move.SubtreeIndex, oldBranch); MoveQualityParameter.ActualValue = new DoubleValue(quality); return base.Apply(); } private Addition add = new Addition(); private Multiplication mul = new Multiplication(); private Constant constant = new Constant(); private ISymbolicExpressionTreeNode Scale(ISymbolicExpressionTreeNode node, double alpha, double beta) { var constAlpha = (ConstantTreeNode)constant.CreateTreeNode(); var constBeta = (ConstantTreeNode)constant.CreateTreeNode(); constAlpha.Value = alpha; constBeta.Value = beta; var sum = add.CreateTreeNode(); var prod = mul.CreateTreeNode(); prod.AddSubtree(node); prod.AddSubtree(constBeta); sum.AddSubtree(prod); sum.AddSubtree(constAlpha); return sum; } public override IDeepCloneable Clone(Cloner cloner) { return new ReplaceBranchMoveEvaluator(this, cloner); } } }