#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.Data;
using HeuristicLab.Operators;
using HeuristicLab.Optimization;
using HeuristicLab.Parameters;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
[Item("ShakeMultiMoveGenerator", "Randomly samples n from all possible shake moves from a given tree.")]
[StorableClass]
public class ShakeMultiMoveGenerator : SingleSuccessorOperator, IStochasticOperator, ISymbolicExpressionTreeMoveOperator, IMultiMoveGenerator {
public ILookupParameter RandomParameter {
get { return (ILookupParameter)Parameters["Random"]; }
}
public IValueLookupParameter SampleSizeParameter {
get { return (IValueLookupParameter)Parameters["SampleSize"]; }
}
public IntValue SampleSize {
get { return SampleSizeParameter.Value; }
set { SampleSizeParameter.Value = value; }
}
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 ShakeMultiMoveGenerator(bool deserializing) : base(deserializing) { }
protected ShakeMultiMoveGenerator(ShakeMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
public ShakeMultiMoveGenerator()
: base() {
Parameters.Add(new LookupParameter("Random", "The random number generator."));
Parameters.Add(new ValueLookupParameter("SampleSize", "The number of moves to generate."));
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 ShakeMultiMoveGenerator(this, cloner);
}
public override IOperation Apply() {
var tree = SymbolicExpressionTreeParameter.ActualValue;
var random = RandomParameter.ActualValue;
int n = SampleSizeParameter.ActualValue.Value;
var moves = ChangeNodeTypeMultiMoveGenerator.GenerateMoves(tree, random, n);
string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
var moveScopes = new List();
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, IRandom random, int n) {
int count = 0;
var g = tree.Root.Grammar;
var possibleChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
from i in Enumerable.Range(0, parent.SubtreeCount)
let currentChild = parent.GetSubtree(i)
where currentChild.HasLocalParameters
select new { parent, i, currentChild }).ToArray();
if (possibleChildren.Length < n) n = possibleChildren.Length;
while (count < n) {
var selected = possibleChildren[random.Next(possibleChildren.Length)];
yield return new ChangeNodeTypeMove(tree, selected.parent, selected.i, (ISymbolicExpressionTreeNode)selected.currentChild.Clone());
count++;
}
}
}
}