[7802] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of HeuristicLab.
|
---|
| 6 | *
|
---|
| 7 | * HeuristicLab is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * HeuristicLab is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | */
|
---|
| 20 | #endregion
|
---|
| 21 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.Operators;
|
---|
| 28 | using HeuristicLab.Optimization;
|
---|
| 29 | using HeuristicLab.Parameters;
|
---|
| 30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 31 |
|
---|
| 32 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
| 33 | [Item("ShakeMultiMoveGenerator", "Randomly samples n from all possible shake moves from a given tree.")]
|
---|
| 34 | [StorableClass]
|
---|
| 35 | public class ShakeMultiMoveGenerator : SingleSuccessorOperator, IStochasticOperator, ISymbolicExpressionTreeMoveOperator, IMultiMoveGenerator {
|
---|
| 36 | public ILookupParameter<IRandom> RandomParameter {
|
---|
| 37 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
| 38 | }
|
---|
| 39 | public IValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
| 40 | get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
| 41 | }
|
---|
| 42 |
|
---|
| 43 | public IntValue SampleSize {
|
---|
| 44 | get { return SampleSizeParameter.Value; }
|
---|
| 45 | set { SampleSizeParameter.Value = value; }
|
---|
| 46 | }
|
---|
[7832] | 47 | public ILookupParameter<ISymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
| 48 | get { return (ILookupParameter<ISymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
|
---|
[7802] | 49 | }
|
---|
| 50 | public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
|
---|
| 51 | get { return (LookupParameter<ChangeNodeTypeMove>)Parameters["ChangeNodeTypeMove"]; }
|
---|
| 52 | }
|
---|
| 53 | protected ScopeParameter CurrentScopeParameter {
|
---|
| 54 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | [StorableConstructor]
|
---|
| 58 | protected ShakeMultiMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
| 59 | protected ShakeMultiMoveGenerator(ShakeMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
|
---|
| 60 | public ShakeMultiMoveGenerator()
|
---|
| 61 | : base() {
|
---|
| 62 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
| 63 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
|
---|
| 64 |
|
---|
[7832] | 65 | Parameters.Add(new LookupParameter<ISymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
|
---|
[7802] | 66 | Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
|
---|
| 67 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
|
---|
| 68 |
|
---|
| 69 | }
|
---|
| 70 |
|
---|
| 71 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 72 | return new ShakeMultiMoveGenerator(this, cloner);
|
---|
| 73 | }
|
---|
| 74 |
|
---|
| 75 | public override IOperation Apply() {
|
---|
[7832] | 76 | var tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
| 77 | var random = RandomParameter.ActualValue;
|
---|
| 78 | int n = SampleSizeParameter.ActualValue.Value;
|
---|
| 79 | var moves = ChangeNodeTypeMultiMoveGenerator.GenerateMoves(tree, random, n);
|
---|
[7802] | 80 | string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
|
---|
| 81 | var moveScopes = new List<Scope>();
|
---|
| 82 |
|
---|
[7832] | 83 | foreach (var m in moves) {
|
---|
[7802] | 84 | m.NewChild.ShakeLocalParameters(random, 1);
|
---|
| 85 | var moveScope = new Scope(moveScopes.Count.ToString());
|
---|
| 86 | moveScope.Variables.Add(new Variable(moveParameterName, m));
|
---|
| 87 | moveScopes.Add(moveScope);
|
---|
| 88 | }
|
---|
| 89 | CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
|
---|
| 90 | return base.Apply();
|
---|
| 91 | }
|
---|
[7832] | 92 |
|
---|
| 93 | public static IEnumerable<ChangeNodeTypeMove> GenerateMoves(ISymbolicExpressionTree tree, IRandom random, int n) {
|
---|
| 94 | int count = 0;
|
---|
| 95 | var g = tree.Root.Grammar;
|
---|
| 96 | var possibleChildren = (from parent in tree.Root.GetSubtree(0).IterateNodesPrefix()
|
---|
| 97 | from i in Enumerable.Range(0, parent.SubtreeCount)
|
---|
| 98 | let currentChild = parent.GetSubtree(i)
|
---|
| 99 | where currentChild.HasLocalParameters
|
---|
| 100 | select new { parent, i, currentChild }).ToArray();
|
---|
| 101 | if (possibleChildren.Length < n) n = possibleChildren.Length;
|
---|
| 102 | while (count < n) {
|
---|
| 103 | var selected = possibleChildren[random.Next(possibleChildren.Length)];
|
---|
| 104 | yield return new ChangeNodeTypeMove(tree, selected.parent, selected.i, (ISymbolicExpressionTreeNode)selected.currentChild.Clone());
|
---|
| 105 | count++;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
[7802] | 108 | }
|
---|
| 109 | }
|
---|