Line | |
---|
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 |
|
---|
8 | namespace HeuristicLab.Algorithms.Bandits.BanditPolicies {
|
---|
9 | // uses a statistical model to sample and update posterior distribution p(Reward | Data)
|
---|
10 | public class ModelPolicyActionInfo : IBanditPolicyActionInfo {
|
---|
11 | private readonly IModel model;
|
---|
12 | private double knownValue;
|
---|
13 | public bool Disabled { get { return Tries == -1; } }
|
---|
14 | public double Value {
|
---|
15 | get {
|
---|
16 | if (Disabled) return knownValue;
|
---|
17 | else
|
---|
18 | return model.SampleExpectedReward(new Random());
|
---|
19 | }
|
---|
20 | }
|
---|
21 |
|
---|
22 | public int Tries { get; private set; }
|
---|
23 | public ModelPolicyActionInfo(IModel model) {
|
---|
24 | this.model = model;
|
---|
25 | }
|
---|
26 |
|
---|
27 | public void UpdateReward(double reward) {
|
---|
28 | Debug.Assert(!Disabled);
|
---|
29 | Tries++;
|
---|
30 | model.Update(reward);
|
---|
31 | }
|
---|
32 |
|
---|
33 | public double SampleExpectedReward(Random random) {
|
---|
34 | return model.SampleExpectedReward(random);
|
---|
35 | }
|
---|
36 |
|
---|
37 | public void Disable(double reward) {
|
---|
38 | this.Tries = -1;
|
---|
39 | this.knownValue = reward;
|
---|
40 | }
|
---|
41 |
|
---|
42 | public void Reset() {
|
---|
43 | Tries = 0;
|
---|
44 | knownValue = 0.0;
|
---|
45 | model.Reset();
|
---|
46 | }
|
---|
47 |
|
---|
48 | public void PrintStats() {
|
---|
49 | model.PrintStats();
|
---|
50 | }
|
---|
51 |
|
---|
52 | public override string ToString() {
|
---|
53 | return string.Format("disabled {0} model {1}", Disabled, model);
|
---|
54 | }
|
---|
55 | }
|
---|
56 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.