Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/MctsSymbolicRegression/Policies/EpsGreedy.cs @ 13662

Last change on this file since 13662 was 13662, checked in by gkronber, 8 years ago

#2581: made cloning constructors protected

File size: 2.9 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Diagnostics.Contracts;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7using HeuristicLab.Common;
8using HeuristicLab.Core;
9using HeuristicLab.Data;
10using HeuristicLab.Parameters;
11
12namespace HeuristicLab.Algorithms.DataAnalysis.MctsSymbolicRegression.Policies {
13  [Item("EpsilonGreedy", "Epsilon greedy policy with parameter eps to balance between exploitation and exploration")]
14  public class EpsilonGreedy : PolicyBase {
15    private class ActionStatistics : IActionStatistics {
16      public double SumQuality { get; set; }
17      public double AverageQuality { get { return SumQuality / Tries; } }
18      public int Tries { get; set; }
19      public bool Done { get; set; }
20    }
21    private List<int> buf = new List<int>();
22
23    public IFixedValueParameter<DoubleValue> EpsParameter {
24      get { return (IFixedValueParameter<DoubleValue>)Parameters["Eps"]; }
25    }
26
27    public double Eps {
28      get { return EpsParameter.Value.Value; }
29      set { EpsParameter.Value.Value = value; }
30    }
31
32    protected EpsilonGreedy(EpsilonGreedy original, Cloner cloner)
33      : base(original, cloner) {
34    }
35    public EpsilonGreedy()
36      : base() {
37      Parameters.Add(new FixedValueParameter<DoubleValue>("Eps", "Rate of random selection 0 (greedy) <= eps <= 1 (random)", new DoubleValue(0.1)));
38    }
39
40    public override IDeepCloneable Clone(Cloner cloner) {
41      return new EpsilonGreedy(this, cloner);
42    }
43
44    public override int Select(IEnumerable<IActionStatistics> actions, IRandom random) {
45      return Select(actions, random, Eps, buf);
46    }
47
48    public override void Update(IActionStatistics action, double q) {
49      var a = action as ActionStatistics;
50      a.SumQuality += q;
51      a.Tries++;
52    }
53
54    public override IActionStatistics CreateActionStatistics() {
55      return new ActionStatistics();
56    }
57
58    private static int Select(IEnumerable<IActionStatistics> actions, IRandom rand, double c, IList<int> buf) {
59      buf.Clear();
60      if (rand.NextDouble() >= c) {
61        // select best
62        var bestQ = double.NegativeInfinity;
63        int aIdx = -1;
64        foreach (var a in actions) {
65          ++aIdx;
66          if (a.Done) continue;
67          var actionQ = a.Tries > 0 ? a.AverageQuality : double.PositiveInfinity; // always try unvisited actions first
68          if (actionQ > bestQ) {
69            buf.Clear();
70            buf.Add(aIdx);
71            bestQ = actionQ;
72          } else if (actionQ >= bestQ) {
73            buf.Add(aIdx);
74          }
75        }
76        return buf[rand.Next(buf.Count)];
77      } else {
78        // random selection
79        int aIdx = -1;
80        foreach (var a in actions) {
81          ++aIdx;
82          if (a.Done) continue;
83          buf.Add(aIdx);
84        }
85        return buf[rand.Next(buf.Count)];
86      }
87    }
88  }
89}
Note: See TracBrowser for help on using the repository browser.