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
#ifndef __PENGUIN_ZONE_HEURISTIC_FREEDOM_HPP__
#define __PENGUIN_ZONE_HEURISTIC_FREEDOM_HPP__
#include "penguin.hpp"
#include "heuristic.hpp"
#include "penguin_heuristic.hpp"
#include <algorithm>
namespace mcts
{
class zone_heuristic : public penguin_heuristic
{
public:
float get_value(const game::penguin& game, uint8_t move) const
{
game::penguin g = *(game::copy(game));
g.play(move);
auto s = g.get_state();
uint32_t* penguins = s.current_player_red ? s.peng_red : s.peng_blue;
uint32_t* other_peng = s.current_player_red ? s.peng_blue : s.peng_red;
uint64_t obstacles = g.create_obstacles_bitboard();
uint64_t moves_this = 0;
uint64_t moves_other = 0;
for(int i = 0; i < 4; ++i) {
//g.update_penguin_moves(&other_peng[i],obstacles);
//g.update_penguin_moves(&penguins[i],obstacles);
}
//std::cout << "CURRENT PLAYER RED : " << s.current_player_red << std::endl;
//std::cout << "NB MOVES RED : " << s.nb_moves_red << std::endl;
//std::cout << "NB MOVES BLUE : " << s.nb_moves_blue << std::endl;
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 ;
}
std::cout << "COUNT_THIS : " << count_this << std::endl;
std::cout << "COUNT_OTHER : " << count_other << std::endl;
std::cout << clamp((float)(count_this - count_other) / (count_this + 1), -1.f, 1.f) << std::endl;
return clamp((float)(count_this - count_other) / (count_this + 1), -1.f, 1.f);
}
int get_count(const game::penguin& game, uint8_t move) const
{
return 100;
}
private:
float clamp(float v, float l, float h) const
{
return std::max(std::min(h,v),l);
}
};
};
#endif
......@@ -15,7 +15,7 @@
* Copyright (c) 2007-2010 Baptiste Lepilleur
*/
#include <iostream>
#include "penguin.hpp"
#include "test_two_players_game.hpp"
#include "test_fast_log.hpp"
......@@ -23,20 +23,22 @@
#include "test_mcts_two_players.hpp"
#include "test_bits.hpp"
#include "learning.hpp"
#include "penguin_heuristic.hpp"
#include "config.hpp"
#include <omp.h>
using namespace std;
int main(int argc, char *argv[])
{
// util::test_fast_log(100000000);
// mcts::test_allocator(10, 2);
// omp_set_num_threads(8);
omp_set_num_threads(8);
game::config& c = game::config::get_config();
//game::run_test_two_players_game(game::penguin());
mcts::run_test_mcts_two_players(game::penguin());
const mcts::penguin_heuristic& h1 = *(c.get_heuristic(1));
const mcts::penguin_heuristic& h2 = *(c.get_heuristic(2));
std::cout << "COUCOU" << std::endl;
mcts::run_test_mcts_two_players(game::penguin(),h1,h2);
//util::test_bits(200000000);
return 0;
return 0;
}
#ifndef __MCTS_SETTINGS_HPP__
#define __MCTS_SETTINGS_HPP__
// Allocated memory for the mcts. If not enough the program may crash
#define MCTS_ALLOCATOR_SIZE 1000000U
#define MCTS_ALLOCATOR_SIZE 100000000U
// Reflection time for every turn of the mcts (in ms)
#define MCTS_TURN_TIME 5000
......
......@@ -9,60 +9,68 @@
#include <fstream>
#include <vector>
#include "openings.hpp"
#include "heuristic.hpp"
namespace mcts
{
template <typename Game>
class mcts_two_players : public mcts<Game>
{
template <typename Game>
class mcts_two_players : public mcts<Game>
{
allocator alloc_;
const util::fast_log fast_log_;
const float C_;
const unsigned int nb_visits_before_expansion_;
const heuristic<Game>& heuristic_;
const bool new_version_;
inline node* select(const std::shared_ptr<Game>& game, std::mt19937& generator, node* parent);
inline void expand(const std::shared_ptr<Game>& game, node* n);
void think(const std::shared_ptr<Game>& game);
public:
mcts_two_players(Game& game, uint32_t milliseconds, float C, unsigned int nb_visits_before_expansion = 8, bool new_version = true);
public:
mcts_two_players(Game& game, uint32_t milliseconds, float C, unsigned int nb_visits_before_expansion = 8,
const heuristic<Game>& heuristic = zero_knowledge<Game>(), bool new_version = true);
void reset();
void init_with_openings(const openings& o);
inline uint16_t select_move();
void last_move(uint16_t move);
void last_moves(uint16_t computer, uint16_t other);
};
};
template <typename Game>
mcts_two_players<Game> make_mcts_two_players(Game& game, uint32_t milliseconds, float C, unsigned int nb_visits_before_expansion = 8, bool new_version = true)
{
return mcts_two_players<Game>(game, milliseconds, C, nb_visits_before_expansion, new_version);
}
template <typename Game>
mcts_two_players<Game> make_mcts_two_players(Game& game, uint32_t milliseconds, float C, unsigned int nb_visits_before_expansion = 8,
const heuristic<Game>& heuristic = zero_knowledge<Game>(),
bool new_version = true)
{
return mcts_two_players<Game>(game, milliseconds, C, nb_visits_before_expansion, heuristic, new_version);
}
template <typename Game>
mcts_two_players<Game>::mcts_two_players(Game& game, uint32_t milliseconds, float C, unsigned int nb_visits_before_expansion, bool new_version)
: mcts<Game>(game, milliseconds), C_(C), nb_visits_before_expansion_(nb_visits_before_expansion), new_version_(new_version)
{
template <typename Game>
mcts_two_players<Game>::mcts_two_players(Game& game, uint32_t milliseconds, float C, unsigned int nb_visits_before_expansion,
const heuristic<Game>& heuristic,
bool new_version)
: mcts<Game>(game, milliseconds), C_(C), nb_visits_before_expansion_(nb_visits_before_expansion),
heuristic_(heuristic), new_version_(new_version)
{
this->generators.assign(util::omp_util::get_num_threads(), std::mt19937());
this->root = alloc_.allocate(1);
}
}
template <typename Game>
void mcts_two_players<Game>::reset()
{
template <typename Game>
void mcts_two_players<Game>::reset()
{
alloc_.clear();
this->root = alloc_.allocate(1);
}
}
template <typename Game>
void mcts_two_players<Game>::init_with_openings(const openings& o)
{
template <typename Game>
void mcts_two_players<Game>::init_with_openings(const openings& o)
{
o.copy_to(this->root, alloc_);
}
}
template <typename Game>
node* mcts_two_players<Game>::select(const std::shared_ptr<Game>& game, std::mt19937& generator, node* parent)
{
template <typename Game>
node* mcts_two_players<Game>::select(const std::shared_ptr<Game>& game, std::mt19937& generator, node* parent)
{
using namespace std;
const unsigned int N = parent->get_statistics().count;
const float log_of_N = fast_log_.log(N);
......@@ -76,63 +84,63 @@ namespace mcts
unsigned int count;
float v;
for (uint16_t i = 0; i < nb_children; ++i)
{
{
node* child = children + k;
count = child->get_statistics().count;
v = -child->get_statistics().value + C_ * sqrt(log_of_N / count);
if (v > best_value_so_far)
{
{
best_value_so_far = v;
best_child_so_far = child;
best_move_so_far = k;
}
}
++k;
if (k == nb_children) k = 0;
}
}
if (best_child_so_far->is_proven())
{
{
if (best_child_so_far->is_lost()) parent->set_won();
else
{
{
bool all_won = true;
for (uint16_t i = 0; i < nb_children; ++i)
{
{
node* child = children + i;
if (!child->is_won())
{
{
all_won = false;
break;
}
}
}
}
if (all_won) parent->set_lost();
}
}
}
}
game->play(best_move_so_far);
return best_child_so_far;
}
}
template <typename Game>
void mcts_two_players<Game>::expand(const std::shared_ptr<Game>& game, node* n)
{
template <typename Game>
void mcts_two_players<Game>::expand(const std::shared_ptr<Game>& game, node* n)
{
unsigned int count = n->get_statistics().count;
if (count >= nb_visits_before_expansion_ && !n->test_and_set())
{
{
unsigned int nb_children = game->number_of_moves();
node* children = alloc_.allocate(nb_children);
for (unsigned int i = 0; i < nb_children; ++i)
{
{
node* child = children + i;
child->get_statistics_ref().count = 1;
child->get_statistics_ref().value = 0;
}
child->get_statistics_ref().count = heuristic_.get_count(*game, i);
child->get_statistics_ref().value = heuristic_.get_value(*game, i);
}
n->set_children(children);
n->set_number_of_children(nb_children);
}
}
}
}
template <typename Game>
void mcts_two_players<Game>::think(const std::shared_ptr<Game>& game)
{
template <typename Game>
void mcts_two_players<Game>::think(const std::shared_ptr<Game>& game)
{
using namespace std;
const chrono::steady_clock::time_point start = chrono::steady_clock::now();
chrono::steady_clock::time_point now;
......@@ -142,72 +150,72 @@ namespace mcts
vector<uint16_t> moves(200);
unsigned int nb_iter = 0;
do
{
{
int size = 1;
node* current = this->root;
visited[0] = current;
while (!game->end_of_game() && !current->is_leaf() && !current->is_proven())
{
{
current = select(game, generator, current);
visited[size++] = current;
}
}
int game_value = 0;
if (current->is_proven())
{
{
if (current->is_won()) game_value = 1;
else
{
{
game_value = -1;
}
}
}
}
else if (game->end_of_game())
{
{
int v = game->value_for_current_player();
if (v > 0)
{
{
game_value = 1;
if (new_version_) current->set_won();
}
}
else if (v < 0)
{
{
game_value = -1;
if (new_version_)
{
{
current->set_lost();
if (size > 1) visited[size - 2]->set_won();
}
}
}
}
}
}
else
{
{
uint8_t player = game->current_player();
expand(game, current);
game->playout(generator);
int v = game->value(player);
if (v > 0) game_value = 1;
else if (v < 0) game_value = -1;
}
}
for (int i = size - 1; i >= 0; --i)
{
{
visited[i]->update(game_value);
game_value = -game_value;
}
}
game->set_state(state);
++nb_iter;
if ((nb_iter & 0x3F) == 0) now = chrono::steady_clock::now();
}
}
while ((nb_iter & 0x3F) != 0 || now < start + this->milliseconds);
}
}
template <typename Game>
uint16_t mcts_two_players<Game>::select_move()
{
template <typename Game>
uint16_t mcts_two_players<Game>::select_move()
{
using namespace std;
if (!this->root->is_proven())
{
#pragma omp parallel
{
#pragma omp parallel
think(game::copy(this->game));
}
}
// std::ofstream ofs ("graph.gv", ofstream::out);
// util::display_node::node_to_dot(ofs, this->root, 1000, 50);
util::display_node::node_to_ascii(cout, this->root, 1);
......@@ -222,52 +230,52 @@ namespace mcts
node* children = this->root->get_children();
unsigned int c;
for (uint16_t i = 0; i < nb_children; ++i)
{
{
node *child = children + k;
if (child->is_lost())
{
{
best_move_so_far = k;
break;
}
}
c = children[k].get_statistics().count;
if (c > best_count_so_far)
{
{
best_count_so_far = c;
best_move_so_far = k;
}
}
++k;
if (k == nb_children) k = 0;
}
}
return best_move_so_far;
}
}
template <typename Game>
void mcts_two_players<Game>::last_moves(uint16_t computer, uint16_t other)
{
template <typename Game>
void mcts_two_players<Game>::last_moves(uint16_t computer, uint16_t other)
{
if (this->root->is_leaf() || this->root->get_children()[computer].is_leaf())
{
{
alloc_.clear();
this->root = alloc_.allocate(1);
}
}
else
{
{
this->root = alloc_.move(&this->root->get_children()[computer].get_children()[other]);
}
}
}
}
template <typename Game>
void mcts_two_players<Game>::last_move(uint16_t move)
{
template <typename Game>
void mcts_two_players<Game>::last_move(uint16_t move)
{
if (this->root->is_leaf())
{
{
alloc_.clear();
this->root = alloc_.allocate(1);
}
}
else
{
{
this->root = alloc_.move(&this->root->get_children()[move], 20);
}
}
}
}
}
......
#ifndef __TEST_MCTS_TWO_PLAYERS_HPP__
#define __TEST_MCTS_TWO_PLAYERS_HPP__
#include "openings.hpp"
#include "mcts_two_players.hpp"
#include "MCTS_SETTINGS.hpp"
......@@ -10,67 +9,95 @@
#include <iomanip>
#include <set>
#include <fstream>
#include "heuristic.hpp"
#include "config.hpp"
#include "test_statistics.hpp"
#include <unistd.h>
#include "penguin.hpp"
namespace mcts
{
template <typename Game>
class test_mcts_two_players
{
openings openings_;
template <typename Game>
class test_mcts_two_players
{
openings* openings_;
std::ofstream savefile;
void play(Game g);
void play(Game g, const heuristic<Game>& h);
void self_play(Game g);
void self_play(Game g, int n, bool with_openings = false);
void self_play(Game g, const heuristic<Game>& h1 = zero_knowledge<Game>(), const heuristic<Game>& h2 = zero_knowledge<Game>());
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);
};
void save_board(const Game& game, int move);
public:
test_mcts_two_players(const Game& g, const heuristic<Game>& h1 = zero_knowledge<Game>(), const heuristic<Game>& h2 = zero_knowledge<Game>());
};
template <typename Game>
test_mcts_two_players<Game>::test_mcts_two_players(const Game& g, const heuristic<Game>& h, const heuristic<Game>& h2)
{
game::config& config = game::config::get_config();
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);
}
savefile.open("Keras/save.csv", std::ios::app);
template <typename Game>
void run_test_mcts_two_players(const Game& g)
{
test_mcts_two_players<Game>{g};
}
if(config.is_ai_vs_ai())
{
self_play(g, h,h2);
}
else
{
play(g, h);
}
savefile.close();
}
template <typename Game>
void run_test_mcts_two_players(const Game& g, const heuristic<Game>& h = zero_knowledge<Game>(),const heuristic<Game>& h2 = zero_knowledge<Game>())
{
test_mcts_two_players<Game> {g, h, h2};
}
template <typename Game>
void test_mcts_two_players<Game>::test_openings(Game g, int nb_learning, int nb_testing)
{
// self_play(g, nb_testing);
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<Game>::select_move(Game& game)
{
template <typename Game>
int test_mcts_two_players<Game>::select_move(Game& game)
{
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;
}
}
std::string move;
getline(std::cin, move);
game.play(m[move]);
return m[move];
}
}
template <typename Game>
void test_mcts_two_players<Game>::save_board(const Game& game, int move){
std::shared_ptr<game::penguin> g = game::copy(game);
game::penguin_state state = g->get_state();
uint32_t pos = g->move_to_pos(move);
uint16_t peng_pos = (uint16_t)(pos >> 16);
uint16_t dest = (uint16_t)pos;
savefile << state.one_fish << ";" << state.two_fish << ";" << state.three_fish << ";" << g->penguin_board(false) << ";" << g->penguin_board(true) << ";" << peng_pos << ";" << dest << std::endl;
}
template <typename Game>
void test_mcts_two_players<Game>::play(Game g)
{
// ProfilerStart("theturk.prof");
auto the_turk = make_mcts_two_players(g, MCTS_TURN_TIME, 0.4, 8);
template <typename Game>
void test_mcts_two_players<Game>::play(Game g, const heuristic<Game>& h)
{
//ProfilerStart("theturk.prof");
auto the_turk = make_mcts_two_players(g, MCTS_TURN_TIME, 0.4, 8, h);
std::cout << "play one game" << std::endl;
std::cout << "who's first? (h)uman/(c)omputer ";
std::string ans;
......@@ -78,190 +105,208 @@ namespace mcts
std::cout << g.to_string() << std::endl;
int human_last_move = -1, computer_last_move = -1;
while (!g.end_of_game())
{
{
if ((ans == "h" && g.current_player() == 0) || (ans == "c" && g.current_player() == 1))
{
{
human_last_move = select_move(g);
}
}
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;
std::cout << g.player_to_string(g.current_player()) << " move: " << g.move_to_string(move) << std::endl;
g.play(move);
}
}
std::cout << g << std::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();
}
}
template <typename Game>
void test_mcts_two_players<Game>::self_play_learn_openings(Game g, int n)
{
template <typename Game>
void test_mcts_two_players<Game>::self_play_learn_openings(Game g, int n)
{
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::cout << "DZMALJDFOPZAIHFOAZHFOZUHAOFHZAO" << std::endl;
std::map<std::set<int>, std::pair<std::uint32_t, double>> learning_examples;
for (int i = 0; i < n; ++i)
{
{
std::cout << i << std::endl;
std::cout << openings_ << std::endl << std::endl;
std::cout << (*openings_) << std::endl << std::endl;
moves.clear();
g.set_state(state);
the_turk_1.reset();
the_turk_1.init_with_openings(openings_);
the_turk_1.init_with_openings(*openings_);
the_turk_2.reset();
the_turk_2.init_with_openings(openings_);
the_turk_2.init_with_openings(*openings_);
int the_turk_1_last_move = -1, the_turk_2_last_move = -1;
int k = 0;
while (!g.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);
}
}
std::uint16_t move = the_turk_1.select_move();
moves.push_back(move);
the_turk_1_last_move = move;
g.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);
}
}
std::uint16_t move = the_turk_2.select_move();
moves.push_back(move);
the_turk_2_last_move = move;
g.play(move);
}
}
++k;
}
std::cout << g.to_string() << std::endl;
}
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)
{
g.play(m);
v = -v;
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] = std::make_pair(1, v);
}
else
{
it->second.second = (it->second.second * it->second.first + v) / (it->second.first + 1);
it->second.first += 1;
}
}
}
openings_->update(g, moves, v);
g.set_state(state);
for (std::uint16_t m : moves)
{
g.play(m);
v = -v;
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] = std::make_pair(1, v);
}
else
{
it->second.second = (it->second.second * it->second.first + v) / (it->second.first + 1);
it->second.first += 1;
}
}
}
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)
{
output << " " << index << ":" << 1;
}
output << std::endl;
}
{
output << example.second.second;
for (int index : example.first)
{
output << " " << index << ":" << 1;
}
output << std::endl;
}
output.close();
}
}
template <typename Game>
void test_mcts_two_players<Game>::self_play(Game g, int n, bool with_openings)
{
template <typename Game>
void test_mcts_two_players<Game>::self_play(Game g, const heuristic<Game>& h1, const heuristic<Game>& h2)
{
//std::cout << getFilePath() << std::endl;
game::config& c = game::config::get_config();
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);
auto the_turk_v1 = make_mcts_two_players(g, c.get_think_time(), 0.6, 2, h1);
auto the_turk_v2 = make_mcts_two_players(g, c.get_think_time(), 0.6, 2, h2);
std::string result_directory(get_test_results_directory());
int nb_win_v1 = 0, nb_win_v2 = 0, nb_draw = 0;
for (int i = 0; i < n; ++i)
{
std::cout << i << std::endl;
for (unsigned int i = 0; i < c.get_game_count(); ++i)
{
//std::cout << i << std::endl;
if(i%2 == 0)
{
state = game::penguin::random_start_state();
}
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;
int k = 0;
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 << c4 << endl;
{
++k;
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);
}
}
std::uint16_t move = the_turk_v1.select_move();
the_turk_v1_last_move = move;
save_board(g, move);
g.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);
}
}
std::uint16_t move = the_turk_v2.select_move();
the_turk_v2_last_move = move;
play(move);
}
}
save_board(g, move);
g.play(move);
}
if(c.should_send_game_to_gui())
std::cout << g;
}
if (g.won(0))
{
{
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
{
{
/*cout << "draw" << endl;*/ ++nb_draw;
}
}
save_stats(result_directory,nb_win_v1,nb_win_v2,nb_draw);
std::cout << std::setw(10) << "v1 nb wins: " << nb_win_v1 << " v2 nb wins: " << nb_win_v2 << " nb draws: " << nb_draw << std::endl;
}
}
}
}
template <typename Game>
void test_mcts_two_players<Game>::self_play(Game g)
{
template <typename Game>
void test_mcts_two_players<Game>::self_play(Game g)
{
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;
......@@ -271,36 +316,36 @@ namespace mcts
std::cout << g << std::endl;
int the_turk_v1_last_move = -1, the_turk_v2_last_move = -1;
while (!g.end_of_game())
{
{
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);
}
}
std::uint16_t move = the_turk_v1.select_move();
the_turk_v1_last_move = move;
std::cout << g.player_to_string(g.current_player()) << " move: " << g.move_to_string(move) << std::endl;
g.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);
}
}
std::uint16_t move = the_turk_v2.select_move();
the_turk_v2_last_move = move;
std::cout << g.player_to_string(g.current_player()) << " move: " << g.move_to_string(move) << std::endl;
g.play(move);
}
std::cout << g << std::endl;
}
}
std::cout << g << std::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;
}
}
}
......
......@@ -179,7 +179,7 @@ public class Controller implements Initializable
*/
public void updateModelAndView()
{
for (int i = 0; i < board.length; i++)
for (int i = 0; i < 60; i++)
{
board[i].setNbFish(gameState.getNbFish(i));
board[i].setPenguinPresence(Tile.PenguinPresence.NO_PENGUIN);
......@@ -382,3 +382,4 @@ public class Controller implements Initializable
}
......@@ -206,3 +206,4 @@ public class UpdateThread extends Thread
}
}
}
......@@ -33,7 +33,7 @@ public class GameState
public void update(String jsonString)
{
//System.out.println(jsonString);
JSONObject json = new JSONObject(jsonString);
can_play.put(Player.Red, json.getJSONObject("can_play").getBoolean("red"));
can_play.put(Player.Blue, json.getJSONObject("can_play").getBoolean("blue"));
......@@ -50,6 +50,7 @@ public class GameState
for (int i = 0; i < jsonPeng.length(); i++)
penguins.get(Player.Blue)[i] = jsonPeng.getInt(i);
}
/**
......
#!/usr/bin/python
from keras.models import Sequential
from keras.layers import Dense
import numpy
# Fix random seed for reproducibility
seed = 7
numpy.random.seed(seed)
# Loading data
dataset = numpy.loadtxt("save.csv", delimiter=";")
# Split into input (X) and output (Y) variables
X = dataset[:,0:5]
Y = dataset[:,5:]
# Creating the model
model = Sequential()
model.add(Dense(12, input_dim=5, init='uniform', activation='relu'))
model.add(Dense(8, init='uniform', activation='relu'))
model.add(Dense(2, init='uniform', activation='softmax'))
# Compiling the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
# Training the model
model.fit(X, Y, nb_epoch=150, batch_size=10)
# Evaluating the model
scores = model.evaluate(X, Y)
print("%s: %.2f%%" % (model.metrics_names[1], scores[1]*100))
This diff is collapsed.
1087952262335610071;37928917878899488;27040324392337416;34496061440;9020668272181248;37;13
1087952262335610071;37928780439946016;27040324392337416;34496061440;9016270225932288;42;18
1087947864289098967;37928780439946016;27040324392337416;8726257664;9016270225932288;35;33
1087947864289098967;37928780439946016;27040290032599048;8726257664;18023469480673280;53;54
1087947864289098967;37928780439946016;18033090777858056;1099647950848;18023469480673280;33;40
1087947864289098967;37928771850011424;18033090777858056;1099647950848;18023469513965568;18;25
1087947864289098967;37928771850011424;18033090777595912;1099647943168;18023469513965568;13;9
1087947864289090775;37928771850011424;18033090777595912;1099647943168;18023194636058632;38;3
1087947589411183831;37928771850011424;18033090777595912;1100182716928;18023194636058632;21;29
1087947589411183831;37928771847914272;18033090777595912;1100182716928;149533614931976;54;47
1087947589411183831;37928771847914272;18692268113928;671154688;149533614931976;40;16
1087947589411183831;37928771847914272;17592756486152;671154688;158329707954184;43;44
1087938793318161623;37928771847914272;17592756486152;281475513647616;158329707954184;27;48
1087938793318161623;37928771713696544;17592756486152;281475513647616;158329707954240;3;6
1087938793318161623;37928771713696544;17592756486144;281475513713152;158329707954240;16;17
1087938793318161623;37928771713631008;17592756486144;281475513713152;158329691177024;25;24
1087938793318161623;37928771713631008;17592722931712;281475513583104;158329691177024;17;10
1087938793318161623;37928771713499936;17592722931712;281475513583104;140737505136704;44;12
1087938793318161623;37928771713499936;536887296;36028797555836416;140737505136704;48;55
1087657318341450967;37928771713499936;536887296;36028797555836416;140737509330944;6;22
1087657318341450903;37928771713499936;536887296;36028797555836160;140737509330944;9;8
1087657318341450903;37928771713499424;536887296;36028797555836160;140737510375424;12;20
1087657318341446807;37928771713499424;536887296;72057594574800128;140737510375424;55;56
1087657318341446807;1899974694535456;536887296;72057594574800128;140737506197504;22;14
1087657318341446807;1899974690341152;536887296;144115188612728064;140737506197504;56;57
1015599724303518871;1899974690341152;536887296;144115188612728064;140741784387584;24;32
1015599724286741655;1899974690341152;536887296;2199560127744;140741784387584;57;41
871484536210885783;1899974690341152;536887296;2199560127744;140741783863296;20;19
871484536209837207;1899974690341152;536887296;2199560160256;140741783863296;8;15
871484536209837207;1899974690340896;536887296;2199560160256;4303896576;47;23
871484536209837207;1759237201985568;536887296;562950490326016;4303896576;41;49
871482337186581655;1759237201985568;536887296;562950490326016;4370481152;19;26
871482337186057367;1759237201985568;536887296;1125900443747328;4370481152;49;50
871482337186057367;1196287248564256;536887296;1125900443747328;70368819691520;32;46
871482332891090071;1196287248564256;536887296;288230376688616448;70368819691520;50;58
871482332891090071;70387341721632;536887296;288230376688616448;70918567116800;23;39
871482332891090071;70387333333024;536887296;288230444871222272;70918567116800;29;36
871482332891090071;70387333333024;16384;288230444871222272;70370958786560;39;31
871481783135276183;70387333333024;16384;288230376420180992;70370958786560;36;28
871481714415799447;70387333333024;16384;288230376420180992;70370958770304;14;7
871481714415799447;70387333333024;0;288230376420179972;70370958770304;10;2
871481714415798423;70387333333024;0;288230376420179972;70369885028480;31;30
871481712268314775;70387333333024;0;2251800082153476;70369885028480;58;51
583251336116603031;70387333333024;0;2251800082153476;35185512939648;46;45
583251336116603031;18589155360;0;2251800082153473;35185512939648;2;0
583251336116603027;18589155360;0;2251800082153473;35202625699968;26;34
583251336116603027;18522046496;0;2251800082153474;35202625699968;0;1
583251336116603026;18522046496;0;2251800082153474;35202625699968;30;30
583251336116603026;18522046496;0;4503599895838722;35202625699968;51;52
580999536302917778;18522046496;0;4503599895838722;35202625699968;30;30
580999536302917778;18522046496;0;576460752571891714;35202625699968;52;59
576495936675547282;18522046496;0;576460752571891714;35202625699968;30;30
1087952262335610071;37928917878899488;27040324392337416;34496061440;9020668272181248;37;13
1087952262335610071;37928780439946016;27040324392337416;34496061440;13469017702400;53;18
1087952262335610071;37928780439946016;18033125137596424;34395398144;13469017702400;27;25
1087952262335610071;37928780305728288;18033125137596424;34395398144;4673461551104;43;29
1087943466242587863;37928780305728288;18033125137596424;70403106021376;4673461551104;25;46
1087943466242587863;37928780305728288;18033125104041992;70403106021376;1374926667776;42;40
1087939068196076759;37928780305728288;18033125104041992;18014432871325696;1374926667776;46;54
1087939068196076759;37858411561550624;18033125104041992;18014432871325696;141012903395328;40;47
1087939068196076759;37858411561550624;18032025592414216;36028831380807680;141012903395328;54;55
1087939068196076759;37858411561550624;17627082932232;36028831380807680;140738025488392;38;3
1087938793318169815;37858411561550624;17627082932232;36028831380800000;140738025488392;13;9
1087938793318161623;37858411561550624;17627082932232;36028831380800000;281475513843720;47;48
1087938793318161623;37717674073195296;17627082932232;288230410513547776;281475513843720;55;58
1087938793318161623;1688877054231328;17627082932232;288230410513547776;281475513647112;18;16
1087938793318161623;1688877054231328;17627082670088;288230393333678592;281475513647112;35;34
1087938793318161623;1688877054231328;17592722931720;288230393333678592;281475245211656;29;28
1087938793318161623;1688877054231328;17592186060808;17609368011264;281475245211656;58;44
799708417166449879;1688877054231328;17592186060808;17609368011264;281474976776216;28;4
799708417166449879;1688876785795872;17592186060808;17600778076672;281474976776216;34;33
799708417166449879;1688859605926688;17592186060808;17600778076672;2251799813750808;48;51
799426942189739223;1688859605926688;17592186060808;17600778207232;2251799813750808;9;17
799426942189739223;1688859605926176;17592186060808;17600778207232;1125899906908184;51;50
797175142376053975;1688859605926176;17592186060808;17600776110208;1125899906908184;21;7
797175142376053975;1688859603829024;17592186060808;17600776110208;1125899906908232;4;6
797175142376053959;1688859603829024;17592186060808;17600776126464;1125899906908232;7;14
797175142376053831;1688859603829024;17592186060808;17600776126464;1125899906909248;3;10
797175142376053831;1688859603829024;17592186060800;17602923479040;1125899906909248;17;31
797175142376053831;1688859603697952;17592186060800;17602923479040;1125899915232320;16;23
797175142376053831;1688859603632416;17592186060800;17594400653312;1125899915232320;33;26
797175142376053831;1688851013697824;17592186060800;17594400653312;1125899915233344;10;11
797175142376052807;1688851013697824;17592186060800;17594333548544;1125899915233344;26;12
797175142376052807;1688850946588960;17592186060800;17594333548544;1125899915755584;11;19
797175142376050759;1688850946588960;17592186060800;17592202842112;1125899915755584;31;24
797175140228567111;1688850946588960;17592186060800;17592202842112;1125899907399744;23;15
797175140228567111;1688850938200352;17592186060800;4503599644168192;1125899907399744;44;52
797175140228567111;1688850938200352;16384;4503599644168192;1125899907924032;19;20
797175140228042823;1688850938200352;16384;4503599644164128;1125899907924032;12;5
797175140228038727;1688850938200352;16384;4503599644164128;1125899907891520;15;8
797175140228005959;1688850938200352;16384;4503603922354208;1125899907891520;24;32
797175140211228743;1688850938200352;16384;4503603922354208;144115188076904768;50;57
797175140211228743;562951031357728;16384;4503603926532128;144115188076904768;14;22
797175140211228743;562951031357728;0;4503603926532128;72057594038976832;57;56
653059952135372871;562951031357728;0;576460756602585120;72057594038976832;52;59
648556352508002375;562951031357728;0;576460756602585120;72057594038976577;8;0
648556352508002375;562951031357472;0;576461302063431712;72057594038976577;32;39
648556348213035079;562951031357472;0;576461302063431712;72057594038976580;0;2
648556348213035078;562951031357472;0;576461302063431712;72057594038976580;39;39
648556348213035078;562951031357472;0;576461302063431712;72057594038976578;2;1
648556348213035074;562951031357472;0;576461302063431712;72057594038976578;39;39
648556348213035074;562951031357472;0;576461302063431712;562949954469954;56;49
576498754175107138;562951031357472;0;576461302063431712;562949954469954;39;39
576498754175107138;562951031357472;0;576461302063431712;2199024304194;49;41
576498754175107138;1077936160;0;576461302063431712;2199024304194;39;39
760344128994979089;288396449854608042;104180925757259844;4611826764505677832;1107296576;14;62
760344128994979089;288396449854591658;104180925757259844;4611826764505677832;2200130552064;6;41
760344128994979089;288396449854591658;104180925757259780;18155144587771912;2200130552064;62;54
760344128994979089;288396449854591658;104180925757259780;18155144587771912;562951060717824;41;49
760344128994979089;288394250831336106;104180925757259780;288371122230001672;562951060717824;54;58
760344128994979089;288394250831336106;86166527247777796;288371122230001672;562951027294464;25;17
760344128961424657;288394250831336106;86166527247777796;288371122230001668;562951027294464;3;2
760344128961424657;288394250831336098;86166527247777796;288371122230001668;562951029260544;17;21
......@@ -31,13 +31,13 @@ AIExecutable = $(AIOutputDir)/penguin
# Source files directory
AISRC = $(AIRoot)/src
# Directories with .h files
AIInclude=-I $(AISRC)/game -I $(AISRC)/util -I $(AISRC)/monte_carlo -I $(AISRC)/mcts -I $(AISRC)/json
AIInclude=-I $(AISRC)/game -I $(AISRC)/util -I $(AISRC)/monte_carlo -I $(AISRC)/mcts -I $(AISRC)/json -I $(AISRC)/heuristics
# 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 main.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 points_heuristic.cpp test_statistics.cpp go_left_up_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
vpath %.cpp $(AISRC)/game $(AISRC)/heuristics $(AISRC)/main $(AISRC)/util $(AISRC)/monte_carlo $(AISRC)/mcts $(AISRC)/gdl
# Flags passed to the compiler
CFLAGS=-g -O3 -ffast-math -fopenmp -Wall -std=c++11
# Flags passed to the linker
......
# Makefile for the Penguin game project.
# This project is composed of two modules: AI(Artificial Intelligence), that manage the game, and GUI that allows you to play it
# To compile the AI run 'make ai'; it is written in C++.
# To compile the GUI run 'make gui'; it is written in java and javafx.
# To compile everything run 'make all' or 'make'.
# To run the GUI (that will launch the AI) run 'make run'.
# Run 'make clean' to delete compiled files and programs. 'make cleanai' and 'make cleangui' also exist
# COMPILERS AND PROGRAMS
# Compiler for the AI: g++ >= 4.9
CC = g++
# JAVA ENVIRONMENT FOR THE GUI
# Java compiler >= 1.8 (usually javac)
JAVAC := javac
# Java jar utility (usually jar)
JAR := jar
# Java application launcher >= 1.8 (usually java)
JAVA := java
all: ai gui
clean: cleanai cleangui
# ============================================ AI (C++) =============================================
#Root directory for the AI module
AIRoot = AI
# Output directory
AIOutputDir = $(AIRoot)/bin
# Name of the executable
AIExecutable = $(AIOutputDir)/penguin
# Source files directory
AISRC = $(AIRoot)/src
# Directories with .h files
AIInclude=-I $(AISRC)/game -I $(AISRC)/util -I $(AISRC)/monte_carlo -I $(AISRC)/mcts -I $(AISRC)/json
# 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 \
<<<<<<< HEAD
test_mcts_two_players.cpp bits.cpp test_bits.cpp penguin_heuristic.cpp points_heuristic.cpp main.cpp config.cpp
=======
test_mcts_two_players.cpp bits.cpp test_bits.cpp penguin_heuristic.cpp movement_freedom_heuristic.cpp main.cpp config.cpp
>>>>>>> 34412ade6e5941a3c916b90b915655977cb2068c
# 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
CFLAGS=-g -O3 -ffast-math -fopenmp -Wall -std=c++11
# Flags passed to the linker
LDFLAGS=-fopenmp -std=c++11 #-lprofiler -Wl,-no_pie
# Generating .o files' names
AIOobjects=$(addprefix $(AIOutputDir)/, $(AISourceFile:.cpp=.o))
# ==== RULES ====
# Main rule for creating the executable
ai: $(AIOutputDir) $(AIExecutable)
$(AIOutputDir):
mkdir -p $(AIOutputDir)
$(AIExecutable): $(AIOobjects)
$(CC) $(LDFLAGS) $(AIOobjects) -o $@
# Creates .o files with automatic generation of dependencies (.d) files
$(AIOutputDir)/%.o: %.cpp
$(CC) -c $(CFLAGS) $(AIInclude) -MD -MP $< -o $@
# Include the dependency files into the makefile
-include $(AIOobjects:.o=.d)
# Deletes .o, .d and the executable
cleanai:
rm -f $(AIOutputDir)/*.o $(AIOutputDir)/*.d $(AIExecutable)
# ============================================ GUI (Java) =============================================
GUIRoot := GUI
# Output root directory
GUIOutDir := $(GUIRoot)/out
# Jar output file name
GUIJarFile := $(GUIOutDir)/penguin.jar
# Compiled classes output directory
GUIOutClassDir := $(GUIOutDir)/classes
# Sources root directory: all your sources will be under this directory (packages and classes)
GUISrcDir := $(GUIRoot)/src
# Packages that will be compiled and included in the jar: every class in these packages will be compiled, if you have classes in a package and in a subpackage as well you must specify both. Example: main package package/subpackage
GUIPackages := main model view controller
# Additional classes tha will be compiled and included in the jar: useful for packages that contains classes that you don't want to include. Otherwise, just use the Packages variable. Specify the path inside the source root WITH EXTENSION. Example: main/Main.java package/subpackage/MyClass.java
GUIAdditionalClasses :=
# Entry point (main class) for jar file. In the form package/subpackage/Class
GUIJAREntryPoint := main.Main
# Additional resources that will be copied in jar file. In the form -C path/to/the/parent/folder resource/path (resource/path will also be the path of the resourc inside the jar)
GUIJARResources := -C $(GUISrcDir) resource/ -C $(GUISrcDir) view/view.fxml
# Classpath option for compilation or execution: colon (:) separated list, must prepend -classpath flag. Example: -classpath Library.jar:Library/Include
GUIClassPath := -classpath $(GUIRoot)/json-20160212.jar
# Compiler additional flags
GUIJAVACFlags :=
# JAR additional flags
GUIJARFlags :=
# Run arguments
GUIRunArgs := $(AIExecutable)
# ========== AUTOMATICALLY GENERATED VARIABLES ==========
# Searching for java files inside the $(Packages): for every package we search for java files and later we strip the leading source folder that is not necessary
GUIClassesSources := $(patsubst $(GUISrcDir)/%, %, $(foreach package, $(GUIPackages), $(wildcard $(GUISrcDir)/$(package)/*.java)))
GUIClassesSources := $(GUIClassesSources) $(GUIAdditionalClasses)
# We tell make to search for java sources inside the source directory
vpath %.java $(GUISrcDir)
# ========== RULES ==========
gui: $(GUIJarFile)
# You need all compiled version of classes to make jar file
$(GUIJarFile): $(addprefix $(GUIOutClassDir)/, $(GUIClassesSources:.java=.class)) $(GUIOutDir)
$(JAR) cvfe $(GUIJarFile) $(GUIJAREntryPoint) -C $(GUIOutClassDir) ./ $(GUIJARResources) $(GUIJARFlags)
$(GUIOutDir):
mkdir -p $(GUIOutDir)
# To compile a class you need the source code and the output folder
$(GUIOutClassDir)/%.class: $(GUISrcDir)/%.java $(GUIOutClassDir)
$(JAVAC) -sourcepath $(GUISrcDir) -d $(GUIOutClassDir) $(GUIClassPath) $(GUIJAVACFlags) $<
$(GUIOutClassDir):
mkdir -p $(GUIOutClassDir)
run:
@$(JAVA) $(GUIClassPath):$(GUIJarFile) main.Main $(GUIRunArgs)
guicleanjar:
-rm -f $(GUIJarFile)
guicleanclass:
-rm -f $(addprefix $(GUIOutClassDir)/, $(GUIClassesSources:.java=.class))
cleangui: guicleanjar guicleanclass
#!/bin/bash
#declare -a machines=("berger" "cavalier" "pion" "roi" "reine" "fou" "pat" "echiquier" "mate")
declare -a machines=("contrex" "perrier")
echo -n User:
read user
for m in "${machines[@]}"
do
#ssh ${user}@${m} '
#gnome-terminal -hold "bash -c ssh ${user}@${m}"
xterm -hold -e "ssh ${user}@${m} 'cd pingouins;make && ./AI/bin/penguin'" &
done
0;0;0;0;0;0
535368492349450263;581484495112372960;36068517145023752;330240;137441050888;39274
535368492349450263;581484495112372960;36068517144958216;1099511892480;137441050888;32
535368492349450263;581484495112372960;36068517144958208;1099511892480;35321813139712;2
#!/bin/bash
for test_ver in `ls test`
do
win_v1=0
win_v2=0
draw=0
rm "test/$test_ver/res.stats"
for file in `ls test/$test_ver/*.stats`
do
read -r line < $file
IFS=' ' read -r -a wins <<< "$line"
win_v1=$((win_v1 + wins[0]))
win_v2=$((win_v2 + wins[1]))
draw=$((draw + wins[2]))
done
echo "$win_v1 $win_v2 $draw" > "test/$test_ver/res.stats"
done