Esempio n. 1
0
void engine_open(engine_t * engine){
   int affinity;
    char *my_dir;
    if( (my_dir = _getcwd( NULL, 0 )) == NULL )
        my_fatal("Can't build path: %s\n",strerror(errno));
	if(SetCurrentDirectory(option_get_string("EngineDir"))==0){
		printf("tellusererror Polyglot:EngineDir error: %s\n",option_get_string("EngineDir"));
		fflush(stdout);
		Sleep(50000);
		my_fatal("EngineDir path error: %s\n",option_get_string("EngineDir"));
	}
	pipeEngine.Open(option_get_string("EngineCommand"));
        //play with affinity
#ifndef NO_AFFINITY
    affinity=option_get_int("Affinity");

	if(affinity!=-1){
		my_log("POLYGLOT SetProcess Affinity\n");
		SetProcessAffinityMask(child,affinity); //
	}
#endif
        //lets go back
    SetCurrentDirectory(my_dir);
        // set priority
    if (option_get_bool("UseNice")){
          my_log("POLYGLOT Adjust Engine Piority\n");
		  SetPriorityClass(child, GetWin32Priority(option_get_int("NiceValue")));
    }
}
Esempio n. 2
0
void material_parameter() {

   // UCI options

   MaterialWeight = (option_get_int("Material") * 256 + 50) / 100;
   OpeningExchangePenalty = option_get_int("Toga Exchange Bonus");
   EndgameExchangePenalty = OpeningExchangePenalty; 

}
Esempio n. 3
0
static int mate_score(int dist) {

   ASSERT(dist!=0);

   if (false) {
   } else if (dist > 0) {
      return +option_get_int("MateScore") - (+dist) * 2 + 1;
   } else if (dist < 0) {
      return -option_get_int("MateScore") + (-dist) * 2;
   }

   return 0;
}
Esempio n. 4
0
static void comp_move(int move) {

   board_t board[1];
   char string[256];

   ASSERT(move_is_ok(move));

   ASSERT(State->state==THINK);
   ASSERT(!XB->analyse);

   if(option_get_bool(Option,"RepeatPV"))
	   send_pv(); // to update time and nodes

   // send the move

   game_get_board(Game,board);

   if (move_is_castle(move,board) && option_get_bool(Option,"Chess960")) {
      if (!move_to_san(move,board,string,256)) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
   } else {
      if (!move_to_can(move,board,string,256)) my_fatal("comp_move(): move_to_can() failed\n");
   }

   gui_send(GUI,"move %s",string);

   // resign?

   if (option_get_bool(Option,"Resign") && Uci->root_move_nb > 1) {

       if (Uci->best_score <= -abs(option_get_int(Option,"ResignScore"))) {

         State->resign_nb++;
         my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");

         if (State->resign_nb >= option_get_int(Option,"ResignMoves")) {
            my_log("POLYGLOT *** RESIGN ***\n");
            gui_send(GUI,"resign");
         }

      } else {

         if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
         State->resign_nb = 0;
      }
   }

   // play the move

   move_step(move);
   no_mess(move);
}
Esempio n. 5
0
int db_open()
{
    char *error = NULL;
    char *filename = option_get_str("db_file");
    
    ldb_options = leveldb_options_create();
    ldb_cache = leveldb_cache_create_lru(option_get_int("cache_size"));
    
    leveldb_options_set_create_if_missing(ldb_options, option_get_int("create_db_if_missing"));
    leveldb_options_set_error_if_exists(ldb_options, option_get_int("error_if_db_exists"));
    leveldb_options_set_paranoid_checks(ldb_options, option_get_int("paranoid_checks"));
    leveldb_options_set_write_buffer_size(ldb_options, option_get_int("write_buffer_size"));
    leveldb_options_set_block_size(ldb_options, option_get_int("block_size"));
    leveldb_options_set_cache(ldb_options, ldb_cache);
    leveldb_options_set_max_open_files(ldb_options, option_get_int("leveldb_max_open_files"));
    leveldb_options_set_block_restart_interval(ldb_options, 8);
    leveldb_options_set_compression(ldb_options, option_get_int("compression"));
    
    // leveldb_options_set_env(options, self->_env);
    leveldb_options_set_info_log(ldb_options, NULL);
    
    ldb = leveldb_open(ldb_options, filename, &error);
    if (error) {
        fprintf(stderr, "ERROR opening db:%s\n", error);
        return 0;
    }
    return 1;
}
Esempio n. 6
0
void pawn_parameter() {

   // UCI options

   PawnStructureWeight = (option_get_int("Pawn Structure") * 256 + 50) / 100;

}
Esempio n. 7
0
void trans_alloc(trans_t * trans) {

    uint32 size, target;

    ASSERT(trans!=NULL);

    // calculate size

    target = option_get_int("Hash");
    if (target < 4) target = 16;
    target *= 1024 * 1024;

    for (size = 1; size != 0 && size <= target; size *= 2)
        ;

    size /= 2;
    ASSERT(size>0&&size<=target);

    // allocate table

    size /= sizeof(entry_t);
    ASSERT(size!=0&&(size&(size-1))==0); // power of 2

    trans->size = size + (ClusterSize - 1); // HACK to avoid testing for end of table
    trans->mask = size - 1;

    trans->table = (entry_t *) my_malloc(trans->size*sizeof(entry_t));

    trans_clear(trans);

    ASSERT(trans_is_ok(trans));
}
Esempio n. 8
0
static void parse_setoption(char string[]) {

   const char * name;
   char * value;

   // init

   name = strstr(string,"name ");
   value = strstr(string,"value ");

   if (name == NULL || value == NULL || name >= value) return; // ignore buttons

   value[-1] = '\0'; // HACK
   name += 5;
   value += 6;

   // update

   option_set(name,value);

   // update transposition-table size if needed

   if (Init && my_string_equal(name,"Hash")) { // Init => already allocated

      ASSERT(!Searching);

      if (option_get_int("Hash") >= 4) {
         trans_free(Trans);
         trans_alloc(Trans);
      }
   }
}
Esempio n. 9
0
void uci_send_ucinewgame(uci_t * uci) {

   ASSERT(uci!=NULL);

   if (option_get_int("UCIVersion") >= 2) {
      engine_send(uci->engine,"ucinewgame");
   }
}
Esempio n. 10
0
static bool kibitz_throttle(bool searching){
    time_t curr_time;
    static time_t lastKibitzMove=0;
    static time_t lastKibitzPV=0;
    curr_time = time(NULL);
    if(searching){   // KibitzPV
        if(curr_time >=
           (option_get_int(Option,"KibitzInterval") + lastKibitzPV)){
            lastKibitzPV=curr_time;
            return TRUE;
        }
    }else{       // KibitzMove
        if(curr_time >=
           (option_get_int(Option,"KibitzInterval") + lastKibitzMove)){
            lastKibitzPV=curr_time;
            lastKibitzMove=curr_time;
            return TRUE;
        }        
    }
    return FALSE;
}
Esempio n. 11
0
void process_message_cb(char *message, void *cb_arg)
{
    struct evbuffer *evb;
    char *encoded_message;
    struct destination_url *destination;
    
    _DEBUG("process_message_cb()\n");
    
    if (message == NULL || strlen(message) < 3) {
        return;
    }
    
    if (option_get_int("max_silence") > 0) {
        last_message_timestamp = time(NULL);
    }
    
    if (!current_destination) {
        // start loop over again for round-robin
        current_destination = destinations;
    }
    LL_FOREACH(current_destination, destination) {
        if (destination->method == EVHTTP_REQ_GET) {
            evb = evbuffer_new();
            encoded_message = simplehttp_encode_uri(message);
            evbuffer_add_printf(evb, destination->path, encoded_message);
            //_DEBUG("process_message_cb(GET %s)\n", (char *)EVBUFFER_DATA(evb));
            new_async_request(destination->address, destination->port, (char *)EVBUFFER_DATA(evb),
                              finish_destination_cb, NULL);
            evbuffer_free(evb);
            free(encoded_message);
        } else {
            //_DEBUG("process_message_cb(POST %s:%d%s)\n", destination->address, destination->port, destination->path);
            new_async_request_with_body(EVHTTP_REQ_POST, destination->address, destination->port, destination->path,
                                        NULL, message, finish_destination_cb, NULL);
        }
        
        if (round_robin) {
            // break and set the next loop to start at the next destination
            current_destination = destination->next;
            break;
        }
    }
}
Esempio n. 12
0
static void init() {

   if (!Init) {

      // late initialisation

      Init = true;

      if (option_get_bool("OwnBook")) {
         book_open(option_get_string("BookFile"));
      }

      trans_alloc();

      pawn_init();
      material_init();
      pst_init();
      eval_init();

      tb_cache(option_get_int("NalimovCache"));
   }
}
Esempio n. 13
0
static void comp_move(int move) {

	board_t board[1];
	char string[256];

	ASSERT(move_is_ok(move));

	ASSERT(State->state==THINK);
	ASSERT(!XB->analyse);

	if(option_get_bool("RepeatPV")==true)
		send_pv(); // to update time and nodes

	// send the move

	game_get_board(Game,board);

	if (move_is_castle(move,board) && option_get_bool("Chess960")) {
		if (!move_to_san(move,board,string,sizeof(string))) my_fatal("comp_move(): move_to_san() failed\n"); // O-O/O-O-O
	} else {
		if (!move_to_can(move,board,string,sizeof(string))) my_fatal("comp_move(): move_to_can() failed\n");
	}

	move_step(move);
	//game ended?
	if(game_status(Game)!= PLAYING){
		//handle ics drawing stuff
		if(XB->ics){
			switch (game_status(Game)){
			case DRAW_MATERIAL:
			case DRAW_FIFTY:
			case DRAW_REPETITION:
				xboard_send(XBoard,"offer draw");
				break;
			default:
				break;
			}
		}
		xboard_send(XBoard,"move %s",string);
		board_update();
		no_mess(move);
		return;
	}

	// engine sended a move while in ponder mode? 
	if(State->state==PONDER){
		if(board->turn==White)
			xboard_send(XBoard,"0-1 {polyglot : engine moves while pondering}\n");
		else
			xboard_send(XBoard,"1-0 {polyglot : engine moves while pondering}\n");
	}
	// resign?
	if (option_get_bool("Resign") && Uci->root_move_nb > 1) {
		int best = Uci->best_score;
		if (option_get_bool("ScoreWhite") && colour_is_black(Uci->board->turn))
			best = -best;

		if (best <= -abs(option_get_int("ResignScore"))) {

			State->resign_nb++;
			my_log("POLYGLOT %d move%s with resign score\n",State->resign_nb,(State->resign_nb>1)?"s":"");

			if (State->resign_nb >= option_get_int("ResignMoves")) {
				my_log("POLYGLOT *** RESIGN ***\n");
				//send move and resign
				//xboard_send(XBoard,"move %s \nresign",string);
				//just resign
				xboard_send(XBoard,"resign",string);
				no_mess(move);
				return;
			}
		} else {
			if (State->resign_nb > 0) my_log("POLYGLOT resign reset (State->resign_nb=%d)\n",State->resign_nb);
			State->resign_nb = 0;
		}
	}
	no_mess(move);
	xboard_send(XBoard,"move %s",string);
}
Esempio n. 14
0
void search() {

   int move;
   int depth;
   int i;
   bool search_ready;

     
   for (i = 0; i < MultiPVMax; i++){
	  save_multipv[SearchCurrent->multipv].mate = 0;
	  save_multipv[SearchCurrent->multipv].depth = 0;
	  save_multipv[SearchCurrent->multipv].max_depth = 0;
	  save_multipv[SearchCurrent->multipv].value = 0;
	  save_multipv[SearchCurrent->multipv].time = 0;
	  save_multipv[SearchCurrent->multipv].node_nb = 0;
	  strcpy(save_multipv[SearchCurrent->multipv].pv_string,""); 
   }
  
   SearchInput->multipv = option_get_int("MultiPV")-1;
   SearchCurrent->multipv = 0;
   
   
   ASSERT(board_is_ok(SearchInput->board));

   // opening book

   if (option_get_bool("OwnBook") && !SearchInput->infinite) {

      move = book_move(SearchInput->board);

      if (move != MoveNone) {

         // play book move

         SearchBest[SearchCurrent->multipv].move = move;
         SearchBest[SearchCurrent->multipv].value = 1;
         SearchBest[SearchCurrent->multipv].flags = SearchExact;
         SearchBest[SearchCurrent->multipv].depth = 1;
         SearchBest[SearchCurrent->multipv].pv[0] = move;
         SearchBest[SearchCurrent->multipv].pv[1] = MoveNone;

         search_update_best();

         return;
      }
   }

   // SearchInput

   gen_legal_moves(SearchInput->list,SearchInput->board);

   if (LIST_SIZE(SearchInput->list) < SearchInput->multipv+1){ 
	  SearchInput->multipv = LIST_SIZE(SearchInput->list)-1;
   }

   if (LIST_SIZE(SearchInput->list) <= 1) {
      SearchInput->depth_is_limited = true;
      SearchInput->depth_limit = 4; // was 1
   }

   // SearchInfo

   if (setjmp(SearchInfo->buf) != 0) {
      ASSERT(SearchInfo->can_stop);
      ASSERT(SearchBest->move!=MoveNone);
      search_update_current();
      return;
   }

   // SearchRoot

   list_copy(SearchRoot->list,SearchInput->list);

   // SearchCurrent

   board_copy(SearchCurrent->board,SearchInput->board);
   my_timer_reset(SearchCurrent->timer);
   my_timer_start(SearchCurrent->timer);

   // init

   trans_inc_date(Trans);

   sort_init();
   search_full_init(SearchRoot->list,SearchCurrent->board);

   // analyze game for evaluation
   
   if (SearchCurrent->board->piece_size[White] < 3 && SearchCurrent->board->piece_size[Black] < 3){
	   trans_endgame = true;
   }
   else{
	   trans_endgame = false;
   }

   
   // iterative deepening

   search_ready = false;

   for (depth = 1; depth < DepthMax; depth++) {
	   for (SearchCurrent->multipv = 0; SearchCurrent->multipv <= SearchInput->multipv; SearchCurrent->multipv++){

		  if (DispDepthStart && SearchCurrent->multipv == 0) send("info depth %d",depth);

		  SearchCurrent->max_extensions = depth * 10;
		  SearchRoot->bad_1 = false;
		  SearchRoot->change = false;

		  board_copy(SearchCurrent->board,SearchInput->board);

		  if (UseShortSearch && depth <= ShortSearchDepth) {
			 search_full_root(SearchRoot->list,SearchCurrent->board,depth,SearchShort);
		  } else {
			 search_full_root(SearchRoot->list,SearchCurrent->board,depth,SearchNormal);
		  }

		  search_update_current();

		  if (DispDepthEnd && SearchCurrent->multipv == SearchInput->multipv) {
			 send("info depth %d seldepth %d time %.0f nodes " S64_FORMAT " nps %.0f",depth,SearchCurrent->max_depth,SearchCurrent->time*1000.0,SearchCurrent->node_nb,SearchCurrent->speed);
		  }

		  // update search info

		  if (depth >= 1) SearchInfo->can_stop = true;

		  if (depth == 1
		   && LIST_SIZE(SearchRoot->list) >= 2
		   && LIST_VALUE(SearchRoot->list,0) >= LIST_VALUE(SearchRoot->list,1) + EasyThreshold) {
			 SearchRoot->easy = true;
		  }

		  if (depth > 1) {
			 SearchRoot->bad_2 = SearchRoot->bad_1;
			 SearchRoot->bad_1 = false;
			 ASSERT(SearchRoot->bad_2==(SearchBest->value<=SearchRoot->last_value-BadThreshold));
		  }

		  SearchRoot->last_value = SearchBest[SearchCurrent->multipv].value;

		  // stop search?

		  if (SearchInput->depth_is_limited && SearchCurrent->multipv >= SearchInput->multipv
		   && depth >= SearchInput->depth_limit) {
			 SearchRoot->flag = true;
		  }

		  if (SearchInput->time_is_limited
		   && SearchCurrent->time * 2 >= SearchInput->time_limit_1
		   && !SearchRoot->bad_2) {
			 SearchRoot->flag = true;
		  }

		  if (SearchInput->time_is_limited
		   && SearchCurrent->time >= SearchInput->time_limit_1 * EasyRatio
		   && SearchRoot->easy) {
			 ASSERT(!SearchRoot->bad_2);
			 ASSERT(!SearchRoot->change);
			 SearchRoot->flag = true;
		  }

		  if (SearchInput->time_is_limited
		   && SearchCurrent->time >= SearchInput->time_limit_1 * EarlyRatio
		   && !SearchRoot->bad_2
		   && !SearchRoot->change) {
			 SearchRoot->flag = true;
		  }

		  if (SearchInfo->can_stop 
		   && (SearchInfo->stop || (SearchRoot->flag && !SearchInput->infinite))) {
			  search_ready = true;
			  break;
		  }
	   }
	   if (search_ready)
		   break;
   }
}
Esempio n. 15
0
static void parse_go(char string[]) {

   const char * ptr;
   bool infinite, ponder;
   int depth, mate, movestogo;
   sint64 nodes;
   double binc, btime, movetime, winc, wtime;
   double time, inc;
   double time_max, alloc;

   // init

   infinite = false;
   ponder = false;

   depth = -1;
   mate = -1;
   movestogo = -1;

   nodes = -1;

   binc = -1.0;
   btime = -1.0;
   movetime = -1.0;
   winc = -1.0;
   wtime = -1.0;

   // parse

   ptr = strtok(string," "); // skip "go"

   for (ptr = strtok(NULL," "); ptr != NULL; ptr = strtok(NULL," ")) {

      if (false) {

      } else if (string_equal(ptr,"binc")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         binc = double(atoi(ptr)) / 1000.0;
         ASSERT(binc>=0.0);

      } else if (string_equal(ptr,"btime")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         btime = double(atoi(ptr)) / 1000.0;
         ASSERT(btime>=0.0);

      } else if (string_equal(ptr,"depth")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         depth = atoi(ptr);
         ASSERT(depth>=0);

      } else if (string_equal(ptr,"infinite")) {

         infinite = true;

      } else if (string_equal(ptr,"mate")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         mate = atoi(ptr);
         ASSERT(mate>=0);

      } else if (string_equal(ptr,"movestogo")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         movestogo = atoi(ptr);
         ASSERT(movestogo>=0);

      } else if (string_equal(ptr,"movetime")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         movetime = double(atoi(ptr)) / 1000.0;
         ASSERT(movetime>=0.0);

      } else if (string_equal(ptr,"nodes")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         nodes = my_atoll(ptr);
         ASSERT(nodes>=0);

      } else if (string_equal(ptr,"ponder")) {

         ponder = true;

      } else if (string_equal(ptr,"searchmoves")) {

         // dummy

      } else if (string_equal(ptr,"winc")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         winc = double(atoi(ptr)) / 1000.0;
         ASSERT(winc>=0.0);

      } else if (string_equal(ptr,"wtime")) {

         ptr = strtok(NULL," ");
         if (ptr == NULL) my_fatal("parse_go(): missing argument\n");

         wtime = double(atoi(ptr)) / 1000.0;
         ASSERT(wtime>=0.0);
      }
   }

   // init

   search_clear();

   // depth limit

   // JAS
   int option_depth = 0;
   option_depth = option_get_int("Search Depth");
   if (option_depth > 0) {
   	  depth = option_depth;
   }
   // JAS end

   if (depth >= 0) {
      SearchInput->depth_is_limited = true;
      SearchInput->depth_limit = depth;
   } else if (mate >= 0) {
      SearchInput->depth_is_limited = true;
      SearchInput->depth_limit = mate * 2 - 1; // HACK: move -> ply
   }

   // time limit

   if (COLOUR_IS_WHITE(SearchInput->board->turn)) {
      time = wtime;
      inc = winc;
   } else {
      time = btime;
      inc = binc;
   }

   if (movestogo <= 0 || movestogo > 30) movestogo = 20; // HACK was 30
   if (inc < 0.0) inc = 0.0;

   // JAS
   int option_movetime = 0;
   option_movetime = option_get_int("Search Time");
   if (option_movetime > 0) {
   	  movetime = option_movetime;
   }
   // JAS end

   if (movetime >= 0.0) {

      // fixed time

      SearchInput->time_is_limited = true;
      SearchInput->time_limit_1 = movetime * 5.0; // HACK to avoid early exit
      SearchInput->time_limit_2 = movetime;

   } else if (time >= 0.0) {

      // dynamic allocation

      time_max = time * 0.95 - 1.0;
      if (time_max < 0.0) time_max = 0.0;

      SearchInput->time_is_limited = true;

      alloc = (time_max + inc * double(movestogo-1)) / double(movestogo);
      alloc *= (option_get_bool("Ponder") ? PonderRatio : NormalRatio);
      if (alloc > time_max) alloc = time_max;
      SearchInput->time_limit_1 = alloc;

      alloc = (time_max + inc * double(movestogo-1)) * 0.5;
      if (alloc < SearchInput->time_limit_1) alloc = SearchInput->time_limit_1;
      if (alloc > time_max) alloc = time_max;
      SearchInput->time_limit_2 = alloc;
   }

   if (infinite || ponder) SearchInput->infinite = true;

   // search

   ASSERT(!Searching);
   ASSERT(!Delay);

   Searching = true;
   Infinite = infinite || ponder;
   Delay = false;

   search();
   search_update_current();

   ASSERT(Searching);
   ASSERT(!Delay);

   Searching = false;
   Delay = Infinite;

   if (!Delay) send_best_move();
}
Esempio n. 16
0
void search_full_init(list_t * list, board_t * board) {

   const char * string;
   int trans_move, trans_min_depth, trans_max_depth, trans_min_value, trans_max_value;

   ASSERT(list_is_ok(list));
   ASSERT(board_is_ok(board));

   // null-move options

   string = option_get_string("NullMove Pruning");

   if (false) {
   } else if (my_string_equal(string,"Always")) {
      UseNull = true;
      UseNullEval = false;
   } else if (my_string_equal(string,"Fail High")) {
      UseNull = true;
      UseNullEval = true;
   } else if (my_string_equal(string,"Never")) {
      UseNull = false;
      UseNullEval = false;
   } else {
      ASSERT(false);
      UseNull = true;
      UseNullEval = true;
   }

   NullReduction = option_get_int("NullMove Reduction");

   string = option_get_string("Verification Search");

   if (false) {
   } else if (my_string_equal(string,"Always")) {
      UseVer = true;
      UseVerEndgame = false;
   } else if (my_string_equal(string,"Endgame")) {
      UseVer = true;
      UseVerEndgame = true;
   } else if (my_string_equal(string,"Never")) {
      UseVer = false;
      UseVerEndgame = false;
   } else {
      ASSERT(false);
      UseVer = true;
      UseVerEndgame = true;
   }

   VerReduction = option_get_int("Verification Reduction");

   // history-pruning options

   UseHistory = option_get_bool("History Pruning");
   HistoryValue = (option_get_int("History Threshold") * 16384 + 50) / 100;

   UseExtendedHistory = option_get_bool("Toga Extended History Pruning");
   HistoryBound = (option_get_int("Toga History Threshold") * 16384 + 50) / 100;

   // futility-pruning options

   UseFutility = option_get_bool("Futility Pruning");
   FutilityMargin1 = option_get_int("Futility Margin");
   FutilityMargin2 = option_get_int("Extended Futility Margin");


   // delta-pruning options

   UseDelta = option_get_bool("Delta Pruning");
   DeltaMargin = option_get_int("Delta Margin");

   // quiescence-search options

   CheckNb = option_get_int("Quiescence Check Plies");
   CheckDepth = 1 - CheckNb;

   // standard sort

   list_note(list);
   list_sort(list);

   // basic sort

   trans_move = MoveNone;
   if (UseTrans) trans_retrieve(Trans,board->key,&trans_move,&trans_min_depth,&trans_max_depth,&trans_min_value,&trans_max_value);

   note_moves(list,board,0,trans_move);
   list_sort(list);
}
Esempio n. 17
0
int read_csv()
{
    leveldb_writeoptions_t *write_options;
    char *input_filename = option_get_str("input_file");
    const char *key_ptr, *val_ptr;
    size_t key_len, val_len;
    uint64_t input_row = 0;
    int stats_every = option_get_int("stats_every");
    char *err = NULL;
    char *buffer = NULL;
    char *input_deliminator = option_get_str("input_deliminator");
    int quiet = option_get_int("quiet");
    const char *comma_ptr;
    size_t buffer_len;
    FILE *input_file = stdin;
    
    if (input_filename) {
        input_file = fopen(input_filename, "r");
    }
    
    write_options = leveldb_writeoptions_create();
    
    while ((buffer_len = simplehttp_get_line(input_file, &buffer))) {
        input_row++;
        
        key_ptr = buffer;
        
        if (!quiet && (input_row > 0) && ((input_row % stats_every) == 0)) {
            fprintf(stderr, "line #%"PRIu64"\r", input_row);
        }
        
        // find the index of the first deliminator
        if ((comma_ptr = simplehttp_strnchr(buffer, buffer_len, *input_deliminator)) == NULL) {
            fprintf(stderr, "%s ERROR: LINE %"PRIu64" DELIMINATOR NOT FOUND %s\n", NAME, input_row, buffer);
            return 0;
        }
        key_len = comma_ptr - buffer;
        
        if (!key_len) {
            fprintf(stderr, "%s WARNING: SKIPPING EMPTY KEY, LINE %"PRIu64" %s\n", NAME, input_row, buffer);
            continue;
        }
        val_ptr = comma_ptr;
        val_ptr += strlen(input_deliminator);
        val_len = (buffer + buffer_len) - val_ptr;
        if (*(val_ptr + val_len - 1) == '\n') {
            val_len--;
        }
        
        leveldb_put(ldb, write_options, key_ptr, key_len, val_ptr, val_len, &err);
        if (err) {
            break;
        }
        
    }
    
    leveldb_writeoptions_destroy(write_options);
    
    if (err) {
        fprintf(stderr, "Error (LINE %"PRIu64"): %s\n", input_row, err);
        return 0;
    }
    
    return 1;
}
Esempio n. 18
0
static void search_update() {

   int move;
   int move_nb;
   board_t board[1];

   ASSERT(!Uci->searching);

   // launch a new search if needed

   if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {

      // opening book

      if (State->state == THINK && option_get_bool("Book")) {

         game_get_board(Game,Uci->board);

         move = book_move(Uci->board,option_get_bool("BookRandom"));

         if (move != MoveNone && move_is_legal(move,Uci->board)) {

            my_log("POLYGLOT *BOOK MOVE*\n");

            search_clear(); // clears Uci->ponder_move
            Uci->best_move = move;

            board_copy(board,Uci->board);
            move_do(board,move);
            Uci->ponder_move = book_move(board,false); // expected move = best book move

            Uci->best_pv[0] = Uci->best_move;
            Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
            Uci->best_pv[2] = MoveNone;

            comp_move(Uci->best_move);

            return;
         }
      }

      // engine search

      my_log("POLYGLOT START SEARCH\n");

      // options

      uci_send_option(Uci,"UCI_Chess960","%s",option_get_bool("Chess960")?"true":"false");

      if (option_get_int("UCIVersion") >= 2) {
         uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
         uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
      }

      uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");

      // position

      move = (State->state == PONDER) ? State->exp_move : MoveNone;
      send_board(move); // updates Uci->board global variable

      // search

      if (State->state == THINK || State->state == PONDER) {

         engine_send_queue(Engine,"go");

         if (XB->time_limit) {

            // fixed time per move

            engine_send_queue(Engine," movetime %.0f",XB->time_max*1000.0);

         } else {

            // time controls

            if (colour_is_white(Uci->board->turn)) {
               engine_send_queue(Engine," wtime %.0f btime %.0f",XB->my_time*1000.0,XB->opp_time*1000.0);
            } else {
               engine_send_queue(Engine," wtime %.0f btime %.0f",XB->opp_time*1000.0,XB->my_time*1000.0);
            }

            if (XB->inc != 0.0) engine_send_queue(Engine," winc %.0f binc %.0f",XB->inc*1000.0,XB->inc*1000.0);

            if (XB->mps != 0) {

               move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
               ASSERT(move_nb>=1&&move_nb<=XB->mps);

               engine_send_queue(Engine," movestogo %d",move_nb);
            }
         }

         if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);

         if (State->state == PONDER) engine_send_queue(Engine," ponder");

         engine_send(Engine,""); // newline

      } else if (State->state == ANALYSE) {

         engine_send(Engine,"go infinite");

      } else {

         ASSERT(false);
      }

      // init search info

      ASSERT(!Uci->searching);

      search_clear();

      Uci->searching = true;
      Uci->pending_nb++;
   }
}
Esempio n. 19
0
void engine_open(engine_t * engine) {

   const char * dir, * command;
   char string[StringSize];
   int argc;
   char * ptr;
   char * argv[256];
   int from_engine[2], to_engine[2];
   pid_t pid;

   ASSERT(engine!=NULL);

   // init

   dir = option_get_string("EngineDir");
   my_log("POLYGLOT Dir \"%s\"\n",dir);

   command = option_get_string("EngineCommand");
   my_log("POLYGLOT Command \"%s\"\n",command);

   // parse the command line and create the argument list

   if (strlen(command) >= StringSize) my_fatal("engine_open(): buffer overflow\n");
   strcpy(string,command);

   argc = 0;

   for (ptr = strtok(string," "); ptr != NULL; ptr = strtok(NULL," ")) {
      argv[argc++] = ptr;
   }

   argv[argc] = NULL;

   // create the pipes

   if (pipe(from_engine) == -1) {
      my_fatal("engine_open(): pipe(): %s\n",strerror(errno));
   }

   if (pipe(to_engine) == -1) {
      my_fatal("engine_open(): pipe(): %s\n",strerror(errno));
   }

   // create the child process

   pid = fork();

   if (pid == -1) {

      my_fatal("engine_open(): fork(): %s\n",strerror(errno));

   } else if (pid == 0) {

      // child = engine

      // close unused pipe descriptors to avoid deadlocks

      my_close(from_engine[0]);
      my_close(to_engine[1]);

      // attach the pipe to standard input

      my_dup2(to_engine[0],STDIN_FILENO);
      my_close(to_engine[0]);

      // attach the pipe to standard output

      my_dup2(from_engine[1],STDOUT_FILENO);
      my_close(from_engine[1]);

      // attach standard error to standard output

      my_dup2(STDOUT_FILENO,STDERR_FILENO);

      // set a low priority

      if (option_get_bool("UseNice"))
      {
          my_log("POLYGLOT Adjust Engine Piority");
          nice(+option_get_int("NiceValue"));
      }

      // change the current directory

      if (dir[0] != '\0' && chdir(dir) == -1) {
         my_fatal("engine_open(): chdir(): %s\n",strerror(errno));
      }

      // launch the new executable file

      execvp(argv[0],&argv[0]);

      // execvp() only returns when an error has occured

      my_fatal("engine_open(): execvp(): %s\n",strerror(errno));

   } else { // pid > 0

      ASSERT(pid>0);

      // parent = PolyGlot

      // close unused pipe descriptors to avoid deadlocks

      my_close(from_engine[1]);
      my_close(to_engine[0]);

      // fill in the engine struct

      engine->io->in_fd = from_engine[0];
      engine->io->out_fd = to_engine[1];
      engine->io->name = "Engine";

      io_init(engine->io);
   }
}
Esempio n. 20
0
void material_init() {

   // UCI options

   bitbase_pieces =  (option_get_int("Bitbase pieces") -2);

   //MaterialWeight = (option_get_int("Material") * 256 + 50) / 100;
   bad_trade_value = option_get_int("Bad Trade Value");

   BishopPairOpening = option_get_int("Bishop Pair Opening");
   BishopPairEndgame = option_get_int("Bishop Pair Endgame");

   Queen_Knight_combo = option_get_int("Queen Knight combo");
   Rook_Bishop_combo = option_get_int("Rook Bishop combo");

   PawnOpening = option_get_int("Opening Pawn Value");
   KnightOpening = option_get_int("Opening Knight Value");
   BishopOpening = option_get_int("Opening Bishop Value");
   RookOpening = option_get_int("Opening Rook Value");
   QueenOpening = option_get_int("Opening Queen Value");

   PawnEndgame = option_get_int("Endgame Pawn Value");
   KnightEndgame = option_get_int("Endgame Knight Value");
   BishopEndgame = option_get_int("Endgame Bishop Value");
   RookEndgame = option_get_int("Endgame Rook Value");
   QueenEndgame = option_get_int("Endgame Queen Value");

   // material table

   Material->size = 0;
   Material->mask = 0;
   Material->table = NULL;
}
Esempio n. 21
0
static void search_update() {

   int move;
   int move_nb;
   board_t board[1];

   ASSERT(!Uci->searching);



   
   // launch a new search if needed

   

   if (State->state == THINK || State->state == PONDER || State->state == ANALYSE) {

      // [VdB] moved up as we need the move number

       game_get_board(Game,Uci->board);

      // opening book

       if (State->state == THINK &&
           option_get_bool(Option,"Book") &&
           Uci->board->move_nb<option_get_int(Option,"BookDepth")
           ) {


         move = book_move(Uci->board,option_get_bool(Option,"BookRandom"));

         if (move != MoveNone && move_is_legal(move,Uci->board)) {

            my_log("POLYGLOT *BOOK MOVE*\n");

            search_clear(); // clears Uci->ponder_move
            Uci->best_move = move;

            board_copy(board,Uci->board);
            move_do(board,move);
            Uci->ponder_move = book_move(board,FALSE); // expected move = best book move

            Uci->best_pv[0] = Uci->best_move;
            Uci->best_pv[1] = Uci->ponder_move; // can be MoveNone
            Uci->best_pv[2] = MoveNone;

            comp_move(Uci->best_move);

            return;
         }
      }

      // engine search

      my_log("POLYGLOT START SEARCH\n");

      // options

      uci_send_option(Uci,"UCI_3Check","%s",
                      option_get_bool(Option,"3Check")?"true":"false");
      uci_send_option(Uci,"UCI_Chess960","%s",
                      option_get_bool(Option,"Chess960")?"true":"false");
      uci_send_option(Uci,"UCI_Atomic","%s",
                      option_get_bool(Option,"Atomic")?"true":"false");
      uci_send_option(Uci,"UCI_Horde","%s",
                      option_get_bool(Option,"Horde")?"true":"false");

      if (option_get_int(Option,"UCIVersion") >= 2) {
         uci_send_option(Uci,"UCI_Opponent","none none %s %s",(XB->computer)?"computer":"human",XB->name);
         uci_send_option(Uci,"UCI_AnalyseMode","%s",(XB->analyse)?"true":"false");
      }

      uci_send_option(Uci,"Ponder","%s",ponder()?"true":"false");

      // position

      move = (State->state == PONDER) ? State->exp_move : MoveNone;
      send_board(move); // updates Uci->board global variable

      // search

      if (State->state == THINK || State->state == PONDER) {

         engine_send_queue(Engine,"go");

         if (XB->time_limit) {

            // fixed time per move
             
             if(XB->node_rate > 0){
                 engine_send_queue(Engine,
                                   " nodes %.0f",
                                   XB->time_max*((double)XB->node_rate));
             }else{
		 double computed_time;
		 double st_fudge;
		 st_fudge=(double) option_get_int(Option,"STFudge");
		 my_log("POLYGLOT Giving engine %.0fmsec extra time.\n",st_fudge);
		 computed_time=XB->time_max*1000.0-st_fudge;
		 if(computed_time< 1.0){
		     computed_time=1.0;
		 }
                 engine_send_queue(Engine,
                                   " movetime %.0f",
                                   computed_time);
             }

         } else {

            // time controls

                 if(XB->node_rate > 0) {
                     double time;
                     move_nb = 40;
                     if (XB->mps != 0){
                         move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
                     }
                     time = XB->my_time / move_nb;
                     if(XB->inc != 0){
                         time += XB->inc;
                     }
                     if(time > XB->my_time){
                         time = XB->my_time;
                     }
                     engine_send_queue(Engine,
                                       " nodes %.0f",
                                       time*XB->node_rate);
                 } else {
                     
                     if (colour_is_white(Uci->board->turn)) {
                         engine_send_queue(Engine,
                                           " wtime %.0f btime %.0f",
                                           XB->my_time*1000.0,XB->opp_time*1000.0);
                     } else {
                         engine_send_queue(Engine,
                                           " wtime %.0f btime %.0f",
                                           XB->opp_time*1000.0,XB->my_time*1000.0);
                     }
                     
                     if (XB->inc != 0.0){
                         engine_send_queue(Engine,
                                           " winc %.0f binc %.0f",
                                           XB->inc*1000.0,XB->inc*1000.0);
                     }
                     if (XB->mps != 0) {

                         move_nb = XB->mps - (Uci->board->move_nb % XB->mps);
                         ASSERT(move_nb>=1&&move_nb<=XB->mps);
                         
                         engine_send_queue(Engine," movestogo %d",move_nb);
                     }
                 }
         }
         if (XB->depth_limit) engine_send_queue(Engine," depth %d",XB->depth_max);

         if (State->state == PONDER) engine_send_queue(Engine," ponder");

         engine_send(Engine,""); // newline

      } else if (State->state == ANALYSE) {

         engine_send(Engine,"go infinite");

      } else {

         ASSERT(FALSE);
      }

      // init search info

      ASSERT(!Uci->searching);

      search_clear();

      Uci->searching = TRUE;
      Uci->pending_nb++;
   }
}
Esempio n. 22
0
static void parse_setoption(char string[]) {

   const char * name;
   char * value;

   // init

   name = strstr(string,"name ");
   value = strstr(string,"value ");

   if (name == NULL || value == NULL || name >= value) return; // ignore buttons

   value[-1] = '\0'; // HACK
   name += 5;
   value += 6;

   // update

   option_set(name,value);

   // update transposition-table size if needed

   if (Init && my_string_equal(name,"Hash")) { // Init => already allocated

      ASSERT(!Searching);

      if (option_get_int("Hash") >= 4) {
         trans_free();
         trans_alloc();
      }
   }

   // update endgame tablebases if needed

   if (my_string_equal(name,"NalimovPath")) {

      ASSERT(!Searching);
      tb_path(option_get_string("NalimovPath"));
   }

   if (my_string_equal(name,"NalimovCache")) {

      ASSERT(!Searching);
      tb_cache(option_get_int("NalimovCache"));
   }

   if (my_string_equal(name,"EgbbPath")) {

      ASSERT(!Searching);
      egbb_path(option_get_string("EgbbPath"));
   }

   if (my_string_equal(name,"EgbbCache")) {

      ASSERT(!Searching);
      egbb_cache(option_get_int("EgbbCache"));
   }

   // update pawn structure if needed

   if (my_string_equal(name,"Pawn Structure")) {

      ASSERT(!Searching);
      pawn_structure_set_option(option_get_int("Pawn Structure"));
      pst_init();
   }

   // update piece activity if needed

   if (my_string_equal(name,"Piece Activity")) {

      ASSERT(!Searching);
      trans_clear();
      pst_init();
   }

   // update king safty if needed

   if (my_string_equal(name,"King Safety")) {

      ASSERT(!Searching);
      trans_clear();
      pst_init();
   }

   // update pawn activity if needed

   if (my_string_equal(name,"Pawn Activity")) {

      ASSERT(!Searching);
      trans_clear();
      pst_init();
   }

   // update material if needed

   if (my_string_equal(name,"Material")) {

      ASSERT(!Searching);
      material_set_option(option_get_int("Material"));
   }

   // update piece value if needed

   if (my_string_equal(name,"Pawn")) {

      ASSERT(!Searching);
      piece_set_option(option_get_int("Pawn"),MAT_PAWN);
   }

   if (my_string_equal(name,"Knight")) {

      ASSERT(!Searching);
      piece_set_option(option_get_int("Knight"),MAT_KNIGHT);
   }

   if (my_string_equal(name,"Bishop")) {

      ASSERT(!Searching);
      piece_set_option(option_get_int("Bishop"),MAT_BISHOP);
   }

   if (my_string_equal(name,"Rook")) {

      ASSERT(!Searching);
      piece_set_option(option_get_int("Rook"),MAT_ROOK);
   }

   if (my_string_equal(name,"Queen")) {

      ASSERT(!Searching);
      piece_set_option(option_get_int("Queen"),MAT_QUEEN);
   }

   if (my_string_equal(name,"Bishop Pair")) {

      ASSERT(!Searching);
      piece_set_option(option_get_int("Bishop Pair"),MAT_BISHOP_PAIR);
   }
}
Esempio n. 23
0
void loop_step(const char * input) {

   char string[65536];
   char buffer[256];

   // read a line

   if (input != NULL) {
      get_local(string,65536,input);
   } else {
      get_stdin(string,65536);
   }

   // parse

   if (false) {

   } else if (string_start_with(string,"debug ")) {

      if (!Searching) {
         parse_debug(string);
      } else {
         ASSERT(false);
      }

   } else if (string_start_with(string,"go ")) {

      if (!Searching && !Delay) {
         init();
         parse_go(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"isready")) {

      if (!Searching && !Delay) {
         init();
      }

      send("readyok"); // no need to wait when searching (fixit SMK)

   } else if (string_equal(string,"ponderhit")) {

      if (Searching) {

         ASSERT(Infinite);

         SearchInput->infinite = false;
         Infinite = false;

      } else if (Delay) {

         send_best_move();
         Delay = false;

      } else {

         ASSERT(false);
      }

   } else if (string_start_with(string,"position ")) {

      if (!Searching && !Delay) {
         init();
         parse_position(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"quit")) {

      ASSERT(!Searching); // violated by Arena UI
      ASSERT(!Delay);

      if (Searching && option_get_int("Threads") > 1) {

         // Arena issue

         SearchInfo->stop = true;
         Infinite = false;

         smp_sleep();
      }

      trans_free();
      tb_close();
      egbb_close();
      smp_close();

      exit(EXIT_SUCCESS);

   } else if (string_start_with(string,"setoption ")) {

      if (!Searching && !Delay) {
         parse_setoption(string);
      } else {
         ASSERT(false);
      }

   } else if (string_equal(string,"stop")) {

      if (Searching) {

         SearchInfo->stop = true;
         Infinite = false;

      } else if (Delay) {

         send_best_move();
         Delay = false;
      }

   } else if (string_equal(string,"uci")) {

      ASSERT(!Searching);
      ASSERT(!Delay);

      send("id name Fruit reloaded %s", my_version(buffer));
      send("id author Fabien Letouzey, Ryan Benitez and Daniel Mehrmann");

      option_list();

      send("uciok");

   } else if (string_equal(string,"ucinewgame")) {

      if (!Searching && !Delay && Init) {
         trans_clear();
         pawn_clear();
         material_clear();
      } else {
         ASSERT(false);
      }
   }
}
Esempio n. 24
0
void search_full_init(list_t * list, board_t * board, int ThreadId) {

   const char * string;
   int trans_move, trans_depth, trans_flags, trans_value;
   int i, j;
   entry_t * found_entry;

   ASSERT(list_is_ok(list));
   ASSERT(board_is_ok(board));

   // null-move options

   string = option_get_string("NullMove Pruning");

   if (false) {
   } else if (my_string_equal(string,"Always")) {
      UseNull = true;
      UseNullEval = false;
   } else if (my_string_equal(string,"Fail High")) {
      UseNull = true;
      UseNullEval = true;
   } else if (my_string_equal(string,"Never")) {
      UseNull = false;
      UseNullEval = false;
   } else {
      ASSERT(false);
      UseNull = true;
      UseNullEval = true;
   }

   NullReduction = option_get_int("NullMove Reduction");

   string = option_get_string("Verification Search");

   if (false) {
   } else if (my_string_equal(string,"Always")) {
      UseVer = true;
      UseVerEndgame = false;
   } else if (my_string_equal(string,"Endgame")) {
      UseVer = true;
      UseVerEndgame = true;
   } else if (my_string_equal(string,"Never")) {
      UseVer = false;
      UseVerEndgame = false;
   } else {
      ASSERT(false);
      UseVer = true;
      UseVerEndgame = true;
   }

   VerReduction = option_get_int("Verification Reduction");

   // history-pruning options

   UseHistory = option_get_bool("History Pruning");
   HistoryValue = (option_get_int("History Threshold") * 16384 + 50) / 100;

   
   // Stockfish Late Move Reductions
   // about 20 elo better than previous Toga History Reductions

   for (i = 0; i < 64; i++){   
      for (j = 0; j < 64; j++){
         if (i == 0 || j == 0){
            quietPvMoveReduction[i][j] = quietMoveReduction[i][j] = 0;
         }
         else{
            double pvReduction =
               log((double) (i)) * log((double) (j)) / 3.0;
            double nonPvReduction =
               (1.0 + log((double) (i))) * log((double) (j)) / 2.5; // JD
            quietPvMoveReduction[i][j] =
               (int) (pvReduction >= 1.0 ?
                      floor(pvReduction) : 0);
            quietMoveReduction[i][j] =
               (int) (nonPvReduction >= 1.0 ?
                      floor(nonPvReduction) : 0);
         }
      }
   }

   // futility-pruning options

   UseFutility = option_get_bool("Futility Pruning");
   FutilityMargin1 = option_get_int("Futility Margin");
   FutilityMargin2 = option_get_int("Extended Futility Margin");

   // delta-pruning options

   UseDelta = option_get_bool("Delta Pruning");
   DeltaMargin = option_get_int("Delta Margin");

   // quiescence-search options

   SearchCurrent[ThreadId]->CheckNb = option_get_int("Quiescence Check Plies");
   SearchCurrent[ThreadId]->CheckDepth = 1 - SearchCurrent[ThreadId]->CheckNb;

   // standard sort

   list_note(list);
   list_sort(list);
   
   // misc
   
   SearchCurrent[ThreadId]->last_move = MoveNone;

   // basic sort

   trans_move = MoveNone;
   if (UseTrans) trans_retrieve(Trans,&found_entry,board->key,&trans_move,&trans_depth,&trans_flags,&trans_value);
   note_moves(list,board,0,trans_move,ThreadId);
   list_sort(list);
}