1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using HeuristicLab.Common;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
8 | using HeuristicLab.Parameters;
|
---|
9 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
10 | using HeuristicLab.PluginInfrastructure;
|
---|
11 | using HeuristicLab.Random;
|
---|
12 |
|
---|
13 | namespace 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 | }
|
---|