Skip to content

Commit

Permalink
Heuristic scoring:
Browse files Browse the repository at this point in the history
- Take into account average value of tiles on board, thus forcing
  earlier merges of high tiles.
- Tune up individual parameters for better results - the chosen values
  are based on the results of CMA ES.
  • Loading branch information
xificurk committed Jun 29, 2014
1 parent 834cef1 commit a64d749
Showing 1 changed file with 19 additions and 12 deletions.
31 changes: 19 additions & 12 deletions 2048.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,13 @@ static float heur_score_table[65536];
static float score_table[65536];

// Heuristic scoring settings
static const float SCORE_LOST_PENALTY = 30000.0f;
static const float SCORE_MONOTONICITY_WEIGHT = 1.0f;
static const float SCORE_MERGES_WEIGHT = 10.0f;
static const float SCORE_EMPTY_WEIGHT = 10.0f;
static const float SCORE_LOST_PENALTY = 200000.0f;
static const float SCORE_MONOTONICITY_POWER = 4.0f;
static const float SCORE_MONOTONICITY_WEIGHT = 47.0f;
static const float SCORE_SUM_POWER = 3.5f;
static const float SCORE_SUM_WEIGHT = 11.0f;
static const float SCORE_MERGES_WEIGHT = 700.0f;
static const float SCORE_EMPTY_WEIGHT = 270.0f;

void init_tables() {
for (unsigned row = 0; row < 65536; ++row) {
Expand All @@ -105,13 +108,15 @@ void init_tables() {


// Heuristic score
float sum = 0;
int empty = 0;
int merges = 0;

int prev = 0;
int counter = 0;
for (int i = 0; i < 4; ++i) {
int rank = line[i];
sum += pow(rank, SCORE_SUM_POWER);
if (rank == 0) {
empty++;
} else {
Expand All @@ -128,18 +133,21 @@ void init_tables() {
merges += 1 + counter;
}

int monotonicity_left = 0;
int monotonicity_right = 0;
float monotonicity_left = 0;
float monotonicity_right = 0;
for (int i = 1; i < 4; ++i) {
if (line[i-1] > line[i]) {
monotonicity_left += pow(line[i-1], 3) - pow(line[i], 3);
monotonicity_left += pow(line[i-1], SCORE_MONOTONICITY_POWER) - pow(line[i], SCORE_MONOTONICITY_POWER);
} else {
monotonicity_right += pow(line[i], 3) - pow(line[i-1], 3);
monotonicity_right += pow(line[i], SCORE_MONOTONICITY_POWER) - pow(line[i-1], SCORE_MONOTONICITY_POWER);
}
}

heur_score_table[row] = SCORE_EMPTY_WEIGHT * empty + SCORE_MERGES_WEIGHT * merges -
SCORE_MONOTONICITY_WEIGHT * std::min(monotonicity_left, monotonicity_right);
heur_score_table[row] = SCORE_LOST_PENALTY +
SCORE_EMPTY_WEIGHT * empty +
SCORE_MERGES_WEIGHT * merges -
SCORE_MONOTONICITY_WEIGHT * std::min(monotonicity_left, monotonicity_right) -
SCORE_SUM_WEIGHT * sum;

// execute a move to the left
for (int i = 0; i < 3; ++i) {
Expand Down Expand Up @@ -287,8 +295,7 @@ static float score_helper(board_t board, const float* table) {

static float score_heur_board(board_t board) {
return score_helper( board , heur_score_table) +
score_helper(transpose(board), heur_score_table) +
SCORE_LOST_PENALTY;
score_helper(transpose(board), heur_score_table);
}

static float score_board(board_t board) {
Expand Down

0 comments on commit a64d749

Please sign in to comment.