Free cookie consent management tool by TermsFeed Policy Generator

source: branches/GP-MoveOperators/HeuristicLab.Encodings.SymbolicExpressionTreeEncoding/3.4/Moves/ChangeNodeTypeMultiMoveGenerator.cs @ 7802

Last change on this file since 7802 was 7802, checked in by gkronber, 12 years ago

#1847 added initial implementation of move operators for symbolic expression tree encoding

File size: 4.5 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.Operators;
29using HeuristicLab.Optimization;
30using HeuristicLab.Parameters;
31using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
32
33namespace HeuristicLab.Encodings.SymbolicExpressionTreeEncoding {
34  [Item("ChangeNodeTypeMultiMoveGenerator", "Randomly samples n from all possible change node type moves from a given tree.")]
35  [StorableClass]
36  public class ChangeNodeTypeMultiMoveGenerator : 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 ChangeNodeTypeMultiMoveGenerator(bool deserializing) : base(deserializing) { }
60    protected ChangeNodeTypeMultiMoveGenerator(ChangeNodeTypeMultiMoveGenerator original, Cloner cloner) : base(original, cloner) { }
61    public ChangeNodeTypeMultiMoveGenerator()
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 ChangeNodeTypeMultiMoveGenerator(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        if (m.NewChild.HasLocalParameters)
90          m.NewChild.ResetLocalParameters(random);
91        var moveScope = new Scope(moveScopes.Count.ToString());
92        moveScope.Variables.Add(new Variable(moveParameterName, m));
93        moveScopes.Add(moveScope);
94      }
95      CurrentScopeParameter.ActualValue.SubScopes.AddRange(moveScopes);
96      return base.Apply();
97    }
98  }
99}
Note: See TracBrowser for help on using the repository browser.