-
Notifications
You must be signed in to change notification settings - Fork 49
/
print_player_strategy.cpp
131 lines (115 loc) · 3.83 KB
/
print_player_strategy.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
/* print_player_strategy.cpp
* Richard Gibson, Jul 29, 2013
* Email: [email protected]
*
* Simple tool to print a player generated by Pure CFR in human-readable format.
*
* Copyright (C) 2013 by Richard Gibson
*/
/* C / C++ includes */
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
/* C project-acpc-server includes */
extern "C" {
}
/* Pure CFR includes */
#include "constants.hpp"
#include "player_module.hpp"
static void print_strategy_r( PlayerModule &player_module,
State &state,
const AbstractGame *ag,
const int p,
const int max_round )
{
if( state.finished || ( state.round >= max_round ) ) {
/* End of game or we've gone past the rounds we care to print */
return;
}
/* Get the possible actions */
Action actions[ MAX_ABSTRACT_ACTIONS ];
const int num_choices = ag->action_abs->get_actions( ag->game, state, actions );
if( p == currentPlayer( ag->game, &state ) ) {
/* Get the state info in a string */
char state_str[ PATH_LENGTH ];
printState( ag->game, &state, PATH_LENGTH, state_str );
/* Remove the card information from the state string by finding the last colon
* and ending the string there, then print the state.
*/
for( int i = strlen( state_str) - 1; i >= 0 && i < PATH_LENGTH; --i ) {
if( state_str[ i ] == ':' ) {
state_str[ i ] = '\0';
break;
}
}
printf( "%s\n", state_str );
/* Print the player's action probabilities for every possible bucket */
const int num_buckets = ag->card_abs->num_buckets( ag->game, state );
for( int bucket = 0; bucket < num_buckets; ++bucket ) {
/* Get the action probabilities */
double action_probs[ MAX_ABSTRACT_ACTIONS ];
player_module.get_action_probs( state, action_probs, bucket );
/* Print 'em out */
printf( " Bucket %d:", bucket );
for( int a = 0; a < num_choices; ++a ) {
if( ( num_choices < 5 ) || ( action_probs[ a ] > 0.001 ) ) {
char action_str[ PATH_LENGTH ];
printAction( ag->game, &actions[ a ], PATH_LENGTH, action_str );
printf( " %lg%%%s", action_probs[ a ] * 100, action_str );
}
}
printf( "\n" );
}
}
/* Recurse */
for( int a = 0; a < num_choices; ++a ) {
State new_state( state );
doAction( ag->game, &actions[ a ], &new_state );
print_strategy_r( player_module, new_state, ag, p, max_round );
}
}
int main( const int argc, const char *argv[] )
{
/* Print usage */
if( argc < 2 ) {
fprintf( stderr, "Usage: %s <player_file> [options]\n", argv[ 0 ] );
fprintf( stderr, "Options:\n" );
fprintf( stderr, " --max-round=<round>\n" );
return 1;
}
/* Create the player, get the abstract game */
int index = 1;
fprintf( stderr, "Loading player module... " );
PlayerModule player_module( argv[ index ] );
fprintf( stderr, "done!\n" );
++index;
const AbstractGame *ag = player_module.get_abstract_game( );
/* Check for options */
int max_round = MAX_ROUNDS;
for( ; index < argc; ++index ) {
if( !strncmp( argv[ index ], "--max-round=", strlen( "--max-round=" ) ) ) {
if( sscanf( &argv[ index ][ strlen( "--max-round=" ) ], "%d",
&max_round ) < 1 ) {
fprintf( stderr, "Could not read max-round from argument [%s]\n",
argv[ index ] );
return 1;
}
if( ( max_round <= 0 ) || ( max_round > MAX_ROUNDS ) ) {
fprintf( stderr, "max-round must be between 1 and %d\n", MAX_ROUNDS );
return 1;
}
} else {
fprintf( stderr, "Unrecognized argument [%s]\n", argv[ index ] );
return 1;
}
}
/* Print the strategy */
fprintf( stderr, "Starting walk of abstract game tree...\n" );
State state;
for( int p = 0; p < ag->game->numPlayers; ++p ) {
initState( ag->game, 0, &state );
printf( "=== PLAYER %d ===\n", p + 1 );
print_strategy_r( player_module, state, ag, p, max_round );
}
return 0;
}