-
Notifications
You must be signed in to change notification settings - Fork 7
/
reward_stub.go
38 lines (30 loc) · 993 Bytes
/
reward_stub.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package mab
import (
"context"
"fmt"
)
// RewardStub is a static non-contextual RewardSource that can be used for testing and development.
type RewardStub struct {
Rewards []Dist
}
// GetRewards gets the static rewards
func (s *RewardStub) GetRewards(context.Context, interface{}) ([]Dist, error) {
return s.Rewards, nil
}
// ContextualRewardStub is a static contextual RewardSource that can be used for testing and development of contextual bandits.
// It assumes that the context can be specified with a string.
type ContextualRewardStub struct {
Rewards map[string][]Dist
}
// GetRewards gets the static rewards for a given banditContext string.
func (c *ContextualRewardStub) GetRewards(ctx context.Context, banditContext interface{}) ([]Dist, error) {
key, ok := banditContext.(string)
if !ok {
return nil, fmt.Errorf("banditContext must be a string")
}
val, ok := c.Rewards[key]
if !ok {
return nil, fmt.Errorf("no distributions for %s", val)
}
return val, nil
}