#JULIALANG AND SHOGI(JAPANESE CHESS)
twitter: @kimrin	

Takeshi KIMURA
IN THIS PRESENTATION:
!

About us	

Why we choose #Julialang	

Shogi and Chess: differences between two games	

principles and technics of Shogi playing software
ABOUT US:

Mecha Lady Shogi team (twitter = @mechajyo, 5 members):	

the team of Shogi Program developers	

with 2 Lady Shogi Professionals, 2 ladies and a geek:)	

developing Japanese Chess Program written in #Julialang !
WHY WE CHOOSE #JULIALANG:
Speed	

most other Shogi programs are written in C/C++	

needed H/W resources: CPU and Memory access (not HDD
access)	

Maintenance	

dynamic language like code style is very useful for developing
Shogi programs
CHESS:
Chess:	

8x8=64 squares	

about 80 available moves in
middle game	

computer programs already
won by human professionals
SHOGI
Shogi	

9x9 = 81 squares	

can reuse captured pieces	

about160 available moves in
endgame	

most of pieces can promote in
enemy’s field	

computer programs NOT YET
win by human professionals!
PRINCIPLE AND TECHNICS IN
SHOGI PLAYING SOFTWARE

Alpha-Beta search	

bit board and magic board technics	

machine-learning evaluation functions
ALPHA-BETA SEARCH
problem:	

in the given Shogi board representation, find best move from
its available moves	

solution:	

search partial tree of game tree and find a best move and root
node’s tree value(Evaluation Value)	

Depth-first search by using a recursive function
function AlphaBeta( gs::GameStatus, WB, depth, alpha, beta)

if depth <= 0.0 

va = eval(gs, alpha, beta)

return va

end



moves = generate(gs)



for i = 1:length(moves)

makeMove( gs, moves[i], WB)



val = -AlphaBeta( gs, WB$1, depth-1.0, -beta, -alpha)



takeBack( gs, moves[i], WB)



if val > alpha # alpha-update

alpha = val



# save this move



if alpha >= beta # beta-cutoff

return beta

end

end

end

alpha

end


return eval	

(leaf node)
generate moves

(good move first)

pseudo code	

(simplified)

recursive

calls

find good moves
cutoff redundant

search
PSEUDO CODE SUMMARY
If node is leaf node, return the value of evaluation function	

Generate child node moves(=available moves)	

For each moves:	

Make move	

Call myself(=this function) in NegaMax manner	

Take back move	

If it’s good move, record this move(=Alpha Update)	

if very good move, cutoff subsequent child node’s search(Beta Cutoff)	

return beta	

return alpha
BITBOARD TECHNICS	

(IN MOVE GENERATION)
In chess programs, board representation is 64bit Unsigned Integer
value: each bit represents existence of pieces	

Pawn, Knight,…, King, for each piece kinds, there are 2 bitmap
values(one for White, another for Black)	

In Shogi programs, square of the board is 81. So in ordinal
programming languages(such as C++), we use 3x32bit Unsigned
Integer = 96bit bitmap values	

#Julialang’s 128bit Unsigned Integer is suitable for storing 81bit Shogi
bitmaps!
BASIC IDEAS OF BITBOARD
for each pieces of white side(for example: White King):	

get piece position that of move from	

dest = BitMapTable( piece, from, White) # available moves table	

dest &= (not White pieces) # with single bitwise AND!	

for each dest bits:	

generates Move and stores move buffers
SOURCE CODE OF WHITE KING
target = (~p.WhitePieces) & MaskOfBoard

!
bbp = p.bb[MJOU] #White King’s bitmap
while bbp > uint128(0)
from = trailing_zeros(bbp)
bbp $= BitSet[from+1]
dest::BitBoard = target & gs.AttackTableNonSlide[MJOU,from+1]
toru::BitBoard = dest & p.BlackPieces #captured pieces
while dest > uint128(0)
to = trailing_zeros(dest)
dest $= BitSet[to+1]
toriflag = ((toru & BitSet[to+1]) > 0)?FLAG_TORI:0
count += 1
out[count] = Move(MJOU,from,to,toriflag,p.square[to+1]&0x0f,0)::Move
InsertMoveAB(count,out,gs)
end
end
MJOU = White King(OU,王)	

tori/toru = capture	

!

Magic board (More advanced technics):	

calculate dest bitmap by multiples MAGIC numbers(one
direction hash)
MACHINE LEARNING

EVALUATION FUNCTIONS
This technic is established by Hoki-san’s “Bonanza” program	

Evaluation function value = dot(WeightVector, FeatureVector)	

Val = w1*f1 + w2*f2 + … + wn*fn	

Hoki-san established the way of learning Weight Vectors by Machine Learning(reinforcement
learning)	

King - OpponentKing - another piece relationships	

King - piece - and another piece relationships	

(Three pieces relationships)	

We use Hoki-san’s fv.bin for calculation function values
CONCLUSION:
We will attend WCSC24 (The 24th World Computer Shogi Championship)	

Mecha Lady Shogi (メカ女子将棋, @mechajyo) is using #Julialang

for her programming language	

メカ=Mecha, 女子=Girl, Lady	

But actually, playing ability of mechajyo is not so strong :(	

We will also attend DenOu Sen tournament (電王戦トーナメント) may will be hold in Nov. 2014	

電王=Electrical King	

Thank you!

#Julialang and Computer Shogi (Japanese Chess)

  • 1.
    #JULIALANG AND SHOGI(JAPANESECHESS) twitter: @kimrin Takeshi KIMURA
  • 2.
    IN THIS PRESENTATION: ! Aboutus Why we choose #Julialang Shogi and Chess: differences between two games principles and technics of Shogi playing software
  • 3.
    ABOUT US: Mecha LadyShogi team (twitter = @mechajyo, 5 members): the team of Shogi Program developers with 2 Lady Shogi Professionals, 2 ladies and a geek:) developing Japanese Chess Program written in #Julialang !
  • 4.
    WHY WE CHOOSE#JULIALANG: Speed most other Shogi programs are written in C/C++ needed H/W resources: CPU and Memory access (not HDD access) Maintenance dynamic language like code style is very useful for developing Shogi programs
  • 5.
    CHESS: Chess: 8x8=64 squares about 80available moves in middle game computer programs already won by human professionals
  • 6.
    SHOGI Shogi 9x9 = 81squares can reuse captured pieces about160 available moves in endgame most of pieces can promote in enemy’s field computer programs NOT YET win by human professionals!
  • 7.
    PRINCIPLE AND TECHNICSIN SHOGI PLAYING SOFTWARE Alpha-Beta search bit board and magic board technics machine-learning evaluation functions
  • 8.
    ALPHA-BETA SEARCH problem: in thegiven Shogi board representation, find best move from its available moves solution: search partial tree of game tree and find a best move and root node’s tree value(Evaluation Value) Depth-first search by using a recursive function
  • 9.
    function AlphaBeta( gs::GameStatus,WB, depth, alpha, beta) if depth <= 0.0 va = eval(gs, alpha, beta) return va end moves = generate(gs) for i = 1:length(moves) makeMove( gs, moves[i], WB) val = -AlphaBeta( gs, WB$1, depth-1.0, -beta, -alpha) takeBack( gs, moves[i], WB) if val > alpha # alpha-update alpha = val # save this move if alpha >= beta # beta-cutoff return beta end end end alpha end return eval (leaf node) generate moves
 (good move first) pseudo code (simplified) recursive
 calls find good moves cutoff redundant
 search
  • 10.
    PSEUDO CODE SUMMARY Ifnode is leaf node, return the value of evaluation function Generate child node moves(=available moves) For each moves: Make move Call myself(=this function) in NegaMax manner Take back move If it’s good move, record this move(=Alpha Update) if very good move, cutoff subsequent child node’s search(Beta Cutoff) return beta return alpha
  • 11.
    BITBOARD TECHNICS (IN MOVEGENERATION) In chess programs, board representation is 64bit Unsigned Integer value: each bit represents existence of pieces Pawn, Knight,…, King, for each piece kinds, there are 2 bitmap values(one for White, another for Black) In Shogi programs, square of the board is 81. So in ordinal programming languages(such as C++), we use 3x32bit Unsigned Integer = 96bit bitmap values #Julialang’s 128bit Unsigned Integer is suitable for storing 81bit Shogi bitmaps!
  • 12.
    BASIC IDEAS OFBITBOARD for each pieces of white side(for example: White King): get piece position that of move from dest = BitMapTable( piece, from, White) # available moves table dest &= (not White pieces) # with single bitwise AND! for each dest bits: generates Move and stores move buffers
  • 13.
    SOURCE CODE OFWHITE KING target = (~p.WhitePieces) & MaskOfBoard ! bbp = p.bb[MJOU] #White King’s bitmap while bbp > uint128(0) from = trailing_zeros(bbp) bbp $= BitSet[from+1] dest::BitBoard = target & gs.AttackTableNonSlide[MJOU,from+1] toru::BitBoard = dest & p.BlackPieces #captured pieces while dest > uint128(0) to = trailing_zeros(dest) dest $= BitSet[to+1] toriflag = ((toru & BitSet[to+1]) > 0)?FLAG_TORI:0 count += 1 out[count] = Move(MJOU,from,to,toriflag,p.square[to+1]&0x0f,0)::Move InsertMoveAB(count,out,gs) end end
  • 14.
    MJOU = WhiteKing(OU,王) tori/toru = capture ! Magic board (More advanced technics): calculate dest bitmap by multiples MAGIC numbers(one direction hash)
  • 15.
    MACHINE LEARNING
 EVALUATION FUNCTIONS Thistechnic is established by Hoki-san’s “Bonanza” program Evaluation function value = dot(WeightVector, FeatureVector) Val = w1*f1 + w2*f2 + … + wn*fn Hoki-san established the way of learning Weight Vectors by Machine Learning(reinforcement learning) King - OpponentKing - another piece relationships King - piece - and another piece relationships (Three pieces relationships) We use Hoki-san’s fv.bin for calculation function values
  • 16.
    CONCLUSION: We will attendWCSC24 (The 24th World Computer Shogi Championship) Mecha Lady Shogi (メカ女子将棋, @mechajyo) is using #Julialang
 for her programming language メカ=Mecha, 女子=Girl, Lady But actually, playing ability of mechajyo is not so strong :( We will also attend DenOu Sen tournament (電王戦トーナメント) may will be hold in Nov. 2014 電王=Electrical King Thank you!