Commit 813987bf authored by Vivien Kraus's avatar Vivien Kraus
Browse files

Implement the tarot-cnn-features program

This program reads a game and prints the tensor-shaped features as a
CSV row.  It prints a row for each step of the game when we can
analyze the decision from the next player.
parent d1cd5e3e
Pipeline #161612 passed with stages
in 25 minutes and 10 seconds
......@@ -45,6 +45,7 @@
/doc/petit-sec.c
/doc/players-and-cards
/doc/players-and-cards.c
/doc/tarot-cnn-features.man
/doc/tarot-deal.man
/doc/tarot-features.man
/doc/tarot-figures
......@@ -128,6 +129,7 @@
/src/tarot-app/resources.c
/src/tarot-app/server.c
/src/tarot-app/tarot
/src/tarot-app/tarot-cnn-features
/src/tarot-app/tarot-deal
/src/tarot-app/tarot-en
/src/tarot-app/tarot-features
......
......@@ -22,14 +22,16 @@ dist_man6_MANS += \
%reldir%/tarot-deal.man \
%reldir%/tarot-mcts.man \
%reldir%/tarot-stacking.man \
%reldir%/tarot-features.man
%reldir%/tarot-features.man \
%reldir%/tarot-cnn-features.man
MAINTAINERCLEANFILES += \
$(srcdir)/%reldir%/tarot.man \
$(srcdir)/%reldir%/tarot-status.man \
$(srcdir)/%reldir%/tarot-deal.man \
$(srcdir)/%reldir%/tarot-mcts.man \
$(srcdir)/%reldir%/tarot-stacking.man \
$(srcdir)/%reldir%/tarot-features.man
$(srcdir)/%reldir%/tarot-features.man \
$(srcdir)/%reldir%/tarot-cnn-features.man
help2man_verbose = $(help2man_verbose_@AM_V@)
help2man_verbose_ = $(help2man_verbose_@AM_DEFAULT_V@)
......@@ -58,3 +60,7 @@ $(srcdir)/%reldir%/tarot-stacking.man: src/tarot-app/tarot_stacking.c
$(srcdir)/%reldir%/tarot-features.man: src/tarot-app/tarot_features.c
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot-features
$(help2man_verbose) ./pre-inst-env help2man tarot-features > $@
$(srcdir)/%reldir%/tarot-cnn-features.man: src/tarot-app/tarot_cnn_features.c
$(MAKE) $(AM_MAKEFLAGS) src/tarot-app/tarot-cnn-features
$(help2man_verbose) ./pre-inst-env help2man tarot-cnn-features > $@
......@@ -15,4 +15,5 @@ src/tarot-app/tarot_deal.c
src/tarot-app/program_commons.c
src/tarot-app/tarot_mcts.c
src/tarot-app/tarot_stacking.c
src/tarot-app/tarot_features.c
\ No newline at end of file
src/tarot-app/tarot_features.c
src/tarot-app/tarot_cnn_features.c
\ No newline at end of file
......@@ -8,8 +8,8 @@ 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-08-25 15:56+0200\n"
"PO-Revision-Date: 2019-08-25 15:58+0200\n"
"POT-Creation-Date: 2019-08-29 01:24+0200\n"
"PO-Revision-Date: 2019-08-29 01:26+0200\n"
"Last-Translator: Vivien Kraus <vivien@planete-kraus.eu>\n"
"Language-Team: French\n"
"Language: fr\n"
......@@ -38,12 +38,14 @@ msgstr "Affiche l'information de version et quitte"
#: src/tarot-app/main.vala:83 src/tarot-app/main.vala:93
#: src/tarot-app/tarot_deal.c:70 src/tarot-app/tarot_mcts.c:100
#: src/tarot-app/tarot_stacking.c:86 src/tarot-app/tarot_features.c:821
#: src/tarot-app/tarot_cnn_features.c:137
msgid "version"
msgstr "version"
#: src/tarot-app/main.vala:100 src/tarot-app/tarot_status.c:58
#: src/tarot-app/tarot_deal.c:110 src/tarot-app/tarot_mcts.c:151
#: src/tarot-app/tarot_stacking.c:116 src/tarot-app/tarot_features.c:850
#: src/tarot-app/tarot_cnn_features.c:165
#, c-format
msgid ""
"%s (libtarot %s)\n"
......@@ -1141,6 +1143,7 @@ msgstr ""
#: src/tarot-app/tarot_deal.c:69 src/tarot-app/tarot_mcts.c:99
#: src/tarot-app/tarot_stacking.c:85 src/tarot-app/tarot_features.c:820
#: src/tarot-app/tarot_cnn_features.c:136
msgid "help"
msgstr "aide"
......@@ -1217,12 +1220,14 @@ msgstr ""
#: src/tarot-app/tarot_deal.c:158 src/tarot-app/tarot_mcts.c:273
#: src/tarot-app/tarot_stacking.c:139 src/tarot-app/tarot_features.c:873
#: src/tarot-app/tarot_cnn_features.c:184
#, c-format
msgid "Error: extra arguments:\n"
msgstr "Erreur : arguments supplémentaires :\n"
#: src/tarot-app/tarot_deal.c:160 src/tarot-app/tarot_mcts.c:275
#: src/tarot-app/tarot_stacking.c:141 src/tarot-app/tarot_features.c:875
#: src/tarot-app/tarot_cnn_features.c:186
#, c-format
msgid "- %s\n"
msgstr "- %s\n"
......@@ -1469,6 +1474,21 @@ msgstr ""
"- -e, --encodées : utilise un encodage one-hot pour les\n"
" caractéristiques catégorielles.\n"
#: src/tarot-app/tarot_cnn_features.c:155
#, c-format
msgid ""
"Usage: tarot-cnn-features [OPTIONS]... < GAME > FEATURES\n"
"Extract the tensor-shaped game features. See the info manual for the\n"
"details. Options:\n"
"- -h, --help: print this message and exit;\n"
"- -v, --version: print the program version and exit.\n"
msgstr ""
"Utilisation : tarot-cnn-features [OPTIONS]... < PARTIE\n"
"Extrait les caractéristiques sous forme de tenseur de la PARTIE.\n"
"Options :\n"
"- -h, --aide : affiche ce message et quitte ;\n"
"- -v, --version : affiche la version du programme et quitte.\n"
#~ msgid "Hello, world!\n"
#~ msgstr "Bonjour, le monde !\n"
......
......@@ -26,3 +26,4 @@ include %reldir%/tarot-deal.am
include %reldir%/tarot-mcts.am
include %reldir%/tarot-stacking.am
include %reldir%/tarot-features.am
include %reldir%/tarot-cnn-features.am
# tarot-cnnfeatures.am
#
# Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
#
# 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, either version 3 of the
# License, or (at your option) any later version.
#
# 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/>.
bin_PROGRAMS += %reldir%/tarot-cnn-features
%canon_reldir%_tarot_cnn_features_SOURCES = \
%reldir%/tarot_cnn_features.c
%canon_reldir%_tarot_cnn_features_LDADD = \
%reldir%/libprogram-commons.la \
lib/libgnu.la \
src/libtarot/libtarot.la \
$(LTLIBINTL) \
$(LTLIBTHREAD) \
@INTL_MACOSX_LIBS@
/*
* tarot_cnn_features.c
*
* Copyright (C) 2019 Vivien Kraus <vivien@planete-kraus.eu>
*
* 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, either version 3 of the
* License, or (at your option) any later version.
*
* 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 "program_commons.h"
#include <tuneconfig.h>
static void parse_options (int argc, char *argv[]);
static void print_row (size_t n, const double *row);
int
main (int argc, char *argv[])
{
TarotGame *game = NULL;
double *data;
size_t n;
const TarotGameEvent *e;
TarotGameIterator *it;
TarotGame *rebuilt;
tarot_program_setup ();
parse_options (argc, argv);
game = tarot_program_read_game ();
rebuilt = tarot_game_alloc ();
it = tarot_game_iterator (game);
while ((e = tarot_game_iterator_next_value (it)) != NULL)
{
TarotCard *cards;
TarotPlayer *players;
size_t n_cards;
size_t n_players;
TarotPlayer next;
int has_next = (tarot_game_get_next (rebuilt, &next) == TAROT_GAME_OK);
if (has_next
&& tarot_game_event_get_deal_all_alloc (e, &n_players, &players) == TAROT_EVENT_OK)
{
TarotPlayer myself = next;
TarotCard my_cards[78];
size_t n_my_cards = 0;
TarotCard i;
TarotGameEvent *my_deal = NULL;
for (i = 0; i < n_players; i++)
{
if (players[i] == myself)
{
my_cards[n_my_cards++] = i;
}
}
my_deal = tarot_game_event_alloc_deal (myself, n_my_cards, my_cards);
if (tarot_game_add_event (rebuilt, my_deal) != 0)
{
assert (0);
}
tarot_game_event_free (my_deal);
free (players);
}
else if (has_next
&& tarot_game_event_get_discard_alloc (e, &n_cards, &cards) == TAROT_EVENT_OK)
{
TarotCard *full_discard = NULL;
size_t n_full;
TarotCard *partial_discard = NULL;
size_t n_partial;
TarotPlayer taker;
TarotGameEvent *my_discard = NULL;
tarot_game_get_taker (rebuilt, &taker);
if (tarot_game_get_full_discard_alloc (game, &n_full, &full_discard) != TAROT_GAME_OK)
{
full_discard = NULL;
}
if (tarot_game_get_public_discard_alloc (game, &n_partial, &partial_discard) != TAROT_GAME_OK)
{
partial_discard = NULL;
}
if (full_discard != NULL && partial_discard != NULL)
{
if (next == taker)
{
my_discard = tarot_game_event_alloc_discard (n_full, full_discard);
}
else
{
my_discard = tarot_game_event_alloc_discard (n_partial, partial_discard);
}
tarot_features_cnn_alloc (rebuilt, &data, &n);
print_row (n, data);
free (data);
}
else
{
my_discard = tarot_game_event_dup (e);
}
if (tarot_game_add_event (rebuilt, my_discard) != 0)
{
assert (0);
}
tarot_game_event_free (my_discard);
free (partial_discard);
free (full_discard);
free (cards);
}
else if (tarot_game_add_event (rebuilt, e) != 0)
{
assert (0);
}
tarot_features_cnn_alloc (rebuilt, &data, &n);
print_row (n, data);
free (data);
}
return EXIT_SUCCESS;
}
static void
parse_options (int argc, char *argv[])
{
int c;
while (1)
{
static struct option long_options[] =
{
{N_ ("help"), no_argument, 0, 'h'},
{N_ ("version"), no_argument, 0, 'v'},
{0, 0, 0, 0}
};
int option_index = 0;
for (option_index = 0; long_options[option_index].name != NULL; option_index++)
{
long_options[option_index].name = _ (long_options[option_index].name);
}
option_index = 0;
c = getopt_long (argc, argv, "hv", long_options, &option_index);
if (c == -1)
break;
switch (c)
{
case 0:
break;
case 'h':
printf (_ ("Usage: tarot-cnn-features [OPTIONS]... < GAME > FEATURES\n\
Extract the tensor-shaped game features. See the info manual for the\n\
details. Options:\n\
- -h, --help: print this message and exit;\n\
- -v, --version: print the program version and exit.\n\
"));
exit (EXIT_SUCCESS);
break;
case 'v':
printf (_ ("%s (libtarot %s)\n\
\n\
Copyright © 2019 Vivien Kraus\nThis is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n"),
"tarot-cnn-features",
tarot_libtarot_version ());
exit (EXIT_SUCCESS);
break;
case '?':
/* getopt_long already printed an error message. */
exit (EXIT_FAILURE);
break;
default:
abort ();
}
}
if (optind < argc)
{
fprintf (stderr, _ ("Error: extra arguments:\n"));
while (optind < argc)
fprintf (stderr, _ ("- %s\n"), argv[optind++]);
exit (EXIT_FAILURE);
}
}
static void
print_row (size_t n, const double *data)
{
size_t i;
for (i = 0; i < n; i++)
{
if (i != 0)
{
printf (",");
}
if (data[i] == 0.0)
{
printf ("0");
}
else if (data[i] == 1.0)
{
printf ("1");
}
else
{
printf ("%f", data[i]);
}
}
printf ("\n");
}
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