Bash script to start UCI engine and get evaluation

Discussion of chess software programming and technical issues.

Moderator: Ras

Bozon
Posts: 6
Joined: Wed Dec 11, 2013 9:54 pm

Bash script to start UCI engine and get evaluation

Post by Bozon »

I would like to write a bash script that passes a FEN string to a UCI engine, then runs the analysis for a specified time and returns the determined evaluation. I succeeded in doing all this, except for getting the evaluation. I don't now yet how to do this in general. One idea would be to parse all the info strings for "score cp" and try to extract what follows, but I realized that the Komodo chess engine has a different output (I have never seen info strings like for stockfish). Is there a general solution to this problem?

To give you more context: I would like to collect evaluations on a specific position from different chess engines and then do some statistics with them.
Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Re: Bash script to start UCI engine and get evaluation

Post by Adam Hair »

Bozon wrote:I would like to write a bash script that passes a FEN string to a UCI engine, then runs the analysis for a specified time and returns the determined evaluation. I succeeded in doing all this, except for getting the evaluation. I don't now yet how to do this in general. One idea would be to parse all the info strings for "score cp" and try to extract what follows, but I realized that the Komodo chess engine has a different output (I have never seen info strings like for stockfish). Is there a general solution to this problem?

To give you more context: I would like to collect evaluations on a specific position from different chess engines and then do some statistics with them.
I have a script that returns the score when the engine completes a specified depth, but not for completion of a specified time. If you will allow me some time, I may be able to modify it into something you can use (the script was written for me).
Bozon
Posts: 6
Joined: Wed Dec 11, 2013 9:54 pm

Re: Bash script to start UCI engine and get evaluation

Post by Bozon »

That would be great already! It would be nice if you could post your script.
Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Re: Bash script to start UCI engine and get evaluation

Post by Adam Hair »

Bozon wrote:That would be great already! It would be nice if you could post your script.
I put together a package that may contain much more than what you need, but I think it could be helpful to someone.

The package contains a simple Ruby script that Miguel Ballicora wrote for me some time ago which I have modified for this situation. It assumes that the UCI command 'isready' is sent before sending a position to an engine. It records the value of 'score cp' and returns the last value sent before the engine sends 'best move'. I will post the code at the end of the message.

The package also contains a small program (and source code) written by Miguel called gestor (a term used in Argentina to denote a middle man, someone who handles business deals between people or companies). gestor can record the communication between programs in Linux. It is a Linux analog to the Windows program InBetween by Odd Malin Gunnar, though I do not believe it has the ability translate commands that InBetween has.

I included gestor because I noticed a problem with Gaviota and Stockfish when trying to make them search a position via a script. Both engines (presumably others) would make an abbreviated search even when the command 'go movetime 5000' was sent. It appeared that the commands were being sent exactly as Polyglot does it, but the results were different. I used gestor to see the communication to and from the engines, but I saw nothing to explain the problem. However, there was a positive side effect from using gestor. Both engines responded to the 'go' command correctly. Evidently, there is a timing issue that is alleviated by using an adapter.

So, the package contains gestor, the ruby script, information on the usage of those two tools, and some examples. You can download it from here:

http://www.mediafire.com/download/hjz05 ... raction.7z

Here is the script code for score_extract.rb:

Code: Select all

def getscore line
    ret = nil
    t1 = line.include? 'info'
    t2 = line.include? 'depth'
    if t1 && t2
        if line.include? 'score mate'
            ret = 'mate_value'
        else
            if line.include? 'score cp'

                arr = line.split('score cp')
                if arr[1] != nil    
                    tokens = arr[1].strip.split(' ')
                    ret = tokens[0]
                end
            end        
        end
    end    
    return ret
end

def line_readyok line
    return line.include? 'readyok'

end
 
simlog = ARGV[0]

lastscore = '---'
File.open simlog, "r" do |f|
    readyok = false

    lines = f.readlines
    lines.each do|line|
        next if !readyok && !line_readyok(line) 
        readyok = true

        if line != nil
            line = line.chomp
            score = getscore(line)
            if (score != nil)
                lastscore = score
            end
            if line.include? 'bestmove'
                puts lastscore
                lastscore = '---'
                readyok = false
            end
        end    
    end
end
Bozon
Posts: 6
Joined: Wed Dec 11, 2013 9:54 pm

Re: Bash script to start UCI engine and get evaluation

Post by Bozon »

Oh dear. I didn't expect this to be so complicated but it seems to work indeed! I'll have to experiment a bit more with this though. Thank you so much for this!
Adam Hair
Posts: 3226
Joined: Wed May 06, 2009 10:31 pm
Location: Fuquay-Varina, North Carolina

Re: Bash script to start UCI engine and get evaluation

Post by Adam Hair »

It is possible that I have made it complicated for no good reason :lol:
Bozon
Posts: 6
Joined: Wed Dec 11, 2013 9:54 pm

Re: Bash script to start UCI engine and get evaluation

Post by Bozon »

I don't think so. I've adapted everything now and it works perfectly. So, thanks again! :)