#region License Information /* HeuristicLab * Copyright (C) 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 HEAL.Attic; using HeuristicLab.Random; namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { [StorableType("959330FD-85FA-4615-A047-3A1493573A42")] [Item("OnePointShaker", "Selects a random node with local parameters and manipulates the selected node.")] public sealed class OnePointShaker : SymbolicExpressionTreeManipulator { 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(StorableConstructorFlag _) : base(_) { } 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 parameters (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.SampleRandom(random); selectedPoint.ShakeLocalParameters(random, shakingFactor); } } } }