Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/Policies/UcbTunedSymbolicExpressionConstructionPolicy.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.6 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.PluginInfrastructure;
11using HeuristicLab.Random;
12
13namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
14  [StorableClass]
15  [Item("UcbTunedSymbolicExpressionConstructionPolicy", "Also uses an estimate of the variance")]
16  public class UcbTunedSymbolicExpressionConstructionPolicy : SymbolicExpressionConstructionPolicyBase {
17    public double R {
18      get { return ((IFixedValueParameter<DoubleValue>)Parameters["R"]).Value.Value; }
19      set { ((IFixedValueParameter<DoubleValue>)Parameters["R"]).Value.Value = value; }
20    }
21
22    public ITabularStateValueFunction StateValueFunction {
23      get {
24        return ((IValueParameter<ITabularStateValueFunction>)Parameters["Quality function"]).Value;
25      }
26      set { ((IValueParameter<ITabularStateValueFunction>)Parameters["Quality function"]).Value = value; }
27    }
28
29
30    protected UcbTunedSymbolicExpressionConstructionPolicy(UcbTunedSymbolicExpressionConstructionPolicy original, Cloner cloner)
31      : base(original, cloner) {
32    }
33
34    [StorableConstructor]
35    protected UcbTunedSymbolicExpressionConstructionPolicy(bool deserializing) : base(deserializing) { }
36
37
38    public UcbTunedSymbolicExpressionConstructionPolicy()
39      : base() {
40      Parameters.Add(new FixedValueParameter<DoubleValue>("R", "The weighting factor for the confidence bound (should be scaled based on the range or the fitness values)", new DoubleValue(1.0)));
41      Parameters.Add(new ValueParameter<ITabularStateValueFunction>("Quality function", "The quality function to use", new TabularAvgStateValueFunction()));
42    }
43
44    protected sealed override int Select(IReadOnlyList<object> followStates, IRandom random) {
45      var bestFollowStates = new List<int>();
46      var bestQuality = double.NegativeInfinity;
47      int totalTries = followStates.Sum(s => StateValueFunction.Tries(s));
48      for (int idx = 0; idx < followStates.Count; idx++) {
49        var s = followStates[idx];
50        double quality;
51        if (StateValueFunction.Tries(s) == 0) {
52          quality = double.PositiveInfinity;
53        } else {
54          double v = StateValueFunction.ValueVariance(s) + Math.Sqrt(2 * Math.Log(totalTries) / StateValueFunction.Tries(s));
55          quality = StateValueFunction.Value(s) + R * Math.Sqrt(Math.Log(totalTries) / StateValueFunction.Tries(s) * v);
56        }
57        if (quality >= bestQuality) {
58          if (quality > bestQuality) {
59            bestFollowStates.Clear();
60            bestQuality = quality;
61          }
62          bestFollowStates.Add(idx);
63        }
64      }
65      return bestFollowStates.SampleRandom(random);
66    }
67
68    public sealed override void Update(IEnumerable<object> stateSequence, double quality) {
69      foreach (var state in stateSequence) {
70        StateValueFunction.Update(state, quality);
71      }
72    }
73
74    protected override object CreateState(ISymbolicExpressionTreeNode root, List<ISymbol> actionSequence, ISymbolicExpressionTreeNode parent, int childIdx) {
75      return StateValueFunction.StateFunction.CreateState(root, actionSequence, parent, childIdx);
76    }
77
78    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
79      return new UcbTunedSymbolicExpressionConstructionPolicy(this, cloner);
80    }
81  }
82}
Note: See TracBrowser for help on using the repository browser.