Skip to content
Snippets Groups Projects
Commit 4f18ca3c authored by Salard Xavier's avatar Salard Xavier
Browse files

Update of the main heuristic

parent 6137baef
No related branches found
No related tags found
No related merge requests found
......@@ -6,6 +6,21 @@
#include "penguin_heuristic.hpp"
#include <algorithm>
#define MAX_NB_MOVES 60.0f
//Count the number of direction in which a penguin can move
uint8_t number_of_direction_penguin_general(uint32_t penguin){
uint8_t res = 0 ;
if(PENGUIN_MOVES_A(penguin)>0) res++;
if(PENGUIN_MOVES_B(penguin)>0) res++;
if(PENGUIN_MOVES_C(penguin)>0) res++;
if(PENGUIN_MOVES_D(penguin)>0) res++;
if(PENGUIN_MOVES_E(penguin)>0) res++;
if(PENGUIN_MOVES_F(penguin)>0) res++;
return res;
}
namespace mcts
{
class generalization_heuristic : public penguin_heuristic
......@@ -13,39 +28,108 @@ namespace mcts
public:
float get_value(const game::penguin& game, uint8_t move) const
{
float result = 0;
float B_free_move=0.15, B_num_dir=0.05, B_points=0.05, B_zone=0.15;
float A_free_move=1.0, A_num_dir=1.0, A_points=0.5, A_zone=1.5;
float X_free_move=0.35, X_num_dir=0.15, X_points=0.2, X_zone=0.3;
//float A_free_move=0.1*10, A_num_dir=0.1*10, A_points=0.05*10, A_zone=0.15*10;
//uint64_t obstacles = game.create_obstacles_bitboard();
game::penguin g = *(game::copy(game));
auto old_state = g.get_state();
g.play(move);
auto actual_state = g.get_state();
auto played_state = g.get_state();
uint64_t obstacles = (~(actual_state.one_fish | actual_state.two_fish | actual_state.three_fish));
/*
uint64_t obstacles = (~(played_state.one_fish | played_state.two_fish | played_state.three_fish));
for(int i = 0; i < 4; i++){
obstacles |= ((uint64_t) 1) << PENGUIN_POS(actual_state.peng_red[i]);
obstacles |= ((uint64_t) 1) << PENGUIN_POS(actual_state.peng_blue[i]);
obstacles |= ((uint64_t) 1) << PENGUIN_POS(played_state.peng_red[i]);
obstacles |= ((uint64_t) 1) << PENGUIN_POS(played_state.peng_blue[i]);
}
*/
uint32_t* penguins = played_state.current_player_red ? played_state.peng_red : played_state.peng_blue;
uint32_t* other_peng = played_state.current_player_red ? played_state.peng_blue : played_state.peng_red;
/*
int nbr_turns = 0; // It count also the penguins so in this implementation it start at 10
for (int i =0; i<60; i++){
if((obstacles >> i) & 1) nbr_turns++;
}
*/
//Movement Freedom Heuristic
//uint32_t* penguins_r = played_state.peng_red;
//uint32_t* penguins_b = played_state.peng_blue;
float nb_moves_us = 0;
float nb_moves_other = 0;
result+= ((A_free_move/nbr_turns) + B_free_move) * movement_freedom_heuristic().get_value(game, move)
+ ((A_num_dir/nbr_turns) + B_num_dir) * number_direction_freedom_heuristic().get_value(game, move)
+ ((A_points/nbr_turns) + B_points) * points_heuristic().get_value(game, move)
+ ((A_zone/nbr_turns) + B_zone) * zone_heuristic().get_value(game, move);
//std::cout<< nbr_turns<< " : "<< result << std::endl;
return result;
for(int i=0; i< 4; i++) {
nb_moves_us += PENGUIN_TOT_MOVES(penguins[i]);
nb_moves_other += PENGUIN_TOT_MOVES(other_peng[i]);
}
float res_movement_freedom = -(nb_moves_us - nb_moves_other)/MAX_NB_MOVES;
//Number Direction Freedom Heuristic
uint8_t nb = 0;
for(int i = 0; i < 4; ++i) {
nb += number_of_direction_penguin_general(penguins[i]);
}
float res_number_direction_freedom = (-12.f + (float)nb)/12.f;
//Points Heuristic
int old_score = old_state.score_red;
int new_score = played_state.score_red;
if(!played_state.current_player_red) {
old_score = old_state.score_blue;
new_score = played_state.score_blue;
}
float res_points = (-2.f + (new_score - old_score));
//Zone Heuristic
uint64_t moves_this = 0;
uint64_t moves_other = 0;
for(int i = 0; i < 4; ++i) {
moves_this |= g.penguin_move_board(penguins[i]);
moves_other |= g.penguin_move_board(other_peng[i]);
}
uint64_t this_owned_moves = moves_this & ~(moves_other);
uint64_t other_owned_moves = moves_other & ~(moves_this);
int count_this = 0;
int count_other = 0;
for(int i = 0; i < 60; i++) {
count_this += (this_owned_moves>>i) & 1 ;
count_other += (other_owned_moves>>i) & 1 ;
}
float res_zone = clamp((float)(count_this - count_other) / (count_this + 1), -1.f, 1.f);
//Final Result
return (X_free_move * res_movement_freedom
+ X_num_dir * res_number_direction_freedom
+ X_points * res_points
+ X_zone * res_zone);
}
int get_count(const game::penguin& game, uint8_t move) const
{
game::penguin g = *(game::copy(game));
auto old_state = g.get_state();
uint64_t obstacles = (~(old_state.one_fish | old_state.two_fish | old_state.three_fish));
for(int i = 0; i < 4; i++){
obstacles |= ((uint64_t) 1) << PENGUIN_POS(old_state.peng_red[i]);
obstacles |= ((uint64_t) 1) << PENGUIN_POS(old_state.peng_blue[i]);
}
int nbr_turns = 0; // It count also the penguins so in this implementation it start at 10
for (int i =0; i<60; i++){
if((obstacles >> i) & 1) nbr_turns++;
}
// There should be a maximum of 60 obstacles, so it shouldn't go below 1.
return (1000/nbr_turns);
}
return 100;
private:
float clamp(float v, float l, float h) const
{
return std::max(std::min(h,v),l);
}
};
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment