/
main.go
300 lines (244 loc) · 6.48 KB
/
main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"os/exec"
"runtime/pprof"
"time"
)
var (
defaultPhrases multiStringValue = []string{
"case nightmare green",
"john bigboote",
"necronomicon",
"the laundry",
"yogsothoth",
"tsathoggua",
"blue hades",
"planet 10",
"monkeyboy",
"yolodyne",
"yuggoth",
"ia! ia!",
"r'lyeh",
"ei!",
}
// These are registered in init(), below.
inputFiles multiStringValue
powerPhrases multiStringValue
aiFlags multiStringValue = []string{"mcai", "cmc", "chanterai", "treeai"}
//aiFlags multiStringValue = []string{}
timeLimit = flag.Int("t", 0, "Time limit, in seconds, to produce output.")
// TODO(myenik) Lol we should think about how to deal with this one...
memLimit = flag.Int("m", 1000, "Memory limit, in megabytes, to produce output")
cpus = flag.Int("c", 1, "Number of processor cores available")
serve = flag.Bool("serve", false, "Launch a web server")
render = flag.Bool("render", false, "Render the game")
border = flag.Int("border", 40, "Pixels of border to render")
hexsize = flag.Int("hexsize", 3, "Which size to render (0=tiny,1=small,2=med,3=large)")
display = flag.Bool("display", false, "Open the GIF after rendering")
gifdelay = flag.Int("gif_delay", 10, "Time in 1/100ths of a second to wait between render frames.")
graph = flag.String("graph", "", "Dump treeai graph to file with this prefix")
profile = flag.String("profile", "", "Output CPU profile to file")
repeat = flag.String("repeat", "", "String for RepeaterAI to run")
seed = flag.Uint64("seed", 0xFFFFFFFFFFFFFFFF, "Use specific seed for single game")
customtag = flag.String("customtag", "", "Custom tag for solution")
debug = flag.Bool("debug", false, "enable logging")
)
// multiStringValue is a flag.Value which can be specified multiple times
// on the command line.
type multiStringValue []string
// String implements flag.Value.String.
func (s *multiStringValue) String() string {
return fmt.Sprintf("%v", *s)
}
// String implements flag.Value.Set, adding each new item to the slice.
func (s *multiStringValue) Set(v string) error {
*s = append(*s, v)
return nil
}
type devNull struct{}
// Write consumes all.
func (d devNull) Write([]byte) (int, error) {
return 0, nil
}
func ArgsOk() error {
if *serve {
return nil
}
if len(inputFiles) == 0 {
return fmt.Errorf("no file names specified")
}
return nil
}
type AISolution struct {
name string
commands string
score float64
}
func main() {
flag.Parse()
if err := ArgsOk(); err != nil {
log.Printf("invalid arguments: %v", err)
flag.Usage()
return
}
if !*debug {
log.SetOutput(devNull{})
}
if len(powerPhrases) == 0 {
powerPhrases = defaultPhrases
}
normalizePhrases()
if *profile != "" {
f, err := os.Create(*profile)
if err != nil {
log.Fatalf("Could not create profile file %s: %v", *profile, err)
}
pprof.StartCPUProfile(f)
}
if *serve {
log.Printf("Running server...")
runServer()
return
}
var timeout <-chan time.Time
timedOut := false
if *timeLimit > 0 {
var t time.Duration
if *timeLimit > 10 {
// Usually provided a 5 second buffer
t = time.Duration(*timeLimit - 5)
} else {
// ... but with hardly any time at all, use
// 90%.
t = time.Duration(0.9 * float64(*timeLimit))
}
timeout = time.After(t * time.Second)
}
var output []OutputEntry
for _, name := range inputFiles {
log.Printf("Processing %s", name)
f, err := os.Open(name)
if err != nil {
log.Fatalf("Could not open input file %s: %v", name, err)
}
problem, err := ParseInputProblem(f)
if err != nil {
log.Fatalf("Could not parse JSON in input file %s: %v", name, err)
}
if *seed != uint64(0xFFFFFFFFFFFFFFFF) { // Hijack seed
for i, s := range problem.SourceSeeds {
if *seed == s {
problem.SourceSeeds = problem.SourceSeeds[i : i+1]
}
}
}
// Take steps with random AI.
// TODO(myenik) make rendering less gross/if'd out everywhere.
for gi, g := range GamesFromProblem(problem) {
if timedOut {
break
}
var aiSolutions []AISolution
for _, ai := range aiFlags {
if timedOut {
break
}
var renderer *GameRenderer
if *render {
renderer = NewGameRenderer(g, *border, *hexsize)
}
aiGame := g.Fork()
log.Printf("Playing %+v", aiGame)
log.Printf("Using AI: %s", ai)
a := NewAI(aiGame, ai, *repeat)
i := 1
runGame:
for {
select {
case <-timeout:
timedOut = true
break runGame
default:
}
log.Printf("Step %d", i)
if *render {
renderer.AddFrame(a.Game())
}
done, err := a.Next()
if done {
log.Println("Game done!")
break
} else if err != nil {
log.Printf("a.Next error: %v", err)
break
}
i++
}
log.Printf("Commands: %s", a.Game().Commands)
a.Game().WriteFinalCommands()
log.Printf("Final Commands: %s", a.Game().FinalCommands)
log.Printf("Final Score: %f", a.Game().FinalScore())
if *render {
gifname := fmt.Sprintf("%s_game%d.gif", name, gi)
gif, err := os.Create(gifname)
if err != nil {
log.Fatalf("Failed to open output file %s: %v\n", gifname, err)
}
renderer.OutputGIF(gif, *gifdelay)
if *display {
c := exec.Command("sensible-browser", gifname)
c.Start()
}
}
aiSolutions = append(aiSolutions, AISolution{
name: ai,
commands: a.Game().FinalCommands.String(),
score: a.Game().FinalScore(),
})
}
best := AISolution{score: -1.0}
for _, a := range aiSolutions {
if a.score > best.score {
best = a
}
}
log.Printf("All solutions: %+v", aiSolutions)
log.Printf("Best solution: %+v", best)
mytag := *customtag
if mytag == "" {
mytag = fmt.Sprintf("Final Score: %v", best.score)
}
output = append(output, OutputEntry{
ProblemId: problem.Id,
Seed: problem.SourceSeeds[gi],
Tag: mytag,
Solution: best.commands,
})
}
}
// Dump output
if err := json.NewEncoder(os.Stdout).Encode(&output); err != nil {
log.Fatalf("Failed to encode output %+v: %v", output, err)
}
if *profile != "" {
pprof.StopCPUProfile()
}
}
func init() {
flag.Var(&inputFiles, "f", "File containing JSON encoded input.")
flag.Var(&powerPhrases, "p", "Phrase of power")
var keys string
comma := ""
for k := range ais {
keys += fmt.Sprintf("%s %s", comma, k)
if comma == "" {
comma = ","
}
}
flag.Var(&aiFlags, "ai", fmt.Sprintf("AI to use (can be multiple):%s", keys))
}