Commit 5f64c57d authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Use a likely imputation for the MCTS

The likely imputation minimizes the number of errors the player would
make.
parent 5b94e89e
......@@ -2764,6 +2764,16 @@ is. This is very useful when imputing the cards of the other players:
if the taker is not likely, then it is stupid to even consider what to
do with this state of imputation.
#+end_deftypefun
#+attr_texinfo: :options {TarotImputationError} tarot_counter_and_game_impute_likely (TarotCounter *@var{counter}, TarotGame *@var{game}, size_t @var{n}, @var{seed})
#+begin_deftypefun
Impute the unknown cards in /game/, which is synchronized with
/counter/. Use the /n/ bytes of /seed/.
The imputation is not uniform; the taker tends to receive more strong
cards than the others. More precisely, this function tries to have a
low /error/ sum (see =tarot_features_errors=).
#+end_deftypefun
** Playing a game against the AI
libtarot has an API endpoint to run a game against the AI.
......
......@@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: tarot 0.4.2.83-ebdc-dirty\n"
"Report-Msgid-Bugs-To: vivien@planete-kraus.eu\n"
"POT-Creation-Date: 2019-10-12 13:25+0200\n"
"POT-Creation-Date: 2019-10-12 13:33+0200\n"
"PO-Revision-Date: 2019-10-12 11:34+0200\n"
"Last-Translator: Vivien Kraus <vivien@planete-kraus.eu>\n"
"Language-Team: French\n"
......@@ -520,12 +520,12 @@ msgstr ""
"Erreur : le module de caractéristiques n'a pas été initialisé. Veuillez "
"appeler tarot_set_datadir.\n"
#: src/libtarot/features/tarot/features_private_impl.h:48
#: src/libtarot/features/tarot/features_private_impl.h:49
#, c-format
msgid "Error: could not open file %s: %s\n"
msgstr "Erreur : impossible d'ouvrir le fichier %s : %s\n"
#: src/libtarot/features/tarot/features_private_impl.h:106
#: src/libtarot/features/tarot/features_private_impl.h:107
#, c-format
msgid ""
"Error: the installed ARFF header (%s/features-header-encoded.txt) is not "
......@@ -534,7 +534,7 @@ msgstr ""
"Erreur : l'en-tête ARFF installé (%s/features-header-encoded.txt) n'est pas "
"compatible avec cette version (%s).\n"
#: src/libtarot/features/tarot/features_private_impl.h:126
#: src/libtarot/features/tarot/features_private_impl.h:127
#, c-format
msgid ""
"Error: the installed ARFF header (%s/features-header.txt) is not compatible "
......
......@@ -113,6 +113,14 @@ tarot_counter_and_game_impute (TarotCounter * counter, TarotGame * game,
return counter_and_game_impute (counter, game, seed_size, seed);
}
TarotImputationError
tarot_counter_and_game_impute_likely (TarotCounter * counter,
TarotGame * game,
size_t seed_size, const void *seed)
{
return counter_and_game_impute_likely (counter, game, seed_size, seed);
}
#include <tarot/counter_private_impl.h>
#include <tarot/cards_private_impl.h>
#include <tarot/player_private_impl.h>
......
......@@ -84,6 +84,14 @@ extern "C"
size_t seed_size,
const void *seed);
/**
* tarot_counter_and_game_impute_likely
*/
TarotImputationError tarot_counter_and_game_impute_likely (TarotCounter *counter,
TarotGame *game,
size_t seed_size,
const void *seed);
#ifdef __cplusplus
}
#endif /* __cplusplus */
......
......@@ -51,6 +51,15 @@ extern "C"
size_t seed_size,
const void *seed);
static TarotImputationError counter_and_game_impute_likely (TarotCounter *
counter,
TarotGame *
game,
size_t
seed_size,
const void
*seed);
#ifdef __cplusplus
}
#endif /* __cplusplus */
......
......@@ -26,10 +26,12 @@
#include <tarot/game_private.h>
#include <tarot/trick_private.h>
#include <tarot/tricks_private.h>
#include <tarot/features_private.h>
#include <assert.h>
#include <stdio.h>
#include <limits.h>
#include <float.h>
#include <math.h>
#include <nettle/yarrow.h>
......@@ -814,8 +816,56 @@ accept (double gain, double temperature, double draw)
return (exp (-gain / temperature) > draw);
}
static inline TarotImputationError
counter_and_game_impute_likely (TarotCounter * counter,
TarotGame * game,
size_t seed_size, const void *seed)
{
double best_errors;
size_t i;
TarotImputationError ret = TAROT_IMPUTATION_FAILED;
unsigned char current_seed[256];
struct yarrow256_ctx generator;
TarotGame best_game;
size_t n_players = game_n_players (game);
int with_call = game_with_call (game);
yarrow256_init (&generator, 0, NULL);
yarrow256_seed (&generator, seed_size, seed);
game_initialize (&best_game, n_players, with_call);
game_copy (&best_game, game);
yarrow256_random (&generator, sizeof (current_seed), current_seed);
for (i = 0; i < 10; i++)
{
TarotCounter candidate_counter;
TarotImputationError candidate_err;
TarotGame candidate_game;
yarrow256_random (&generator, sizeof (current_seed), current_seed);
counter_initialize (&candidate_counter);
game_initialize (&candidate_game, n_players, with_call);
game_copy (&candidate_game, game);
counter_load_from_game (&candidate_counter, &candidate_game);
candidate_err =
counter_and_game_impute (&candidate_counter, &candidate_game,
sizeof (current_seed), current_seed);
if (candidate_err == TAROT_IMPUTATION_OK)
{
double candidate_errors = features_errors (&candidate_game);
if (ret != TAROT_IMPUTATION_OK || best_errors > candidate_errors)
{
ret = candidate_err;
best_errors = candidate_errors;
game_copy (&best_game, &candidate_game);
}
}
}
game_copy (game, &best_game);
counter_load_from_game (counter, game);
return ret;
}
#include <tarot/game_private_impl.h>
#include <tarot/trick_private_impl.h>
#include <tarot/tricks_private_impl.h>
#include <tarot/features_private_impl.h>
#endif /* not H_TAROT_COUNTER_PRIVATE_IMPL_INCLUDED */
......@@ -29,6 +29,7 @@
#include <string.h>
#include <float.h>
#include <errno.h>
#include "xalloc.h"
static char *features_header_data = NULL;
static char *features_header_encoded_data = NULL;
......
......@@ -510,8 +510,8 @@ mcts_impute (TarotGame * game, struct yarrow256_ctx *rng)
yarrow256_random (rng, sz, (void *) imputation_seed);
counter_load_from_game (&counter, game);
ret =
(counter_and_game_impute (&counter, game, sz, (void *) imputation_seed) !=
TAROT_IMPUTATION_OK);
(counter_and_game_impute_likely
(&counter, game, sz, (void *) imputation_seed) != TAROT_IMPUTATION_OK);
return ret;
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment