/
agent.go
97 lines (80 loc) · 1.54 KB
/
agent.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package tyche
import (
"github.com/elos/autonomous"
"github.com/elos/data"
"github.com/elos/models"
)
type Agent struct {
autonomous.Life
autonomous.Managed
autonomous.Stopper
identifier data.ID
bettingPower int
auction *Auction
stream Consumer
}
func NewAgent(name string, power int, auction *Auction) *Agent {
return &Agent{
Life: autonomous.NewLife(),
Stopper: make(autonomous.Stopper),
auction: auction,
stream: make(Consumer),
bettingPower: power,
identifier: data.ID(name),
}
}
func (a *Agent) Consumer() Consumer {
return a.stream
}
type SleepAgent struct {
*Agent
}
func NewSleepAgent(power int, auction *Auction) *SleepAgent {
return &SleepAgent{
Agent: NewAgent("sleep", power, auction),
}
}
func (a *SleepAgent) Start() {
action := models.NewAction()
action.Name = "Sleep Bid"
bid := 0
Run:
for {
select {
case <-a.stream:
a.auction.Bid("sleep", float64(bid), action)
case <-a.Stopper:
break Run
}
if bid < a.bettingPower {
bid += 5
}
}
a.auction.Forfeit("sleep")
}
type ReadingAgent struct {
*Agent
}
func NewReadingAgent(power int, auction *Auction) *ReadingAgent {
return &ReadingAgent{
Agent: NewAgent("reading", power, auction),
}
}
func (a *ReadingAgent) Start() {
action := models.NewAction()
action.Name = "Read Bid"
bid := 0
Run:
for {
select {
case <-a.stream:
a.auction.Bid("reading", float64(bid), action)
case <-a.Stopper:
break Run
}
if bid < a.bettingPower {
bid += 10
}
}
a.auction.Forfeit("reading")
}