Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Engine.play fails and crashes with CrazyAra (UCI) #627

Closed
shubhank008 opened this issue Sep 7, 2020 · 12 comments
Closed

Engine.play fails and crashes with CrazyAra (UCI) #627

shubhank008 opened this issue Sep 7, 2020 · 12 comments
Labels
engine Chess engine integration

Comments

@shubhank008
Copy link

Using the example code from the docs for CrazyAra engine (UCI support) crashes the python script as soon as engine.play is called.

import chess
import chess.engine

engine = chess.engine.SimpleEngine.popen_uci("/root/CrazyAra_0.8.0_Linux_MKL/CrazyAra")
## For CPU, comment if want to use GPU
engine.configure({"Context": "cpu"})

board = chess.Board()
while not board.is_game_over():
	result = engine.play(board, chess.engine.Limit(time=0.1))
	board.push(result.move)

engine.quit()

Error:

python3.7 pychess.py
Traceback (most recent call last):
  File "pychess.py", line 10, in <module>
    result = engine.play(board, chess.engine.Limit(time=0.1))
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 2386, in play
    return future.result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 435, in result
    return self.__get_result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1316, in play
    return await self.communicate(UciPlayCommand)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 768, in communicate
    return await command.result
chess.engine.EngineTerminatedError: engine process died unexpectedly (exit code: -11)

Running and use other manual commands like board, print(board), push_move, legal_moves, etc. still works just trying to do a engine vs engine play fails.

@niklasf niklasf added the engine Chess engine integration label Sep 7, 2020
@niklasf
Copy link
Owner

niklasf commented Sep 7, 2020

Can you please include debug logs? They might contain a hint.

@shubhank008
Copy link
Author

Here it is:

root@Ubuntu-1804-bionic-64-minimal ~ # python3.7 pychess.py
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:chess.engine:<UciProtocol (pid=31999)>: Connection made
DEBUG:chess.engine:<UciProtocol (pid=31999)>: << uci
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>                                   _
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>                    _..           /   ._   _.  _        /\   ._   _.
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>                  .' _ `\         \_  |   (_|  /_  \/  /--\  |   (_|
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>                 /  /e)-,\                         /
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>                /  |  ,_ |                    __    __    __    __
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>               /   '-(-.)/          bw     8 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>             .'--.   \  `                 7 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>            /    `\   |                  6 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>          /`       |  / /`\.-.          5 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>        .'        ;  /  \_/__/         4 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>      .'`-'_     /_.'))).-` \         3 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>     / -'_.'---;`'-))).-'`\_/        2 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>    (__.'/   /` .'`                 1 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>     (_.'/ /` /`                       a  b  c  d  e  f  g  h
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>       _|.' /`
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> jgs.-` __.'|  Developers: Johannes Czech, Moritz Willig, Alena Beyer
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>     .-'||  |  Source-Code: QueensGambit/CrazyAra (GPLv3-License)
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>        \_`/   Inspiration: A0-paper by Silver, Hubert, Schrittwieser et al.
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>               ASCII-Art: Joan G. Stark, Chappell, Burton
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >>
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> id name CrazyAra 0.8.0 (May 16 2020)
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> id author Johannes Czech, Moritz Willig, Alena Beyer et al.
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name UCI_Variant type combo default crazyhouse var crazyhouse
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Search_Type type combo default mcts var mcts
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Context type combo default cpu var cpu
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name First_Device_ID type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Last_Device_ID type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Batch_Size type spin default 16 min 1 max 8192
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Threads type spin default 2 min 1 max 512
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_CPuct_Init type spin default 250 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name CPuct_Base type spin default 19652 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Dirichlet_Epsilon type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Dirichlet_Alpha type spin default 20 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_U_Init_Divisor type spin default 100 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Q_Value_Weight type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Q_Thresh_Init type spin default 50 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Q_Thresh_Max type spin default 90 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Q_Thresh_Base type spin default 1965 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Max_Search_Depth type spin default 99 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Temperature type spin default 170 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Temperature_Moves type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Quantile_Clipping type spin default 25 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Temperature_Decay type spin default 92 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Node_Temperature type spin default 170 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Virtual_Loss type spin default 100 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Nodes type spin default 0 min 0 max 99999999
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Allow_Early_Stopping type check default true
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Use_Raw_Network type check default false
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Use_Transposition_Table type check default true
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Model_Directory type string default model/
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Move_Overhead type spin default 50 min 0 max 5000
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Centi_Random_Move_Factor type spin default 0 min 0 max 99
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name SyzygyPath type string default <empty>
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Log_File type string default
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Use_NPS_Time_Manager type check default true
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> option name Random_Playout type check default true
DEBUG:chess.engine:<UciProtocol (pid=31999)>: >> uciok
DEBUG:chess.engine:<UciProtocol (pid=31999)>: << ucinewgame
DEBUG:chess.engine:<UciProtocol (pid=31999)>: << isready
DEBUG:chess.engine:<UciProtocol (pid=31999)>: Process exited
DEBUG:chess.engine:<UciProtocol (pid=31999)>: Connection lost (exit code: -11, error: None)
Traceback (most recent call last):
  File "pychess.py", line 13, in <module>
    result = engine.play(board, chess.engine.Limit(time=0.1))
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 2386, in play
    return future.result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 435, in result
    return self.__get_result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1316, in play
    return await self.communicate(UciPlayCommand)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 768, in communicate
    return await command.result
chess.engine.EngineTerminatedError: engine process died unexpectedly (exit code: -11)

@shubhank008
Copy link
Author

I think the order of commands being sent is wrong, if I manually run CrazyAra and issue ucinewgame before isready it crashes and throws segfault error too, but doing isready first (to load the neural net) and then ucinewgame works and outputs:
info string newgame

@shubhank008
Copy link
Author

So I went to engine.py file and in play() function manually changed order or calling _ucinewgame and _isready and now the code executed further but crashed with this error

root@Ubuntu-1804-bionic-64-minimal ~/CrazyAra_0.8.0_Linux_MKL # python3.7 pychess.py
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:chess.engine:<UciProtocol (pid=341)>: Connection made
DEBUG:chess.engine:<UciProtocol (pid=341)>: << uci
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                                   _
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                    _..           /   ._   _.  _        /\   ._   _.
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                  .' _ `\         \_  |   (_|  /_  \/  /--\  |   (_|
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                 /  /e)-,\                         /
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                /  |  ,_ |                    __    __    __    __
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>               /   '-(-.)/          bw     8 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>             .'--.   \  `                 7 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>            /    `\   |                  6 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>          /`       |  / /`\.-.          5 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>        .'        ;  /  \_/__/         4 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>      .'`-'_     /_.'))).-` \         3 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>     / -'_.'---;`'-))).-'`\_/        2 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>    (__.'/   /` .'`                 1 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>     (_.'/ /` /`                       a  b  c  d  e  f  g  h
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>       _|.' /`
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> jgs.-` __.'|  Developers: Johannes Czech, Moritz Willig, Alena Beyer
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>     .-'||  |  Source-Code: QueensGambit/CrazyAra (GPLv3-License)
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>        \_`/   Inspiration: A0-paper by Silver, Hubert, Schrittwieser et al.
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>               ASCII-Art: Joan G. Stark, Chappell, Burton
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> id name CrazyAra 0.8.0 (May 16 2020)
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> id author Johannes Czech, Moritz Willig, Alena Beyer et al.
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name UCI_Variant type combo default crazyhouse var crazyhouse
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Search_Type type combo default mcts var mcts
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Context type combo default cpu var cpu
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name First_Device_ID type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Last_Device_ID type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Batch_Size type spin default 16 min 1 max 8192
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Threads type spin default 2 min 1 max 512
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_CPuct_Init type spin default 250 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name CPuct_Base type spin default 19652 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Dirichlet_Epsilon type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Dirichlet_Alpha type spin default 20 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_U_Init_Divisor type spin default 100 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Q_Value_Weight type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Q_Thresh_Init type spin default 50 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Q_Thresh_Max type spin default 90 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Q_Thresh_Base type spin default 1965 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Max_Search_Depth type spin default 99 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Temperature type spin default 170 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Temperature_Moves type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Quantile_Clipping type spin default 25 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Temperature_Decay type spin default 92 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Node_Temperature type spin default 170 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Virtual_Loss type spin default 100 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Nodes type spin default 0 min 0 max 99999999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Allow_Early_Stopping type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Use_Raw_Network type check default false
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Use_Transposition_Table type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Model_Directory type string default model/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Move_Overhead type spin default 50 min 0 max 5000
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Random_Move_Factor type spin default 0 min 0 max 99
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name SyzygyPath type string default <empty>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Log_File type string default
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Use_NPS_Time_Manager type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Random_Playout type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> uciok
DEBUG:chess.engine:<UciProtocol (pid=341)>: << isready
DEBUG:chess.engine:<UciProtocol (pid=341)>: << ucinewgame
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string json file: model/model-0.44052-1.08962-0.777-0.768-symbol.json
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model from model/model-0.44052-1.08962-0.777-0.768-symbol.json
WARNING:chess.engine:<UciProtocol (pid=341)>: stderr >> [11:32:25] src/nnvm/legacy_json_util.cc:204: Warning: loading symbol saved by MXNet version 10501 with lower version of MXNet v10500. May cause undefined behavior. Please update MXNet if you encounter any issue
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model parameters from: model/model-0.44052-1.08962-0.777-0.768-0096.params
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Bind successfull!
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string isPolicyMap: 1
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string json file: model/model-0.44052-1.08962-0.777-0.768-symbol.json
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model from model/model-0.44052-1.08962-0.777-0.768-symbol.json
WARNING:chess.engine:<UciProtocol (pid=341)>: stderr >> [11:32:25] src/nnvm/legacy_json_util.cc:204: Warning: loading symbol saved by MXNet version 10501 with lower version of MXNet v10500. May cause undefined behavior. Please update MXNet if you encounter any issue
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model parameters from: model/model-0.44052-1.08962-0.777-0.768-0096.params
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Bind successfull!
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string isPolicyMap: 1
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string json file: model/model-0.44052-1.08962-0.777-0.768-symbol.json
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model from model/model-0.44052-1.08962-0.777-0.768-symbol.json
WARNING:chess.engine:<UciProtocol (pid=341)>: stderr >> [11:32:25] src/nnvm/legacy_json_util.cc:204: Warning: loading symbol saved by MXNet version 10501 with lower version of MXNet v10500. May cause undefined behavior. Please update MXNet if you encounter any issue
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model parameters from: model/model-0.44052-1.08962-0.777-0.768-0096.params
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Bind successfull!
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string isPolicyMap: 1
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> readyok
Traceback (most recent call last):
  File "pychess.py", line 13, in <module>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string newgame
    result = engine.play(board, chess.engine.Limit(time=0.1))
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 2387, in play
    return future.result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 435, in result
    return self.__get_result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1317, in play
    return await self.communicate(UciPlayCommand)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 768, in communicate
    return await command.result
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 979, in _line_received
    self.line_received(engine, line)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1276, in line_received
    self._readyok(engine)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1282, in _readyok
    engine._position(board)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1173, in _position
    self._setoption("UCI_Variant", uci_variant)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1136, in _setoption
    value = self.options[name].parse(value)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 261, in parse
    raise EngineError("invalid value for combo option {!r}, got: {} (available: {})".format(self.name, value, ", ".join(self.var) if self.var else "-"))
chess.engine.EngineError: invalid value for combo option 'UCI_Variant', got: chess (available: crazyhouse)
^CException ignored in: <module 'threading' from '/usr/lib/python3.7/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 1307, in _shutdown
    lock.acquire()
KeyboardInterrupt

@QueensGambit
Copy link

QueensGambit commented Sep 7, 2020

Thank you @shubhank008 for figuring this out.
I'll make sure that CrazyAra can handle ucinewgame before the isready command in the next release.

Your last error arises from the fact that release 0.8.0 does not support classical chess yet and only supports crazyhouse unfortunately.
If you replace chess.Board() by chess.variant.CrazyhouseBoard() the UCI_Variant option should be setup correctly.
You also need to set the UCI option Model_Directory (default: model/) or change the current working directory (os.chdir) if you run the python script from a different directory than the executable.

import chess
import chess.engine
import chess.variant
import os

os.chdir("/root/CrazyAra_0.8.0_Linux_MKL/")
engine = chess.engine.SimpleEngine.popen_uci("/root/CrazyAra_0.8.0_Linux_MKL/CrazyAra")
## For CPU, comment if want to use GPU
engine.configure({"Context": "cpu"})

board = chess.variant.CrazyhouseBoard()

while not board.is_game_over():
	result = engine.play(board, chess.engine.Limit(time=0.1))
	board.push(result.move)

engine.quit()

One exemplary project where CrazyAra was running successfully in combination with python-chess is the python Lichess Bot API

Note, however that lichess-bot runs with python-chess 0.24.1.

@niklasf
Copy link
Owner

niklasf commented Sep 7, 2020

Meanwhile calling engine.ping() should be another workaround for #627 (comment), then.

@shubhank008
Copy link
Author

Thank you @shubhank008 for figuring this out.
I'll make sure that CrazyAra can handle ucinewgame before the isready command in the next release.

Your last error arises from the fact that release 0.8.0 does not support classical chess yet and only supports crazyhouse unfortunately.
If you replace chess.Board() by chess.variant.CrazyhouseBoard() the UCI_Variant option should be setup correctly.
You also need to set the UCI option Model_Directory (default: model/) or change the current working directory (os.chdir) if you run the python script from a different directory than the executable.

import chess
import chess.engine
import chess.variant
import os

os.chdir("/root/CrazyAra_0.8.0_Linux_MKL/")
engine = chess.engine.SimpleEngine.popen_uci("/root/CrazyAra_0.8.0_Linux_MKL/CrazyAra")
## For CPU, comment if want to use GPU
engine.configure({"Context": "cpu"})

board = chess.variant.CrazyhouseBoard()

while not board.is_game_over():
	result = engine.play(board, chess.engine.Limit(time=0.1))
	board.push(result.move)

engine.quit()

One exemplary project where CrazyAra was running successfully in combination with python-chess is the python Lichess Bot API

Note, however that lichess-bot runs with python-chess 0.24.1.

woah the man himself :D And yes it seems I should have read the release notes more carefully as they do mention 0.8 is for crazyhouse only.
Auto setting isready if ucinewgame is sent should fix it I suppose, till then I am fine with the manual edit I made in python-chess engine.py file
And yes, I figured there was another error if python file was not in same directory as executable so fixed it

Any plans for classic for 0.8/0.9 or 0.7 is good enough (asking cause there were some huge improvements mentioned for memory and loadtimes in 0.8 or were they for crazyhouse only ?)
Do you recommend using model 45 or 96 for classic chess in 0.7 ?

tldr: I just want to use CrazyAra in classic chess and not Crazyhouse (not yet anyway)

@shubhank008
Copy link
Author

So I went to engine.py file and in play() function manually changed order or calling _ucinewgame and _isready and now the code executed further but crashed with this error

root@Ubuntu-1804-bionic-64-minimal ~/CrazyAra_0.8.0_Linux_MKL # python3.7 pychess.py
DEBUG:asyncio:Using selector: EpollSelector
DEBUG:chess.engine:<UciProtocol (pid=341)>: Connection made
DEBUG:chess.engine:<UciProtocol (pid=341)>: << uci
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                                   _
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                    _..           /   ._   _.  _        /\   ._   _.
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                  .' _ `\         \_  |   (_|  /_  \/  /--\  |   (_|
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                 /  /e)-,\                         /
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>                /  |  ,_ |                    __    __    __    __
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>               /   '-(-.)/          bw     8 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>             .'--.   \  `                 7 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>            /    `\   |                  6 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>          /`       |  / /`\.-.          5 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>        .'        ;  /  \_/__/         4 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>      .'`-'_     /_.'))).-` \         3 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>     / -'_.'---;`'-))).-'`\_/        2 /__////__////__////__////
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>    (__.'/   /` .'`                 1 ////__////__////__////__/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>     (_.'/ /` /`                       a  b  c  d  e  f  g  h
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>       _|.' /`
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> jgs.-` __.'|  Developers: Johannes Czech, Moritz Willig, Alena Beyer
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>     .-'||  |  Source-Code: QueensGambit/CrazyAra (GPLv3-License)
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>        \_`/   Inspiration: A0-paper by Silver, Hubert, Schrittwieser et al.
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>               ASCII-Art: Joan G. Stark, Chappell, Burton
DEBUG:chess.engine:<UciProtocol (pid=341)>: >>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> id name CrazyAra 0.8.0 (May 16 2020)
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> id author Johannes Czech, Moritz Willig, Alena Beyer et al.
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name UCI_Variant type combo default crazyhouse var crazyhouse
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Search_Type type combo default mcts var mcts
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Context type combo default cpu var cpu
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name First_Device_ID type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Last_Device_ID type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Batch_Size type spin default 16 min 1 max 8192
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Threads type spin default 2 min 1 max 512
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_CPuct_Init type spin default 250 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name CPuct_Base type spin default 19652 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Dirichlet_Epsilon type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Dirichlet_Alpha type spin default 20 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_U_Init_Divisor type spin default 100 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Q_Value_Weight type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Q_Thresh_Init type spin default 50 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Q_Thresh_Max type spin default 90 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Q_Thresh_Base type spin default 1965 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Max_Search_Depth type spin default 99 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Temperature type spin default 170 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Temperature_Moves type spin default 0 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Quantile_Clipping type spin default 25 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Temperature_Decay type spin default 92 min 0 max 100
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Node_Temperature type spin default 170 min 1 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Virtual_Loss type spin default 100 min 0 max 99999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Nodes type spin default 0 min 0 max 99999999
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Allow_Early_Stopping type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Use_Raw_Network type check default false
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Use_Transposition_Table type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Model_Directory type string default model/
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Move_Overhead type spin default 50 min 0 max 5000
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Centi_Random_Move_Factor type spin default 0 min 0 max 99
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name SyzygyPath type string default <empty>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Log_File type string default
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Use_NPS_Time_Manager type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> option name Random_Playout type check default true
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> uciok
DEBUG:chess.engine:<UciProtocol (pid=341)>: << isready
DEBUG:chess.engine:<UciProtocol (pid=341)>: << ucinewgame
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string json file: model/model-0.44052-1.08962-0.777-0.768-symbol.json
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model from model/model-0.44052-1.08962-0.777-0.768-symbol.json
WARNING:chess.engine:<UciProtocol (pid=341)>: stderr >> [11:32:25] src/nnvm/legacy_json_util.cc:204: Warning: loading symbol saved by MXNet version 10501 with lower version of MXNet v10500. May cause undefined behavior. Please update MXNet if you encounter any issue
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model parameters from: model/model-0.44052-1.08962-0.777-0.768-0096.params
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Bind successfull!
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string isPolicyMap: 1
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string json file: model/model-0.44052-1.08962-0.777-0.768-symbol.json
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model from model/model-0.44052-1.08962-0.777-0.768-symbol.json
WARNING:chess.engine:<UciProtocol (pid=341)>: stderr >> [11:32:25] src/nnvm/legacy_json_util.cc:204: Warning: loading symbol saved by MXNet version 10501 with lower version of MXNet v10500. May cause undefined behavior. Please update MXNet if you encounter any issue
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model parameters from: model/model-0.44052-1.08962-0.777-0.768-0096.params
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Bind successfull!
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string isPolicyMap: 1
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string json file: model/model-0.44052-1.08962-0.777-0.768-symbol.json
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model from model/model-0.44052-1.08962-0.777-0.768-symbol.json
WARNING:chess.engine:<UciProtocol (pid=341)>: stderr >> [11:32:25] src/nnvm/legacy_json_util.cc:204: Warning: loading symbol saved by MXNet version 10501 with lower version of MXNet v10500. May cause undefined behavior. Please update MXNet if you encounter any issue
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Loading the model parameters from: model/model-0.44052-1.08962-0.777-0.768-0096.params
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string Bind successfull!
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string isPolicyMap: 1
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> readyok
Traceback (most recent call last):
  File "pychess.py", line 13, in <module>
DEBUG:chess.engine:<UciProtocol (pid=341)>: >> info string newgame
    result = engine.play(board, chess.engine.Limit(time=0.1))
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 2387, in play
    return future.result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 435, in result
    return self.__get_result()
  File "/usr/lib/python3.7/concurrent/futures/_base.py", line 384, in __get_result
    raise self._exception
  File "/usr/lib/python3.7/asyncio/tasks.py", line 442, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1317, in play
    return await self.communicate(UciPlayCommand)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 768, in communicate
    return await command.result
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 979, in _line_received
    self.line_received(engine, line)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1276, in line_received
    self._readyok(engine)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1282, in _readyok
    engine._position(board)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1173, in _position
    self._setoption("UCI_Variant", uci_variant)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 1136, in _setoption
    value = self.options[name].parse(value)
  File "/usr/local/lib/python3.7/dist-packages/chess/engine.py", line 261, in parse
    raise EngineError("invalid value for combo option {!r}, got: {} (available: {})".format(self.name, value, ", ".join(self.var) if self.var else "-"))
chess.engine.EngineError: invalid value for combo option 'UCI_Variant', got: chess (available: crazyhouse)
^CException ignored in: <module 'threading' from '/usr/lib/python3.7/threading.py'>
Traceback (most recent call last):
  File "/usr/lib/python3.7/threading.py", line 1307, in _shutdown
    lock.acquire()
KeyboardInterrupt

@QueensGambit does 0.7 also not support classic chess ? Because same variant error in 0.7 too

@shubhank008
Copy link
Author

@QueensGambit so I deleted 0.8 and downloaded 0.7 and changed my code to use
engine = chess.engine.SimpleEngine.popen_uci("/root/CrazyAra_0.7.0_Linux_MKL/CrazyAra")

However, even with that I got same error as above for variant. Then I changed the board code to variant option like you mentioned to Crazyhouse and the code is running now and the engine is playing against itself (engine vs engine) and I can even see it.
board = chess.variant.CrazyhouseBoard()

So does even 0.7 also not support classic chess or is it intentionally so to play classic chess through python-chess we need to still use Crazyhouse variant ?

My current code:

import chess
import chess.engine
import chess.variant
import logging
import time

#logging.basicConfig(level=logging.DEBUG)

engine = chess.engine.SimpleEngine.popen_uci("/root/CrazyAra_0.7.0_Linux_MKL/CrazyAra")
## For CPU, comment if want to use GPU
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/root/CrazyAra_0.7.0_Linux_MKL/
# source ~/.bashrc
#engine.configure({"Context": "cpu"})

#board = chess.Board()
board = chess.variant.CrazyhouseBoard()
while not board.is_game_over():
	result = engine.play(board, chess.engine.Limit(time=0.1))
	board.push(result.move)
	print(board)
	print("==")
	print("==")
	time.sleep(1)

print(board.fen())
engine.quit()

#engine = chess.engine.SimpleEngine.popen_uci("/root/CrazyAra_0.8.0_Linux_MKL/CrazyAra")
  

@QueensGambit
Copy link

@shubhank008 Nice, to hear that the code is running with board = chess.variant.CrazyhouseBoard().

Yes, CrazyAra <= 0.8.0 only supports crazyhouse so far.
Model 45 and model 96 were created during reinforcement learning.
Model 96 is about 50 Elo stronger than model 45 in direct comparison and the strongest available model for crazyhouse so far.

You can find an independent comparison between different versions and engines here:

For release 0.9.0, I plan to support additional chess variants.
I also trained some networks using supervised learning on human games for classical chess and atomic chess.
They aren't very strong yet but I could publish them with a suitable binary as a pre-release.

@shubhank008
Copy link
Author

@shubhank008 Nice, to hear that the code is running with board = chess.variant.CrazyhouseBoard().

Yes, CrazyAra <= 0.8.0 only supports crazyhouse so far.
Model 45 and model 96 were created during reinforcement learning.
Model 96 is about 50 Elo stronger than model 45 in direct comparison and the strongest available model for crazyhouse so far.

You can find an independent comparison between different versions and engines here:

For release 0.9.0, I plan to support additional chess variants.
I also trained some networks using supervised learning on human games for classical chess and atomic chess.
They aren't very strong yet but I could publish them with a suitable binary as a pre-release.

@QueensGambit maybe its due to my lack of understanding, but does no version or model of CrazyAra supports classic/normal chess ? I tried 0.7 and 0.6 yet both had same error and although the board runs using variant option it plays Crazyhouse while I wanted to do normal chess.
Or CrazyAra is solely for Crazyhouse ?

@QueensGambit
Copy link

QueensGambit commented Sep 7, 2020

All available releases for CrazyAra (<= 0.8.0) still only support Crazyhouse.

However, the code base of MCTS is setup to easily support additional variants in the future.
It uses Multi-Variant Stockfish's position representation and hence supports all lichess variants.

Alternatively, one could use the position representation of Fairy-Stockfish to further allow Asian chess variants (e.g. shogi, xiangqi, janggi, ...).

The main thing which is still missing are neural network models for each variant.

Currently, we also add Pommerman, a non-chess game, as a state interface:


If you are only interested in classical chess, you might want to use official Stockfish or Lc0 in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
engine Chess engine integration
Projects
None yet
Development

No branches or pull requests

3 participants