6 Commits

10 changed files with 321 additions and 279 deletions

View File

@ -1,17 +0,0 @@
type
Color* = enum
cRed, cGreen, cBlue, cYellow, cPurple
const
colorNames: array[Color, string] =
["Red", "Green", "Blue", "Yellow", "Purple"]
colorAbbrevs: array[Color, char] = ['R', 'G', 'B', 'Y', 'P']
proc `$`*(c: Color): string =
result = colorNames[c]
proc abbrev*(c: Color): char =
result = colorAbbrevs[c]

View File

@ -1,5 +1,5 @@
import algorithm, random, sugar
import fixedseq, game
import faststack, fixedseq, game
proc nextPermutation(x: var FixedSeq): bool =

View File

@ -1,5 +1,5 @@
--threads: on
--d: release
--opt: speed
--passC: -flto
--passL: -flto
# --threads: on
# --d: release
# --opt: speed
# --passC: -flto
# --passL: -flto

204
faststack.nim Normal file
View File

@ -0,0 +1,204 @@
import bitops, strutils, random
proc show(i: SomeInteger, bitlength = 16) =
echo BiggestInt(i).toBin(bitlength)
type
Color* = enum
cRed, cGreen, cBlue, cYellow, cPurple
ColorStack* = object
data: uint16
len*: uint8
const
masks = [
0'u16, # dummy value just to get the indices right
0b0_000_000_000_000_111,
0b0_000_000_000_111_111,
0b0_000_000_111_111_111,
0b0_000_111_111_111_111,
0b0_111_111_111_111_111,
]
allColors* = ColorStack(len: 5, data: 0b0_000_001_010_011_100)
template offset(s: ColorStack, idx: Natural): uint8 =
# Compute the bit offset for a given index.
# Dependent on the stack's length.
(s.len - 1 - idx.uint8) * 3
# items are stored from left to right but right-aligned, so we
# need to shift right to access anything other than the last
template offset(s: ColorStack, idx: BackwardsIndex): uint8 =
# for backwards index, we are still shifting right but
# the lower the index the less we have to shift
(idx.uint8 - 1) * 3
proc add*(s: var ColorStack, c: Color) =
# e.g. if stack is 0b0000000000000100:
# and color is 0b00000011
# shift: 0b0000000000100000
# bitor: 0b0000000000100000 and 0b00000011
# results in 0b0000000000100011
s.data = (s.data shl 3).bitor(cast[uint8](c))
inc s.len
proc high*(s: ColorStack): uint8 = s.len - 1
proc low*(s: ColorStack): uint8 = 0 # just... always 0, I guess
proc `[]`*(s: ColorStack, i: uint8 | BackwardsIndex): Color =
# shift, then mask everything but the three rightmost bits
result = Color(
(s.data shr s.offset(i)) and masks[1]
)
proc `[]=`*(s: var ColorStack, i: uint8 | BackwardsIndex, c: Color) =
let offset = s.offset(i)
s.data = (s.data and bitnot(masks[1] shl offset)) or (c.uint16 shl offset)
iterator items*(s: ColorStack): Color =
# s.len is unsigned so it will wrap around if we do s.len - 1 in that case
if s.len != 0:
for i in countdown(s.len - 1, 0'u8):
yield Color((s.data shr (i * 3)) and masks[1])
iterator pairs*(s: ColorStack): (uint8, Color) =
var count = 0'u8
for color in s:
yield (count, color)
inc count
proc find*(s: ColorStack, needle: Color): int8 =
for i in 0'u8 .. s.high:
if s[i] == needle:
return i.int8
return -1
proc moveSubstack*(src, dst: var ColorStack, startIdx: uint8) =
if startIdx >= src.len:
raise newException(IndexDefect, "index " & $startIdx & " is out of bounds.")
# Moves a sub-stack from the top of src to the top of dst
# shift the dst stack by the length of the substack to make room
let nToMove = src.len - startIdx
let shift = nToMove * 3
dst.data = dst.data shl shift
# then we mask the source data to present only the items
# being moved, and OR that with the shifted dst data
dst.data = dst.data or (src.data and masks[nToMove])
dst.len += nToMove
# then we shift the source to get rid of the moved items
src.data = src.data shr shift
src.len -= nToMove
proc moveSubstackPre*(src, dst: var ColorStack, startIdx: uint8) =
if startIdx >= src.len:
raise newException(IndexDefect, "index " & $startIdx & " is out of bounds.")
# Moves a sub-stack from the top of src to the bottom of dst
let nToMove = src.len - startIdx
# shift src to position the substack above its destination,
# get rid of everything to the left of the substack,
# and OR that with the existing dst data
let newLen = dst.len + nToMove
dst.data = dst.data or ( (src.data shl (dst.len * 3)) and masks[newLen] )
dst.len = newLen
# get rid of the substack we just moved
src.data = src.data shr (nToMove * 3)
src.len -= nToMove
proc swap*(s: var ColorStack, i1, i2: uint8) =
# Swap the values at two indices in the stack
if i1 == i2: return
# i1 and i2 are unsigned, so we have to watch out for underflows
let diff = if i1 > i2:
(i1 - i2) * 3
else:
(i2 - i1) * 3
# take masks[1] from above (rightmost position) and shift to position of i1.
# then do the same for i2, and OR them together.
let mask = (masks[1] shl s.offset(i1)) or (masks[1] shl s.offset(i2))
# get rid of everything but the two values we're swapping
let masked = s.data and mask
# shift by the distance between values in both directions, combine, then mask
let swapped = ((masked shl diff) or (masked shr diff)) and mask
# finally, AND with the inverse of mask so that only the values being
# swapped are erased, and combine that with the swapped values
s.data = (s.data and mask.bitnot) or swapped
proc shuffle*(r: var Rand, s: var ColorStack) =
# Fisher-Yates shuffle
for i in countdown(s.high, 1'u8):
let j = r.rand(i).uint8
if j != i:
s.swap(i, j)
proc reverse*(s: var ColorStack, first, last: uint8) =
var x = first
var y = last
while x < y:
s.swap(x, y)
inc x
dec y
iterator asInt*(s: ColorStack): int8 =
for i in 0'u8 .. s.high:
yield int8(s[i]) # now we do have to convert
proc `$`*(s: ColorStack): string =
result = "St@["
for c in s:
if result[^1] != '[':
result.add(", ")
result.add($c)
result.add("]")
proc check(s: ColorStack) =
# ensure length is accurate
var d = s.data
for i in 0'u8 .. 4'u8:
if (d and masks[1]) > 4:
raise newException(RangeDefect, "Value out of range.")
if d > 0 and i >= s.len:
raise newException(RangeDefect, "Invalid length.")
else:
d = d shr 3
when isMainModule:
var one: ColorStack
one.add(cRed)
one.add(cGreen)
one.add(cBlue)
one.add(cYellow)
one.add(cPurple)
var two: ColorStack
one.moveSubstack(two, 2)
echo one, " ", one.len
echo two, " ", two.len
echo two.find(cRed)

View File

@ -25,16 +25,6 @@ proc `$`*(s: FixedSeq): string =
result.add("]")
proc `==`*[T1: FixedSeq, T2: FixedSeq](a: T1, b: T2): bool =
# generics are so that we can compare ShiftStack vs regular FixedSeq
if a.len != b.len:
return false
for i in 0 ..< a.len:
if a.data[i] != b.data[i]:
return false
return true
proc `[]`*(s: FixedSeq, i: Natural): FixedSeq.Contents =
if i > s.last:
raise newException(IndexDefect, "index " & $i & " is out of bounds.")
@ -134,7 +124,7 @@ proc shuffle*(s: var FixedSeq, r: var Rand) =
proc moveSubstack*(src, dst: var FixedSeq; start: Natural) =
var count: FixedSeq.Pointer = 0 # have to track this separately apparently
var count: typeof(src.last) = 0 # have to track this separately apparently
for idx in start .. src.last:
swap(src.data[idx], dst.data[dst.last + 1 + count])
inc count
@ -143,7 +133,7 @@ proc moveSubstack*(src, dst: var FixedSeq; start: Natural) =
proc moveSubstackPre*(src, dst: var FixedSeq; start: Natural) =
let ssLen = FixedSeq.Pointer(src.last - start + 1) # length of substack
let ssLen = typeof(src.last)(src.last - start + 1) # length of substack
for i in countdown(dst.last, 0):
swap(dst.data[i], dst.data[i + ssLen])
@ -154,5 +144,3 @@ proc moveSubstackPre*(src, dst: var FixedSeq; start: Natural) =
dst.last += ssLen
src.last -= ssLen
include shiftstack

View File

@ -1,17 +1,40 @@
import hashes, options
import fixedseq, colors
export colors
import fixedseq, faststack
export faststack.Color, faststack.ColorStack, faststack.allColors
type
ColorStack* = FixedSeq[5, Color, int8]
# type
# Color* = enum
# cRed, cGreen, cBlue, cYellow, cPurple
# ColorStack* = FixedSeq[5, Color, int8]
proc initColorStack*: ColorStack =
result.initFixedSeq
# proc initColorStack*: ColorStack =
# result.initFixedSeq
proc `$`*[T](s: FixedSeq[T, Color, int8]): string =
# proc getAllColors: ColorStack =
# var i = 0'u8
# for c in Color.low .. Color.high:
# result[i] = c
const
colorNames: array[Color, string] =
["Red", "Green", "Blue", "Yellow", "Purple"]
colorAbbrevs: array[Color, char] = ['R', 'G', 'B', 'Y', 'P']
proc `$`*(c: Color): string =
result = colorNames[c]
proc abbrev*(c: Color): char =
result = colorAbbrevs[c]
proc `$`*(s: ColorStack): string =
result.add("St@[")
for i, color in s:
result.add($color)
@ -28,7 +51,7 @@ type
tForward = 1
Square* = object
camels*: ShiftStack
camels*: ColorStack
tile*: Option[Tile]
Board* = object
@ -60,8 +83,8 @@ proc hash*(b: Board): Hash =
proc init*(b: var Board) =
for sq in b.squares.mitems:
sq.camels.initFixedSeq
# for sq in b.squares.mitems:
# sq.camels.initFixedSeq
b.initialized = true
@ -90,7 +113,7 @@ proc setState*(b: var Board;
b.leader = some(leadCamel)
proc diceRemaining*(b: Board): ColorStack =
proc diceRemaining*(b: Board): FixedSeq[5, Color, int8] =
result.initFixedSeq
for color, isRolled in b.diceRolled:
if not isRolled: result.add(color)
@ -118,11 +141,11 @@ proc advance*(b: var Board, die: Die) =
endPos += int(t)
if t == tBackward: prepend = true
let stackStart = b[startPos].camels.find(color)
let stackStart = b[startPos].camels.find(color).uint8
if prepend:
b[startPos].camels.moveSubstackPre(b[endPos].camels, stackStart)
let stackLen = b[startPos].camels.len - stackStart
for i in 0 ..< stackLen:
for i in 0'u8 ..< stackLen:
# we know how many camels we added to the bottom, so set the position for each of those
b.camels[b[endPos].camels[i]] = endPos
else:
@ -136,4 +159,4 @@ proc advance*(b: var Board, die: Die) =
if endPos >= b.camels[b.leader.get]:
b.leader = some(b[endPos].camels[^1])
b.diceRolled[color] = true
b.diceRolled[color] = true

View File

@ -1,191 +0,0 @@
# optimized bit-shifting versions of the FixedSequence substack operations
import bitops, macros
import colors
macro show(expr: untyped) =
let node = expr.toStrLit
quote do:
echo `node`, " => ", `expr`
proc getMasks(): (array[9, uint64], array[9, uint64]) =
# on little-endian architectures, casting an array[8, Color] to uint64 effectively
# reverses it. So we switch these masks so that we can refer to them consistently.
let
left = [
0'u64,
0xff_00_00_00_00_00_00_00'u64,
0xff_ff_00_00_00_00_00_00'u64,
0xff_ff_ff_00_00_00_00_00'u64,
0xff_ff_ff_ff_00_00_00_00'u64,
0xff_ff_ff_ff_ff_00_00_00'u64,
0xff_ff_ff_ff_ff_ff_00_00'u64,
0xff_ff_ff_ff_ff_ff_ff_00'u64,
0xff_ff_ff_ff_ff_ff_ff_ff'u64,
]
right = [
0'u64,
0x00_00_00_00_00_00_00_ff'u64,
0x00_00_00_00_00_00_ff_ff'u64,
0x00_00_00_00_00_ff_ff_ff'u64,
0x00_00_00_00_ff_ff_ff_ff'u64,
0x00_00_00_ff_ff_ff_ff_ff'u64,
0x00_00_ff_ff_ff_ff_ff_ff'u64,
0x00_ff_ff_ff_ff_ff_ff_ff'u64,
0xff_ff_ff_ff_ff_ff_ff_ff'u64,
]
when cpuEndian == bigEndian:
result = (left, right)
when cpuEndian == littleEndian:
result = (right, left)
type ShiftStack* = FixedSeq[8, Color, int8]
const (masksLeft, masksRight) = getMasks()
template `shl`(a: array[8, Color], offset: Natural): array[8, Color] =
when cpuEndian == bigEndian:
cast[array[8, Color]](cast[uint64](a) shl (offset * 8))
when cpuEndian == littleEndian: # direction is reversed
cast[array[8, Color]](cast[uint64](a) shr (offset * 8))
template `shr`(a: array[8, Color], offset: Natural): array[8, Color] =
when cpuEndian == bigEndian:
cast[array[8, Color]](cast[uint64](a) shr (offset * 8))
when cpuEndian == littleEndian:
cast[array[8, Color]](cast[uint64](a) shl (offset * 8))
template `and`(a: array[8, Color], mask: uint64): array[8, Color] =
cast[array[8, Color]](cast[uint64](a) and mask)
template `or`(a: array[8, Color], mask: uint64): array[8, Color] =
cast[array[8, Color]](cast[uint64](a) or mask)
template `or`(a: array[8, Color], mask: array[8, Color]): array[8, Color] =
cast[array[8, Color]](cast[uint64](a) or cast[uint64](mask))
import strutils # remove later
proc moveSubstack*(src, dst: var ShiftStack; start: Natural) =
# shift the source stack to position the substack above its final resting place
# offset is the length of the destination stack, minus the number of items NOT being moved
# number of items not being moved is the same as the start index
var substack: array[8, Color]
if dst.len == start: # no shift necessary in this case
substack = src.data
elif dst.len > start:
substack = src.data shr (dst.len - start)
elif dst.len < start:
substack = src.data shl (start - dst.len)
# next, mask the source data to present only the items being moved
# dst.len of 0 corresponds to last mask in masksRight, aka masksRight[^1]
substack = substack and masksRight[^(dst.len + 1)]
# then combine
dst.data = dst.data or substack
# then git rid of the moved items from the source stack
src.data = src.data and masksLeft[start]
# a little bookkeeping
let ssLen = int8(src.len - start)
src.last -= ssLen
dst.last += ssLen
proc moveSubstackPre*(src, dst: var ShiftStack; start: Natural) =
let ssLen = int8(src.len - start)
# shift the destination stack to make room for the new items
dst.data = dst.data shr ssLen
# shift source stack to line up the substack with its final resting place
let substack = src.data shl start
# combine
dst.data = dst.data or substack
# get rid of the moved items
src.data = src.data and masksLeft[start]
# more bookkeeping
src.last -= ssLen
dst.last += ssLen
proc testMove[T1, T2: FixedSeq](a1, a2: var T1; b1, b2: var T2; i: Natural): bool =
let (orig_a1, orig_a2) = (a1, a2)
let (orig_b1, orig_b2) = (b1, b2)
a1.moveSubstack(a2, i)
b1.moveSubstack(b2, i)
if a1 != b1 or a2 != b2:
echo "Failed!"
show orig_b1
show orig_b2
echo "<<move ", i, ">>"
show b1
show b2
return false
return true
when isMainModule:
var c1 = initFixedSeq(5, Color, int8)
var c2 = initFixedSeq(5, Color, int8)
var s1: ShiftStack
s1.initFixedSeq
var s2: ShiftStack
s2.initFixedSeq
c1.add(cPurple)
c1.add(cRed)
c1.add(cYellow)
c1.add(cBlue)
c1.add(cGreen)
s1.add(cPurple)
s1.add(cRed)
s1.add(cYellow)
s1.add(cBlue)
s1.add(cGreen)
# show s1
# show s2
# echo "<<move 2>>"
# s1.moveSubstack(s2, 2)
# show s1
# show s2
import random
randomize()
var r = initRand(rand(int64))
var success = true
for n in 1 .. 1_000_000:
var ranFirst, ranSecond: bool
if c1.len > 0:
let i = r.rand(c1.high)
ranFirst = true
if not testMove(c1, c2, s1, s2, i):
success = false
echo "Failed after ", n, " iterations."
break
else:
ranFirst = false
if c2.len > 0:
let j = r.rand(c2.high)
ranSecond = true
if not testMove(c2, c1, s2, s1, j):
success = false
echo "Failed after ", n, " iterations."
break
else:
ranSecond = false
if (not ranFirst) and (not ranSecond):
echo "Ran neither first nor second move."
break
if success:
echo "Success."

View File

@ -1,5 +1,5 @@
import cpuinfo, math, options, random, tables
import combinators, game, fixedseq
import combinators, game, faststack, fixedseq
type
@ -38,7 +38,7 @@ proc percents*(scores: ScoreSet): WinPercents =
# ======================
iterator legEndStates(b: Board): Board =
var diceRemaining: ColorStack
var diceRemaining: FixedSeq[5, Color, int8]
diceRemaining.initFixedSeq
for i, c in b.diceRolled:
if not c: diceRemaining.add(i)
@ -59,6 +59,39 @@ proc getLegScores*(b: Board): ScoreSet =
# Full-game simulations
# =====================
# get rid of this later
import strutils
proc showSpaces*(b: Board; start, stop: Natural): string =
let numSpaces = stop - start + 1
let width = 4 * numSpaces - 1
var lines: array[7, string]
# start by building up an empty board
for i in 0 .. 6: # gotta initialize the strings
lines[i] = newString(width)
for c in lines[i].mitems:
c = ' '
# fill in the dividers
lines[5] = repeat("=== ", numSpaces - 1)
lines[5].add("===")
# now populate the board
for sp in 0 ..< numSpaces:
# fill in the square numbers
let squareNum = sp + start
let cellMid = 4 * sp + 1
for i, chr in $squareNum:
lines[6][cellMid + i] = chr
# fill in the camel stacks
for i, color in b.squares[squareNum].camels:
let lineNum = 4 - i # lines go to 6, but bottom 2 are reserved
let repr = '|' & color.abbrev & '|'
for j, chr in repr:
lines[lineNum][cellMid - 1 + j] = chr
result = lines.join("\n")
# get rid of this later
proc randomGame*(b: Board, r: var Rand): Color =
var projection = b
while true:

View File

@ -1,7 +1,32 @@
import math, random, strformat, times
import math, random, strformat, times, std/monotimes
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]] =
for c in Color:
let v = r.rand(1..3)
@ -18,18 +43,15 @@ proc newRandomGame(r: var Rand): Board =
result.setState(dice, [])
proc testGames(n: SomeInteger = 100): auto =
var r = initRand(rand(int64))
let dice = randomDice(r)
var b: Board
b.init
b.setState(dice, [])
b.display(1, 5)
let startTime = cpuTime()
let scores = b.randomGames(n, parallel = true)
result = cpuTime() - startTime
scores.display()
proc games(nTests, nSamples: SomeInteger, parallel = true): TestResults =
var r = getRand()
var scores: ScoreSet
for i in 1 .. nTests:
let b = newRandomGame(r)
let dur = executionTime:
let s = b.randomGames(nSamples, parallel = parallel)
result.ops += s.sum()
result.time += dur
proc testLegs(n: Natural = 100): auto =
@ -72,24 +94,4 @@ proc testSpread(nTests, nSamples: Natural) =
when isMainModule:
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)
games(10, 10_000_000).summarize()

2
ui.nim
View File

@ -1,5 +1,5 @@
import os, math, strutils, strformat
import fixedseq, game, simulation
import faststack, fixedseq, game, simulation
const help =