1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Diagnostics;
|
---|
4 | using System.Linq;
|
---|
5 | using System.Text;
|
---|
6 | using System.Threading.Tasks;
|
---|
7 | using HeuristicLab.Common;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Algorithms.Bandits.BanditPolicies {
|
---|
10 | public class ActiveLearningPolicy : IBanditPolicy {
|
---|
11 | public int SelectAction(Random random, IEnumerable<IBanditPolicyActionInfo> actionInfos) {
|
---|
12 | var myActionInfos = actionInfos.OfType<DefaultPolicyActionInfo>();
|
---|
13 | int totalTries = myActionInfos.Where(a => !a.Disabled).Sum(a => a.Tries);
|
---|
14 | const double delta = 0.1;
|
---|
15 | int k = myActionInfos.Where(a => !a.Disabled).Count();
|
---|
16 | var bestActions = new List<int>();
|
---|
17 | var us = new List<double>();
|
---|
18 | var ls = new List<double>();
|
---|
19 | int aIdx = -1;
|
---|
20 | foreach (var aInfo in myActionInfos) {
|
---|
21 | aIdx++;
|
---|
22 | if (aInfo.Disabled) continue;
|
---|
23 | double q;
|
---|
24 | double u;
|
---|
25 | double l;
|
---|
26 | if (aInfo.Tries == 0) {
|
---|
27 | u = double.PositiveInfinity;
|
---|
28 | l = double.NegativeInfinity;
|
---|
29 | } else {
|
---|
30 | q = aInfo.SumReward / aInfo.Tries;
|
---|
31 | var b = Math.Sqrt(Math.Log(2.0 * k * totalTries / delta) / (2.0 * aInfo.Tries));
|
---|
32 | u = q + 0.5 * b;
|
---|
33 | l = q - 0.5 * b;
|
---|
34 | }
|
---|
35 | bestActions.Add(aIdx);
|
---|
36 | us.Add(u);
|
---|
37 | ls.Add(l);
|
---|
38 | }
|
---|
39 | var active = new List<int>();
|
---|
40 | var maxL = ls.Max();
|
---|
41 | for (int i = 0; i < us.Count; i++) {
|
---|
42 | if (us[i] >= maxL) active.Add(bestActions[i]);
|
---|
43 | }
|
---|
44 | Debug.Assert(active.Any());
|
---|
45 | return active.SelectRandom(random);
|
---|
46 | }
|
---|
47 |
|
---|
48 | public IBanditPolicyActionInfo CreateActionInfo() {
|
---|
49 | return new DefaultPolicyActionInfo();
|
---|
50 | }
|
---|
51 | public override string ToString() {
|
---|
52 | return "ActiveLearningPolicy";
|
---|
53 | }
|
---|
54 | }
|
---|
55 | }
|
---|