/
infoset.go
305 lines (258 loc) · 8.31 KB
/
infoset.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
package alphacats
import (
"encoding/binary"
"encoding/gob"
"fmt"
"github.com/timpalpant/alphacats/cards"
"github.com/timpalpant/alphacats/gamestate"
)
type InfoSetWithAvailableActions struct {
gamestate.InfoSet
AvailableActions []gamestate.Action
}
func (is *InfoSetWithAvailableActions) MarshalBinary() ([]byte, error) {
bufSize := is.InfoSet.MarshalBinarySize() + len(is.AvailableActions) + 1
for _, action := range is.AvailableActions {
if action.HasPrivateInfo() {
bufSize += 2
}
}
buf := make([]byte, 0, bufSize)
buf, err := is.InfoSet.MarshalTo(buf)
if err != nil {
return nil, err
}
// Append available actions.
nInfoSetBytes := len(buf)
for _, action := range is.AvailableActions {
packed := gamestate.EncodeAction(action)
buf = append(buf, packed[0])
// Actions are "varint" encoded: we only copy the private bits
// if they are non-zero, which is indicated by the last bit of
// the first byte.
if action.HasPrivateInfo() {
buf = append(buf, packed[1], packed[2])
}
}
// Append number of available actions bytes so we can split off when unmarshaling.
nAvailableActionBytes := uint8(len(buf) - nInfoSetBytes)
buf = append(buf, nAvailableActionBytes)
return buf, nil
}
func (is *InfoSetWithAvailableActions) UnmarshalBinary(buf []byte) error {
nAvailableActionBytes := int(uint8(buf[len(buf)-1]))
buf = buf[:len(buf)-1]
actionsBuf := buf[len(buf)-nAvailableActionBytes:]
isBuf := buf[:len(buf)-nAvailableActionBytes]
if err := is.InfoSet.UnmarshalBinary(isBuf); err != nil {
return err
}
if len(is.AvailableActions) > 0 { // Clear
is.AvailableActions = is.AvailableActions[:0]
}
for len(actionsBuf) > 0 {
packed := gamestate.EncodedAction{}
packed[0] = actionsBuf[0]
actionsBuf = actionsBuf[1:]
if packed.HasPrivateInfo() {
packed[1] = actionsBuf[0]
packed[2] = actionsBuf[1]
actionsBuf = actionsBuf[2:]
}
is.AvailableActions = append(is.AvailableActions, packed.Decode())
}
return nil
}
// AbstractedInfoSet abstracts away private history.
// The main difference in this abstraction is that the exact ordering in which
// private cards were received in the history is neglected.
// A second difference is that cards known to be in the draw pile (but not known where)
// are forgotten. This can happen if a SeeTheFuture card is played followed by
// a shuffle.
type AbstractedInfoSet struct {
Player gamestate.Player
PublicHistory gamestate.History
Hand cards.Set
P0PlayedCards cards.Set
P1PlayedCards cards.Set
DrawPile cards.Stack
AvailableActions []gamestate.Action
}
func (a AbstractedInfoSet) String() string {
return fmt.Sprintf("%s. Hand: %s, Draw pile: %s. Public history: %s. P0 played: %s, P1 played: %s. Available actions: %v",
a.Player, a.Hand, a.DrawPile, a.PublicHistory, a.P0PlayedCards, a.P1PlayedCards, a.AvailableActions)
}
func newAbstractedInfoSet(is *gamestate.InfoSet, availableActions []gamestate.Action) AbstractedInfoSet {
result := AbstractedInfoSet{
Player: is.Player,
Hand: is.Hand,
AvailableActions: availableActions,
}
// TODO(palpant): This duplicates most of gamestate logic, but from the POV of a single player.
for i := 0; i < 13; i++ {
result.DrawPile.SetNthCard(i, cards.TBD)
}
for i := 0; i < is.History.Len(); i++ {
packed := is.History.GetPacked(i)
result.PublicHistory.AppendPacked(hidePrivateInfo(packed))
action := packed.Decode()
switch action.Type {
case gamestate.PlayCard:
if action.Player == gamestate.Player0 {
result.P0PlayedCards.Add(action.Card)
} else {
result.P1PlayedCards.Add(action.Card)
}
switch action.Card {
case cards.SeeTheFuture:
for i, card := range action.CardsSeen {
if card != cards.Unknown {
result.DrawPile.SetNthCard(i, card)
}
}
case cards.DrawFromTheBottom:
result.DrawPile.RemoveCard(result.DrawPile.Len() - 1)
case cards.Shuffle:
result.DrawPile = clearDrawPile(result.DrawPile)
}
case gamestate.InsertExplodingKitten:
if action.Player == gamestate.Player0 {
result.P0PlayedCards.Add(cards.Defuse)
} else {
result.P1PlayedCards.Add(cards.Defuse)
}
if action.PositionInDrawPile != 0 {
result.DrawPile.InsertCard(cards.ExplodingKitten, int(action.PositionInDrawPile-1))
} else {
// TODO(palpant): Inserting the kitten randomly need not totally obliterate
// our knowledge of the draw pile, since once we observe the Kitten we know
// which of the N random states we're now in.
result.DrawPile.InsertCard(cards.ExplodingKitten, 0)
result.DrawPile = clearDrawPile(result.DrawPile)
}
case gamestate.DrawCard:
result.DrawPile.RemoveCard(0)
}
}
return result
}
func clearDrawPile(drawPile cards.Stack) cards.Stack {
for j := 0; j < drawPile.Len(); j++ {
drawPile.SetNthCard(j, cards.TBD)
}
return drawPile
}
func hidePrivateInfo(a gamestate.EncodedAction) gamestate.EncodedAction {
a[1] = 0
a[2] = 0
return a
}
// Key implements cfr.InfoSet.
func (is *AbstractedInfoSet) Key() []byte {
// Doing extra work to exactly size the buffer (and avoid any additional
// allocations ends up being faster than letting it auto-size)
historySize := is.PublicHistory.Len() + 1
for i := 0; i < is.PublicHistory.Len(); i++ {
packed := is.PublicHistory.GetPacked(i)
if packed.HasPrivateInfo() {
historySize += 2
}
}
cardsSize := 4 * 8
availableActionsSize := len(is.AvailableActions) + 1
for _, action := range is.AvailableActions {
if action.HasPrivateInfo() {
availableActionsSize += 2
}
}
bufSize := cardsSize + historySize + availableActionsSize
buf := make([]byte, 0, bufSize)
// First do sets of cards.
var hBuf [8]byte
binary.LittleEndian.PutUint64(hBuf[:], uint64(is.Hand))
buf = append(buf, hBuf[:]...)
binary.LittleEndian.PutUint64(hBuf[:], uint64(is.P0PlayedCards))
buf = append(buf, hBuf[:]...)
binary.LittleEndian.PutUint64(hBuf[:], uint64(is.P1PlayedCards))
buf = append(buf, hBuf[:]...)
// Then draw pile.
binary.LittleEndian.PutUint64(hBuf[:], uint64(is.DrawPile))
buf = append(buf, hBuf[:]...)
// Then history, prefixed by length.
buf = append(buf, uint8(is.PublicHistory.Len()))
for i := 0; i < is.PublicHistory.Len(); i++ {
action := is.PublicHistory.GetPacked(i)
buf = append(buf, action[0])
// Actions are "varint" encoded: we only copy the private bits
// if they are non-zero, which is indicated by the last bit of
// the first byte.
if action.HasPrivateInfo() {
buf = append(buf, action[1], action[2])
}
}
// Then available actions, prefixed by length.
buf = append(buf, uint8(len(is.AvailableActions)))
for _, action := range is.AvailableActions {
packed := gamestate.EncodeAction(action)
buf = append(buf, packed[0])
// Actions are "varint" encoded: we only copy the private bits
// if they are non-zero, which is indicated by the last bit of
// the first byte.
if action.HasPrivateInfo() {
buf = append(buf, packed[1], packed[2])
}
}
return buf
}
func (is *AbstractedInfoSet) MarshalBinary() ([]byte, error) {
return is.Key(), nil
}
func (is *AbstractedInfoSet) UnmarshalBinary(buf []byte) error {
is.PublicHistory.Clear()
is.Hand = cards.Set(binary.LittleEndian.Uint64(buf))
buf = buf[8:]
is.P0PlayedCards = cards.Set(binary.LittleEndian.Uint64(buf))
buf = buf[8:]
is.P1PlayedCards = cards.Set(binary.LittleEndian.Uint64(buf))
buf = buf[8:]
is.DrawPile = cards.Stack(binary.LittleEndian.Uint64(buf))
buf = buf[8:]
nActions := uint8(buf[0])
buf = buf[1:]
for i := 0; i < int(nActions); i++ {
var packed gamestate.EncodedAction
packed[0] = buf[0]
buf = buf[1:]
if packed.HasPrivateInfo() {
packed[1] = buf[0]
packed[2] = buf[1]
buf = buf[2:]
}
is.PublicHistory.AppendPacked(packed)
}
if len(is.AvailableActions) > 0 { // Clear
is.AvailableActions = is.AvailableActions[:0]
}
nActions = uint8(buf[0])
buf = buf[1:]
for len(buf) > 0 {
packed := gamestate.EncodedAction{}
packed[0] = buf[0]
buf = buf[1:]
if packed.HasPrivateInfo() {
packed[1] = buf[0]
packed[2] = buf[1]
buf = buf[2:]
}
is.AvailableActions = append(is.AvailableActions, packed.Decode())
}
if len(is.AvailableActions) != int(nActions) {
panic(fmt.Errorf("expected %d available actions, got %d",
nActions, len(is.AvailableActions)))
}
return nil
}
func init() {
gob.Register(&InfoSetWithAvailableActions{})
gob.Register(&AbstractedInfoSet{})
}