#region License Information
/* HeuristicLab
* Copyright (C) 2002-2013 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 HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Data;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[StorableClass]
[Item("OnePointShaker", "Selects a random node with local parameters and manipulates the selected node.")]
public sealed class OnePointShaker : TracingSymbolicExpressionTreeManipulator {
private const string ShakingFactorParameterName = "ShakingFactor";
#region parameter properties
public IValueParameter ShakingFactorParameter {
get { return (IValueParameter)Parameters[ShakingFactorParameterName]; }
}
#endregion
#region properties
public double ShakingFactor {
get { return ShakingFactorParameter.Value.Value; }
set { ShakingFactorParameter.Value.Value = value; }
}
#endregion
[StorableConstructor]
private OnePointShaker(bool deserializing) : base(deserializing) { }
private OnePointShaker(OnePointShaker original, Cloner cloner) : base(original, cloner) { }
public OnePointShaker()
: base() {
Parameters.Add(new FixedValueParameter(ShakingFactorParameterName, "The shaking factor that should be used for the manipulation of constants (default=1.0).", new DoubleValue(1.0)));
}
public override IDeepCloneable Clone(Cloner cloner) {
return new OnePointShaker(this, cloner);
}
protected override void Manipulate(IRandom random, ISymbolicExpressionTree tree) {
OnePointShaker.Shake(random, tree, ShakingFactor);
}
public static void Shake(IRandom random, ISymbolicExpressionTree tree, double shakingFactor) {
List parametricNodes = new List();
tree.Root.ForEachNodePostfix(n => {
if (n.HasLocalParameters) parametricNodes.Add(n);
});
if (parametricNodes.Count > 0) {
var selectedPoint = parametricNodes.SelectRandom(random);
selectedPoint.ShakeLocalParameters(random, shakingFactor);
}
}
}
}