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;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.Operators;
|
---|
29 | using HeuristicLab.Optimization;
|
---|
30 | using HeuristicLab.Parameters;
|
---|
31 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
|
---|
34 | [Item("ShakeMultiMoveGenerator", "Randomly samples n from all possible shake moves from a given tree.")]
|
---|
35 | [StorableClass]
|
---|
36 | public class ShakeMultiMoveGenerator : SingleSuccessorOperator, IStochasticOperator, ISymbolicExpressionTreeMoveOperator, IMultiMoveGenerator {
|
---|
37 | public ILookupParameter<IRandom> RandomParameter {
|
---|
38 | get { return (ILookupParameter<IRandom>)Parameters["Random"]; }
|
---|
39 | }
|
---|
40 | public IValueLookupParameter<IntValue> SampleSizeParameter {
|
---|
41 | get { return (IValueLookupParameter<IntValue>)Parameters["SampleSize"]; }
|
---|
42 | }
|
---|
43 |
|
---|
44 | public IntValue SampleSize {
|
---|
45 | get { return SampleSizeParameter.Value; }
|
---|
46 | set { SampleSizeParameter.Value = value; }
|
---|
47 | }
|
---|
48 | public ILookupParameter<SymbolicExpressionTree> SymbolicExpressionTreeParameter {
|
---|
49 | get { return (ILookupParameter<SymbolicExpressionTree>)Parameters["SymbolicExpressionTree"]; }
|
---|
50 | }
|
---|
51 | public ILookupParameter<ChangeNodeTypeMove> ChangeNodeTypeMoveParameter {
|
---|
52 | get { return (LookupParameter<ChangeNodeTypeMove>)Parameters["ChangeNodeTypeMove"]; }
|
---|
53 | }
|
---|
54 | protected ScopeParameter CurrentScopeParameter {
|
---|
55 | get { return (ScopeParameter)Parameters["CurrentScope"]; }
|
---|
56 | }
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected ShakeMultiMoveGenerator(bool deserializing) : base(deserializing) { }
|
---|
60 | protected ShakeMultiMoveGenerator(ShakeMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
|
---|
61 | public ShakeMultiMoveGenerator()
|
---|
62 | : base() {
|
---|
63 | Parameters.Add(new LookupParameter<IRandom>("Random", "The random number generator."));
|
---|
64 | Parameters.Add(new ValueLookupParameter<IntValue>("SampleSize", "The number of moves to generate."));
|
---|
65 |
|
---|
66 | Parameters.Add(new LookupParameter<SymbolicExpressionTree>("SymbolicExpressionTree", "The symbolic expression tree for which moves should be generated."));
|
---|
67 | Parameters.Add(new LookupParameter<ChangeNodeTypeMove>("ChangeNodeTypeMove", "The moves that should be generated in subscopes."));
|
---|
68 | Parameters.Add(new ScopeParameter("CurrentScope", "The current scope where the moves should be added as subscopes."));
|
---|
69 |
|
---|
70 | }
|
---|
71 |
|
---|
72 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
73 | return new ShakeMultiMoveGenerator(this, cloner);
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IOperation Apply() {
|
---|
77 | SymbolicExpressionTree tree = SymbolicExpressionTreeParameter.ActualValue;
|
---|
78 | var moves = ChangeNodeTypeMoveGenerator.GenerateMoves(tree).ToArray();
|
---|
79 | string moveParameterName = ChangeNodeTypeMoveParameter.ActualName;
|
---|
80 | var moveScopes = new List<Scope>();
|
---|
81 | var random = RandomParameter.ActualValue;
|
---|
82 | int n = SampleSizeParameter.ActualValue.Value;
|
---|
83 |
|
---|
84 | var rand = Enumerable.Range(0, moves.Length).Select(i => random.NextDouble()).ToArray();
|
---|
85 | Array.Sort(rand, moves);
|
---|
86 | n = Math.Min(n, moves.Length);
|
---|
87 |
|
---|
88 | foreach (var m in moves.Take(n)) {
|
---|
89 | m.NewChild.ShakeLocalParameters(random, 1);
|
---|
90 | var moveScope = new Scope(moveScopes.Count.ToString());
|
---|
91 | moveScope.Variables.Add(new Variable(moveParameterName, m));
|
---|
92 | moveScopes.Add(moveScope);
|
---|
93 | }
|
---|
94 | CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
|
---|
95 | return base.Apply();
|
---|
96 | }
|
---|
97 | }
|
---|
98 | }
|
---|