#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"]; }
}
public ILookupParameter> FragmentsParameter {
get { return (ILookupParameter>)Parameters["Fragments"]; }
}
public ILookupParameter> FragmentOutputsParameter {
get { return (ILookupParameter>)Parameters["FragmentOutputs"]; }
}
[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."));
Parameters.Add(new LookupParameter>("Fragments"));
Parameters.Add(new LookupParameter>("FragmentOutputs"));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new ReplaceBranchMoveMaker(this, cloner);
}
public override IOperation Apply() {
var fragments = FragmentsParameter.ActualValue;
var fragmentOutputs = FragmentOutputsParameter.ActualValue;
var move = ReplaceBranchMoveParameter.ActualValue;
DoubleValue moveQuality = MoveQualityParameter.ActualValue;
DoubleValue quality = QualityParameter.ActualValue;
var newNode = (ISymbolicExpressionTreeNode)move.NewBranch.Clone();
var oldBranch = move.Parent.GetSubtree(move.SubtreeIndex);
move.Parent.RemoveSubtree(move.SubtreeIndex);
move.Parent.InsertSubtree(move.SubtreeIndex, Scale(newNode, move.Alpha, move.Beta));
// move oldBranch into pool
var fragmentStart = fragments[move.FragmentIndex].Root.GetSubtree(0);
fragmentStart.RemoveSubtree(0);
fragmentStart.AddSubtree(oldBranch);
fragmentOutputs[move.FragmentIndex] = new DoubleArray(move.NewOutput);
quality.Value = moveQuality.Value;
// update solution
SymbolicExpressionTreeParameter.ActualValue = move.Tree;
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 constNode = node as ConstantTreeNode;
if (constNode != null) {
constNode.Value = constNode.Value * beta + alpha;
return constNode;
}
var varNode = node as VariableTreeNode;
if (varNode != null) {
varNode.Weight = varNode.Weight * beta;
var constAlpha = (ConstantTreeNode)constant.CreateTreeNode();
constAlpha.Value = alpha;
var sum = add.CreateTreeNode();
sum.AddSubtree(varNode); sum.AddSubtree(constAlpha);
return sum;
}
if (node.Symbol is Addition && node.GetSubtree(0).Symbol is Multiplication &&
node.GetSubtree(1).Symbol is Constant && node.GetSubtree(0).GetSubtree(1).Symbol is Constant) {
var constAlpha = node.GetSubtree(1) as ConstantTreeNode;
var constBeta = node.GetSubtree(0).GetSubtree(1) as ConstantTreeNode;
constAlpha.Value = beta * constAlpha.Value + alpha;
constBeta.Value = constBeta.Value * beta;
return node;
} else {
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;
}
}
}
}