<s> import numpy as np <EOL> import AlphaGo . go as go <EOL> def get_board ( state ) : <EOL> """<STR_LIT>""" <EOL> planes = np . zeros ( ( <NUM_LIT:3> , state . size , state . size ) ) <EOL> planes [ <NUM_LIT:0> , : , : ] = state . board == state . current_player <EOL> planes [ <NUM_LIT:1> , : , : ] = state . board == - state . current_player <EOL> planes [ <NUM_LIT:2> , : , : ] = state . board == go . EMPTY <EOL> return planes <EOL> def get_turns_since ( state , maximum = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> planes = np . zeros ( ( maximum , state . size , state . size ) ) <EOL> depth = <NUM_LIT:0> <EOL> for move in state . history [ : : - <NUM_LIT:1> ] : <EOL> if move is not go . PASS_MOVE : <EOL> if state . board [ move ] != go . EMPTY : <EOL> ( x , y ) = move <EOL> if np . sum ( planes [ : , x , y ] ) == <NUM_LIT:0> : <EOL> planes [ depth , x , y ] = <NUM_LIT:1> <EOL> if depth < maximum - <NUM_LIT:1> : <EOL> depth += <NUM_LIT:1> <EOL> return planes <EOL> def get_liberties ( state , maximum = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> planes = np . zeros ( ( maximum , state . size , state . size ) ) <EOL> for i in range ( maximum ) : <EOL> planes [ i , state . liberty_counts == i + <NUM_LIT:1> ] = <NUM_LIT:1> <EOL> planes [ maximum - <NUM_LIT:1> , state . liberty_counts >= maximum ] = <NUM_LIT:1> <EOL> return planes <EOL> def get_capture_size ( state , maximum = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> planes = np . zeros ( ( maximum , state . size , state . size ) ) <EOL> for ( x , y ) in state . get_legal_moves ( ) : <EOL> n_captured = <NUM_LIT:0> <EOL> for neighbor_group in state . get_groups_around ( ( x , y ) ) : <EOL> ( gx , gy ) = next ( iter ( neighbor_group ) ) <EOL> if ( state . liberty_counts [ gx ] [ gy ] == <NUM_LIT:1> ) and ( state . board [ gx , gy ] != state . current_player ) : <EOL> n_captured += len ( state . group_sets [ gx ] [ gy ] ) <EOL> planes [ min ( n_captured , maximum - <NUM_LIT:1> ) , x , y ] = <NUM_LIT:1> <EOL> return planes <EOL> def get_self_atari_size ( state , maximum = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> planes = np . zeros ( ( maximum , state . size , state . size ) ) <EOL> for ( x , y ) in state . get_legal_moves ( ) : <EOL> lib_set_after = set ( state . liberty_sets [ x ] [ y ] ) <EOL> group_set_after = set ( ) <EOL> group_set_after . add ( ( x , y ) ) <EOL> for neighbor_group in state . get_groups_around ( ( x , y ) ) : <EOL> ( gx , gy ) = next ( iter ( neighbor_group ) ) <EOL> if state . board [ gx , gy ] == state . current_player : <EOL> lib_set_after |= state . liberty_sets [ gx ] [ gy ] <EOL> group_set_after |= state . group_sets [ gx ] [ gy ] <EOL> if ( x , y ) in lib_set_after : <EOL> lib_set_after . remove ( ( x , y ) ) <EOL> if len ( lib_set_after ) == <NUM_LIT:1> : <EOL> group_size = len ( group_set_after ) <EOL> planes [ min ( group_size - <NUM_LIT:1> , maximum - <NUM_LIT:1> ) , x , y ] = <NUM_LIT:1> <EOL> return planes <EOL> def get_liberties_after ( state , maximum = <NUM_LIT:8> ) : <EOL> """<STR_LIT>""" <EOL> planes = np . zeros ( ( maximum , state . size , state . size ) ) <EOL> for ( x , y ) in state . get_legal_moves ( ) : <EOL> lib_set_after = set ( state . liberty_sets [ x ] [ y ] ) <EOL> for neighbor_group in state . get_groups_around ( ( x , y ) ) : <EOL> ( gx , gy ) = next ( iter ( neighbor_group ) ) <EOL> if state . board [ gx , gy ] == state . current_player : <EOL> lib_set_after |= state . liberty_sets [ gx ] [ gy ] <EOL> if ( x , y ) in lib_set_after : <EOL> lib_set_after . remove ( ( x , y ) ) <EOL> planes [ min ( maximum - <NUM_LIT:1> , len ( lib_set_after ) - <NUM_LIT:1> ) , x , y ] = <NUM_LIT:1> <EOL> return planes <EOL> def get_ladder_capture ( state ) : <EOL> raise NotImplementedError ( ) <EOL> def get_ladder_escape ( state ) : <EOL> raise NotImplementedError ( ) <EOL> def get_sensibleness ( state ) : <EOL> """<STR_LIT>""" <EOL> feature = np . zeros ( ( <NUM_LIT:1> , state . size , state . size ) ) <EOL> for ( x , y ) in state . get_legal_moves ( include_eyes = False ) : <EOL> feature [ <NUM_LIT:0> , x , y ] = <NUM_LIT:1> <EOL> return feature <EOL> FEATURES = { <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:3> , <EOL> "<STR_LIT>" : get_board <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : lambda state : np . ones ( ( <NUM_LIT:1> , state . size , state . size ) ) <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:8> , <EOL> "<STR_LIT>" : get_turns_since <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:8> , <EOL> "<STR_LIT>" : get_liberties <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:8> , <EOL> "<STR_LIT>" : get_capture_size <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:8> , <EOL> "<STR_LIT>" : get_self_atari_size <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:8> , <EOL> "<STR_LIT>" : get_liberties_after <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : get_ladder_capture <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : get_ladder_escape <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : get_sensibleness <EOL> } , <EOL> "<STR_LIT>" : { <EOL> "<STR_LIT:size>" : <NUM_LIT:1> , <EOL> "<STR_LIT>" : lambda state : np . zeros ( ( <NUM_LIT:1> , state . size , state . size ) ) <EOL> } <EOL> } <EOL> DEFAULT_FEATURES = [ <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" , <EOL> "<STR_LIT>" , "<STR_LIT>" ] <EOL> class Preprocess ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , feature_list = DEFAULT_FEATURES ) : <EOL> """<STR_LIT>""" <EOL> self . output_dim = <NUM_LIT:0> <EOL> self . feature_list = feature_list <EOL> self . processors = [ None ] * len ( feature_list ) <EOL> for i in range ( len ( feature_list ) ) : <EOL> feat = feature_list [ i ] . lower ( ) <EOL> if feat in FEATURES : <EOL> self . processors [ i ] = FEATURES [ feat ] [ "<STR_LIT>" ] <EOL> self . output_dim += FEATURES [ feat ] [ "<STR_LIT:size>" ] <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % feat ) <EOL> def state_to_tensor ( self , state ) : <EOL> """<STR_LIT>""" <EOL> feat_tensors = [ proc ( state ) for proc in self . processors ] <EOL> f , s = self . output_dim , state . size <EOL> return np . concatenate ( feat_tensors ) . reshape ( ( <NUM_LIT:1> , f , s , s ) ) </s>
