Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/Policies/DepthFirstSymbolicExpressionConstructionPolicy.cs @ 13398

Last change on this file since 13398 was 12955, checked in by gkronber, 9 years ago

#2471: implemented deterministic BFS and DFS for iterated symbolic expression construction

File size: 1.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics.Contracts;
4using System.Linq;
5using HeuristicLab.Common;
6using HeuristicLab.Core;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
9using HeuristicLab.Random;
10
11namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
12  [StorableClass]
13  [Item("DepthFirstSymbolicExpressionConstructionPolicy", "Deterministic depth-first search (but tries terminal states first)")]
14  public class DepthFirstSymbolicExpressionConstructionPolicy : SymbolicExpressionConstructionPolicyBase {
15
16    public DepthFirstSymbolicExpressionConstructionPolicy()
17      : base() {
18    }
19
20    protected override int Select(IReadOnlyList<object> followStates, IRandom random) {
21      Contract.Assert(followStates.Any());
22      int i = 0;
23      foreach (var followState in followStates) {
24        if ((bool)followState) return i;
25        i++;
26      }
27      return 0; // return the first non-terminal follow state
28    }
29
30    public sealed override void Update(IEnumerable<object> stateSequence, double quality) {
31      // ignore
32    }
33
34    protected override object CreateState(ISymbolicExpressionTreeNode root, List<ISymbol> actionSequence, ISymbolicExpressionTreeNode parent, int childIdx) {
35      return parent.GetSubtree(childIdx).Symbol.MaximumArity == 0; // is it a terminal symbol?
36    }
37
38    #region IItem
39    protected DepthFirstSymbolicExpressionConstructionPolicy(DepthFirstSymbolicExpressionConstructionPolicy original, Cloner cloner)
40      : base(original, cloner) {
41    }
42
43    [StorableConstructor]
44    protected DepthFirstSymbolicExpressionConstructionPolicy(bool deserializing) : base(deserializing) { }
45
46    public override IDeepCloneable Clone(Cloner cloner) {
47      return new DepthFirstSymbolicExpressionConstructionPolicy(this, cloner);
48    }
49
50    #endregion
51  }
52}
Note: See TracBrowser for help on using the repository browser.