1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics.Contracts;
|
---|
4 | using System.Linq;
|
---|
5 | using HeuristicLab.Common;
|
---|
6 | using HeuristicLab.Core;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
9 | using HeuristicLab.Random;
|
---|
10 |
|
---|
11 | namespace 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 | }
|
---|