Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/Policies/EpsGreedySymbolicExpressionConstructionPolicy.cs @ 12955

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

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

File size: 3.2 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using HeuristicLab.Common;
5using HeuristicLab.Core;
6using HeuristicLab.Data;
7using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
8using HeuristicLab.Parameters;
9using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
10using HeuristicLab.Random;
11
12namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
13  [StorableClass]
14  [Item("EpsGreedySymbolicExpressionConstructionPolicy", "")]
15  public class EpsGreedySymbolicExpressionConstructionPolicy : SymbolicExpressionConstructionPolicyBase {
16
17    public double Eps {
18      get { return ((IFixedValueParameter<DoubleValue>)Parameters["Eps"]).Value.Value; }
19      set { ((IFixedValueParameter<DoubleValue>)Parameters["Eps"]).Value.Value = value; }
20    }
21
22    public IStateValueFunction StateValueFunction {
23      get {
24        return ((IValueParameter<IStateValueFunction>)Parameters["Quality function"]).Value;
25      }
26      set { ((IValueParameter<IStateValueFunction>)Parameters["Quality function"]).Value = value; }
27    }
28
29    public EpsGreedySymbolicExpressionConstructionPolicy()
30      : base() {
31      Parameters.Add(new FixedValueParameter<DoubleValue>("Eps", "The fraction of random pulls", new PercentValue(0.1, true)));
32      Parameters.Add(new ValueParameter<IStateValueFunction>("Quality function", "The quality function to use", new TabularAvgStateValueFunction()));
33    }
34
35    protected override int Select(IReadOnlyList<object> followStates, IRandom random) {
36      var idxs = Enumerable.Range(0, followStates.Count);
37      if (random.NextDouble() < Eps) {
38        return idxs.SampleRandom(random);
39      }
40
41      // find best action
42      var bestFollowStates = new List<int>();
43      var bestQuality = double.NegativeInfinity;
44      for (int idx = 0; idx < followStates.Count; idx++) {
45        double quality = StateValueFunction.Value(followStates[idx]);
46
47        if (quality >= bestQuality) {
48          if (quality > bestQuality) {
49            bestFollowStates.Clear();
50            bestQuality = quality;
51          }
52          bestFollowStates.Add(idx);
53        }
54      }
55      return bestFollowStates.SampleRandom(random);
56    }
57
58    public sealed override void Update(IEnumerable<object> stateSequence, double quality) {
59      foreach (var state in stateSequence) {
60        StateValueFunction.Update(state, quality);
61      }
62    }
63
64    protected override object CreateState(ISymbolicExpressionTreeNode root, List<ISymbol> actionSequence, ISymbolicExpressionTreeNode parent, int childIdx) {
65      return StateValueFunction.StateFunction.CreateState(root, actionSequence, parent, childIdx);
66    }
67
68    #region IItem
69    protected EpsGreedySymbolicExpressionConstructionPolicy(EpsGreedySymbolicExpressionConstructionPolicy original, Cloner cloner)
70      : base(original, cloner) {
71    }
72
73    [StorableConstructor]
74    protected EpsGreedySymbolicExpressionConstructionPolicy(bool deserializing) : base(deserializing) { }
75
76    public override HeuristicLab.Common.IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
77      return new EpsGreedySymbolicExpressionConstructionPolicy(this, cloner);
78    }
79
80    #endregion
81  }
82}
Note: See TracBrowser for help on using the repository browser.