Commit faf0d9d9 authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Remove the MCTS API

parent 60730888
......@@ -1675,67 +1675,6 @@ operation succeeded and now /game/ knows all the cards of everyone
This operation uses the /n/ bytes of /seed/ to initialize the
pseudo-random number generator.
#+end_deftypefun
** Artificial intelligence through the Monte-Carlo Tree Search
#+attr_texinfo: :options {struct} TarotMcts
#+begin_deftp
A tree structure that holds the MCTS tree. It has a fixed size of
memory available to store the nodes, but since some nodes require more
memory than others (discard and handful nodes hold all cards), it is
not possible to convert it to a maximum number of nodes.
There are a few parameters for the algorithm. All of them have a
default value which is not specified. Reproducibility is thus only
guaranteed with the same minor version of libtarot, if all parameters
are defined manually.
#+end_deftp
#+attr_texinfo: :options enum TarotMctsError
#+begin_deftp
Everything that can happen when populating the MCTS tree:
- =TAROT_MCTS_OK=: no error;
- =TAROT_MCTS_NONEXT=: the game is not expecting a move from a player
(dog);
- =TAROT_MCTS_UNKNOWNNEXT=: the game expects a move from a player
whose cards we don't know;
- =TAROT_MCTS_NODATA=: it was not possible to expand a single node
within the MCTS. Likely cause: the game is corrupted. Other cause:
you have bad luck when imputing the game;
- =TAROT_MCTS_NOMEM=: the memory limit has been reached.
#+end_deftp
#+attr_texinfo: :options {TarotMcts *} tarot_mcts_alloc (size_t @var{maxpool})
#+begin_deftypefun
#+texinfo: @deftypefunx {size_t} tarot_mcts_construct (size_t @var{max_mem}, char *@var{mem}, size_t *@var{alignment}, size_t @var{maxpool})
Allocate a new MCTS tree, that has a memory pool of /maxpool/ bytes.
#+end_deftypefun
#+attr_texinfo: :options {void} tarot_mcts_free (TarotMcts *@var{mcts})
#+begin_deftypefun
Dispose of /mcts/.
#+end_deftypefun
#+attr_texinfo: :options {TarotMctsError} tarot_mcts_set_base (TarotMcts *@var{mcts}, const TarotGame *@var{base})
#+begin_deftypefun
#+texinfo: @deftypefunx {void} tarot_mcts_seed (TarotMcts *@var{mcts}, size_t @var{seedsize}, const void *@var{seed})
Set up the initial conditions, /base/ and the random generator /seed/
(/seedsize/ bytes, not 0) in /mcts/.
#+end_deftypefun
#+attr_texinfo: :options {TarotMctsError} tarot_mcts_run (TarotMcts *@var{mcts}, size_t @var{niterations})
#+begin_deftypefun
Run /niterations/ of Monte Carlo Tree Search in /mcts/. You can call
this function multiple times, if you feat it would block your program
for too long.
The best number of iterations is returned by
=tarot_mcts_default_iterations=.
#+end_deftypefun
#+attr_texinfo: :options {const TarotGameEvent *} tarot_mcts_get_best (const TarotMcts *@var{mcts})
#+begin_deftypefun
Get the best candidate in /mcts/, or =NULL= if there are no successful
iterations.
#+end_deftypefun
** The libtarot test suite
In this section we present the test suite to show different aspects of
libtarot.
......
......@@ -40,7 +40,6 @@ include %reldir%/pkgconfig/Makefile.am
include %reldir%/cards/Makefile.am
include %reldir%/counter/Makefile.am
include %reldir%/layout/Makefile.am
include %reldir%/mcts/Makefile.am
include %reldir%/simulation/Makefile.am
include %reldir%/features/Makefile.am
include %reldir%/solo/Makefile.am
......
# tarot implements the rules of the tarot game
# Copyright (C) 2019 Vivien Kraus
# Copyright (C) 2019, 2020 Vivien Kraus
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
......@@ -15,7 +15,7 @@
AM_CPPFLAGS += -I $(srcdir)/%reldir% -I %reldir%
include_HEADERS += %reldir%/tarot.h %reldir%/tarot/deprecated.h
include_HEADERS += %reldir%/tarot.h
src_libtarot_libtarot_la_SOURCES += \
%reldir%/tarot.c \
......
......@@ -77,119 +77,4 @@ tarot_libtarot_version (void)
return effective_version;
}
void
tarot_mcts_set_parameter (TarotMcts * mcts, double value)
{
(void) mcts;
(void) value;
}
double
tarot_mcts_get_parameter (const TarotMcts * mcts)
{
(void) mcts;
return 0;
}
void
tarot_mcts_set_simulation_random (TarotMcts * mcts, double value)
{
(void) mcts;
(void) value;
}
double
tarot_mcts_get_simulation_random (const TarotMcts * mcts)
{
(void) mcts;
return 0;
}
void
tarot_mcts_set_simulation_agreed (TarotMcts * mcts, double value)
{
(void) mcts;
(void) value;
}
double
tarot_mcts_get_simulation_agreed (const TarotMcts * mcts)
{
(void) mcts;
return 0;
}
size_t
tarot_mcts_default_iterations ()
{
return 100;
}
const struct TarotMctsNode *
tarot_mcts_root (const TarotMcts * mcts)
{
(void) mcts;
return NULL;
}
const TarotGameEvent *
tarot_mcts_get (const TarotMctsNode * node, size_t *n_simulations,
int *sum_scores)
{
(void) node;
*n_simulations = 0;
*sum_scores = 0;
return NULL;
}
const TarotMctsNode *
tarot_mcts_parent (const TarotMctsNode * node)
{
(void) node;
return NULL;
}
const TarotMctsNode *
tarot_mcts_first_child (const TarotMctsNode * node)
{
(void) node;
return NULL;
}
const TarotMctsNode *
tarot_mcts_next_sibling (const TarotMctsNode * node)
{
(void) node;
return NULL;
}
const TarotMctsNode *
tarot_mcts_previous_sibling (const TarotMctsNode * node)
{
(void) node;
return NULL;
}
size_t
tarot_mcts_save_to_xml (const TarotMcts * mcts, size_t start, size_t max,
char *dest_out)
{
(void) mcts;
(void) start;
if (max != 0)
{
dest_out[0] = '\0';
}
return 0;
}
char *
tarot_mcts_save_to_xml_alloc (const TarotMcts * mcts)
{
char *ret = xmalloc (1);
ret[0] = '\0';
(void) mcts;
return ret;
}
#include <tarot/features_private_impl.h>
......@@ -26,10 +26,8 @@
#include <tarot/counter.h>
#include <tarot/layout.h>
#include <tarot/simulation.h>
#include <tarot/mcts.h>
#include <tarot/features.h>
#include <tarot/solo.h>
#include <tarot/deprecated.h>
#include <tarot/perceptron.h>
#include <tarot/ai.h>
......
/* tarot implements the rules of the tarot game
* Copyright (C) 2019 Vivien Kraus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_DEPRECATED_INCLUDED
#define H_TAROT_DEPRECATED_INCLUDED
#include <tarot/game_event.h>
#include <tarot/mcts.h>
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
/**
* tarot_mcts_set_parameter:
*/
void tarot_mcts_set_parameter (TarotMcts * mcts, double value);
/**
* tarot_mcts_get_parameter:
*/
double tarot_mcts_get_parameter (const TarotMcts * mcts);
/**
* tarot_mcts_set_simulation_random:
*/
void tarot_mcts_set_simulation_random (TarotMcts * mcts, double weight);
/**
* tarot_mcts_get_simulation_random:
*/
double tarot_mcts_get_simulation_random (const TarotMcts * mcts);
/**
* tarot_mcts_set_simulation_agreed:
*/
void tarot_mcts_set_simulation_agreed (TarotMcts * mcts, double weight);
/**
* tarot_mcts_get_simulation_agreed:
*/
double tarot_mcts_get_simulation_agreed (const TarotMcts * mcts);
/**
* tarot_mcts_default_iterations:
*/
size_t tarot_mcts_default_iterations ();
struct TarotMctsNode;
#define TAROT_MCTS_NULL NULL
const struct TarotMctsNode *tarot_mcts_root (const TarotMcts * mcts);
/**
* tarot_mcts_get:
* @n_simulations: (out):
* @sum_scores: (out):
*/
const struct TarotGameEvent *tarot_mcts_get (const struct TarotMctsNode
*node, size_t *n_simulations,
int *sum_scores);
const struct TarotMctsNode *tarot_mcts_parent (const struct TarotMctsNode
*node);
const struct TarotMctsNode *tarot_mcts_first_child (const struct
TarotMctsNode *node);
const struct TarotMctsNode *tarot_mcts_next_sibling (const struct
TarotMctsNode *node);
const struct TarotMctsNode *tarot_mcts_previous_sibling (const struct
TarotMctsNode
*node);
/**
* tarot_mcts_save_to_xml:
* @dest_out: (array length=max):
*/
size_t tarot_mcts_save_to_xml (const TarotMcts * mcts, size_t start,
size_t max, char *dest_out);
char *tarot_mcts_save_to_xml_alloc (const TarotMcts * mcts);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* not H_TAROT_DEPRECATED_INCLUDED */
......@@ -336,33 +336,6 @@ extern "C"
TarotPlayer taker;
};
struct TarotMctsNode;
typedef struct TarotMctsNode TarotMctsNode;
struct TarotMctsNode
{
TarotMctsNode *up;
TarotMctsNode *left;
TarotMctsNode *right;
int sum_scores;
size_t n_simulations;
TarotPlayer myself;
TarotGameEvent event;
};
struct TarotMcts
{
TarotMctsNode *root;
TarotGame base;
struct yarrow256_ctx generator;
double tune_parameter;
double tune_simulation_random;
double tune_simulation_agreed;
char *pool_tail;
size_t pool_remaining;
char *pool_head;
};
struct TarotSolo
{
TarotGame game;
......
......@@ -40,15 +40,6 @@
#include <string.h>
#include "xalloc.h"
#define tarot_mcts_dup mcts_dup_as_is
static inline TarotMcts *
tarot_mcts_dup (const TarotMcts * mcts)
{
(void) mcts;
return NULL;
}
#define TAROT_BOXED_TYPE(Type, type) \
static void * \
copy_##type (void *source) \
......@@ -82,7 +73,6 @@ TAROT_BOXED_TYPE (Counter, counter)
TAROT_BOXED_TYPE (Game, game)
TAROT_BOXED_TYPE (GameEvent, game_event)
TAROT_BOXED_TYPE (GameIterator, game_iterator)
TAROT_BOXED_TYPE (Mcts, mcts)
TAROT_BOXED_TYPE (Perceptron, perceptron)
TAROT_BOXED_TYPE (Solo, solo)
/*** END file-header ***/
......
......@@ -47,7 +47,6 @@ extern "C"
GType tarot_game_get_type (void);
GType tarot_game_event_get_type (void);
GType tarot_game_iterator_get_type (void);
GType tarot_mcts_get_type (void);
GType tarot_perceptron_get_type (void);
GType tarot_solo_get_type (void);
......
# tarot implements the rules of the tarot game
# Copyright (C) 2019 Vivien Kraus
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, version 3 of the License.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
AM_CPPFLAGS += -I $(srcdir)/%reldir% -I %reldir%
tarotinclude_HEADERS += %reldir%/tarot/mcts.h
src_libtarot_libtarot_la_SOURCES += \
%reldir%/mcts.c \
%reldir%/tarot/mcts_private.h \
%reldir%/tarot/mcts_private_impl.h
ENUM_HEADER_FILES += %reldir%/tarot/mcts.h
INTROSPECTED_SOURCES += %reldir%/mcts.c
$(srcdir)/%reldir%/tarot/mcts_private_impl.h: src/libtarot/cards/tarot/cards_private.h
$(srcdir)/%reldir%/mcts.c: src/libtarot/cards/tarot/cards_private.h
/* tarot implements the rules of the tarot game
* Copyright (C) 2018, 2019, 2020 Vivien Kraus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <tarot/mcts_private.h>
#include "xalloc.h"
#include <string.h>
#ifdef __FRAMAC__
#undef alignof
#define alignof(x) (16)
#endif /* __FRAMAC__ */
size_t
tarot_mcts_construct (size_t max_mem, char *mem, size_t *alignment,
size_t max_pool)
{
return mcts_construct (max_mem, mem, alignment, max_pool);
}
TarotMcts *
tarot_mcts_alloc (size_t max)
{
size_t alignment;
size_t required = mcts_construct (0, NULL, &alignment, max);
size_t unused;
void *mem = xmalloc (required);
assert (alignment >= 1 && alignment <= alignof (max_align_t));
unused = mcts_construct (required, mem, &alignment, max);
assert (unused == required);
(void) unused;
return mem;
}
void
tarot_mcts_copy (TarotMcts * dest, const TarotMcts * source)
{
mcts_copy (dest, source);
}
TarotMcts *
tarot_mcts_dup (size_t new_max_mem, const TarotMcts * source)
{
TarotMcts *ret = tarot_mcts_alloc (new_max_mem);
mcts_copy (ret, source);
return ret;
}
void
tarot_mcts_free (TarotMcts * mcts)
{
free (mcts);
}
TarotMctsError
tarot_mcts_set_base (TarotMcts * mcts, const TarotGame * base)
{
return mcts_set_base (mcts, base);
}
void
tarot_mcts_seed (TarotMcts * mcts, size_t seed_size, const void *seed)
{
mcts_seed (mcts, seed_size, seed);
}
TarotMctsError
tarot_mcts_run (TarotMcts * mcts, size_t n_iterations)
{
return mcts_run (mcts, n_iterations);
}
const TarotGameEvent *
tarot_mcts_get_best (const TarotMcts * mcts)
{
return mcts_get_best (mcts);
}
#include <tarot/mcts_private_impl.h>
/* tarot implements the rules of the tarot game
* Copyright (C) 2018, 2019 Vivien Kraus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_MCTS_INCLUDED
#define H_TAROT_MCTS_INCLUDED
#include <stddef.h>
#include <tarot/player.h>
#ifdef __cplusplus
extern "C"
{
#endif /* __cplusplus */
struct TarotMcts;
typedef struct TarotMcts TarotMcts;
struct TarotGameEvent;
struct TarotGame;
/**
* tarot_mcts_construct:
* @mem_out: (array length=max_mem):
* @alignment: (out):
*/
size_t tarot_mcts_construct (size_t max_mem, char *mem_out,
size_t *alignment, size_t max_pool);
void tarot_mcts_copy (TarotMcts * dest, const TarotMcts * source);
/**
* tarot_mcts_alloc: (constructor)
*/
TarotMcts *tarot_mcts_alloc (size_t max_mem);
TarotMcts *tarot_mcts_dup (size_t new_max_mem, const TarotMcts * source);
void tarot_mcts_free (TarotMcts * mcts);
typedef enum
{
TAROT_MCTS_OK = 0,
TAROT_MCTS_NONEXT,
TAROT_MCTS_UNKNOWNNEXT,
TAROT_MCTS_NODATA,
TAROT_MCTS_NOMEM
} TarotMctsError;
/**
* tarot_mcts_set_base:
* @base: (type TarotGame):
*/
TarotMctsError tarot_mcts_set_base (TarotMcts * mcts,
const struct TarotGame *base);
/**
* tarot_mcts_seed:
* @seed: (array length=seed_size) (element-type char):
*/
void tarot_mcts_seed (TarotMcts * mcts, size_t seed_size, const void *seed);
/**
* tarot_mcts_run:
*/
TarotMctsError tarot_mcts_run (TarotMcts * mcts, size_t n_iterations);
/**
* tarot_mcts_get_best:
* @returns: (type Tarot.GameEvent) (transfer none):
*/
const struct TarotGameEvent *tarot_mcts_get_best (const TarotMcts * mcts);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* not H_TAROT_MCTS_INCLUDED */
/* Local Variables: */
/* mode: c */
/* End: */
/* tarot implements the rules of the tarot game
* Copyright (C) 2018, 2019 Vivien Kraus
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3 of the License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef H_TAROT_MCTS_PRIVATE_INCLUDED
#define H_TAROT_MCTS_PRIVATE_INCLUDED
#include <tarot_private.h>