uci icon indicating copy to clipboard operation
uci copied to clipboard

Add depth and time bound (movetime) Go commands

Open abhijeetnvaidya opened this issue 9 years ago • 2 comments

I used this module recently and it worked very nicely (although understanding promises was a bit of a learning curve for me), so thanks for uploading this project. In go infinite + stop approach, stockfish sometimes outputs a "bestmove" without "ponder" (especially for low time values with high multipv settings) as a result, the regular expression fails, why not change it to simple string split? In my testing I found that go-infinite + stop approach is somewhat less stable than the go movetime one. The later lets engine overshoot few milliseconds to but gives correct output all the time.

//---------------------------------------------------------------------
Engine.prototype.depthLimitedGoCommand = function (infoHandler,
                                                   depth) {
    var self = this;
    var deferred = Q.defer();

    var engineStdoutListener = function (data) {
        var lines = data.toString().split(endOfLineRegExp);
        var last_multipv = "";
        for (var i = 0; i < lines.length; i++) {
            var stringifiedLine = S(lines[i]);
            if (stringifiedLine.includes('multipv') && infoHandler) {
                last_multipv = lines[i];
                infoHandler(lines[i]);
            } else if (stringifiedLine.startsWith('bestmove')) {
                self.engineProcess.stdout.removeListener('data', engineStdoutListener);
                var bestmove_components = stringifiedLine.split(" ");
                if (bestmove_components.length > 1) {
                    //deferred.resolve(utilities.convertToMoveObject(bestmove_components[1]));
                    deferred.resolve(last_multipv);
                } else {
                    throw new Error('Invalid format of bestmove. Expected "bestmove <move>". Returned "' + lines[i] + '"');
                }
            }
        }
    };

    this.engineProcess.stdout.on('data', engineStdoutListener);
    var commandString = 'go depth ' + depth;
    this.engineProcess.stdin.write(commandString + endOfLine);
    return deferred.promise;
};

Engine.prototype.timeBoundGoCommand = function (infoHandler,
                                                t) {
    var self = this;
    var deferred = Q.defer();

    var engineStdoutListener = function (data) {
        var lines = data.toString().split(endOfLineRegExp);
        var last_multipv = "";
        for (var i = 0; i < lines.length; i++) {
            var stringifiedLine = S(lines[i]);
            if (stringifiedLine.includes('multipv') && infoHandler) {
                last_multipv = lines[i];
                infoHandler(lines[i]);
            } else if (stringifiedLine.startsWith('bestmove')) {
                self.engineProcess.stdout.removeListener('data', engineStdoutListener);
                var bestmove_components = stringifiedLine.split(" ");
                if (bestmove_components.length > 1) {
                    //deferred.resolve(utilities.convertToMoveObject(bestmove_components[1]));
                    deferred.resolve(last_multipv);
                } else {
                    throw new Error('Invalid format of bestmove. Expected "bestmove <move>". Returned "' + lines[i] + '"');
                }
            }
        }
    };

    this.engineProcess.stdout.on('data', engineStdoutListener);
    var commandString = 'go movetime ' + t;
    this.engineProcess.stdin.write(commandString + endOfLine);
    return deferred.promise;
};

abhijeetnvaidya avatar Mar 10 '16 22:03 abhijeetnvaidya

@abhijeetnvaidya Glad that you liked the library :+1: . I don't fully understand which part of the code fails (right now my guess is line 278). Can you please post your code and stockfish version with which you saw this behavior. This info will help me in fixing the bug. Also please post the full exception trace.

imor avatar Mar 11 '16 04:03 imor

Glad that you liked the library :+1: . I don't fully understand which part of the code fails (right now my guess is line 278). Can you please post your code and stockfish version with which you saw this behavior. This info will help me in fixing the bug. Also please post the full exception trace.

make like this: ...).then(function () { console.log('New game started'); return engine.positionCommand('startpos', 'e2e4 e7e5'); }).then(function () { console.log('Starting position set'); console.log('Starting analysis'); return engine.goInfiniteCommand(function infoHandler(info) { console.log(info); }); }).delay(5000).then(function () {... with delay 5 sec

and append console.log like this in main.js: ...var engineStdoutListener = function (data) { console.log('engineStdoutListener',data.toString()); var lines = data.toString().split(endOfLineRegExp);...

im using engine: new Engine(__dirname+'/engines/stockfish_7_x64');

valanchik avatar Jun 20 '16 19:06 valanchik