69 lines
2.6 KiB
GDScript
69 lines
2.6 KiB
GDScript
extends SceneTree
|
|
## Times the greedy bot: plays greedy-vs-greedy games with every Set 1 card and reports how long
|
|
## each choose() call takes. Util.errors must stay empty.
|
|
##
|
|
## godot --headless --script res://tests/bot_speed.gd -- [--games N] [--seed N] [--bot greedy|nocards]
|
|
|
|
|
|
func _init() -> void:
|
|
var games := 20
|
|
var first := 1
|
|
var bot := "greedy"
|
|
var args := OS.get_cmdline_user_args()
|
|
var i := 0
|
|
while i < args.size():
|
|
match args[i]:
|
|
"--games":
|
|
i += 1
|
|
games = int(args[i])
|
|
"--seed":
|
|
i += 1
|
|
first = int(args[i])
|
|
"--bot":
|
|
i += 1
|
|
bot = args[i]
|
|
i += 1
|
|
var pool := Cards.load_cards()
|
|
var rules := Rules.defaults()
|
|
var maker: Callable = Play.bots()[bot]
|
|
var times := PackedFloat64Array()
|
|
var slowest := {"ms": 0.0, "at": ""}
|
|
var game_ms := 0.0
|
|
var failed := 0
|
|
for seed in range(first, first + games):
|
|
Util.errors.clear()
|
|
var s = RulesEngine.create_game({"pool": pool, "decks": Play.deal(pool, seed, rules), "seed": seed, "rules": rules})
|
|
var players := [maker.call(seed * 2 + 1), maker.call(seed * 2 + 2)]
|
|
var started := Time.get_ticks_usec()
|
|
var n := 0
|
|
while s != null and not s.over and n < Play.MAX_ACTIONS:
|
|
n += 1
|
|
var p: int = RulesEngine.decider(s)
|
|
var legal := RulesEngine.legal_actions(s)
|
|
var view := RulesEngine.view_for(s, p)
|
|
var t0 := Time.get_ticks_usec()
|
|
var a: Dictionary = players[p].choose(view, legal, p)
|
|
var ms := (Time.get_ticks_usec() - t0) / 1000.0
|
|
times.append(ms)
|
|
if ms > slowest.ms:
|
|
var w = RulesEngine.open_window(s)
|
|
var where: String = ("window %s" % w.kind) if w != null else ("choice %s" % s.choice.kind if s.choice != null else "step %s" % s.step)
|
|
slowest = {"ms": ms, "at": "seed %d turn %d, %s, picked %s" % [seed, s.turn, where, RulesEngine.action_key(a)]}
|
|
if not RulesEngine.apply_action(s, a, legal):
|
|
break
|
|
game_ms += (Time.get_ticks_usec() - started) / 1000.0
|
|
if s == null or not s.over or not Util.errors.is_empty():
|
|
failed += 1
|
|
print("seed %d: %s" % [seed, "; ".join(Util.errors) if not Util.errors.is_empty() else "didn't finish"])
|
|
times.sort()
|
|
var total := 0.0
|
|
for t in times:
|
|
total += t
|
|
var count := times.size()
|
|
print("%s: %d games, %d choose() calls" % [bot, games, count])
|
|
print(" per choose: mean %.3f ms, median %.3f, p99 %.2f, max %.1f" % [
|
|
total / count, times[count / 2], times[mini(count - 1, int(count * 0.99))], times[count - 1]])
|
|
print(" slowest: %s" % slowest.at)
|
|
print(" per game: %.0f ms with the engine, %.0f ms in the bot" % [game_ms / games, total / games])
|
|
print(" %d game(s) failed" % failed)
|
|
quit(0 if failed == 0 else 1)
|