#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.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Operators; using HeuristicLab.Optimization; using HeuristicLab.Parameters; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding { [Item("ShakeExhaustiveMoveGenerator", "Operator that generates moves for shaking a single symbolic expression tree node.")] [StorableClass] public class ShakeMoveGenerator : SingleSuccessorOperator, ISymbolicExpressionTreeMoveOperator, IExhaustiveMoveGenerator, IStochasticOperator { public override bool CanChangeName { get { return false; } } public ILookupParameter RandomParameter { get { return (ILookupParameter)Parameters["Random"]; } } public ILookupParameter SymbolicExpressionTreeParameter { get { return (ILookupParameter)Parameters["SymbolicExpressionTree"]; } } public ILookupParameter ChangeNodeTypeMoveParameter { get { return (LookupParameter)Parameters["ChangeNodeTypeMove"]; } } protected ScopeParameter CurrentScopeParameter { get { return (ScopeParameter)Parameters["CurrentScope"]; } } [StorableConstructor] protected ShakeMoveGenerator(bool deserializing) : base(deserializing) { } protected ShakeMoveGenerator(ShakeMoveGenerator original, Cloner cloner) : base(original, cloner) { } public ShakeMoveGenerator() : base() { Parameters.Add(new LookupParameter("Random", "The random number generator.")); Parameters.Add(new LookupParameter("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated.")); Parameters.Add(new LookupParameter("ChangeNodeTypeMove", "The moves that should be generated in subscopes.")); Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes.")); } public override IDeepCloneable Clone(Cloner cloner) { return new ShakeMoveGenerator(this, cloner); } public override IOperation Apply() { var tree = SymbolicExpressionTreeParameter.ActualValue; var moves = GenerateMoves(tree); string moveParameterName = ChangeNodeTypeMoveParameter.ActualName; var moveScopes = new List(); var random = RandomParameter.ActualValue; foreach (var m in moves) { m.NewChild.ShakeLocalParameters(random, 1); var moveScope = new Scope(moveScopes.Count.ToString()); moveScope.Variables.Add(new Variable(moveParameterName, m)); moveScopes.Add(moveScope); } CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes); return base.Apply(); } public static IEnumerable GenerateMoves(ISymbolicExpressionTree tree) { return (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix() from i in Enumerable.Range(0, parent.SubtreeCount) let currentChild = parent.GetSubtree(i) where currentChild.HasLocalParameters let newChild = (ISymbolicExpressionTreeNode)currentChild.Clone() select new ChangeNodeTypeMove(tree, parent, i, newChild)); } } }