Compare commits
1 Commits
camelpos
...
2687bd2198
Author | SHA1 | Date | |
---|---|---|---|
2687bd2198 |
52
fastrand.nim
Normal file
52
fastrand.nim
Normal file
@ -0,0 +1,52 @@
|
|||||||
|
import random, math
|
||||||
|
import times, std/monotimes, strformat, strutils
|
||||||
|
|
||||||
|
|
||||||
|
proc formatNum(n: SomeNumber): string =
|
||||||
|
let s = $(n.round)
|
||||||
|
let t = s[0 .. s.len - 3]
|
||||||
|
var count = 1
|
||||||
|
for i in countdown(t.high, 0):
|
||||||
|
result.insert($t[i], 0)
|
||||||
|
if count mod 3 == 0 and i != 0:
|
||||||
|
result.insert(",", 0)
|
||||||
|
count += 1
|
||||||
|
|
||||||
|
|
||||||
|
proc formatRate(n: Natural, d: Duration): string =
|
||||||
|
result = formatNum(1_000_000'f64 * n.float64 / d.inMicroseconds.float64)
|
||||||
|
|
||||||
|
|
||||||
|
proc fastRand(r: var Rand, x: Natural): uint64 =
|
||||||
|
# return ((r.next shr 32) * x.uint64) shr 32
|
||||||
|
let x = x.uint64
|
||||||
|
if x <= (uint64.high shl 32):
|
||||||
|
return ((r.next shr 32) * x.uint64) shr 32
|
||||||
|
else:
|
||||||
|
return r.next mod x.uint64
|
||||||
|
|
||||||
|
|
||||||
|
proc testFastRand(num = 1_000_000_000): Duration =
|
||||||
|
var r = initRand(rand(int64))
|
||||||
|
let start = getMonoTime()
|
||||||
|
for i in 1 .. num:
|
||||||
|
discard r.fastRand(5)
|
||||||
|
result = getMonoTime() - start
|
||||||
|
# echo "fastrand execution rate: ", 1000 * num / dur.inMilliseconds.int, " generated per second."
|
||||||
|
|
||||||
|
|
||||||
|
proc testStdRand(num = 1_000_000_000): Duration =
|
||||||
|
var r = initRand(rand(int64))
|
||||||
|
let start = getMonoTime()
|
||||||
|
for i in 1 .. num:
|
||||||
|
discard r.rand(4)
|
||||||
|
result = getMonoTime() - start
|
||||||
|
# echo "std rand execution rate: ", 1000 * num / dur.inMilliseconds.int, " generated per second."
|
||||||
|
|
||||||
|
|
||||||
|
randomize()
|
||||||
|
let runs = 100_000_000
|
||||||
|
let fr = testFastRand(runs)
|
||||||
|
echo "fastrand execution rate: ", formatNum(1_000_000 * runs / fr.inMicroseconds.int)
|
||||||
|
let sr = testStdRand(runs)
|
||||||
|
echo "standard execution rate: ", formatNum(1_000_000 * runs / sr.inMicroseconds.int)
|
26
game.nim
26
game.nim
@ -53,13 +53,9 @@ type
|
|||||||
camels*: ColorStack
|
camels*: ColorStack
|
||||||
tile*: Option[Tile]
|
tile*: Option[Tile]
|
||||||
|
|
||||||
CamelPos* = object
|
|
||||||
square*: range[1..16]
|
|
||||||
stackIdx*: int8
|
|
||||||
|
|
||||||
Board* = object
|
Board* = object
|
||||||
squares*: array[1..16, Square]
|
squares*: array[1..16, Square]
|
||||||
camels*: array[Color, CamelPos]
|
camels*: array[Color, range[1..16]]
|
||||||
diceRolled*: array[Color, bool]
|
diceRolled*: array[Color, bool]
|
||||||
leader*: Option[Color]
|
leader*: Option[Color]
|
||||||
gameOver*: bool
|
gameOver*: bool
|
||||||
@ -107,17 +103,13 @@ proc setState*(b: var Board;
|
|||||||
tiles: openArray[tuple[t: Tile, p: int]]) =
|
tiles: openArray[tuple[t: Tile, p: int]]) =
|
||||||
for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking
|
for (color, dest) in camels: # note that `camels` is ordered, as this determines stacking
|
||||||
b[dest].camels.add(color)
|
b[dest].camels.add(color)
|
||||||
let height = b[dest].camels.high
|
b.camels[color] = dest
|
||||||
b.camels[color] = CamelPos(square: dest, stackIdx: height)
|
|
||||||
|
|
||||||
for (tile, dest) in tiles:
|
for (tile, dest) in tiles:
|
||||||
b[dest].tile = some(tile)
|
b[dest].tile = some(tile)
|
||||||
|
|
||||||
for sq in b.squares:
|
let leadCamel = b[max(b.camels)].camels[^1] # top camel in the last currently-occupied space
|
||||||
if sq.camels.len > 0:
|
b.leader = some(leadCamel)
|
||||||
let squareLeader = sq.camels[^1]
|
|
||||||
if b.leader.isNone or b.leader.get != squareLeader:
|
|
||||||
b.leader = some(squareLeader)
|
|
||||||
|
|
||||||
|
|
||||||
proc diceRemaining*(b: Board): ColorStack =
|
proc diceRemaining*(b: Board): ColorStack =
|
||||||
@ -134,7 +126,7 @@ proc resetDice*(b: var Board) =
|
|||||||
proc advance*(b: var Board, die: Die) =
|
proc advance*(b: var Board, die: Die) =
|
||||||
let
|
let
|
||||||
(color, roll) = die
|
(color, roll) = die
|
||||||
startPos = b.camels[color].square
|
startPos = b.camels[color]
|
||||||
var endPos = startPos + roll
|
var endPos = startPos + roll
|
||||||
|
|
||||||
if endPos > 16: # camel has passed the finish line
|
if endPos > 16: # camel has passed the finish line
|
||||||
@ -152,18 +144,18 @@ proc advance*(b: var Board, die: Die) =
|
|||||||
if prepend:
|
if prepend:
|
||||||
b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart)
|
b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart)
|
||||||
let stackLen = b[startPos].camels.len - stackStart
|
let stackLen = b[startPos].camels.len - stackStart
|
||||||
for i in 0'i8 ..< stackLen:
|
for i in 0 ..< stackLen:
|
||||||
# we know how many camels we added to the bottom, so set the position for each of those
|
# we know how many camels we added to the bottom, so set the position for each of those
|
||||||
b.camels[b[endPos].camels[i]] = CamelPos(square: endPos, stackIdx: i) # replace with cast later?
|
b.camels[b[endPos].camels[i]] = endPos
|
||||||
else:
|
else:
|
||||||
let dstPrevHigh = b[endPos].camels.high
|
let dstPrevHigh = b[endPos].camels.high
|
||||||
b[startPos].camels.moveSubstack(b[endPos].camels, stackStart)
|
b[startPos].camels.moveSubstack(b[endPos].camels, stackStart)
|
||||||
# the camels that have moved start immediately after the previous high camel
|
# the camels that have moved start immediately after the previous high camel
|
||||||
for i in (dstPrevHigh + 1) .. b[endPos].camels.high:
|
for i in (dstPrevHigh + 1) .. b[endPos].camels.high:
|
||||||
b.camels[b[endPos].camels[i]] = CamelPos(square: endPos, stackIdx: i)
|
b.camels[b[endPos].camels[i]] = endPos
|
||||||
|
|
||||||
# if we are stacking on or moving past the previous leader
|
# if we are stacking on or moving past the previous leader
|
||||||
if endPos >= b.camels[b.leader.get].square:
|
if endPos >= b.camels[b.leader.get]:
|
||||||
b.leader = some(b[endPos].camels[^1])
|
b.leader = some(b[endPos].camels[^1])
|
||||||
|
|
||||||
b.diceRolled[color] = true
|
b.diceRolled[color] = true
|
70
test.nim
70
test.nim
@ -1,32 +1,7 @@
|
|||||||
import math, random, strformat, times, std/monotimes
|
import math, random, strformat, times
|
||||||
import fixedseq, game, simulation, ui
|
import fixedseq, game, simulation, ui
|
||||||
|
|
||||||
|
|
||||||
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))
|
|
||||||
|
|
||||||
|
|
||||||
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
proc randomDice(r: var Rand): seq[tuple[c: Color, p: int]] =
|
||||||
for c in Color:
|
for c in Color:
|
||||||
let v = r.rand(1..3)
|
let v = r.rand(1..3)
|
||||||
@ -43,15 +18,18 @@ proc newRandomGame(r: var Rand): Board =
|
|||||||
result.setState(dice, [])
|
result.setState(dice, [])
|
||||||
|
|
||||||
|
|
||||||
proc games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
|
proc testGames(n: SomeInteger = 100): auto =
|
||||||
var r = getRand()
|
var r = initRand(rand(int64))
|
||||||
var scores: ScoreSet
|
let dice = randomDice(r)
|
||||||
for i in 1 .. nTests:
|
var b: Board
|
||||||
let b = newRandomGame(r)
|
b.init
|
||||||
let dur = executionTime:
|
b.setState(dice, [])
|
||||||
let s = b.randomGames(nSamples, parallel = parallel)
|
b.display(1, 5)
|
||||||
result.ops += s.sum()
|
|
||||||
result.time += dur
|
let startTime = cpuTime()
|
||||||
|
let scores = b.randomGames(n)
|
||||||
|
result = cpuTime() - startTime
|
||||||
|
scores.display()
|
||||||
|
|
||||||
|
|
||||||
proc testLegs(n: Natural = 100): auto =
|
proc testLegs(n: Natural = 100): auto =
|
||||||
@ -94,4 +72,24 @@ proc testSpread(nTests, nSamples: Natural) =
|
|||||||
|
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
games(10, 10_000_000).summarize()
|
randomize()
|
||||||
|
var r = initRand(rand(int64))
|
||||||
|
let b = newRandomGame(r)
|
||||||
|
b.display(1, 5)
|
||||||
|
echo b.showSpaces(1, 16)
|
||||||
|
|
||||||
|
let scores = b.getLegScores
|
||||||
|
echo scores.showPercents
|
||||||
|
# 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
|
||||||
|
|
||||||
|
# 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 ""
|
||||||
|
|
||||||
|
# testSpread(100, 1_000_000)
|
||||||
|
Reference in New Issue
Block a user