Skip to content

Commit

Permalink
Merge pull request #62 from kobanium/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
yssaya authored Apr 18, 2022
2 parents 4d0da9b + 1c51509 commit a66c76b
Show file tree
Hide file tree
Showing 13 changed files with 1,298 additions and 191 deletions.
8 changes: 4 additions & 4 deletions src/common/param.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
#pragma once
namespace Ver {
constexpr unsigned char major = 3; // 2...komaochi, 3...Swish
constexpr unsigned char minor = 1; //
constexpr unsigned short usi_engine = 27; // 1...18 AobaZero, 16...26 komaochi, 27...Swish AobaZero
constexpr unsigned char minor = 2; //
// usi_engine is no use. MUST increase "minor" for kicking old engine by server. Only major and minor are sent to client.
constexpr unsigned short usi_engine = 32; // 1...18 AobaZero, 16...26 komaochi, 27...Swish AobaZero
}

//#define AOBA_UNIQUE ".jBQoNA7kEd"
#define AOBA_UNIQUE ".oeWK7ZhnLN"

namespace Param {
Expand All @@ -26,7 +26,7 @@ namespace Param {
}

const int HEADER_SIZE = 64; // version 2 byte(major,minor), resign_th(2 byte), handicap rate 14 byte (2*7), average winrate(2 byte)
const int HANDICAP_TYPE = 7; // hirate(0),ky(1),ka(2),hi(3),2mai(4),4mai(5),6mai(6)
const int HANDICAP_TYPE = 7;// hirate(0),ky(1),ka(2),hi(3),2mai(4),4mai(5),6mai(6)
//const int FORMAT_VER = 2; // AobaZero, AobaKomaochi
const int FORMAT_VER = 3; // AobaZero(Swish, NN input are same as AobaKomaochi)

2 changes: 1 addition & 1 deletion src/playshogi/playshogi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ Other options:
static void load_book_file(string file) noexcept {

std::ifstream ifs(file);
if (!ifs) die(ERR_INT("Cannot open %s", file));
if (!ifs) die(ERR_INT("Cannot open %s", file.c_str()));

string str;
while (std::getline(ifs, str)) {
Expand Down
2 changes: 1 addition & 1 deletion src/usi-engine/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ CPU_ONLY = 0
ifeq ($(CPU_ONLY), 1)
CXXFLAGS += -DUSE_CPU_ONLY
endif
CXXFLAGS += -I. -I../common -DMINIMUM -DTLP -DCSA_LAN -DUSI -DYSS_ZERO -DNO_LOGGING
CXXFLAGS += -I. -I../common -DMINIMUM -DTLP -DUSI -DYSS_ZERO -DNO_LOGGING -DDFPN
CXXFLAGS += -fopenmp
LDFLAGS += -fopenmp

Expand Down
72 changes: 44 additions & 28 deletions src/usi-engine/bona/dfpn.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <assert.h>
#include <string.h>
#include <stdlib.h>
#include <string>
#include "shogi.h"
#include "dfpn.h"

Expand Down Expand Up @@ -37,20 +38,22 @@ static void CONV select_child( const tree_t * restrict ptree,
static void CONV num2str( char buf[16], unsigned int num );

#if defined(TLP)
static dfpn_tree_t dfpn_tree[ TLP_MAX_THREADS ];
static dfpn_tree_t dfpn_tree[ TLP_NUM_WORK ];
#else
static dfpn_tree_t dfpn_tree;
#endif

int CONV
dfpn( tree_t * restrict ptree, int turn, int ply )
dfpn( tree_t * restrict ptree, int turn, int ply, unsigned int *pret_move, int dfpn_node_limit )
{
dfpn_tree_t * restrict pdfpn_tree;
node_t * restrict pnode;
float fcpu_percent, fnps, fsat;
unsigned int cpu0, cpu1, elapse0, elapse1, u;
int iret, i;


*pret_move = MOVE_NA;

if ( get_cputime( &cpu0 ) < 0 ) { return -1; }
if ( get_elapsed( &elapse0 ) < 0 ) { return -1; }

Expand All @@ -75,7 +78,10 @@ dfpn( tree_t * restrict ptree, int turn, int ply )
pdfpn_tree->root_ply = ply;
pdfpn_tree->turn_or = turn;
pdfpn_tree->sum_phi_max = 0;
pdfpn_tree->node_limit = 50000000;
//pdfpn_tree->node_limit = DFPN_NODE_LIMIT;//50000000;
pdfpn_tree->node_limit = dfpn_node_limit;

bool fView = (ply==1);

pnode = pdfpn_tree->anode + ply;
pnode->phi = INF_1;
Expand All @@ -95,7 +101,7 @@ dfpn( tree_t * restrict ptree, int turn, int ply )

num2str( buf1, pnode->phi );
num2str( buf2, pnode->delta );
Out( "Research [%s:%s]\n", buf1, buf2 );
if ( fView ) PRT( "Research [%s:%s]\n", buf1, buf2 );
pnode->phi = INF;
pnode->delta = INF;
iret = mid( ptree, pdfpn_tree, ply );
Expand All @@ -109,36 +115,44 @@ dfpn( tree_t * restrict ptree, int turn, int ply )
{
DFPNOut( "UNSOLVED %d\n", iret );
switch ( iret ) {
case DFPN_ERRNO_MAXNODE: Out( "RESULT: MAX NODE\n" ); break;
case DFPN_ERRNO_MAXPLY: Out( "RESULT: MAX PLY\n" ); break;
case DFPN_ERRNO_SUMPHI: Out( "RESULT: MAX SUM_PHI\n" ); break;
case DFPN_ERRNO_DELTA2P: Out( "RESULT: MAX DELTA2+1\n" ); break;
case DFPN_ERRNO_MAXNODE: if ( fView ) PRT( "RESULT: MAX NODE\n" ); break;
case DFPN_ERRNO_MAXPLY: if ( fView ) PRT( "RESULT: MAX PLY\n" ); break;
case DFPN_ERRNO_SUMPHI: if ( fView ) PRT( "RESULT: MAX SUM_PHI\n" ); break;
case DFPN_ERRNO_DELTA2P: if ( fView ) PRT( "RESULT: MAX DELTA2+1\n" ); break;
default:
assert( DFPN_ERRNO_SIGNAL == iret );
Out( "RESULT: SIGNAL\n" );
if ( fView ) PRT( "RESULT: SIGNAL\n" );
break;
}
}
else if ( pnode->delta == INF )
{
const char *str;
for ( i = 0; i < pnode->nmove && pnode->children[i].phi != INF; i++ );
assert( i < pnode->nmove );
str = str_CSA_move(pnode->children[i].move);
Out( "RESULT: WIN %s\n", str );
DFPNOut( "WIN %s\n", str );
// const char *str = str_CSA_move(pnode->children[i].move);
*pret_move = pnode->children[i].move;
std::string s = string_CSA_move(*pret_move);
if ( fView ) PRT( "RESULT: WIN %s\n", s.c_str());
DFPNOut( "WIN %s\n", s.c_str());
}
else {
Out( "RESULT: LOSE\n" );
if ( fView ) PRT( "RESULT: LOSE\n" );
DFPNOut( "LOSE\n" );
}

fsat = dfpn_hash_sat();
if ( 3.0 < fsat )
{
dfpn_hash_age += 1U;
dfpn_hash_age &= DFPN_AGE_MASK;
}
pdfpn_tree->node_sum += ptree->node_searched;
if ( pdfpn_tree->node_sum > 30000 ) {
pdfpn_tree->node_sum = 0;
fsat = dfpn_hash_sat();
} else {
pdfpn_tree->node_sum = 0;
fsat = 0;
}

if ( 3.0 < fsat ) {
dfpn_hash_age += 1U;
dfpn_hash_age &= DFPN_AGE_MASK;
}

if ( get_cputime( &cpu1 ) < 0 ) { return -1; }
if ( get_elapsed( &elapse1 ) < 0 ) { return -1; }
Expand All @@ -148,15 +162,17 @@ dfpn( tree_t * restrict ptree, int turn, int ply )
fnps /= (float)( elapse1 - elapse0 + 1U );
node_per_second = (unsigned int)( fnps + 0.5 );

Out( "n=%" PRIu64 " sat=%3.1f%%", ptree->node_searched, fsat );
Out( " age=%u sum_phi=%u", dfpn_hash_age, pdfpn_tree->sum_phi_max );
Out( " time=%.2f cpu=%.1f%% nps=%.2fK\n",
if ( !fView ) return iret;

PRT( "n=%" PRIu64 " sat=%3.1f%%", ptree->node_searched, fsat );
PRT( " age=%u sum_phi=%u", dfpn_hash_age, pdfpn_tree->sum_phi_max );
PRT( " time=%.2f cpu=%.1f%% nps=%.2fK,ply=%d,limit=%d\n",
(float)( elapse1 - elapse0 + 1U ) / 1000.0F,
fcpu_percent, fnps / 1e3F );

Out( "\n" );
fcpu_percent, fnps / 1e3F, ply, dfpn_node_limit );
PRT( "\n" );

return 1;
// return 1;
return iret;
}


Expand Down
1 change: 1 addition & 0 deletions src/usi-engine/bona/dfpn.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ typedef struct {

typedef struct {
uint64_t node_limit;
uint64_t node_sum;
int turn_or;
int root_ply;
unsigned int sum_phi_max;
Expand Down
4 changes: 2 additions & 2 deletions src/usi-engine/bona/dfpnhash.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ int CONV dfpn_ini_hash( void )
n2 = 1U << dfpn_hash_log2;
size = sizeof( dfpn_hash_entry_t ) * ( n2 + 1 + DFPN_NUM_REHASH );

dfpn_hash_tbl = memory_alloc( size );
dfpn_hash_tbl = (dfpn_hash_entry_t*)memory_alloc( size );
if ( dfpn_hash_tbl == NULL ) { return -1; }

dfpn_hash_mask = n2 -1;
Expand Down Expand Up @@ -90,7 +90,7 @@ void CONV dfpn_hash_probe( const dfpn_tree_t * restrict pdfpn_tree,
uint64_t hash_key_curr, word1, word2, word3, hash_key, nodes;
unsigned int index, hand_b, phi, delta, u, offset, hand_b_curr;
int relation, is_phi_loop, is_delta_loop;

(void)ply;
#define REL_SUPE 0
#define REL_INFE 1

Expand Down
4 changes: 2 additions & 2 deletions src/usi-engine/bona/problem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,8 @@ solve_mate_problems( tree_t * restrict ptree, unsigned int nposition )

Out( "Problem #%d %s\n",
uposition, ptree->nsuc_check[1] ? "(in check)" : "" );

iret = dfpn( ptree, root_turn, 1 );
unsigned int move;
iret = dfpn( ptree, root_turn, 1, &move );
if ( iret < 0 ) { return iret; }

if ( istatus == record_eof ) { break; }
Expand Down
31 changes: 29 additions & 2 deletions src/usi-engine/bona/proce.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -561,6 +561,33 @@ usi_go( tree_t * restrict ptree, char **lasts )
token = "infinite";
}

if ( ! strcmp( token, "mate" ) ) {
int n = DFPN_NODE_LIMIT;
token = strtok_r( NULL, str_delimiters, lasts );
if ( token && ! strcmp( token, "nodes" ) ) {
token = strtok_r( NULL, str_delimiters, lasts );
if ( token ) n = atoi(token);
}
// "go mate nodes 100"
// "checkmate G*8f 9f9g 8f8g 9g9h 8g8h"
unsigned int move;
iret = dfpn( ptree, root_turn, 1, &move, n );
PRT("iret=%d,n=%d\n",iret,n);
if ( move == MOVE_NA ) {
if ( iret >= 0 ) {
USIOut("checkmate nomate\n"); // ハッシュFullで停止でもこの表示
} else {
USIOut("checkmate timeout\n");
}
} else {
char buf[7];
csa2usi( ptree, str_CSA_move(move), buf );
USIOut("checkmate %s\n",buf);
}
return 1;
}


#if defined(YSS_ZERO)
fUSIMoveCount = 0;
if ( ! strcmp( token, "visit" ) ) {
Expand Down Expand Up @@ -2061,8 +2088,8 @@ static int CONV cmd_dfpn( tree_t * restrict ptree, char **lasts )
else if ( ! strcmp( str, "go" ) )
{
AbortDifficultCommand;

return dfpn( ptree, root_turn, 1 );
unsigned int move;
return dfpn( ptree, root_turn, 1, &move );
}
else if ( ! strcmp( str, "connect" ) )
{
Expand Down
6 changes: 4 additions & 2 deletions src/usi-engine/bona/shogi.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ extern unsigned char ailast_one[512];
//#define BNZ_VER "28" // 20220225 balanced_opening ply<=2 always use value
//#define BNZ_VER "29" // 20220406 policy softmax temperature 1.0 -> 1.8
//#define BNZ_VER "30" // 20220406 kldgain 0.000006, balanced_opening within +150 ELO
#define BNZ_VER "31" // 20220407 cancel balanced_opening. resign ok under 30 moves in autousi.
//#define BNZ_VER "31" // 20220407 cancel balanced_opening. resign ok under 30 moves in autousi.
#define BNZ_VER "32" // 20220418 initial winrate is adjusted(aka, first play urgency, fpu), +20 ELO. dfpn for all node visits >= 10, +40 ELO.
#define BNZ_NAME "AobaZero"

//#define BNZ_VER "16" // 20210528 komaochi, mate3
Expand Down Expand Up @@ -1323,7 +1324,8 @@ extern unsigned int time_last_send;
#if defined(DFPN)
# define DFPNOut( ... ) if ( dfpn_sckt != SCKT_NULL ) \
sckt_out( dfpn_sckt, __VA_ARGS__ )
int CONV dfpn( tree_t * restrict ptree, int turn, int ply );
const int DFPN_NODE_LIMIT = 50000000;
int CONV dfpn( tree_t * restrict ptree, int turn, int ply, unsigned int *pret_move, int dfpn_node_limit = DFPN_NODE_LIMIT );
int CONV dfpn_ini_hash( void );
extern unsigned int dfpn_hash_log2;
extern sckt_t dfpn_sckt;
Expand Down
12 changes: 12 additions & 0 deletions src/usi-engine/bona/yss_dcnn.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ typedef struct child {
float value; // win rate (win=+1, loss=0)
float bias; // policy
int exact_value; // WIN or LOSS or DRAW
int mate_bit; // 3手詰やdfpnの長手数の詰を行ったか。bit単位で
#ifdef USE_LCB
float squared_eval_diff; // Variable used for calculating variance of evaluations. for LCB
int acc_virtual_loss; // accumulate virtual loss. for LCB
Expand All @@ -40,6 +41,8 @@ typedef struct hash_shogi {
uint64 hashcode64; // sequence hash
uint64 hash64pos; // position hash, we check both hash key.
int deleted; //
int mate_bit;
float win_sum;
int games_sum; // sum of children selected
int col; // color 1 or 2
int age; //
Expand All @@ -57,6 +60,15 @@ enum {
EX_NONE, EX_WIN, EX_LOSS, EX_DRAW // exact value.
};

const int MATE_CLEAR = 0; // bitで判定
const int MATE_3 = 1;
const int MATE_DFPN_0 = 2;
const int MATE_DFPN_1 = 4;
const int MATE_DFPN_2 = 8;
const int MATE_DFPN_3 = 16;
const int MATE_DFPN_4 = 32;


extern int fAddNoise;
extern int fVisitCount;
extern int fUSIMoveCount;
Expand Down
Loading

0 comments on commit a66c76b

Please sign in to comment.