#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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Regression {
[Item("ReplaceBranchMoveMaker", "")]
[StorableClass]
public class ReplaceBranchMoveMaker : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IMoveMaker {
public override bool CanChangeName {
get { return false; }
}
public ILookupParameter QualityParameter {
get { return (ILookupParameter)Parameters["Quality"]; }
}
public ILookupParameter MoveQualityParameter {
get { return (ILookupParameter)Parameters["MoveQuality"]; }
}
public ILookupParameter ReplaceBranchMoveParameter {
get { return (ILookupParameter)Parameters["ReplaceBranchMove"]; }
}
public ILookupParameter SymbolicExpressionTreeParameter {
get { return (ILookupParameter)Parameters["SymbolicExpressionTree"]; }
}
[StorableConstructor]
protected ReplaceBranchMoveMaker(bool deserializing) : base(deserializing) { }
protected ReplaceBranchMoveMaker(ReplaceBranchMoveMaker original, Cloner cloner) : base(original, cloner) { }
public ReplaceBranchMoveMaker()
: base() {
Parameters.Add(new LookupParameter("Quality", "The quality of the solution."));
Parameters.Add(new LookupParameter("ReplaceBranchMove", "The move to evaluate."));
Parameters.Add(new LookupParameter("MoveQuality", "The relative quality of the move."));
Parameters.Add(new LookupParameter("SymbolicExpressionTree", "The symbolic expression tree on which the move should be applied."));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ReplaceBranchMoveMaker(this, cloner);
}
public override IOperation Apply() {
var move = ReplaceBranchMoveParameter.ActualValue;
DoubleValue moveQuality = MoveQualityParameter.ActualValue;
DoubleValue quality = QualityParameter.ActualValue;
var newNode = (ISymbolicExpressionTreeNode)move.NewBranch.Clone();
move.Parent.RemoveSubtree(move.SubtreeIndex);
move.Parent.InsertSubtree(move.SubtreeIndex, Scale(newNode, move.Alpha, move.Beta));
quality.Value = moveQuality.Value;
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;
}
}
}