2021-07-15 21:56:17 +00:00
|
|
|
import math, random, strformat, times, std/monotimes
|
2021-07-15 04:49:10 +00:00
|
|
|
import fixedseq, game, simulation, ui
|
2021-07-13 22:54:54 +00:00
|
|
|
|
|
|
|
|
2021-07-15 21:56:17 +00:00
|
|
|
type
|
|
|
|
TestResults = object
|
|
|
|
ops: int
|
|
|
|
time: Duration
|
|
|
|
|
|
|
|
|
|
|
|
proc summarize(tr: TestResults) =
|
|
|
|
let secs = tr.time.inMilliseconds.float / 1000
|
|
|
|
stdout.write("Test completed:\n")
|
|
|
|
stdout.write(" " & $tr.ops, " operations in " & $round(secs, 2) & " seconds\n")
|
|
|
|
stdout.write(" " & $round(tr.ops.float / secs, 2) & " operations per second")
|
|
|
|
stdout.flushFile()
|
|
|
|
|
|
|
|
|
|
|
|
template executionTime(body: untyped): Duration =
|
|
|
|
let start = getMonoTime()
|
|
|
|
body
|
|
|
|
getMonoTime() - start
|
|
|
|
|
|
|
|
|
|
|
|
proc getRand(): Rand =
|
|
|
|
randomize()
|
|
|
|
result = initRand(rand(int64))
|
|
|
|
|
|
|
|
|
2021-07-13 22:54:54 +00:00
|
|
|
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
|
|
|
for c in Color:
|
|
|
|
let v = r.rand(1..3)
|
|
|
|
result.add((c, v))
|
|
|
|
result.shuffle
|
|
|
|
|
|
|
|
|
2021-07-15 04:49:10 +00:00
|
|
|
proc newRandomGame(r: var Rand): Board =
|
|
|
|
var dice: array[5, tuple[c: Color, p: int]]
|
|
|
|
for i in 0 .. 4:
|
|
|
|
dice[i] = (Color(i), r.rand(1..3))
|
|
|
|
|
|
|
|
result.init
|
|
|
|
result.setState(dice, [])
|
|
|
|
|
|
|
|
|
2021-07-15 21:56:17 +00:00
|
|
|
proc games(nTests, nSamples: SomeInteger): TestResults =
|
|
|
|
var r = getRand()
|
|
|
|
var scores: ScoreSet
|
|
|
|
for i in 1 .. nTests:
|
|
|
|
let b = newRandomGame(r)
|
|
|
|
let dur = executionTime:
|
|
|
|
let s = b.randomGames(nSamples)
|
|
|
|
result.ops += s.sum()
|
|
|
|
result.time += dur
|
2021-07-13 22:54:54 +00:00
|
|
|
|
|
|
|
|
|
|
|
proc testLegs(n: Natural = 100): auto =
|
|
|
|
var boards: seq[Board]
|
|
|
|
var r = initRand(rand(int64))
|
|
|
|
for i in 1 .. n:
|
|
|
|
var b: Board
|
|
|
|
b.init
|
|
|
|
let dice = randomDice(r)
|
|
|
|
b.setState(dice, [])
|
|
|
|
boards.add(b)
|
|
|
|
stdout.write("Constructed: " & $i & "\r")
|
|
|
|
echo ""
|
|
|
|
|
|
|
|
echo "Running..."
|
|
|
|
let start = cpuTime()
|
|
|
|
for b in boards:
|
|
|
|
discard b.getLegScores
|
|
|
|
result = cpuTime() - start
|
|
|
|
|
|
|
|
|
2021-07-13 23:16:47 +00:00
|
|
|
proc testSpread(nTests, nSamples: Natural) =
|
|
|
|
var b: Board
|
|
|
|
b.init
|
|
|
|
var r = initRand(rand(int64))
|
|
|
|
let dice = randomDice(r)
|
|
|
|
b.setState(dice, [])
|
|
|
|
b.display(1, 5)
|
|
|
|
let spread = randomSpread(b, nTests, nSamples)
|
|
|
|
|
|
|
|
stdout.writeLine("Variance:")
|
|
|
|
for c in Color:
|
|
|
|
let variance = 100 * (spread.hi[c] - spread.lo[c])
|
|
|
|
stdout.writeLine(fmt"{c}: {round(variance, 2):.2f}%")
|
|
|
|
|
|
|
|
let diff = 100 * (max(spread.hi) - min(spread.lo))
|
|
|
|
stdout.writeLine(fmt"Win percentage differential: {round(diff, 2):.2f}%")
|
|
|
|
|
|
|
|
stdout.flushFile()
|
|
|
|
|
|
|
|
|
2021-07-13 22:54:54 +00:00
|
|
|
when isMainModule:
|
2021-07-15 21:56:17 +00:00
|
|
|
games(100, 1_000_000).summarize()
|
2021-07-13 22:54:54 +00:00
|
|
|
# let start_states = 2_000
|
|
|
|
# let executionTime = testLegs(start_states)
|
|
|
|
# echo "Execution time: ", executionTime
|
|
|
|
# echo "Leg simulations per second: ", float(start_states * 29_160) / executionTime
|
|
|
|
|
2021-07-13 23:16:47 +00:00
|
|
|
# for i in 1 .. 1:
|
|
|
|
# let num_games = 100_000_005
|
|
|
|
# let executionTime = testGames(num_games)
|
|
|
|
# echo "Execution time: ", executionTime
|
|
|
|
# echo "Full-game simulations per second: ", float(num_games) / executionTime
|
|
|
|
# echo ""
|
|
|
|
|
2021-07-15 04:49:10 +00:00
|
|
|
# testSpread(100, 1_000_000)
|