// Static evaluation.  Returns score
score_t eval(position_t *p, bool verbose) {
  // seed rand_r with a value of 1, as per
  // http://linux.die.net/man/3/rand_r
  static __thread unsigned int seed = 1;
  // verbose = true: print out components of score
  ev_score_t score[2] = { 0, 0 };
  //  int corner[2][2] = { {INF, INF}, {INF, INF} };
  ev_score_t bonus;

  //char buf[MAX_CHARS_IN_MOVE];
  color_t c;
  for (fil_t f = 0; f < BOARD_WIDTH; f++) {
    for (rnk_t r = 0; r < BOARD_WIDTH; r++) {
      square_t sq = square_of(f, r);
      piece_t x = p->board[sq];

      //if (verbose) {
      //  square_to_str(sq, buf, MAX_CHARS_IN_MOVE);
      //}

      switch (ptype_of(x)) {
        case EMPTY:
          break;
        case PAWN:
          c = color_of(x);

          // MATERIAL heuristic: Bonus for each Pawn
          bonus = PAWN_EV_VALUE;
          // if (verbose) {
          //  printf("MATERIAL bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf);
          // }
          score[c] += bonus;

          // PBETWEEN heuristic
          bonus = pbetween(p, f, r);
          // if (verbose) {
          //   printf("PBETWEEN bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf);
          // }
          score[c] += bonus;

          // PCENTRAL heuristic
          bonus = pcentral(f, r);
          // if (verbose) {
          //   printf("PCENTRAL bonus %d for %s Pawn on %s\n", bonus, color_to_str(c), buf);
         //  }
          score[c] += bonus;
          break;

        case KING:
          c = color_of(x);

          // KFACE heuristic
          bonus = kface(p, f, r);
          // if (verbose) {
          //   printf("KFACE bonus %d for %s King on %s\n", bonus,
          //          color_to_str(c), buf);
          // }
          score[c] += bonus;

          // KAGGRESSIVE heuristic
          color_t othercolor = opp_color(c);
          square_t otherking = p->kloc[othercolor];
          fil_t otherf = fil_of(otherking);
          rnk_t otherr = rnk_of(otherking);
          bonus = kaggressive(f, r, otherf, otherr);
          assert(bonus == kaggressive_old(p, f, r));

          // if (verbose) {
          //   printf("KAGGRESSIVE bonus %d for %s King on %s\n", bonus, color_to_str(c), buf);
         //  }
          score[c] += bonus;
          break;
        case INVALID:
          break;
        default:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
      }
      laser_map_black[sq] = 0;
      laser_map_white[sq] = 0;
    }
  }
   
  int black_pawns_unpinned = mark_laser_path(p, laser_map_white, WHITE, 1);  // 1 = path of laser with no moves
  
  ev_score_t w_hattackable = HATTACK * (int) h_attackable;
  score[WHITE] += w_hattackable;
  // if (verbose) {
  //   printf("HATTACK bonus %d for White\n", w_hattackable);
  // }

  // PAWNPIN Heuristic --- is a pawn immobilized by the enemy laser.
  int b_pawnpin = PAWNPIN * black_pawns_unpinned;
  score[BLACK] += b_pawnpin;

  int b_mobility = MOBILITY * mobility(p, BLACK);
  score[BLACK] += b_mobility;
  // if (verbose) {
  //   printf("MOBILITY bonus %d for Black\n", b_mobility);
  // }

  int white_pawns_unpinned = mark_laser_path(p, laser_map_black, BLACK, 1);  // 1 = path of laser with no moves
  
  ev_score_t b_hattackable = HATTACK * (int) h_attackable;
  score[BLACK] += b_hattackable;
  // if (verbose) {
  //   printf("HATTACK bonus %d for Black\n", b_hattackable);
  // }

  int w_mobility = MOBILITY * mobility(p, WHITE);
  score[WHITE] += w_mobility;
  // if (verbose) {
  //   printf("MOBILITY bonus %d for White\n", w_mobility);
  // }
  int w_pawnpin = PAWNPIN * white_pawns_unpinned;
  score[WHITE] += w_pawnpin;


  // score from WHITE point of view
  ev_score_t tot = score[WHITE] - score[BLACK];

  if (RANDOMIZE) {
    ev_score_t  z = rand_r(&seed) % (RANDOMIZE*2+1);
    tot = tot + z - RANDOMIZE;
  }

  if (color_to_move_of(p) == BLACK) {
    tot = -tot;
  }

  return tot / EV_SCORE_RATIO;
}
Example #2
0
// Static evaluation.  Returns score
score_t eval(position_t *p, bool verbose, full_board_t* board) {
  // seed rand_r with a value of 1, as per
  // http://linux.die.net/man/3/rand_r
  static __thread unsigned int seed = 1;
  // verbose = true: print out components of score
  ev_score_t score[2] = { 0, 0 };
  //  int corner[2][2] = { {INF, INF}, {INF, INF} };
  ev_score_t bonus;

  color_t c = WHITE;
  while (true) {
    for (pnum_t pnum = 0; pnum <= board->pawn_count[c]; pnum++) {
      square_t sq = board->pieces[c][pnum];
      rnk_t r = rnk_of(sq);
      fil_t f = fil_of(sq);
      piece_t x = board->board[sq];

      switch (ptype_of(x)) {
        case EMPTY:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
        case PAWN:
          // PBETWEEN heuristic
          bonus = pbetween(p, r, f, board);
          score[c] += bonus;

          // PCENTRAL heuristic
          bonus = pcentral(r, f);
          score[c] += bonus;
          break;

        case KING:
          // KFACE heuristic
          bonus = kface(p, r, f, board);
          score[c] += bonus;

          // KAGGRESSIVE heuristic
          bonus = kaggressive(p, r, f, board);
          score[c] += bonus;
          break;
        case INVALID:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
        default:
          tbassert(false, "Jose says: no way!\n");   // No way, Jose!
      }
    }
    if (c == BLACK)
      break;
    c = BLACK;
  }

  score[WHITE] += board->pawn_count[WHITE] * PAWN_EV_VALUE;
  score[BLACK] += board->pawn_count[BLACK] * PAWN_EV_VALUE;

  square_t white_laser_list[MAX_NUM_PIECES];
  square_t black_laser_list[MAX_NUM_PIECES];

  int whitePathCount = get_laser_path_list(p, white_laser_list, WHITE, board);
  int blackPathCount = get_laser_path_list(p, black_laser_list, BLACK, board);

  ev_score_t w_hattackable = HATTACK * h_squares_attackable(p, WHITE, white_laser_list, whitePathCount, board);
  score[WHITE] += w_hattackable;
  ev_score_t b_hattackable = HATTACK * h_squares_attackable(p, BLACK, black_laser_list, blackPathCount, board);
  score[BLACK] += b_hattackable;

  int w_mobility_list = MOBILITY * mobility_list(p, WHITE, black_laser_list, blackPathCount, board);
  score[WHITE] += w_mobility_list;

  int b_mobility_list = MOBILITY * mobility_list(p, BLACK, white_laser_list, whitePathCount, board);
  score[BLACK] += b_mobility_list;

  // PAWNPIN Heuristic --- is a pawn immobilized by the enemy laser.
  int w_pawnpin = PAWNPIN * pawnpin(p, WHITE, black_laser_list, blackPathCount, board); //use other color's laser map
  score[WHITE] += w_pawnpin;
  int b_pawnpin = PAWNPIN * pawnpin(p, BLACK, white_laser_list, whitePathCount, board); //use other color's laser map
  score[BLACK] += b_pawnpin;

  // score from WHITE point of view
  ev_score_t tot = score[WHITE] - score[BLACK];

  if (RANDOMIZE) {
    ev_score_t  z = rand_r(&seed) % (RANDOMIZE*2+1);
    tot = tot + z - RANDOMIZE;
  }

  if (color_to_move_of(p) == BLACK) {
    tot = -tot;
  }



  return tot / EV_SCORE_RATIO;
}
Example #3
0
void Plink::perm_testQTDT(Perm & perm)
{

  //////////////////////////////
  // Use individual-major coding
  
  if (par::SNP_major) 
    SNP2Ind();
  
  
  // for now, no covariates
  if ( par::clist_number > 0 ) 
    error("Cannot specify covariates with QFAM for now...\n");


  ////////////////////////////////////////////////
  // Specify special adaptive QFAM mode (i.e. one SNP
  // at a time)



  /////////////////////////////
  // Set up permutation indices
  
  vector<int> pbetween(family.size());
  vector<bool> pwithin(family.size(),false);
  for (int i=0; i < family.size(); i++)
    pbetween[i] = i;
  
  
  ///////////////
  // Output files

  string f = ".qfam";
  if (par::QFAM_within1) f += ".within";
  else if (par::QFAM_within2) f += ".parents";
  else if (par::QFAM_between) f += ".between";
  else if (par::QFAM_total) f += ".total";
  
  printLOG("Writing QFAM statistics to [ " + par::output_file_name + f + " ]\n");
  
  if (!par::permute) 
    printLOG("** Warning ** QFAM results require permutation to correct for family structure\n");
  else
    printLOG("Important: asymptotic p-values not necessarily corrected for family-structure:\n"
	     "           use empirical p-values for robust p-values from QFAM\n"
	     "           and consult the above file only for parameter estimates\n");
  

  ofstream QOUT((par::output_file_name+f).c_str(),ios::out); // dummy
  QOUT.precision(4);
  QOUT << setw(4) << "CHR" << " " 
       << setw(par::pp_maxsnp) << "SNP" << " " 
       << setw(10) << "BP" << " "
       << setw(4) << "A1" << " "
       << setw(10) << "TEST" << " "
       << setw(8) << "NIND" << " "
       << setw(10) << "BETA" << " ";
  if (par::display_ci)    
    QOUT << setw(8) << "SE" << " "
	 << setw(8) << "LOWER" << " "
	 << setw(8) << "UPPER" << " ";	    	      
  QOUT << setw(12) << "STAT" << " "
       << setw(12) << "P\n";


  //////////////////////
  // Familial clustering

  // C holds which family an individual belongs to 
  // (as element in the family[] array
  
  vector<int> C;
  map<Family*,int> famcnt;
  for (int f = 0 ; f < family.size() ; f++)
    famcnt.insert( make_pair( family[f] , f ) );      
  
  vector<Individual*>::iterator person = sample.begin(); 
  while ( person != sample.end() )
    {
      map<Family*,int>::iterator f = famcnt.find( (*person)->family );
      
      if ( f == famcnt.end() )
	error("Internal error in QFAM, allocating families to individuals...\n");
      else
	C.push_back( f->second );
      
      person++;  
    } 
  
  printLOG(int2str(family.size())+" nuclear families in analysis\n");
      
  if ( family.size()<2 )
    error("Halting: not enough nuclear families for this analysis\n");
  


  ////////////////////
  // Run original QFAM

  perm.setTests(nl_all);
  perm.setPermClusters(*this);

  // Force adaptive perm
  par::adaptive_perm = true;

  vector_t orig = calcQTDT(C, QOUT, false, perm, pbetween, pwithin);

  QOUT.close(); 



  ////////////////
  // Permutation

  if ( ! par::permute ) 
    return;
  
  // Adpative permutation will already have been conducted in original 
  // function call for QFAM (i.e. per-SNP adaptive permutation)

  if (!par::silent)
    cout << "\n\n";
  

  ////////////////////
  // Display results
  
  ofstream TDT;   

  f += ".perm";    
  TDT.open((par::output_file_name+f).c_str(),ios::out);
  printLOG("Writing QFAM permutation results to [ " 
	   + par::output_file_name + f + " ] \n"); 
  TDT.precision(4);
  
  TDT << setw(4) << "CHR" << " "
      << setw(par::pp_maxsnp) << "SNP" << " ";
  
  if (par::perm_TDT_basic) TDT << setw(12) << "STAT" << " ";
  
  TDT << setw(12) << "EMP1" << " ";
  TDT << setw(12) << "NP" << " " << "\n";  
  
  for (int l=0; l<nl_all; l++)
    {	
      
      TDT << setw(4) << locus[l]->chr << " "
	  << setw(par::pp_maxsnp) << locus[l]->name << " "; 
      
      if (orig[l] < -0.5)
	TDT << setw(12) << "NA"  << " " 
	    << setw(12) << "NA"  << " " 
	    << setw(12) << "NA";
      else
	{
	  TDT << setw(12) << orig[l] << " "
	      << setw(12) << perm.pvalue(l) << " "
	      << setw(12) << perm.reps_done(l);	  
	}
      TDT << "\n";
    }
  
  TDT.close();

  
  // Adjusted p-values, assumes 1-df chi-squares
  
  if (par::multtest)
    {
      
      vector<double> obp(0);
      for (int l=0; l<nl_all;l++)
	obp.push_back(inverse_chiprob(perm.pvalue(l),1));      
      
      multcomp(obp,f);
    }

  
   
}