how difficult to write a program or script that...

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

casaschi
Posts: 164
Joined: Wed Dec 23, 2009 1:57 pm

how difficult to write a program or script that...

Post by casaschi »

hello,

how difficult would it be to write a program or script that:

1) on a regular interval, loads input.pgn and starts analyzing the final position of the game in the file
2) on a regular interval, outputs output.pgn containing the game from input.pgn annotated with a score evaluation so far and a PV (while only the last move is analyzed each time, output.pgn would contain all past annotations for the earlier moves)
3) everything built as much as possible using existing tools, like uci engines and scripts
4) running preferrably as a unix script

any suggestions were to start? maybe a UCI library of some sort?

thanks.
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: how difficult to write a program or script that...

Post by Don »

casaschi wrote:hello,

how difficult would it be to write a program or script that:

1) on a regular interval, loads input.pgn and starts analyzing the final position of the game in the file
2) on a regular interval, outputs output.pgn containing the game from input.pgn annotated with a score evaluation so far and a PV (while only the last move is analyzed each time, output.pgn would contain all past annotations for the earlier moves)
3) everything built as much as possible using existing tools, like uci engines and scripts
4) running preferrably as a unix script

any suggestions were to start? maybe a UCI library of some sort?

thanks.
That is very easy to do, but does require some tool to parse and make sense of PGN files. I wrote my own tcl, java and C package which knows how to parse PGN and a C routine (for linux only) that knows how to connect to a chess program.

My pgn parsing routines do not handle comment well, but if you have comments you can strip them out with pgn-extract. Handling comments that are properly formed and nested would be pretty trivial anyway.

You mention using existing tools, I think that might even be possible with a little creativity. pgn-extract can output a pgn file in long algebraic, which is the format you need to send information to a UCI program. with tools like sed, grep, awk, tail, etc you can build a string to send to a uci program in a pipeline. (You can also produce fen strings with pgn-extract but you lose history which may or many not affect the results with respect to drawish positions.)

I have a 20 line tcl script called "plumbing" which lets you put uci commands in a file and send them to a UCI program as if UCI were synchronous. I use it all the time, it's one of those simple things that can be used for everything.

This is all pretty easy to do, just requires a couple of hours of work most of which is debugging and testing. I'm really good at this type of stuff, probably most of the chess developers here are too.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: how difficult to write a program or script that...

Post by bob »

Don wrote:
casaschi wrote:hello,

how difficult would it be to write a program or script that:

1) on a regular interval, loads input.pgn and starts analyzing the final position of the game in the file
2) on a regular interval, outputs output.pgn containing the game from input.pgn annotated with a score evaluation so far and a PV (while only the last move is analyzed each time, output.pgn would contain all past annotations for the earlier moves)
3) everything built as much as possible using existing tools, like uci engines and scripts
4) running preferrably as a unix script

any suggestions were to start? maybe a UCI library of some sort?

thanks.
That is very easy to do, but does require some tool to parse and make sense of PGN files. I wrote my own tcl, java and C package which knows how to parse PGN and a C routine (for linux only) that knows how to connect to a chess program.

My pgn parsing routines do not handle comment well, but if you have comments you can strip them out with pgn-extract. Handling comments that are properly formed and nested would be pretty trivial anyway.

You mention using existing tools, I think that might even be possible with a little creativity. pgn-extract can output a pgn file in long algebraic, which is the format you need to send information to a UCI program. with tools like sed, grep, awk, tail, etc you can build a string to send to a uci program in a pipeline. (You can also produce fen strings with pgn-extract but you lose history which may or many not affect the results with respect to drawish positions.)

I have a 20 line tcl script called "plumbing" which lets you put uci commands in a file and send them to a UCI program as if UCI were synchronous. I use it all the time, it's one of those simple things that can be used for everything.

This is all pretty easy to do, just requires a couple of hours of work most of which is debugging and testing. I'm really good at this type of stuff, probably most of the chess developers here are too.
If I were going to do this, I would do it inside Crafty I think. Although it is trivial to tell crafty to "read a PGN file and then start to analyze" I think it would make more sense to tell it "read a PGN file, analyze for some amount of time, produce the output.pgn file, and then wait until the PGN input is modified, then read it and repeat...

That would not be much code to write...
casaschi
Posts: 164
Joined: Wed Dec 23, 2009 1:57 pm

Re: how difficult to write a program or script that...

Post by casaschi »

If I were going to do this, I would do it inside Crafty I think. Although it is trivial to tell crafty to "read a PGN file and then start to analyze" I think it would make more sense to tell it "read a PGN file, analyze for some amount of time, produce the output.pgn file, and then wait until the PGN input is modified, then read it and repeat...

That would not be much code to write...
Good idea.
I'll start having a look at the crafty code where the "annotate" command is implemented.
bob
Posts: 20943
Joined: Mon Feb 27, 2006 7:30 pm
Location: Birmingham, AL

Re: how difficult to write a program or script that...

Post by bob »

casaschi wrote:
If I were going to do this, I would do it inside Crafty I think. Although it is trivial to tell crafty to "read a PGN file and then start to analyze" I think it would make more sense to tell it "read a PGN file, analyze for some amount of time, produce the output.pgn file, and then wait until the PGN input is modified, then read it and repeat...

That would not be much code to write...
Good idea.
I'll start having a look at the crafty code where the "annotate" command is implemented.
It is simpler than that.

1. you can force a "read xxx.pgn" command into the input buffer, call Option() and Crafty will read that file in.

2. then force a "st=n" command into the input buffer" to set the target time limit.

3. then call "Iterate()" to search for that long.

Getting the output depends on what you want. You could "ftell()" the log file before you call iterate, then fseek() back to that point and read the log to get all the search output. Or you could maybe use "whisper_text" which has the PV of the last search...
User avatar
Don
Posts: 5106
Joined: Tue Apr 29, 2008 4:27 pm

Re: how difficult to write a program or script that...

Post by Don »

casaschi wrote:
If I were going to do this, I would do it inside Crafty I think. Although it is trivial to tell crafty to "read a PGN file and then start to analyze" I think it would make more sense to tell it "read a PGN file, analyze for some amount of time, produce the output.pgn file, and then wait until the PGN input is modified, then read it and repeat...

That would not be much code to write...
Good idea.
I'll start having a look at the crafty code where the "annotate" command is implemented.
Yes, I think Crafty has all the support routines you would need.