using System; using System.Collections.Generic; using System.Diagnostics.Contracts; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; using HeuristicLab.Random; namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction { [StorableClass] [Item("DepthFirstSymbolicExpressionConstructionPolicy", "Deterministic depth-first search (but tries terminal states first)")] public class DepthFirstSymbolicExpressionConstructionPolicy : SymbolicExpressionConstructionPolicyBase { public DepthFirstSymbolicExpressionConstructionPolicy() : base() { } protected override int Select(IReadOnlyList followStates, IRandom random) { Contract.Assert(followStates.Any()); int i = 0; foreach (var followState in followStates) { if ((bool)followState) return i; i++; } return 0; // return the first non-terminal follow state } public sealed override void Update(IEnumerable stateSequence, double quality) { // ignore } protected override object CreateState(ISymbolicExpressionTreeNode root, List actionSequence, ISymbolicExpressionTreeNode parent, int childIdx) { return parent.GetSubtree(childIdx).Symbol.MaximumArity == 0; // is it a terminal symbol? } #region IItem protected DepthFirstSymbolicExpressionConstructionPolicy(DepthFirstSymbolicExpressionConstructionPolicy original, Cloner cloner) : base(original, cloner) { } [StorableConstructor] protected DepthFirstSymbolicExpressionConstructionPolicy(bool deserializing) : base(deserializing) { } public override IDeepCloneable Clone(Cloner cloner) { return new DepthFirstSymbolicExpressionConstructionPolicy(this, cloner); } #endregion } }