PraxTube / chess-ai

A chess AI that uses alpha-beta to find the best move
MIT License
0 stars 1 forks source link

Engine: Remove class CastleRights #8

Closed PraxTube closed 1 year ago

PraxTube commented 1 year ago

The caste rights are currently stored in a class. This is extremely unnecessary boilerplate and awkward to use. Instead, we could use a numpy.array of shape (,4) that stores booleans. The changes that would need to be made are also only inside of the chess_engine.py file.

PraxTube commented 1 year ago

Using a numpy.array is actually slower then using a python build-in list. This is actually not too surprising, given that we have an extremely small array size, but it's still important to note. Here are the benchmarks to back it up.

Numpy castling rights, benchmark.csv

Category,Total,Average,Valid Moves,Fen
Fen Conversion,199.54,19.95,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Legal Move Gen,157.64,157.64,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Making Move,57.06,5.71,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Evaluate Board,211.88,211.88,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Best Move Gen,0.88,8.84,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Fen Conversion,222.51,22.25,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Legal Move Gen,634.53,634.53,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Making Move,62.65,6.26,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Evaluate Board,220.74,220.74,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Best Move Gen,1.08,10.78,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Fen Conversion,114.68,11.47,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Legal Move Gen,187.53,187.53,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Making Move,55.68,5.57,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Evaluate Board,178.01,178.01,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Best Move Gen,0.29,2.86,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1

List castling rights, benchmark.csv

Category,Total,Average,Valid Moves,Fen
Fen Conversion,163.73,16.37,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Legal Move Gen,147.23,147.23,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Making Move,47.08,4.71,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Evaluate Board,201.61,201.61,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Best Move Gen,0.83,8.3,20,rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
Fen Conversion,185.75,18.57,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Legal Move Gen,599.95,599.95,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Making Move,46.52,4.65,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Evaluate Board,207.93,207.93,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Best Move Gen,1.01,10.14,46,r3k2r/ppp1bppp/b1n2n2/1N2B3/2B1q3/2Q1PN2/PPP2PPP/R3K2R b KQkq - 0 1
Fen Conversion,94.55,9.46,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Legal Move Gen,175.72,175.72,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Making Move,46.63,4.66,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Evaluate Board,172.17,172.17,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1
Best Move Gen,0.27,2.75,7,4k3/8/8/3PP3/3pp3/8/8/3K4 w - - 0 1

Numpy castling rights, benchmark_depth.txt

=========
Benchmark best move for depth 1, number of iterations 500
=========

Total: 4.1885s, Average: 8.38ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (20, []))]

=========
Benchmark best move for depth 2, number of iterations 50
=========

Total: 8.3535s, Average: 167.07ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (420, ['b8a6']))]

=========
Benchmark best move for depth 3, number of iterations 20
=========

Total: 42.3421s, Average: 2117.11ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (1303, ['b8a6', 'b1a3']))]

=========
Benchmark best move for depth 4, number of iterations 5
=========

Total: 38.5728s, Average: 7714.57ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (10672, ['b8a6', 'b1a3', 'g8f6']))]

=========
Benchmark best move for depth 5, number of iterations 1
=========

Total: 56.4787s, Average: 56478.66ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('h3', (34688, ['b8a6', 'b1a3', 'g8f6', 'g1f3']))]

List castling rights, benchmark_depth.txt

=========
Benchmark best move for depth 1, number of iterations 500
=========

Total: 4.0459s, Average: 8.09ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (20, []))]

=========
Benchmark best move for depth 2, number of iterations 50
=========

Total: 7.9419s, Average: 158.84ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (420, ['b8a6']))]

=========
Benchmark best move for depth 3, number of iterations 20
=========

Total: 40.2694s, Average: 2013.47ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (1303, ['b8a6', 'b1a3']))]

=========
Benchmark best move for depth 4, number of iterations 5
=========

Total: 36.5924s, Average: 7318.48ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('Nh3', (10672, ['b8a6', 'b1a3', 'g8f6']))]

=========
Benchmark best move for depth 5, number of iterations 1
=========

Total: 53.795s, Average: 53795.01ms - 20 - rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
[('h3', (34688, ['b8a6', 'b1a3', 'g8f6', 'g1f3']))]