Permalink
Browse files

we are able to get root moves and their pvs

  • Loading branch information...
rjmacready committed Dec 19, 2015
1 parent 8918cb4 commit 4f4c50e947ae362473911c8ed353201d2b8be4f6
Showing with 227 additions and 24 deletions.
  1. +151 −20 src/guile.cpp
  2. +6 −0 src/guile.h
  3. +8 −1 src/search.cpp
  4. +1 −0 src/search.h
  5. BIN src/stockfish
  6. +61 −3 src/userscripts.scm
View
@@ -1,33 +1,148 @@
#include "guile.h"
#include "evaluate.h"
#include "misc.h"
#include "movegen.h"
#include "movepick.h"
#include "search.h"
#include "timeman.h"
#include "thread.h"
#include "tt.h"
#include "uci.h"
SCM display;
SCM evaluate;
SCM get_multipv;
SCM pick_best;
// Position
SCM get_side_to_move(SCM pos)
{
SCM get_side_to_move(SCM pos) {
Position *rpos = (Position*)scm_to_pointer(pos);
return scm_from_int(rpos->side_to_move());
}
SCM get_fen(SCM pos) {
Position *rpos = (Position*)scm_to_pointer(pos);
return scm_from_locale_string(rpos->fen().c_str());
}
SCM get_pinned_pieces(SCM pos, SCM color) {
Position *rpos = (Position*)scm_to_pointer(pos);
Color rcolor = (Color)scm_to_int(color);
return scm_from_uint64(rpos->pinned_pieces(rcolor));
}
// ... other stuff
// Root Moves
SCM get_root_moves() {
return scm_from_pointer(&(Threads.main()->rootMoves), NULL);
//return SCM_UNDEFINED;
}
SCM get_root_moves_len(SCM movesList) {
Search::RootMoveVector *rptr = (Search::RootMoveVector*) scm_to_pointer(movesList);
return scm_from_int(rptr->size());
//return SCM_UNDEFINED;
}
SCM get_root_move_at(SCM movesList, SCM idx) {
Search::RootMoveVector *rptr = (Search::RootMoveVector*) scm_to_pointer(movesList);
int ridx = scm_to_int(idx);
return scm_from_pointer(&((*rptr)[ ridx ]), NULL);
}
SCM get_score(SCM rootMove) {
if(scm_is_false(rootMove))
return SCM_BOOL_F;
Search::RootMove* rroot = (Search::RootMove*) scm_to_pointer(rootMove);
return scm_from_int(rroot->score);
}
SCM get_pv(SCM rootMove) {
if(scm_is_false(rootMove))
return SCM_BOOL_F;
Search::RootMove* rroot = (Search::RootMove*) scm_to_pointer(rootMove);
return scm_from_pointer(&(rroot->pv), NULL);
}
SCM get_pv_len(SCM pv) {
if(scm_is_false(pv))
return scm_from_int(0);
return scm_from_int(
(*((std::vector<Move>*) scm_to_pointer(pv))).size()
);
}
SCM get_pv_entry_at(SCM pv, SCM idx) {
if(scm_is_false(pv))
return SCM_BOOL_F;
// std::vector<Move> rvec = *((std::vector<Move>*) scm_to_pointer(pv));
int ridx = scm_to_int(idx);
// Move m = rvec[ridx];
// printf("get_pv_entry_at: %s\n", UCI::move(m, false).c_str());
std::vector<Move>* moves = (std::vector<Move>*) scm_to_pointer(pv);
//printf("get_pv_entry_at: %x %d\n", moves, idx);
//printf("size: %d\n", moves->size());
return scm_from_pointer(&(
(*moves)[ ridx ]), NULL);
}
SCM to_str(SCM pvEntry) {
// Move mv = *((Move*) scm_to_pointer(pvEntry));
return scm_from_locale_string(UCI::move(
*((Move*) scm_to_pointer(pvEntry))
, false).c_str());
}
// Move
SCM from_sq(SCM move) {
return SCM_UNDEFINED;
}
SCM to_sq(SCM move) {
return SCM_UNDEFINED;
}
// ---
void* init_guile(void* data)
{
scm_gc();
scm_c_primitive_load("/home/user/Stockfish/src/userscripts.scm");
display = scm_c_public_lookup("guile", "display");
scm_call_1(scm_variable_ref(display), scm_from_stringn("from C\n", 7, NULL, SCM_FAILED_CONVERSION_ERROR));
// , 7, NULL, SCM_FAILED_CONVERSION_ERROR
scm_call_1(scm_variable_ref(display), scm_from_locale_string("from C\n"));
scm_c_define_gsubr("get-root-moves", 0, 0, 0, (void*)&get_root_moves);
scm_c_define_gsubr("get-root-moves-len", 1, 0, 0, (void*)&get_root_moves_len);
scm_c_define_gsubr("side-to-move", 1, 0, 0, (void*)&get_side_to_move);
scm_c_define_gsubr("get-fen", 1, 0, 0, (void*)&get_fen);
scm_c_define_gsubr("get-root-move-at", 2, 0, 0, (void*)&get_root_move_at);
scm_c_define_gsubr("get-score", 1, 0, 0, (void*)&get_score);
scm_c_define_gsubr("get-pv", 1, 0, 0, (void*)&get_pv);
scm_c_define_gsubr("get-pv-len", 1, 0, 0, (void*)&get_pv_len);
scm_c_define_gsubr("get-pv-entry-at", 2, 0, 0, (void*)&get_pv_entry_at);
scm_c_define_gsubr("to-str", 1, 0, 0, (void*)&to_str);
evaluate = scm_c_public_lookup("userscripts", "evaluate");
//evaluate = scm_c_public_lookup("guile-user", "evaluate");
get_multipv = scm_c_public_lookup("userscripts", "get-multipv");
pick_best = scm_c_public_lookup("userscripts", "pick-best");
scm_call_1(scm_variable_ref(display), scm_variable_ref(evaluate));
// scm_call_1(scm_variable_ref(display), scm_variable_ref(evaluate));
scm_c_primitive_load("/home/user/Stockfish/src/main.scm");
@@ -41,26 +156,42 @@ void* init_guile(void* data)
return NULL;
}
size_t guile_get_multipv() {
if(get_multipv != NULL) {
return (size_t)scm_to_int(scm_call_0(scm_variable_ref(get_multipv)));
}
return 0;
}
Value guile_evaluate(const Position& pos, Value v)
{
assert(evaluate != NULL);
//SCM str = scm_from_stringn("from C\n", 7, NULL, SCM_FAILED_CONVERSION_ERROR);
//SCM display = scm_c_public_lookup("", "display"); // guile
if(evaluate != NULL) {
//SCM str = scm_from_stringn("from C\n", 7, NULL, SCM_FAILED_CONVERSION_ERROR);
//SCM display = scm_c_public_lookup("", "display"); // guile
//printf("thread: %d %d\n", std::thread::id(), std::this_thread::get_id());
//printf("thread: %d %d\n", std::thread::id(), std::this_thread::get_id());
//evaluate = scm_c_public_lookup("userscripts", "evaluate");
//scm_call_1(scm_variable_ref(display), scm_variable_ref(evaluate));
//evaluate = scm_c_public_lookup("userscripts", "evaluate");
//scm_call_1(scm_variable_ref(display), scm_variable_ref(evaluate));
// printf("was %d\n", v);
// printf("was %d\n", v);
SCM r = scm_call_2(scm_variable_ref(evaluate),
scm_from_pointer((void*)&pos, NULL),
scm_from_int(v));
SCM r = scm_call_2(scm_variable_ref(evaluate),
scm_from_pointer((void*)&pos, NULL),
scm_from_int(v));
// printf("now is %d\n", (Value)scm_to_int(r));
// printf("now is %d\n", (Value)scm_to_int(r));
return (Value)scm_to_int(r);
return (Value)scm_to_int(r);
}
return v;
}
void guile_pick_best(size_t multiPV)
{
if(pick_best != NULL) {
scm_call_1(scm_variable_ref(pick_best), scm_from_int(multiPV));
}
}
View
@@ -6,13 +6,19 @@
#include <libguile.h>
#include <thread>
#include "evaluate.h"
//#include "search.h"
//#include "movepick.h"
#include "types.h"
#include "position.h"
//#include "thread.h"
//extern "C" SCM get_side_to_move(SCM pos);
void* init_guile(void* data);
void guile_pick_best(size_t multiPV);
size_t guile_get_multipv();
Value guile_evaluate(const Position& pos, Value v);
#endif
View
@@ -376,7 +376,10 @@ void Thread::search() {
size_t multiPV = Options["MultiPV"];
Skill skill(Options["Skill Level"]);
size_t scm_multiPV = guile_get_multipv();
multiPV = std::max(scm_multiPV, multiPV);
// When playing with strength handicap enable MultiPV search that we will
// use behind the scenes to retrieve a set of possible moves.
if (skill.enabled())
@@ -543,6 +546,10 @@ void Thread::search() {
if (EasyMove.stableCnt < 6 || easyPlayed)
EasyMove.clear();
// printf("score of 1st is %d\n", Threads.main()->rootMoves[0].score);
guile_pick_best(multiPV);
// If skill level is enabled, swap best PV line with the sub-optimal one
if (skill.enabled())
std::swap(rootMoves[0], *std::find(rootMoves.begin(),
View
@@ -28,6 +28,7 @@
#include "misc.h"
#include "position.h"
#include "types.h"
#include "guile.h"
namespace Search {
View
Binary file not shown.
View
@@ -1,15 +1,73 @@
(define-module (userscripts)
#:export (evaluate))
#:export (evaluate get-multipv pick-best))
(define (__evaluate pos v)
(+ (- (random 1000) 500) v))
(define (_evaluate pos v)
(define (evaluate pos v)
v)
(define (evaluate pos v)
(define (_evaluate pos v)
(if (= (side-to-move pos) 0)
v
(* -1 v)))
(define (map-pv pv fn)
(let ((len (get-pv-len pv)))
; (if (< 0 len)
(do ((i 0 (1+ i)))
((>= i len))
(let ((entry (get-pv-entry-at pv i)))
;(display "call function on ")
;(display i)
;(display "\n")
(apply fn (list entry i))))))
; #f)))
(define (map-roots roots fn)
(let ((len (get-root-moves-len roots)))
; (if (< 0 len)
(do ((i 0 (1+ i)))
((>= i len))
(let ((entry (get-root-move-at roots i)))
(apply fn (list entry i))))))
; #f)))
(define (pick-best multiPV)
(display "pick-best was called\n")
(let* ((root-moves (get-root-moves))
(head-move (get-root-move-at root-moves 0))
(score (get-score head-move))
(pv (get-pv head-move))
(len (get-pv-len pv))
(entry (get-pv-entry-at pv 0)))
(display "called get-root-moves\n")
(map-roots root-moves
(lambda (entry i)
(display "root ")
(display i)
(display "\n")
(let ((pv (get-pv entry)))
;(display "get-pv with result\n")
(map-pv pv (lambda (entry i)
(display "\t")
(display (to-str entry))
(display " ")
(display (get-score entry))
(display "\n"))))
))
(display "score of 1st move is ")
(display score)
(display " ")
(display (to-str entry))
(display " there are ")
(display len)
(display " moves\n")
))
(define (get-multipv)
8)

0 comments on commit 4f4c50e

Please sign in to comment.