Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • francesco-bariatti/pingouins
  • Samuel.Felton/pingouins
  • Lucas.Clement/pingouins
3 results
Show changes
Showing
with 586 additions and 0 deletions
File moved
File moved
File moved
File moved
#include "mcts_two_players.hpp"
#include "test_mcts_two_players.hpp"
#include <iostream>
#include <iomanip>
#include <map>
//#include <gperftools/profiler.h>
using namespace std;
using namespace game;
namespace mcts
{
// test_mcts_two_players::test_mcts_two_players() : openings_(connect4())
// {
// //self_play(1000);
// play();
// //test_openings(10000, 1000);
// }
// void test_mcts_two_players::test_openings(int nb_learning, int nb_testing)
// {
// // self_play(nb_testing);
// self_play_learn_openings(nb_learning);
// self_play(nb_testing, true);
// }
// template <typename Game>
// int test_mcts_two_players::select_move(Game& game)
// {
// cout << game.player_to_string(game.current_player()) << " move: ";
// map<string, int> m;
// for (int i = 0; i < game.number_of_moves(); ++i)
// {
// m[game.move_to_string(i)] = i;
// }
// string move;
// getline(cin, move);
// game.play(m[move]);
// return m[move];
// }
// void test_mcts_two_players::play()
// {
// // ProfilerStart("theturk.prof");
// connect4 c4;
// auto the_turk = make_mcts_two_players(c4, 5000, 0.3, 4);
// cout << "play one game" << endl;
// cout << "who's first? (h)uman/(c)omputer ";
// string ans;
// getline(cin, ans);
// cout << c4 << endl;
// int human_last_move = -1, computer_last_move = -1;
// while (!c4.end_of_game())
// {
// if ((ans == "h" && c4.current_player() == 0) || (ans == "c" && c4.current_player() == 1))
// {
// human_last_move = select_move(c4);
// }
// else
// {
// if (human_last_move != -1 && computer_last_move != -1)
// {
// the_turk.last_moves(computer_last_move, human_last_move);
// }
// uint16_t move = the_turk.select_move();
// computer_last_move = move;
// cout << c4.player_to_string(c4.current_player()) << " move: " << c4.move_to_string(move) << endl;
// c4.play(move);
// }
// cout << c4 << endl;
// }
// if (c4.value(0) == 1) cout << c4.player_to_string(0) << " won";
// else if (c4.value(1) == 1) cout << c4.player_to_string(1) << " won";
// else cout << "draw";
// cout << endl;
// // ProfilerStop();
// }
// void test_mcts_two_players::self_play_learn_openings(int n)
// {
// connect4 c4;
// vector<uint16_t> moves(200);
// auto state = c4.get_state();
// auto the_turk_1 = make_mcts_two_players(c4, 1000, 0.6, 2);
// auto the_turk_2 = make_mcts_two_players(c4, 1000, 0.6, 2);
// map<set<int>, pair<uint32_t, double>> learning_examples;
// for (int i = 0; i < n; ++i)
// {
// cout << i << endl;
// cout << openings_ << endl << endl;
// moves.clear();
// c4.set_state(state);
// the_turk_1.reset();
// the_turk_1.init_with_openings(openings_);
// the_turk_2.reset();
// the_turk_2.init_with_openings(openings_);
// int the_turk_1_last_move = -1, the_turk_2_last_move = -1;
// int k = 0;
// while (!c4.end_of_game())
// {
// if (k == 1) the_turk_2.last_move(the_turk_1_last_move);
// if (k % 2 == 0)
// {
// if (the_turk_2_last_move != -1)
// {
// the_turk_1.last_moves(the_turk_1_last_move, the_turk_2_last_move);
// }
// uint16_t move = the_turk_1.select_move();
// moves.push_back(move);
// the_turk_1_last_move = move;
// c4.play(move);
// }
// else
// {
// if (the_turk_2_last_move != -1)
// {
// the_turk_2.last_moves(the_turk_2_last_move, the_turk_1_last_move);
// }
// uint16_t move = the_turk_2.select_move();
// moves.push_back(move);
// the_turk_2_last_move = move;
// c4.play(move);
// }
// ++k;
// }
// std::cout << c4 << std::endl;
// int v = c4.value(0);
// cout << "value for first player " << v << endl;
// c4.set_state(state);
// openings_.update(c4, moves, v);
// c4.set_state(state);
// for (uint16_t m : moves)
// {
// c4.play(m);
// v = -v;
// set<int> input_vector = std::move(c4.to_input_vector());
// auto it = learning_examples.find(input_vector);
// if (it == learning_examples.end())
// {
// learning_examples[input_vector] = make_pair(1, v);
// }
// else
// {
// it->second.second = (it->second.second * it->second.first + v) / (it->second.first + 1);
// it->second.first += 1;
// }
// }
// }
// cout << "number of learning examples: " << learning_examples.size() << endl;
// ofstream output("learning_examples.txt");
// for (const auto& example : learning_examples)
// {
// output << example.second.second;
// for (int index : example.first)
// {
// output << " " << index << ":" << 1;
// }
// output << endl;
// }
// output.close();
// }
// void test_mcts_two_players::self_play(int n, bool with_openings)
// {
// connect4 c4;
// auto state = c4.get_state();
// auto the_turk_v1 = make_mcts_two_players(c4, 1000, 0.6, 2);
// auto the_turk_v2 = make_mcts_two_players(c4, 1000, 0.6, 2);
// int nb_win_v1 = 0, nb_win_v2 = 0, nb_draw = 0;
// for (int i = 0; i < n; ++i)
// {
// cout << i << endl;
// c4.set_state(state);
// the_turk_v1.reset();
// the_turk_v2.reset();
// if (with_openings) the_turk_v2.init_with_openings(openings_);
// int the_turk_v1_last_move = -1, the_turk_v2_last_move = -1;
// int k = 0;
// while (!c4.end_of_game())
// {
// if (with_openings && k == 1 && i % 2 == 0) the_turk_v2.last_move(the_turk_v1_last_move);
// ++k;
// // cout << c4 << endl;
// if ((i % 2 == 0 && c4.current_player() == 0) || (i % 2 == 1 && c4.current_player() == 1))
// {
// if (the_turk_v1_last_move != -1 && the_turk_v2_last_move != -1)
// {
// the_turk_v1.last_moves(the_turk_v1_last_move, the_turk_v2_last_move);
// }
// uint16_t move = the_turk_v1.select_move();
// the_turk_v1_last_move = move;
// c4.play(move);
// }
// else
// {
// if (the_turk_v1_last_move != -1 && the_turk_v2_last_move != -1)
// {
// the_turk_v2.last_moves(the_turk_v2_last_move, the_turk_v1_last_move);
// }
// uint16_t move = the_turk_v2.select_move();
// the_turk_v2_last_move = move;
// c4.play(move);
// }
// }
// if (c4.value(0) == 1)
// {
// if (i % 2 == 0)
// {
// /*cout << "v1 won" << endl;*/ ++nb_win_v1;
// }
// else
// {
// /*cout << "v2 won" << endl;*/ ++nb_win_v2;
// }
// }
// else if (c4.value(1) == 1)
// {
// if (i % 2 == 0)
// {
// /*cout << "v2 won" << endl;*/ ++nb_win_v2;
// }
// else
// {
// /*cout << "v1 won" << endl;*/ ++nb_win_v1;
// }
// }
// else
// {
// /*cout << "draw" << endl;*/ ++nb_draw;
// }
// cout << setw(10) << "v1 nb wins: " << nb_win_v1 << " v2 nb wins: " << nb_win_v2 << " nb draws: " << nb_draw << endl;
// }
// }
// void test_mcts_two_players::self_play()
// {
// connect4 c4;
// auto the_turk_v1 = make_mcts_two_players(c4, 1000, 1.2, 2);
// auto the_turk_v2 = make_mcts_two_players(c4, 1000, 1.2, 2);
// cout << "play one game" << endl;
// cout << "who's first? the_turk_(v1)/the_turk_(v2) ";
// string ans;
// getline(cin, ans);
// cout << c4 << endl;
// int the_turk_v1_last_move = -1, the_turk_v2_last_move = -1;
// while (!c4.end_of_game())
// {
// if ((ans == "v1" && c4.current_player() == 0) || (ans == "v2" && c4.current_player() == 1))
// {
// if (the_turk_v1_last_move != -1 && the_turk_v2_last_move != -1)
// {
// the_turk_v1.last_moves(the_turk_v1_last_move, the_turk_v2_last_move);
// }
// uint16_t move = the_turk_v1.select_move();
// the_turk_v1_last_move = move;
// cout << c4.player_to_string(c4.current_player()) << " move: " << c4.move_to_string(move) << endl;
// c4.play(move);
// }
// else
// {
// if (the_turk_v1_last_move != -1 && the_turk_v2_last_move != -1)
// {
// the_turk_v2.last_moves(the_turk_v2_last_move, the_turk_v1_last_move);
// }
// uint16_t move = the_turk_v2.select_move();
// the_turk_v2_last_move = move;
// cout << c4.player_to_string(c4.current_player()) << " move: " << c4.move_to_string(move) << endl;
// c4.play(move);
// }
// cout << c4 << endl;
// }
// if (c4.value(0) == 1) cout << c4.player_to_string(0) << " won";
// else if (c4.value(1) == 1) cout << c4.player_to_string(1) << " won";
// else cout << "draw";
// cout << endl;
// }
}
#ifndef __TEST_MCTS_TWO_PLAYERS_HPP__
#define __TEST_MCTS_TWO_PLAYERS_HPP__
#include "openings.hpp"
#include "mcts_two_players.hpp"
#include "test_mcts_two_players.hpp"
#include "morpion.hpp"
#include "MCTS_SETTINGS.hpp"
#include <string>
#include <iostream>
#include <iomanip>
#include <map>
//#include <gperftools/profiler.h>
using namespace std;
using namespace game;
#include <iomanip>
#include <set>
#include <fstream>
namespace mcts
{
test_mcts_two_players::test_mcts_two_players() : openings_(morpion())
template <typename Game>
class test_mcts_two_players
{
//self_play(1000);
play();
//test_openings(10000, 1000);
openings openings_;
void play(Game g);
void self_play(Game g);
void self_play(Game g, int n, bool with_openings = false);
void self_play_learn_openings(Game g, int n);
void test_openings(Game g, int nb_learning, int nb_testing);
int select_move(Game& game);
public:
test_mcts_two_players(const Game& g);
};
template <typename Game>
test_mcts_two_players<Game>::test_mcts_two_players(const Game& g) : openings_(g)
{
//self_play(g, 1000);
play(g);
//test_openings(g, 10000, 1000);
}
void test_mcts_two_players::test_openings(int nb_learning, int nb_testing)
template <typename Game>
void run_test_mcts_two_players(const Game& g)
{
// self_play(nb_testing);
self_play_learn_openings(nb_learning);
self_play(nb_testing, true);
test_mcts_two_players<Game>{g};
}
template <typename Game>
void test_mcts_two_players<Game>::test_openings(Game g, int nb_learning, int nb_testing)
{
// self_play(g, nb_testing);
self_play_learn_openings(g, nb_learning);
self_play(g, nb_testing, true);
}
template <typename Game>
int test_mcts_two_players::select_move(Game& game)
int test_mcts_two_players<Game>::select_move(Game& game)
{
cout << game.player_to_string(game.current_player()) << " move: ";
map<string, int> m;
std::cout << game.player_to_string(game.current_player()) << " move: ";
std::map<std::string, int> m;
for (int i = 0; i < game.number_of_moves(); ++i)
{
m[game.move_to_string(i)] = i;
}
string move;
getline(cin, move);
std::string move;
getline(std::cin, move);
game.play(m[move]);
return m[move];
}
void test_mcts_two_players::play()
template <typename Game>
void test_mcts_two_players<Game>::play(Game g)
{
// ProfilerStart("theturk.prof");
morpion mor;
auto the_turk = make_mcts_two_players(mor, 5000, 0.3, 4);
cout << "play one game" << endl;
cout << "who's first? (h)uman/(c)omputer ";
string ans;
getline(cin, ans);
cout << mor << endl;
auto the_turk = make_mcts_two_players(g, MCTS_TURN_TIME, 0.4, 8);
std::cout << "play one game" << std::endl;
std::cout << "who's first? (h)uman/(c)omputer ";
std::string ans;
getline(std::cin, ans);
std::cout << g.to_string() << std::endl;
int human_last_move = -1, computer_last_move = -1;
while (!mor.end_of_game())
while (!g.end_of_game())
{
if ((ans == "h" && mor.current_player() == 0) || (ans == "c" && mor.current_player() == 1))
if ((ans == "h" && g.current_player() == 0) || (ans == "c" && g.current_player() == 1))
{
human_last_move = select_move(mor);
human_last_move = select_move(g);
}
else
{
......@@ -66,39 +91,39 @@ namespace mcts
}
uint16_t move = the_turk.select_move();
computer_last_move = move;
cout << mor.player_to_string(mor.current_player()) << " move: " << mor.move_to_string(move) << endl;
mor.play(move);
std::cout << g.player_to_string(g.current_player()) << " move: " << g.move_to_string(move) << std::endl;
g.play(move);
}
cout << mor << endl;
std::cout << g << std::endl;
}
if (mor.value(0) == 1) cout << mor.player_to_string(0) << " won";
else if (mor.value(1) == 1) cout << mor.player_to_string(1) << " won";
else cout << "draw";
cout << endl;
if (g.won(0)) std::cout << g.player_to_string(0) << " won";
else if (g.won(1)) std::cout << g.player_to_string(1) << " won";
else std::cout << "draw";
std::cout << std::endl;
// ProfilerStop();
}
void test_mcts_two_players::self_play_learn_openings(int n)
template <typename Game>
void test_mcts_two_players<Game>::self_play_learn_openings(Game g, int n)
{
morpion mor;
vector<uint16_t> moves(200);
auto state = mor.get_state();
auto the_turk_1 = make_mcts_two_players(mor, 1000, 0.6, 2);
auto the_turk_2 = make_mcts_two_players(mor, 1000, 0.6, 2);
map<set<int>, pair<uint32_t, double>> learning_examples;
std::vector<uint16_t> moves(200);
auto state = g.get_state();
auto the_turk_1 = make_mcts_two_players(g, 1000, 0.6, 2);
auto the_turk_2 = make_mcts_two_players(g, 1000, 0.6, 2);
std::map<std::set<int>, std::pair<std::uint32_t, double>> learning_examples;
for (int i = 0; i < n; ++i)
{
cout << i << endl;
cout << openings_ << endl << endl;
std::cout << i << std::endl;
std::cout << openings_ << std::endl << std::endl;
moves.clear();
mor.set_state(state);
g.set_state(state);
the_turk_1.reset();
the_turk_1.init_with_openings(openings_);
the_turk_2.reset();
the_turk_2.init_with_openings(openings_);
int the_turk_1_last_move = -1, the_turk_2_last_move = -1;
int k = 0;
while (!mor.end_of_game())
while (!g.end_of_game())
{
if (k == 1) the_turk_2.last_move(the_turk_1_last_move);
if (k % 2 == 0)
......@@ -107,10 +132,10 @@ namespace mcts
{
the_turk_1.last_moves(the_turk_1_last_move, the_turk_2_last_move);
}
uint16_t move = the_turk_1.select_move();
std::uint16_t move = the_turk_1.select_move();
moves.push_back(move);
the_turk_1_last_move = move;
mor.play(move);
g.play(move);
}
else
{
......@@ -118,28 +143,28 @@ namespace mcts
{
the_turk_2.last_moves(the_turk_2_last_move, the_turk_1_last_move);
}
uint16_t move = the_turk_2.select_move();
std::uint16_t move = the_turk_2.select_move();
moves.push_back(move);
the_turk_2_last_move = move;
mor.play(move);
g.play(move);
}
++k;
}
std::cout << mor << std::endl;
int v = mor.value(0);
cout << "value for first player " << v << endl;
mor.set_state(state);
openings_.update(mor, moves, v);
mor.set_state(state);
for (uint16_t m : moves)
std::cout << g.to_string() << std::endl;
int v = g.value(0);
std::cout << "value for first player " << v << std::endl;
g.set_state(state);
openings_.update(g, moves, v);
g.set_state(state);
for (std::uint16_t m : moves)
{
mor.play(m);
g.play(m);
v = -v;
set<int> input_vector = std::move(mor.to_input_vector());
std::set<int> input_vector = std::move(g.to_input_vector());
auto it = learning_examples.find(input_vector);
if (it == learning_examples.end())
{
learning_examples[input_vector] = make_pair(1, v);
learning_examples[input_vector] = std::make_pair(1, v);
}
else
{
......@@ -148,50 +173,50 @@ namespace mcts
}
}
}
cout << "number of learning examples: " << learning_examples.size() << endl;
ofstream output("learning_examples.txt");
for (const auto& example : learning_examples)
std::cout << "number of learning examples: " << learning_examples.size() << std::endl;
std::ofstream output("learning_examples.txt");
for (const auto& example : learning_examples)
{
output << example.second.second;
for (int index : example.first)
for (int index : example.first)
{
output << " " << index << ":" << 1;
}
output << endl;
output << std::endl;
}
output.close();
}
void test_mcts_two_players::self_play(int n, bool with_openings)
template <typename Game>
void test_mcts_two_players<Game>::self_play(Game g, int n, bool with_openings)
{
morpion mor;
auto state = mor.get_state();
auto the_turk_v1 = make_mcts_two_players(mor, 1000, 0.6, 2);
auto the_turk_v2 = make_mcts_two_players(mor, 1000, 0.6, 2);
auto state = g.get_state();
auto the_turk_v1 = make_mcts_two_players(g, 1000, 0.6, 2);
auto the_turk_v2 = make_mcts_two_players(g, 1000, 0.6, 2);
int nb_win_v1 = 0, nb_win_v2 = 0, nb_draw = 0;
for (int i = 0; i < n; ++i)
{
cout << i << endl;
mor.set_state(state);
std::cout << i << std::endl;
g.set_state(state);
the_turk_v1.reset();
the_turk_v2.reset();
if (with_openings) the_turk_v2.init_with_openings(openings_);
int the_turk_v1_last_move = -1, the_turk_v2_last_move = -1;
int k = 0;
while (!mor.end_of_game())
while (!g.end_of_game())
{
if (with_openings && k == 1 && i % 2 == 0) the_turk_v2.last_move(the_turk_v1_last_move);
++k;
// cout << mor << endl;
if ((i % 2 == 0 && mor.current_player() == 0) || (i % 2 == 1 && mor.current_player() == 1))
// cout << c4 << endl;
if ((i % 2 == 0 && g.current_player() == 0) || (i % 2 == 1 && g.current_player() == 1))
{
if (the_turk_v1_last_move != -1 && the_turk_v2_last_move != -1)
{
the_turk_v1.last_moves(the_turk_v1_last_move, the_turk_v2_last_move);
}
uint16_t move = the_turk_v1.select_move();
std::uint16_t move = the_turk_v1.select_move();
the_turk_v1_last_move = move;
mor.play(move);
g.play(move);
}
else
{
......@@ -199,12 +224,12 @@ namespace mcts
{
the_turk_v2.last_moves(the_turk_v2_last_move, the_turk_v1_last_move);
}
uint16_t move = the_turk_v2.select_move();
std::uint16_t move = the_turk_v2.select_move();
the_turk_v2_last_move = move;
mor.play(move);
play(move);
}
}
if (mor.value(0) == 1)
if (g.won(0))
{
if (i % 2 == 0)
{
......@@ -215,7 +240,7 @@ namespace mcts
/*cout << "v2 won" << endl;*/ ++nb_win_v2;
}
}
else if (mor.value(1) == 1)
else if (g.won(1))
{
if (i % 2 == 0)
{
......@@ -230,33 +255,33 @@ namespace mcts
{
/*cout << "draw" << endl;*/ ++nb_draw;
}
cout << setw(10) << "v1 nb wins: " << nb_win_v1 << " v2 nb wins: " << nb_win_v2 << " nb draws: " << nb_draw << endl;
std::cout << std::setw(10) << "v1 nb wins: " << nb_win_v1 << " v2 nb wins: " << nb_win_v2 << " nb draws: " << nb_draw << std::endl;
}
}
void test_mcts_two_players::self_play()
template <typename Game>
void test_mcts_two_players<Game>::self_play(Game g)
{
morpion mor;
auto the_turk_v1 = make_mcts_two_players(mor, 1000, 1.2, 2);
auto the_turk_v2 = make_mcts_two_players(mor, 1000, 1.2, 2);
cout << "play one game" << endl;
cout << "who's first? the_turk_(v1)/the_turk_(v2) ";
string ans;
getline(cin, ans);
cout << mor << endl;
auto the_turk_v1 = make_mcts_two_players(g, 1000, 1.2, 2);
auto the_turk_v2 = make_mcts_two_players(g, 1000, 1.2, 2);
std::cout << "play one game" << std::endl;
std::cout << "who's first? the_turk_(v1)/the_turk_(v2) ";
std::string ans;
getline(std::cin, ans);
std::cout << g << std::endl;
int the_turk_v1_last_move = -1, the_turk_v2_last_move = -1;
while (!mor.end_of_game())
while (!g.end_of_game())
{
if ((ans == "v1" && mor.current_player() == 0) || (ans == "v2" && mor.current_player() == 1))
if ((ans == "v1" && g.current_player() == 0) || (ans == "v2" && g.current_player() == 1))
{
if (the_turk_v1_last_move != -1 && the_turk_v2_last_move != -1)
{
the_turk_v1.last_moves(the_turk_v1_last_move, the_turk_v2_last_move);
}
uint16_t move = the_turk_v1.select_move();
std::uint16_t move = the_turk_v1.select_move();
the_turk_v1_last_move = move;
cout << mor.player_to_string(mor.current_player()) << " move: " << mor.move_to_string(move) << endl;
mor.play(move);
std::cout << g.player_to_string(g.current_player()) << " move: " << g.move_to_string(move) << std::endl;
g.play(move);
}
else
{
......@@ -264,17 +289,19 @@ namespace mcts
{
the_turk_v2.last_moves(the_turk_v2_last_move, the_turk_v1_last_move);
}
uint16_t move = the_turk_v2.select_move();
std::uint16_t move = the_turk_v2.select_move();
the_turk_v2_last_move = move;
cout << mor.player_to_string(mor.current_player()) << " move: " << mor.move_to_string(move) << endl;
mor.play(move);
std::cout << g.player_to_string(g.current_player()) << " move: " << g.move_to_string(move) << std::endl;
g.play(move);
}
cout << mor << endl;
std::cout << g << std::endl;
}
if (mor.value(0) == 1) cout << mor.player_to_string(0) << " won";
else if (mor.value(1) == 1) cout << mor.player_to_string(1) << " won";
else cout << "draw";
cout << endl;
if (g.won(0)) std::cout << g.player_to_string(0) << " won";
else if (g.won(1)) std::cout << g.player_to_string(1) << " won";
else std::cout << "draw";
std::cout << std::endl;
}
}
#endif
File moved
File moved
File moved
File moved
File moved
File moved