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

Merge branch '178-purge-the-useless-code' into 'develop'

Resolve "Purge the useless code"

Closes #178

See merge request !184
parents 83bd66de 2d31cf61
Pipeline #206694 passed with stages
in 43 minutes and 6 seconds
/* 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
......@@ -30,38 +30,6 @@ extern "C"
struct TarotGameEvent;
/* All features at once */
/**
* tarot_features_arff_header:
* @encoded: (type boolean):
* @returns: (transfer none):
*/
const char *tarot_features_arff_header (int encoded);
/**
* tarot_features_arff_data:
* @base: (type TarotGame):
* @added: (type TarotGameEvent):
* @encoded: (type boolean):
* @dest_out: (array length=max):
*/
size_t tarot_features_arff_data (const struct TarotGame *base,
const struct TarotGameEvent *added,
int score, int encoded, size_t max,
char *dest_out);
/**
* tarot_features_arff_data_alloc:
* @base: (type TarotGame):
* @added: (type TarotGameEvent):
* @encoded: (type boolean):
* @returns: (transfer full):
*/
char *tarot_features_arff_data_alloc (const struct TarotGame *base,
const struct TarotGameEvent *added,
int score, int encoded);
/* General-purpose features */
/**
......@@ -380,94 +348,6 @@ extern "C"
const struct TarotGameEvent *card,
size_t *n_halfpoints, int *oudler);
/**
* tarot_features_strongest_player:
* @with_call: (type boolean):
* @owners: (array length=n_cards):
* @confidence: (out):
* @call: (out):
*/
TarotPlayer tarot_features_strongest_player (size_t n_players,
int with_call,
size_t n_cards,
const TarotPlayer * owners,
double *confidence,
TarotCard * call);
/**
* tarot_features_errors:
*/
double tarot_features_errors (const TarotGame * game);
struct TarotPredictor;
typedef struct TarotPredictor TarotPredictor;
struct TarotGameEvent;
struct TarotGame;
/**
* tarot_predictor_construct:
* @mem_out: (array length=max_mem):
* @alignment: (out):
*/
size_t tarot_predictor_construct (size_t max_mem, char *mem_out,
size_t *alignment);
void tarot_predictor_copy (TarotPredictor * predictor,
const TarotPredictor * source);
/**
* tarot_predictor_alloc: (constructor)
*/
TarotPredictor *tarot_predictor_alloc (void);
TarotPredictor *tarot_predictor_dup (const TarotPredictor * predictor);
void tarot_predictor_free (TarotPredictor * predictor);
/**
* tarot_predictor_set_game:
* @game: (type TarotGame):
*/
void tarot_predictor_set_game (TarotPredictor * predictor,
const struct TarotGame *game);
/**
* tarot_predictor_best:
* @score: (out):
* @returns: (nullable) (type Tarot.GameEvent) (transfer none):
*/
const struct TarotGameEvent *tarot_predictor_best (const TarotPredictor *
predictor,
double *score);
/**
* tarot_predictor_eval:
* @score_out: (array length=max):
* @candidates: (array length=n_candidates) (type TarotGameEvent*) (transfer none):
*/
size_t tarot_predictor_eval (const TarotPredictor * predictor,
size_t n_candidates,
struct TarotGameEvent **candidates,
size_t start, size_t max, double *score_out);
/**
* tarot_features_cnn_add:
* @data_out: (array length=max):
*/
size_t tarot_features_cnn_add (const TarotGame * game, size_t max,
double *data_out);
/**
* tarot_features_cnn_alloc:
* @data: (out) (transfer full) (array length=n):
* @n: (out):
*/
void tarot_features_cnn_alloc (const TarotGame * game, double **data,
size_t *n);
#ifdef __cplusplus
}
#endif /* __cplusplus */
......
/* 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
......@@ -23,14 +23,6 @@
extern "C"
{
#endif /* __cplusplus */
void _tarot_features_init (const char *pkgdatadir);
static const char *features_arff_header (const char *pkgdatadir,
int encoded);
static size_t features_arff_data (const TarotGame * game,
const TarotGameEvent * added, int score,
int encoded, size_t max, char *dest);
static void features_variant (const TarotGame * game, size_t *n_players,
int *with_call);
static void features_position (const TarotGame * game, size_t *i_trick,
......@@ -127,33 +119,6 @@ extern "C"
static void strategy_play_points (const TarotGame * game,
const TarotGameEvent * card,
size_t *n_halfpoints, int *oudler);
static TarotPlayer features_strongest_player (size_t n_players,
int with_call,
size_t n_cards,
const TarotPlayer * owners,
double *confidence,
TarotCard * call);
static double features_errors (const TarotGame * game);
static double features_errors_just_bids (const TarotGame * game);
static size_t predictor_construct (size_t max_mem, char *mem,
size_t *alignment);
static void predictor_copy (TarotPredictor * dest,
const TarotPredictor * source);
static void predictor_set_game (TarotPredictor * predictor,
const TarotGame * game);
static const TarotGameEvent *predictor_best (const TarotPredictor *
predictor, double *score);
static size_t predictor_eval (const TarotPredictor * predictor,
size_t n_candidates,
TarotGameEvent ** candidates,
size_t start, size_t max, double *score);
static void features_cnn_add_event (const TarotGame * base,
const TarotGameEvent * event,
double *data);
static size_t features_cnn_add (const TarotGame * game, size_t max,
double *data);
#ifdef __cplusplus
}
......
/* 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
......@@ -21,7 +21,6 @@
#include <tarot/game_event_private.h>
#include <tarot/cards_private.h>
#include <tarot/counter_private.h>
#include <tarot/cnn_private.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
......@@ -30,714 +29,14 @@
#include <errno.h>
#include "xalloc.h"
static char *features_header_data = NULL;
static char *features_header_encoded_data = NULL;
static inline FILE *
features_fopen_in_dir (const char *dir, const char *basename)
{
FILE *f;
char *filename =
xmalloc (strlen (dir) + strlen ("/") + strlen (basename) + 1);
strcpy (filename, dir);
strcat (filename, "/");
strcat (filename, basename);
f = fopen (filename, "r");
if (f == NULL)
{
fprintf (stderr, _("Error: could not open file %s: %s\n"), filename,
strerror (errno));
abort ();
}
free (filename);
return f;
}
static inline void
features_buffer_push (size_t *allocated, char **buffer, size_t *used, char c)
{
if (*allocated == *used)
{
*allocated *= 2;
*buffer = xrealloc (*buffer, *allocated);
}
assert (*used < *allocated);
(*buffer)[(*used)++] = c;
}
static inline char *
features_read_whole_file (FILE * f)
{
size_t allocated = 1;
char *buffer = xmalloc (allocated);
size_t used = 0;
int i;
while (1)
{
i = fgetc (f);
if (i == EOF)
{
features_buffer_push (&allocated, &buffer, &used, 0);
return buffer;
}
else
{
features_buffer_push (&allocated, &buffer, &used,
(char) (unsigned char) (unsigned int) i);
}
}
}
static inline const char *
features_arff_header (const char *pkgdatadir, int encoded)
{
if (encoded && features_header_encoded_data == NULL)
{
FILE *f =
features_fopen_in_dir (pkgdatadir, "features-header-encoded.txt");
static const char *sought = "@relation tarot-" VERSION "-encoded";
features_header_encoded_data = features_read_whole_file (f);
fclose (f);
if (strncmp (features_header_encoded_data, sought, strlen (sought)) !=
0)
{
fprintf (stderr,
_
("Error: the installed ARFF header (%s/features-header-encoded.txt) is not compatible with this version (%s).\n"),
pkgdatadir, VERSION);
abort ();
}
return features_header_encoded_data;
}
else if (encoded)
{
return features_header_encoded_data;
}
else if (!encoded && features_header_data == NULL)
{
FILE *f = features_fopen_in_dir (pkgdatadir, "features-header.txt");
static const char *sought = "@relation tarot-" VERSION;
features_header_data = features_read_whole_file (f);
fclose (f);
if (strncmp (features_header_data, sought, strlen (sought)) != 0)
{
fprintf (stderr,
_
("Error: the installed ARFF header (%s/features-header.txt) is not compatible with this version (%s).\n"),
pkgdatadir, VERSION);
abort ();
}
return features_header_data;
}
else if (!encoded)
{
return features_header_data;
}
assert (0);
return NULL;
}
static size_t
features_data_push (char ch, size_t *max, char **dest)
{
if (*max == 1)
{
**dest = '\0';
*max = 0;
*dest = NULL;
}
else if (*max != 0)
{
**dest = ch;
*dest = *dest + 1;
*max = *max - 1;
}
return 1;
}
static size_t
features_data_push_string (const char *str, size_t *max, char **dest)
{
size_t ret = 0;
while (*str != '\0')
{
ret += features_data_push (*str, max, dest);
str++;
}
return ret;
}
static size_t
features_data_push_int (int i, size_t *max, char **dest)
{
char repr[32];
snprintf (repr, sizeof (repr), "%d", i);
return features_data_push_string (repr, max, dest);
}
static size_t
features_data_push_ordered (int mini, int maxi, int value, size_t *max,
char **dest)
{
static const char *sep[] = {
"",
","
};
int has_sep = 0;
int i;
size_t ret = 0;
assert (value >= mini);
assert (value <= maxi);
for (i = mini; i <= value; i++)
{
ret += features_data_push_string (sep[has_sep], max, dest);
ret += features_data_push_string ("1", max, dest);
has_sep = 1;
}
for (i = value + 1; i <= maxi; i++)
{
ret += features_data_push_string (sep[has_sep], max, dest);
ret += features_data_push_string ("0", max, dest);
has_sep = 1;
}
return ret;
}
static size_t
features_data_push_yesno (int v, size_t *max, char **dest)
{
if (v)
{
return features_data_push_string ("yes", max, dest);
}
return features_data_push_string ("no", max, dest);
}
#define INT(i) ret += features_data_push_int ((i), &max, &dest);
#define STRING(s) ret += features_data_push_string (s, &max, &dest);
#define COMMA STRING (",");
#define ORDERED(mini, maxi, value) ret += features_data_push_ordered (mini, maxi, value, &max, &dest);
#define YESNO(v) ret += features_data_push_yesno (v, &max, &dest);
static inline size_t
features_arff_data (const TarotGame * game, const TarotGameEvent * added,
int score, int encoded, size_t max, char *dest)
{
size_t f_n_players;
int f_with_call;
size_t f_trick;
size_t f_position;
int f_partner_known;
TarotBid f_bid_minimum;
size_t f_n_kings;
size_t f_n_marriages;
size_t f_n_extended_marriages;
size_t f_n_queens;
size_t f_n_knights;
size_t f_n_shortest;
size_t f_n_second_shortest;
size_t f_n_second_longest;
size_t f_n_longest;
int f_has_petit;
int f_has_excuse;
int f_has_twentyone;
size_t f_n_trumps;
TarotNumber f_max_trump;
TarotNumber f_min_trump;
TarotNumber f_q1_trumps;
TarotNumber f_q3_trumps;
TarotNumber f_median_trumps;
int f_team_attack;
int f_team_defence;
size_t f_n_trick_players_already_played;
size_t f_n_allies_after_last_opponent;
int f_play_called_suit;
size_t f_play_n_times_lead;
size_t f_play_suit_remaining;
size_t f_play_suit_remaining_points;
int f_ally_may_be_minor_master;
int f_ennemy_may_be_minor_master;
size_t f_n_trumps_remaining;
int f_ally_may_be_trump_master;
int f_ennemy_may_be_trump_master;
size_t f_n_halfpoints_in_trick;
size_t f_n_oudlers_in_trick;
TarotBid s_bid;
int s_outbid;
int s_call_shortest;
int s_call_second_shortest;
int s_call_second_longest;
int s_call_longest;
int s_call_myself;
int s_call_marriage;
size_t s_discard_queens;
size_t s_discard_knights;
size_t s_discard_jacks;
size_t s_discard_junk;
size_t s_discard_shortest;
size_t s_discard_second_shortest;
size_t s_discard_second_longest;
size_t s_discard_longest;
int s_lead_called_suit;
size_t s_lead_suit_length;
size_t s_lead_n_times_lead;
int s_lead_trumps;
size_t s_n_smaller_kept;
size_t s_n_greater_nonface_kept;
size_t s_n_points_kept;
size_t s_n_greater_kept;
int s_play_excuse;
int s_trump_master;
int s_trump_obvious_master;
size_t s_halfpoints;
int s_oudler;
TarotSuit order[4];
TarotCounter counter;
size_t ret = 0;
counter_initialize (&counter);
counter_load_from_game (&counter, game);
features_variant (game, &f_n_players, &f_with_call);
features_position (game, &f_trick, &f_position);
features_partner_known (game, &f_partner_known);
features_bid_minimum (game, &f_bid_minimum);
features_count_faces (game, &f_n_kings, &f_n_marriages,
&f_n_extended_marriages, &f_n_queens, &f_n_knights);
features_count_minor (game, &f_n_shortest, &f_n_second_shortest,
&f_n_second_longest, &f_n_longest, order);
features_count_oudlers (game, &f_has_petit, &f_has_excuse,
&f_has_twentyone);
features_count_trumps (game, &f_n_trumps, &f_max_trump, &f_min_trump,
&f_q1_trumps, &f_q3_trumps, &f_median_trumps);
features_team (game, &f_team_attack, &f_team_defence);
features_trick_players (game, &f_n_trick_players_already_played,
&f_n_allies_after_last_opponent);
features_minor_suit (game, &counter, &f_play_called_suit,
&f_play_n_times_lead, &f_play_suit_remaining,
&f_play_suit_remaining_points,
&f_ally_may_be_minor_master,
&f_ennemy_may_be_minor_master);
features_trump (game, &counter, &f_n_trumps_remaining,
&f_ally_may_be_trump_master, &f_ennemy_may_be_trump_master);
features_points (game, &f_n_halfpoints_in_trick, &f_n_oudlers_in_trick);
strategy_bid (game, added, &s_bid);
strategy_outbid (game, added, &s_outbid);
strategy_call_by_length (game, added, &s_call_shortest,
&s_call_second_shortest,
&s_call_second_longest, &s_call_longest);
strategy_call_myself (game, added, &s_call_myself);
strategy_call_marriage (game, added, &s_call_marriage);
strategy_discard_save (game, added, &s_discard_queens,
&s_discard_knights, &s_discard_jacks);
strategy_discard_junk (game, added, &s_discard_junk);
strategy_discard_by_length (game, added, &s_discard_shortest,
&s_discard_second_shortest,
&s_discard_second_longest, &s_discard_longest);
strategy_lead (game, &counter, added, &s_lead_called_suit,
&s_lead_suit_length, &s_lead_n_times_lead, &s_lead_trumps);
strategy_purity (game, added, &s_n_smaller_kept, &s_n_greater_nonface_kept,
&s_n_points_kept, &s_n_greater_kept);
strategy_excuse (game, added, &s_play_excuse);
strategy_trump_master (game, &counter, added, &s_trump_master,
&s_trump_obvious_master);
strategy_play_points (game, added, &s_halfpoints, &s_oudler);
if (encoded)
{
INT (f_n_players == 3);
COMMA;
INT (f_n_players == 4);
COMMA;
INT (f_n_players == 5);
COMMA;
INT (f_with_call);
COMMA;
ORDERED (0, 24, f_trick);
COMMA;
ORDERED (0, 5, f_position);
COMMA;
INT (tarot_game_event_type (added) == TAROT_BID_EVENT);
COMMA;
INT (tarot_game_event_type (added) == TAROT_DECL_EVENT);
COMMA;
INT (tarot_game_event_type (added) == TAROT_CALL_EVENT);
COMMA;
INT (tarot_game_event_type (added) == TAROT_DISCARD_EVENT);
COMMA;
INT (tarot_game_event_type (added) == TAROT_HANDFUL_EVENT);
COMMA;
INT (tarot_game_event_type (added) == TAROT_CARD_EVENT);
COMMA;
INT (f_partner_known);
COMMA;
INT (f_bid_minimum == TAROT_PASS);
COMMA;
INT (f_bid_minimum == TAROT_TAKE);
COMMA;
INT (f_bid_minimum == TAROT_PUSH);
COMMA;
INT (f_bid_minimum == TAROT_STRAIGHT_KEEP);
COMMA;
INT (f_bid_minimum == TAROT_DOUBLE_KEEP);
COMMA;
ORDERED (0, 4, f_n_kings);
COMMA;
ORDERED (0, 4, f_n_marriages);
COMMA;
ORDERED (0, 4, f_n_extended_marriages);
COMMA;
ORDERED (0, 4, f_n_queens);
COMMA;
ORDERED (0, 4, f_n_knights);
COMMA;
ORDERED (0, 14, f_n_shortest);
COMMA;
ORDERED (0, 14, f_n_second_shortest);
COMMA;
ORDERED (0, 14, f_n_second_longest);
COMMA;
ORDERED (0, 14, f_n_longest);
COMMA;
INT (f_has_petit);
COMMA;
INT (f_has_excuse);
COMMA;
INT (f_has_twentyone);
COMMA;
ORDERED (0, 21, f_n_trumps);
COMMA;
ORDERED (0, 21, f_max_trump);
COMMA;
ORDERED (0, 21, f_min_trump);
COMMA;