source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/Policies/BoltzmannExplorationSymbolicExpressionConstructionPolicy.cs @ 12967

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

#2471: implemented linear state value approximation using sparse binary features and implemented a state function for generating features for symbolic expression trees

File size: 3.1 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("BoltzmannExplorationSymbolicExpressionConstructionPolicy", "")]
16  public class BoltzmannExplorationSymbolicExpressionConstructionPolicy : SymbolicExpressionConstructionPolicyBase {
17    public double Beta {
18      get { return ((IFixedValueParameter<DoubleValue>)Parameters["Beta"]).Value.Value; }
19      set { ((IFixedValueParameter<DoubleValue>)Parameters["Beta"]).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
30    protected BoltzmannExplorationSymbolicExpressionConstructionPolicy(BoltzmannExplorationSymbolicExpressionConstructionPolicy original, Cloner cloner)
31      : base(original, cloner) {
32    }
33
34    [StorableConstructor]
35    protected BoltzmannExplorationSymbolicExpressionConstructionPolicy(bool deserializing) : base(deserializing) { }
36
37
38    public BoltzmannExplorationSymbolicExpressionConstructionPolicy()
39      : base() {
40      Parameters.Add(new FixedValueParameter<DoubleValue>("Beta", "The weighting factor beta", new DoubleValue(1.0)));
41      Parameters.Add(new ValueParameter<IStateValueFunction>("Quality function", "The quality function to use", new TabularAvgStateValueFunction()));
42    }
43
44    protected sealed override int Select(IReadOnlyList<object> followStates, IRandom random) {
45      var idxs = Enumerable.Range(0, followStates.Count);
46
47      // windowing
48      var max = followStates.Select(s => StateValueFunction.Value(s)).Max();
49      var min = followStates.Select(s => StateValueFunction.Value(s)).Min();
50      double range = max - min;
51      if (range.IsAlmost(0.0)) return idxs.SampleRandom(random);
52
53      var w = from s in followStates
54              select Math.Exp(Beta * (StateValueFunction.Value(s) - min) / range);
55
56      return idxs.SampleProportional(random, 1, w).First();
57
58    }
59
60    public sealed override void Update(IEnumerable<object> stateSequence, double quality) {
61      foreach (var state in stateSequence) {
62        StateValueFunction.Update(state, quality);
63      }
64    }
65
66    protected override object CreateState(ISymbolicExpressionTreeNode root, List<ISymbol> actionSequence, ISymbolicExpressionTreeNode parent, int childIdx) {
67      return StateValueFunction.StateFunction.CreateState(root, actionSequence, parent, childIdx);
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new BoltzmannExplorationSymbolicExpressionConstructionPolicy(this, cloner);
72    }
73  }
74}
Note: See TracBrowser for help on using the repository browser.