Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/QualityFunctions/TabularStateValueFunctionBase.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.4 KB
Line 
1using System;
2using System.Collections.Generic;
3using HeuristicLab.Common;
4using HeuristicLab.Core;
5using HeuristicLab.Parameters;
6using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
7
8namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
9  [StorableClass]
10  public abstract class TabularStateValueFunctionBase : ParameterizedNamedItem, ITabularStateValueFunction {
11    [Storable]
12    private readonly Dictionary<object, double> q = new Dictionary<object, double>();
13    [Storable]
14    private readonly Dictionary<object, double> qVariance = new Dictionary<object, double>();
15    [Storable]
16    private readonly Dictionary<object, int> tries = new Dictionary<object, int>();
17
18    public IStateFunction StateFunction {
19      get {
20        return ((IValueParameter<IStateFunction>)Parameters["State function"]).Value;
21      }
22      set { ((IValueParameter<IStateFunction>)Parameters["State function"]).Value = value; }
23    }
24
25    protected TabularStateValueFunctionBase()
26      : base() {
27      Parameters.Add(new ValueParameter<IStateFunction>("State function", "The function that is used to map partial trees to states", new DefaultStateFunction()));
28    }
29
30    public int Tries(object state) {
31      var t = 0;
32      if (!tries.TryGetValue(state, out t)) return 0;
33      return t;
34    }
35
36    public double Value(object state) {
37      // an action that has never been tried has q == infinity
38      double quality;
39      if (!q.TryGetValue(state, out quality)) return double.PositiveInfinity;
40      return quality;
41    }
42
43    public double ValueVariance(object state) {
44      // an action that has never been tried has qVariance == infinity
45      double var;
46      if (!qVariance.TryGetValue(state, out var)) return double.PositiveInfinity;
47      return var / Tries(state);
48    }
49
50    public virtual void Update(object state, double observedQuality) {
51      int t;
52      if (!tries.TryGetValue(state, out t)) {
53        t = 0;
54        tries.Add(state, t + 1);
55        q.Add(state, observedQuality);
56        qVariance.Add(state, 0); // naive initialization
57      } else {
58        tries[state] = t + 1;
59        var delta = observedQuality - q[state];
60        var curMean = CalculateNewQ(state, observedQuality);
61        q[state] = curMean;
62        qVariance[state] = qVariance[state] + delta * (observedQuality - curMean); // iterative calculation of mean
63      }
64    }
65
66    protected abstract double CalculateNewQ(object state, double observedQuality);
67
68
69    #region item
70    [StorableConstructor]
71    protected TabularStateValueFunctionBase(bool deserializing) : base(deserializing) { }
72    protected TabularStateValueFunctionBase(TabularStateValueFunctionBase original, Cloner cloner)
73      : base(original, cloner) {
74      // TODO: these become really large when using this class only from BasicAlgorithms it would not be necessary to clone and reset everything, (pause is not allowed)
75
76      this.q = new Dictionary<object, double>(original.q);
77      this.tries = new Dictionary<object, int>(original.tries);
78      this.qVariance = new Dictionary<object, double>(original.qVariance);
79    }
80    #endregion
81
82    //public void InitializeState() {
83    //  ClearState();
84    //}
85    //
86    //public void ClearState() {
87    //  q.Clear();
88    //  tries.Clear();
89    //  qVariance.Clear();
90    //}
91  }
92}
Note: See TracBrowser for help on using the repository browser.