source: branches/HeuristicLab.Algorithms.IteratedSentenceConstruction/HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction/3.3/Policies/BoltzmannExplorationSymbolicExpressionConstructionPolicy.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.3 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 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 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<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 idxs = Enumerable.Range(0, followStates.Count);
46      // find best action
47      if (followStates.Any(s => StateValueFunction.Tries(s) == 0)) {
48        return idxs.Where(idx => StateValueFunction.Tries(followStates[idx]) == 0).SampleRandom(random);
49      }
50
51      // windowing
52      var max = followStates.Select(s => StateValueFunction.Value(s)).Max();
53      var min = followStates.Select(s => StateValueFunction.Value(s)).Min();
54      double range = max - min;
55      if (range.IsAlmost(0.0)) return idxs.SampleRandom(random);
56
57      var w = from s in followStates
58              select Math.Exp(Beta * (StateValueFunction.Value(s) - min) / range);
59
60      return idxs.SampleProportional(random, 1, w).First();
61
62    }
63
64    public sealed override void Update(IEnumerable<object> stateSequence, double quality) {
65      foreach (var state in stateSequence) {
66        StateValueFunction.Update(state, quality);
67      }
68    }
69
70    protected override object CreateState(ISymbolicExpressionTreeNode root, List<ISymbol> actionSequence, ISymbolicExpressionTreeNode parent, int childIdx) {
71      return StateValueFunction.StateFunction.CreateState(root, actionSequence, parent, childIdx);
72    }
73
74    public override IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
75      return new BoltzmannExplorationSymbolicExpressionConstructionPolicy(this, cloner);
76    }
77  }
78}
Note: See TracBrowser for help on using the repository browser.