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.Random;
|
---|
11 |
|
---|
12 | namespace HeuristicLab.Algorithms.IteratedSymbolicExpressionConstruction {
|
---|
13 | [StorableClass]
|
---|
14 | [Item("EpsGreedySymbolicExpressionConstructionPolicy", "")]
|
---|
15 | public class EpsGreedySymbolicExpressionConstructionPolicy : SymbolicExpressionConstructionPolicyBase {
|
---|
16 |
|
---|
17 | public double Eps {
|
---|
18 | get { return ((IFixedValueParameter<DoubleValue>)Parameters["Eps"]).Value.Value; }
|
---|
19 | set { ((IFixedValueParameter<DoubleValue>)Parameters["Eps"]).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 | public EpsGreedySymbolicExpressionConstructionPolicy()
|
---|
30 | : base() {
|
---|
31 | Parameters.Add(new FixedValueParameter<DoubleValue>("Eps", "The fraction of random pulls", new PercentValue(0.1, true)));
|
---|
32 | Parameters.Add(new ValueParameter<IStateValueFunction>("Quality function", "The quality function to use", new TabularAvgStateValueFunction()));
|
---|
33 | }
|
---|
34 |
|
---|
35 | protected override int Select(IReadOnlyList<object> followStates, IRandom random) {
|
---|
36 | var idxs = Enumerable.Range(0, followStates.Count);
|
---|
37 | if (random.NextDouble() < Eps) {
|
---|
38 | return idxs.SampleRandom(random);
|
---|
39 | }
|
---|
40 |
|
---|
41 | // find best action
|
---|
42 | var bestFollowStates = new List<int>();
|
---|
43 | var bestQuality = double.NegativeInfinity;
|
---|
44 | for (int idx = 0; idx < followStates.Count; idx++) {
|
---|
45 | double quality = StateValueFunction.Value(followStates[idx]);
|
---|
46 |
|
---|
47 | if (quality >= bestQuality) {
|
---|
48 | if (quality > bestQuality) {
|
---|
49 | bestFollowStates.Clear();
|
---|
50 | bestQuality = quality;
|
---|
51 | }
|
---|
52 | bestFollowStates.Add(idx);
|
---|
53 | }
|
---|
54 | }
|
---|
55 | return bestFollowStates.SampleRandom(random);
|
---|
56 | }
|
---|
57 |
|
---|
58 | public sealed override void Update(IEnumerable<object> stateSequence, double quality) {
|
---|
59 | foreach (var state in stateSequence) {
|
---|
60 | StateValueFunction.Update(state, quality);
|
---|
61 | }
|
---|
62 | }
|
---|
63 |
|
---|
64 | protected override object CreateState(ISymbolicExpressionTreeNode root, List<ISymbol> actionSequence, ISymbolicExpressionTreeNode parent, int childIdx) {
|
---|
65 | return StateValueFunction.StateFunction.CreateState(root, actionSequence, parent, childIdx);
|
---|
66 | }
|
---|
67 |
|
---|
68 | #region IItem
|
---|
69 | protected EpsGreedySymbolicExpressionConstructionPolicy(EpsGreedySymbolicExpressionConstructionPolicy original, Cloner cloner)
|
---|
70 | : base(original, cloner) {
|
---|
71 | }
|
---|
72 |
|
---|
73 | [StorableConstructor]
|
---|
74 | protected EpsGreedySymbolicExpressionConstructionPolicy(bool deserializing) : base(deserializing) { }
|
---|
75 |
|
---|
76 | public override HeuristicLab.Common.IDeepCloneable Clone(HeuristicLab.Common.Cloner cloner) {
|
---|
77 | return new EpsGreedySymbolicExpressionConstructionPolicy(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | #endregion
|
---|
81 | }
|
---|
82 | }
|
---|