Esempio n. 1
0
void uci_clear(uci_t * uci) {

   ASSERT(uci_is_ok(uci));

   ASSERT(!uci->searching);

   uci->best_move = MoveNone;
   uci->ponder_move = MoveNone;

   uci->score = 0;
   uci->depth = 0;
   uci->sel_depth = 0;
   line_clear(uci->pv);

   uci->best_score = 0;
   uci->best_depth = 0;
   uci->best_sel_depth = 0;
   line_clear(uci->best_pv);

   uci->node_nb = 0;
   uci->time = 0.0;
   uci->speed = 0.0;
   uci->cpu = 0.0;
   uci->hash = 0.0;
   line_clear(uci->current_line);

   uci->root_move = MoveNone;
   uci->root_move_pos = 0;
   uci->root_move_nb = board_mobility(uci->board);
}
Esempio n. 2
0
static void history_up(t_tokenline *tl)
{
	int entry;

	if (tl->hist_step == -1)
		entry = history_previous(tl, tl->hist_end);
	else
		entry = history_previous(tl, tl->hist_step);
	if (entry == -1)
		return;
	line_clear(tl);
	set_line(tl, tl->hist_buf + entry);
	tl->hist_step = entry;
}
Esempio n. 3
0
static void history_down(t_tokenline *tl)
{
	int i;

	if (tl->hist_step == -1)
		return;

	line_clear(tl);
	if (tl->hist_step == tl->hist_end) {
		tl->hist_step = -1;
		return;
	}

	i = tl->hist_step;
	while (tl->hist_buf[i]) {
		if (++i == TL_MAX_HISTORY_SIZE)
			i = 0;
	}
	if (++i == TL_MAX_HISTORY_SIZE)
		i = 0;

	set_line(tl, tl->hist_buf + i);
	tl->hist_step = i;
}
Esempio n. 4
0
static void epd_test_file(const char file_name[]) {

   FILE * file;
   int hit, tot;
   char epd[StringSize];
   char am[StringSize], bm[StringSize], id[StringSize];
   board_t board[1];
   char string[StringSize];
   int move;
   char pv_string[StringSize];
   bool correct;
   double depth_tot, time_tot, node_tot;

   ASSERT(file_name!=NULL);

   // init

   file = fopen(file_name,"r");
   if (file == NULL) my_fatal("epd_test_file(): can't open file \"%s\": %s\n",file_name,strerror(errno));

   hit = 0;
   tot = 0;

   depth_tot = 0.0;
   time_tot = 0.0;
   node_tot = 0.0;

   // loop

   while (my_file_read_line(file,epd,StringSize)) {

      if (UseTrace) printf("%s\n",epd);

      if (!epd_get_op(epd,"am",am,StringSize)) strcpy(am,"");
      if (!epd_get_op(epd,"bm",bm,StringSize)) strcpy(bm,"");
      if (!epd_get_op(epd,"id",id,StringSize)) strcpy(id,"");

      if (my_string_empty(am) && my_string_empty(bm)) {
         my_fatal("epd_test(): no am or bm field in EPD\n");
      }

      // init

      uci_send_ucinewgame(Uci);
      uci_send_isready_sync(Uci);

      ASSERT(!Uci->searching);

      // position

      if (!board_from_fen(board,epd)) ASSERT(false);
      if (!board_to_fen(board,string,StringSize)) ASSERT(false);

      engine_send(Engine,"position fen %s",string);

      // search

      engine_send(Engine,"go movetime %.0f depth %d",MaxTime*1000.0,MaxDepth);
      // engine_send(Engine,"go infinite");

      // engine data

      board_copy(Uci->board,board);

      uci_clear(Uci);
      Uci->searching = true;
      Uci->pending_nb++;

      FirstMove = MoveNone;
      FirstDepth = 0;
      FirstSelDepth = 0;
      FirstScore = 0;
      FirstTime = 0.0;
      FirstNodeNb = 0;
      line_clear(FirstPV);

      LastMove = MoveNone;
      LastDepth = 0;
      LastSelDepth = 0;
      LastScore = 0;
      LastTime = 0.0;
      LastNodeNb = 0;
      line_clear(LastPV);

      // parse engine output

      while (engine_step()) {

         // stop search?

         if (Uci->depth > MaxDepth
          || Uci->time >= MaxTime
          || (Uci->depth - FirstDepth >= DepthDelta
           && Uci->depth > MinDepth
           && Uci->time >= MinTime
           && is_solution(FirstMove,board,bm,am))) {
            engine_send(Engine,"stop");
         }
      }

      move = FirstMove;
      correct = is_solution(move,board,bm,am);

      if (correct) hit++;
      tot++;

      if (correct) {
         depth_tot += double(FirstDepth);
         time_tot += FirstTime;
         node_tot += double(FirstNodeNb);
      }

      printf("%s %d %4d %4d",id,correct,hit,tot);

      if (!line_to_san(LastPV,Uci->board,pv_string,StringSize)) ASSERT(false);
      printf(" - %2d %6.2f %9lld %+6.2f %s\n",FirstDepth,FirstTime,FirstNodeNb,double(LastScore)/100.0,pv_string);
   }

   printf("%d/%d",hit,tot);

   if (hit != 0) {

      depth_tot /= double(hit);
      time_tot /= double(hit);
      node_tot /= double(hit);

      printf(" - %.1f %.2f %.0f",depth_tot,time_tot,node_tot);
   }

   printf("\n");

   fclose(file);
}
Esempio n. 5
0
/* 
 * Minimum Width
 * The minimum width is the distance between the closest parallel lines
 * between which all of the point are placed.
 * To find the minimal width,
 * we'll go through a sequence of pairs of parallel lines the hold all of the points between them.
 * In each pair, one of the lines must coincide with a polygon edge.
 * The other line will be the most distant line that still touches the polygon
 * (There are at most n pairs). 
 * 1. For each pair, we'll find the distance between the two lines,
 *    and the width is the minimal distance.
 * 2. We'll find the pairs in the following way:
 * 3. The first pair will consist of two vertical lines that
 *    go thru the most left and the most right points.
 * 4. Then, for each pair, (exactly like in the diameter)
 *    we rotate the two parallel lines in the angle that
 *    move one line to lie on a new edge. 
 */
real_t convexhull_compute_min_width(line_t *width, polygon_t *chull)
{
  int i;
  real_t *ang, dist, value, rotate;
  real_t da, db, alpha, beta, xmin, xmax;
  dlink_t *ax, *ax_next;
  dlink_t *bx, *bx_next;
  dlink_t *x, *lx, *rx;
  point_t *a, *b, *p, *q;
  line_t *line;

  assert(width);
  assert(chull);
  assert(polygon_get_count(chull) >= 3);

  ang = (real_t *)malloc(polygon_get_count(chull) * sizeof(real_t));
  assert(ang);

  // Find the angle between verteces
  i = 0;
  ax = chull->head->prev;
  bx = chull->tail->next;
  do {
    a = (point_t *)ax->object;
    b = (point_t *)bx->object;

    ang[i] = arctan2r(point_get_y(b) - point_get_y(a),
		      point_get_x(b) - point_get_x(a));
    ang[i] = ang[i] > M_PI ? ang[i] - M_PI : ang[i];

    ax->spare = (void *)&ang[i];
    printf("(%lf,%lf) -> (%lf,%lf) < %lf deg\n",
	   point_get_x(a), point_get_y(a),
	   point_get_x(b), point_get_y(b),
	   ang[i] * 180 / M_PI);

    i++;
    ax = bx;
    bx = bx->next;
  } while (bx != chull->head);
  printf("Initialized the angle between verteces\n");

  // Find the most left and right point
  lx = NULL;
  rx = NULL;
  for (x = chull->tail->next; x != chull->head; x = x->next) {
    p = (point_t *)x->object;
    if (lx == NULL || point_get_x(p) < xmin) {
      lx = x;
      xmin = point_get_x(p);
    }
    if (rx == NULL || point_get_x(p) > xmax) {
      rx = x;
      xmax = point_get_x(p);
    }
  }
  printf("Founded the leftmost and rightmost points\n");

  ax = lx;
  bx = rx;
  dist = 0.0;
  p = NULL;
  q = NULL;
  rotate = M_PI_2; // pi / 2;
  line = line_new();

  do {
    a = (point_t *)ax->object;
    b = (point_t *)bx->object;
    printf("(%lf,%lf) -> (%lf,%lf)\n",
	   point_get_x(a), point_get_y(a),
	   point_get_x(b), point_get_y(b));

    value = get_distance_of_p2p(a, b);
    if (p == NULL || q == NULL || value < dist) {
      p = a;
      q = b;
      dist = value;
    }

    alpha = *((real_t *)ax->spare);
    beta = *((real_t *)bx->spare);

    printf("rotate: %lf, alpha: %lf, beta: %lf\n",
	   rotate * 180 / M_PI, alpha * 180 / M_PI, beta * 180 / M_PI);
    /*
    circle(((point_t *)a->object)->x, ((point_t *)a->object)->y, 10, 255, 0, 0);
    circle(((point_t *)b->object)->x, ((point_t *)b->object)->y, 10, 255, 0, 0);
    keyhit();
    circle(((point_t *)a->object)->x, ((point_t *)a->object)->y, 10, 0, 0, 0);
    circle(((point_t *)b->object)->x, ((point_t *)b->object)->y, 10, 0, 0, 0);
    */

    // Make alpha and beta in the range of acute angle
    if (alpha >= rotate) da = alpha - rotate;
    else da = M_PI + alpha - rotate;

    if (beta >= rotate) db = beta - rotate;
    else db = M_PI + beta - rotate;

    if (da < db) {
      rotate = alpha;
      if (ax->next == chull->head) ax_next = chull->tail->next;
      else ax_next -= ax->next;

      line_set_endpoints(line, a, (point_t *)ax_next->object);

      value = distance_from_point_to_line(b, line);
      if (p == NULL || q == NULL || value < dist) {
	p = a;
	q = b;
	dist = value;
      }
      line_clear(line);
      ax = ax_next;
    } else {
      rotate = beta;
      if (bx->next == chull->head) bx_next = chull->tail->next;
      else bx_next = bx->next;
      
      line_set_endpoints(line, b, (point_t *)bx_next->object);
      
      value = distance_from_point_to_line(a, line);
      if (p == NULL || q == NULL || value < dist) {
	p = b;
	q = a;
	dist = value;
      }
      line_clear(line);
      bx = bx_next;
    }
  } while (!((ax == rx && bx == lx) || (bx == rx && ax == lx)));

  for (x = chull->tail->next; x != chull->head; x = x->next)
    x->spare = NULL;

  line_set_endpoints(width, p, q);
  //line_assign(width, p, q);

  free(ang);

  return dist;
}
Esempio n. 6
0
void search(const board_t * board, int depth_max, double time_max) {

   char string[256];

   ASSERT(board_is_ok(board));
   ASSERT(depth_max>=1&&depth_max<DepthMax);
   ASSERT(time_max>=0.0);

   // engine

   Depth = 0;

   BestMove = MoveNone;
   BestValue = 0;
   line_clear(BestPV);

   NodeNb = 0;
   LeafNb = 0;
   Time = 0.0;

   Move = MoveNone;
   MovePos = 0;
   MoveNb = 0;

   // init

   uci_send_ucinewgame(Uci);
   uci_send_isready_sync(Uci);

   // position

   if (!board_to_fen(board,string,256)) ASSERT(false);
   engine_send(Engine,"position fen %s",string);

   // search

   engine_send_queue(Engine,"go");

   engine_send_queue(Engine," movetime %.0f",time_max*1000.0);
   engine_send_queue(Engine," depth %d",depth_max);

   engine_send(Engine,""); // newline

   // wait for feed-back

   while (true) {

      engine_get(Engine,string,256);

      if (false) {

      } else if (match(string,"bestmove * ponder *")) {

         BestMove = move_from_can(Star[0],board);
         ASSERT(BestMove!=MoveNone&&move_is_legal(BestMove,board));

         break;

      } else if (match(string,"bestmove *")) {

         BestMove = move_from_can(Star[0],board);
         ASSERT(BestMove!=MoveNone&&move_is_legal(BestMove,board));

         break;
      }
   }

   printf("\n");
}