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

Added direction movement freedom heuristic

parents 4a0088d2 5f1d07cb
No related branches found
No related tags found
No related merge requests found
ai_vs_ai=1
ai_think_time=1000
ai_think_time=5000
ai_vs_ai_game_count=100
heuristic_ai_1=left_up
heuristic_ai_1=movement_freedom
heuristic_ai_2=default
send_game_to_gui=1
......@@ -2,6 +2,9 @@
#include <iostream>
#include <fstream>
#include <algorithm>
#include "movement_freedom_heuristic.hpp"
#include "points_heuristic.hpp"
namespace game {
......@@ -78,6 +81,11 @@ namespace game {
*h = new mcts::go_left_up_heuristic();
}else if(value == "direction_freeddom") {
*h = new mcts::number_direction_freedom_heuristic();
}else if(value == "points") {
std::cout << "POINTS" << std::endl;
*h = new mcts::points_heuristic();
}else if(value == "movement_freedom") {
*h = new mcts::movement_freedom_heuristic();
}else if(value == "default") {
*h = new mcts::penguin_heuristic();
} else {
......
......@@ -63,7 +63,7 @@ namespace game
std::string player_to_string(std::uint8_t player) const; //String representation of a player
std::string move_to_string(std::uint16_t m) const; //String representation of a move (for example, A1)
json to_JSON() const;
std::string to_string() const; //String representation of the entire game
std::string to_string() const; //String representatcion of the entire game
std::set<int> to_input_vector() const;
void from_input_vector(const std::set<int>& input);
penguin_state get_state(); //Return the state
......
#ifndef __MCTS_SETTINGS_HPP__
#define __MCTS_SETTINGS_HPP__
// Allocated memory for the mcts. If not enough the program may crash
#define MCTS_ALLOCATOR_SIZE 10000000U
#define MCTS_ALLOCATOR_SIZE 100000000U
// Reflection time for every turn of the mcts (in ms)
#define MCTS_TURN_TIME 5000
......
#include <iostream>
#include "penguin_heuristic.hpp"
#include "movement_freedom_heuristic.hpp"
#include "penguin.hpp"
#define MAX_NB_MOVES 60
using namespace std;
namespace mcts
{
float movement_freedom_heuristic::get_value(const game::penguin& game, uint8_t move) const
{
std::shared_ptr<game::penguin> played = game::copy(game);
//TEST SOLUTION 1
played->play(move);
game::penguin_state state = played->get_state();
uint32_t* penguins = state.peng_blue;
if(!state.current_player_red) {
penguins = state.peng_red;
}
uint32_t nb_moves = 0;
for(int i=0; i< 4; i++) {
nb_moves += PENGUIN_TOT_MOVES(penguins[i]);
}
float res = 2.0f*(float)nb_moves / (float)MAX_NB_MOVES - 1.0f;
return res;
//TEST SOLUTION 2
/*
played->play(move);
uint32_t nb_moves = played->number_of_moves();
float res = 2.0f*(float)nb_moves / (float)MAX_NB_MOVES - 1.0f;
return res;
*/
//TEST SOLUTION 3
/*
played->play(move);
game::penguin_state state = played->get_state();
uint32_t nb_moves = state.nb_moves_blue;
if(!state.current_player_red) {
nb_moves = state.nb_moves_red;
}
float res = 2.0f*(float)nb_moves / (float)MAX_NB_MOVES - 1.0f;
return res;
*/
}
int movement_freedom_heuristic::get_count(const game::penguin& game, uint8_t move) const
{
return 100000000;
}
}
#ifndef __MOVEMENT_FREEDOM_HEURISTIC_HPP__
#define __MOVEMENT_FREEDOM_HEURISTIC_HPP__
#include "penguin.hpp"
#include "penguin_heuristic.hpp"
namespace mcts
{
class movement_freedom_heuristic : public penguin_heuristic
{
public:
float get_value(const game::penguin& game, uint8_t move) const;
int get_count(const game::penguin& game, uint8_t move) const;
};
};
#endif
......@@ -17,20 +17,20 @@ namespace mcts
float go_left_up_heuristic::get_value(const game::penguin& game, uint8_t move) const
{
std::shared_ptr<game::penguin> played = game::copy(game);
played->play(move);
game::penguin_state state = played->get_state();
uint32_t* penguins = state.peng_blue;
if(!state.current_player_red) {
penguins = state.peng_red;
}
uint32_t sPos = 0;
for(int i=0; i< 4; i++) {
sPos+=PENGUIN_POS(penguins[i]);
}
double res = (double) (-(-118.0 + sPos) / 118.0);
return res;
std::shared_ptr<game::penguin> played = game::copy(game);
played->play(move);
game::penguin_state state = played->get_state();
uint32_t* penguins = state.peng_blue;
if(!state.current_player_red) {
penguins = state.peng_red;
}
uint32_t sPos = 0;
for(int i=0; i< 4; i++) {
sPos+=PENGUIN_POS(penguins[i]);
}
double res = (double) (-(-118.0 + sPos) / 118.0);
return res;
}
int go_left_up_heuristic::get_count(const game::penguin& game, uint8_t move) const
......
#include "points_heuristic.hpp"
#include <iostream>
namespace mcts
{
float points_heuristic::get_value(const game::penguin& game, uint8_t move) const
{
const std::shared_ptr<game::penguin> played = game::copy(game);
const game::penguin_state old_state = played->get_state();
played->play(move);
const game::penguin_state new_state = played->get_state();
const bool current_player_is_red = old_state.current_player_red;
int old_score = old_state.score_red;
int new_score = new_state.score_red;
if(!current_player_is_red) {
old_score = old_state.score_blue;
new_score = new_state.score_blue;
}
return (-2.f + (new_score - old_score));
}
int points_heuristic::get_count(const game::penguin& game, uint8_t move) const
{
return 100 ;
}
}
#ifndef __POINTS_HEURISTIC_HPP__
#define __POINTS_HEURISTIC_HPP__
#include "penguin_heuristic.hpp"
namespace mcts
{
class points_heuristic : public penguin_heuristic
{
public:
float get_value(const game::penguin& game, uint8_t move) const;
int get_count(const game::penguin& game, uint8_t move) const;
};
};
#endif
......@@ -245,22 +245,24 @@ void test_mcts_two_players<Game>::self_play(Game g, const heuristic<Game>& h1, c
{
if (i % 2 == 0)
{
/*cout << "v1 won" << endl;*/ ++nb_win_v1;
std::cout << "v1 won" << std::endl;
++nb_win_v1;
}
else
{
/*cout << "v2 won" << endl;*/ ++nb_win_v2;
std::cout << "v2 won" << std::endl;
++nb_win_v2;
}
}
else if (g.won(1))
{
if (i % 2 == 0)
{
/*cout << "v2 won" << endl;*/ ++nb_win_v2;
std::cout << "v2 won" <<std::endl; ++nb_win_v2;
}
else
{
/*cout << "v1 won" << endl;*/ ++nb_win_v1;
std::cout << "v1 won" << std::endl; ++nb_win_v1;
}
}
else
......
......@@ -35,7 +35,7 @@ AIInclude=-I $(AISRC)/game -I $(AISRC)/util -I $(AISRC)/monte_carlo -I $(AISRC)/
# Cpp source files
AISourceFile = omp_util.cpp fast_log.cpp display_node.cpp penguin.cpp test_two_players_game.cpp monte_carlo.cpp \
test_fast_log.cpp statistics.cpp node.cpp allocator.cpp test_allocator.cpp openings.cpp mcts_two_players.cpp \
test_mcts_two_players.cpp bits.cpp test_bits.cpp penguin_heuristic.cpp number_direction_freedom_heuristic.cpp main.cpp config.cpp
test_mcts_two_players.cpp bits.cpp test_bits.cpp penguin_heuristic.cpp number_direction_freedom_heuristic.cpp main.cpp config.cpp movement_freedom_heuristic.cpp
# Directories in which make will search for files
vpath %.cpp $(AISRC)/game $(AISRC)/main $(AISRC)/util $(AISRC)/monte_carlo $(AISRC)/mcts $(AISRC)/gdl
# Flags passed to the compiler
......
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