Communicating a plan to a chess engine

Discussion of anything and everything relating to chess playing software and machines.

Moderators: hgm, Rebel, chrisw

arturo100
Posts: 53
Joined: Thu Jun 25, 2009 10:34 am

Communicating a plan to a chess engine

Post by arturo100 »

Following an advice I got from Louis Zulli in this discussion: http://talkchess.com/forum/viewtopic.php?t=44589, I would like White try to send to an engine the following string of moves: Nh1, Nf2, Nh3, Ng5, Qh5, under certain controlled circumstances that I will detail below.


This is the position: [D]r1b2r1k/1pq3pp/p1nb4/5p2/2Bp1P2/3P1QN1/PP1BR1PP/5RK1 w - - 0 18

Basically the script should do the following:

1) Return an evaluation score for the starting position.
For each move in the plan:
a) Return the evaluation score after the next move of the plan.

If the score exceeds the previous evaluation score plus the dropff value stop: exit since the plan has worked.
If the score is less than the previous evaluation score minus the dropoff value: don't play this move of the plan since you are probably hanging a piece, getting mated or promoted by the opponent. Instead, play the move suggested by the best line and don't update the moves of the plan that have been played. Return to a).
If the score is within the range update the moves of the plan that have been played and return to a).

Please, note tha the dropoff score is either the maximum loss I am willing to take in order to play the plan or the minimum gain I expect from the plan execution.

I am proficient with c language and bash scripting. I could probably code something also in R and perl, but I really don't know how to communicate with the engine. I would be grateful if someone could post an example script so that I could learn by following along. Also, would it be easier to work with an UCI or a Xboard/Winboard engine for this specific purpose?

Thanks in advance.

Arturo.
tvrzsky
Posts: 128
Joined: Sat Sep 23, 2006 7:10 pm
Location: Prague

Re: Communicating a plan to a chess engine

Post by tvrzsky »

Hi, communication with the engine through pipe is pretty easy thing. Maybe this little Perl script can show you how to do it. It takes an EPD file and prints evaluation of each position.

Code: Select all

#!/usr/bin/perl 
 use IPC::Open2; 

 $engine = $ARGV[2]; 
 $epdfile = $ARGV[3]; 
 if ($ARGV[0] eq 't') { $limit = 'movetime' } 
 elsif ($ARGV[0] eq 'd') { $limit = 'depth' } 
 else { die ("bad argument: $ARGV[0]") } 
 die ("bad argument: $ARGV[1]") unless ($ARGV[1] =~ /^\d+$/); 
 $limit = $limit.' '.$ARGV[1]; 
 $output = ">output.txt"; 

 $engine = open2(*FROMENG, *TOENG, $engine); 
 TOENG->autoflush(1); 
 FROMENG->autoflush(1); 
 $| = 1;                                

 open EPDFILE, $epdfile or die ("can't open $epdfile!!!"); 
 open OUTPUT, $output or die ("can't open $output!!!"); 
 print TOENG "uci\n"; 
 while (<EPDFILE>) 
 &#123; 
   if ( m§(?&#58;(?&#58;&#91;1-8KkQqRrBbNnPp&#93;+/)&#123;7&#125;&#91;1-8KkQqRrBbNnPp&#93;+) (?&#58;&#91;wb&#93;) (?&#58;-|K?Q?k?q?) (?&#58;-|&#91;a-h&#93;&#91;1-8&#93;)§o ) 
   &#123; 
     print TOENG "position fen $&\ngo $limit\n"; 
     my $line = <FROMENG>; 
     my $score = '???'; 
     my $ismate; 
     while ($line !~ /bestmove/) 
     &#123; 
       if ($line =~ /score &#40;cp|mate&#41; (-?\d+) /) 
       &#123; 
         $score = $2; 
         $ismate = $1; 
       &#125; 
       $line = <FROMENG>; 
     &#125; 
     chomp; 
     print OUTPUT "$_ ".($ismate eq 'mate' ? 'mate ' &#58; '').$score."\n"; 
   &#125; 
 &#125; 
 print TOENG "quit\n"; 
 close EPDFILE; 
 close TOENG; 
 close FROMENG; 
 close OUTPUT;
 
jefk
Posts: 626
Joined: Sun Jul 25, 2010 10:07 pm
Location: the Netherlands
Full name: Jef Kaan

Re: Communicating a plan to a chess engine

Post by jefk »

in fact i would reverse the problem, namely,
communicating a plan from a chess engine to a human.

useful to continue looking for 'best' opening lines and thus
define the 'best' opening repertoire; ok maybe that would
be then to complicated for humans but for a good comp
book its still a good strategy. and for top-GM's as well.

and for that purpose a supercomputer (other thread) indeed
could be useful.i guess. although not cheap.

would i hit a draw margin ? well considering the exponential
growth of the chess tree(s) i don't think so, although
it might hit endgame bases earlier of course.

and when playing against itself, well then the obvious
most cost effective strategy would be to offer a draw.
and if the other comp clone would have a bug, well
still then your own supercomp -when identical- will
have the same bug, so still then offering a draw is
the best strategy..