简体   繁体   中英

Php proc_open() and stockfish chess engine anomaly depth 1

Looking forward to integrate stockfish chess engine into a php-cli script.

There is an unexpected behavior, the stockfish program is quitting directly without "thinking", it returns only the position at depth 1, when called from php.

For better understanding, while running the stockfish program from the command line, here is the expected behavior (gif):

在此处输入图像描述


From php , the following is working ( starting position, whites to play , asking for 50 depth), it returns a move a2a3 , the position at depth 1, which is a pretty bad move!

The answer is instantaneous, where going trough all the depth levels should take at least many seconds.

It is identical with any FEN positions , always returning the depth 1.

$descr = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w"),
    2 => array("pipe", "w")
);
$pipes = array();
$process = proc_open("stockfish", $descr, $pipes);
if (is_resource($process)) {
    fwrite($pipes[0], "uci\n");
    fwrite($pipes[0], "ucinewgame\n");
    fwrite($pipes[0], "position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - - 0 1\n");    
    fwrite($pipes[0], "go depth 50\n");
    fclose($pipes[0]);    
    // Read all output from the pipe
    while (!feof($pipes[1])) {    
        echo fgets($pipes[1]);
    }
    fclose($pipes[1]);
    proc_close($process);

}
// RETURN last line:  bestmove a2a3

All versions of stockfish had been tested, 8, 9, 10, with the same result.

I tried many options and different ways to run shell commands from php, including posix_mkfifo() piping, but none are working as expected, always returning a move at depth 1.

Another example , same behavior, return always "a2a3".

  file_put_contents(".COMFISH","uci\nucinewgame\nsetoption name Threads value 1\nposition fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - - 0 1\ngo depth 50");
  $COM = explode(" ",system("stockfish < .COMFISH"))[1];
  var_dump($COM);
  // RETURN a2a3

This might be directly linked on how that stockfish binary is written, (multi threadings), and not a php behavior, but I am looking for an explanation here?

From wiki :

Stockfish can use up to 512 CPU threads in multiprocessor systems.

Well, this was fairly simple, the pipe was closed too early.

Leaving here the base of a working code for future readers.

$descr = [0 => ["pipe", "r"], 1 => ["pipe", "w"]];
$pipes = [];
$process = proc_open("stockfish", $descr, $pipes);
if (is_resource($process)) {
    fwrite($pipes[0], "uci\n");
    fwrite($pipes[0], "ucinewgame\n");
    fwrite($pipes[0], "position fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - - 0 1\n");    
    fwrite($pipes[0], "setoption name Skill Level value 17\n"); // Set level between 1 and 20
    fwrite($pipes[0], "go movetime 5000\n"); // Return bestmove after 5 seconds          
    while (!feof($pipes[1])) {    
        echo fgets($pipes[1]);
    }
    fclose($pipes[0]);
    fclose($pipes[1]);
    proc_close($process);    
}
// RETURN last line: bestmove e2e4 ponder e7e6

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM