This repository has been archived by the owner on Jun 28, 2018. It is now read-only.
/
main.go
149 lines (118 loc) · 3.45 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
package main
import (
"fmt"
"github.com/pimms/suckbot/agent"
"github.com/pimms/suckbot/arg"
"github.com/pimms/suckbot/env"
"github.com/pimms/suckbot/util"
"math/rand"
"time"
"bufio"
"os"
)
func main() {
arg.BindArgs()
rand.Seed(time.Now().UTC().UnixNano())
var visual bool = arg.Visual()
var rounds int = arg.NumRounds()
var delay int = arg.DelayMS()
var renderer t_renderer
if visual {
renderer.createWindow()
}
// Initialize the controller and the agent
controller := createController()
var perfs []util.SimPerf
var max = controller.GetMaxPermCount()
fmt.Printf("Max permutations: %d\n", max)
perfs = make([]util.SimPerf, controller.GetMaxPermCount())
var posPerm, dirtPerm uint64 = 0, 0
for controller.CanPermute(posPerm, dirtPerm) {
a := new(agent.Agent)
a.Initialize(controller.GetStartingTile())
controller.Permute(posPerm, dirtPerm)
permNo := controller.GetPermNumber(posPerm, dirtPerm)
// Run the simulation
for i := 0; i < rounds; i++ {
if visual {
renderer.pollEvents()
if renderer.shouldExit {
break
}
renderer.renderFrame(controller, a)
if arg.Manual() {
reader := bufio.NewReader(os.Stdin)
_, _ = reader.ReadString('\n')
} else {
time.Sleep(time.Duration(delay) * time.Millisecond)
}
}
controller.Tick()
a.Tick(&perfs[permNo])
perfs[permNo].SetCleanTilesThisTick(controller.GetCleanTilesCount())
}
if arg.Verbose() {
printSimPerf(perfs[permNo])
}
// Increment the permutations - if no more permutations
// are allowed with the incremented dirtPerm, increment
// posPerm. If posPerm now holds an invalid value, the
// embracing for-loop will terminate.
dirtPerm++
if !controller.CanPermute(posPerm, dirtPerm) {
dirtPerm = 0
posPerm++
}
}
printSimPerfs(&perfs)
}
func printSimPerf(perf util.SimPerf) {
s := make([]util.SimPerf, 1, 1)
s[0] = perf
printSimPerfs(&s)
}
func printSimPerfs(simPerf *[]util.SimPerf) {
fmt.Printf("PERFORMANCE\n")
fmt.Printf("Num simulations -> %d\n", len(*simPerf))
fmt.Printf("Ticks per simulation -> %d\n", arg.NumRounds())
fmt.Printf("ATTRIBUTE AVG MIN MAX\n")
printPerfStat("Total score", simPerf, util.GetTotalScore)
printPerfStat("Agent moves", simPerf, util.GetAgentMoves)
printPerfStat("Agent cleans", simPerf, util.GetAgentCleans)
printPerfStat("Agent clean %", simPerf, util.GetAgentCleanPercent)
printPerfStat("Dirty entry %", simPerf, util.GetDirtyEntryPercent)
printPerfStat("Dirty duration", simPerf, util.GetAvgDirtyTicks)
print("-----------------------------------\n")
printPerfStat("Clean tile-tick", simPerf, util.GetCleanTicks)
printPerfStat("Simple score", simPerf, util.GetSimpleScore)
print("\n\n")
}
func printPerfStat(context string,
s *[]util.SimPerf, fp func(util.SimPerf) float64) {
var min float64 = 1000000000.0
var max float64 = -1000000000.0
var avg float64 = 0.0
var count = float64(len(*s))
for i := 0; i < len(*s); i++ {
var f = fp((*s)[i])
if f < min {
min = f
}
if f > max {
max = f
}
avg += f / count
}
fmt.Printf("%s%s%s%s\n",
util.StrW(context, 30),
util.StrW(fmt.Sprintf("%0.3f", avg), 10),
util.StrW(fmt.Sprintf("%0.3f", min), 10),
util.StrW(fmt.Sprintf("%0.3f", max), 10))
}
func createController() *env.Controller {
var controller *env.Controller
controller = new(env.Controller)
var tileMap = env.LoadMap(arg.File())
controller.InitController(tileMap)
return controller
}