<s> from __future__ import print_function <EOL> __author__ = '<STR_LIT>' <EOL> from . gomokutask import GomokuTask <EOL> from pybrain . rl . environments . twoplayergames . gomokuplayers import ModuleDecidingPlayer <EOL> from pybrain . rl . environments . twoplayergames import GomokuGame <EOL> from pybrain . rl . environments . twoplayergames . gomokuplayers . gomokuplayer import GomokuPlayer <EOL> from pybrain . structure . networks . custom . capturegame import CaptureGameNetwork <EOL> class RelativeGomokuTask ( GomokuTask ) : <EOL> """<STR_LIT>""" <EOL> useNetworks = False <EOL> maxGames = <NUM_LIT:3> <EOL> minTemperature = <NUM_LIT:0> <EOL> maxTemperature = <NUM_LIT> <EOL> verbose = False <EOL> numMovesCoeff = <NUM_LIT:0.5> <EOL> def __init__ ( self , size , ** args ) : <EOL> self . setArgs ( ** args ) <EOL> self . size = size <EOL> self . task = GomokuTask ( self . size ) <EOL> self . env = self . task . env <EOL> self . maxmoves = self . env . size [ <NUM_LIT:0> ] * self . env . size [ <NUM_LIT:1> ] <EOL> self . minmoves = <NUM_LIT:9> <EOL> def __call__ ( self , p1 , p2 ) : <EOL> self . temp = self . minTemperature <EOL> if self . useNetworks : <EOL> p1 = ModuleDecidingPlayer ( p1 , self . task . env , temperature = self . temp ) <EOL> p2 = ModuleDecidingPlayer ( p2 , self . task . env , temperature = self . temp ) <EOL> else : <EOL> assert isinstance ( p1 , GomokuPlayer ) <EOL> assert isinstance ( p2 , GomokuPlayer ) <EOL> p1 . game = self . task . env <EOL> p2 . game = self . task . env <EOL> p1 . color = GomokuGame . BLACK <EOL> p2 . color = - p1 . color <EOL> self . player = p1 <EOL> self . opponent = p2 <EOL> coeffSum = <NUM_LIT:0.> <EOL> res = <NUM_LIT:0.> <EOL> for i in range ( self . maxGames ) : <EOL> coeff = <NUM_LIT:1> / ( <NUM_LIT:10> * self . temp + <NUM_LIT:1> ) <EOL> res += coeff * self . _oneGame ( ) <EOL> coeffSum += coeff <EOL> if i > <NUM_LIT:0> : <EOL> self . _globalWarming ( ) <EOL> return res / coeffSum <EOL> def _globalWarming ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . temp == <NUM_LIT:0> : <EOL> self . temp = <NUM_LIT> <EOL> else : <EOL> self . temp *= <NUM_LIT> <EOL> if self . temp > self . maxTemperature : <EOL> return False <EOL> elif self . _setTemperature ( ) == False : <EOL> self . temp = self . minTemperature <EOL> return False <EOL> return True <EOL> def _setTemperature ( self ) : <EOL> if self . useNetworks : <EOL> self . opponent . temperature = self . temp <EOL> self . player . temperature = self . temp <EOL> return True <EOL> elif hasattr ( self . opponent , '<STR_LIT>' ) : <EOL> randPart = self . temp / ( self . temp + <NUM_LIT:1> ) <EOL> self . opponent . randomPartMoves = randPart <EOL> self . player . randomPartMoves = randPart <EOL> return True <EOL> else : <EOL> return False <EOL> def _oneGame ( self , preset = None ) : <EOL> """<STR_LIT>""" <EOL> self . env . reset ( ) <EOL> if preset != None : <EOL> self . env . _setStone ( GomokuGame . BLACK , preset ) <EOL> self . env . movesDone += <NUM_LIT:1> <EOL> self . env . playToTheEnd ( self . opponent , self . player ) <EOL> else : <EOL> self . env . playToTheEnd ( self . player , self . opponent ) <EOL> moves = self . env . movesDone <EOL> win = self . env . winner == self . player . color <EOL> if self . verbose : <EOL> print ( ( '<STR_LIT>' , preset , '<STR_LIT>' , self . temp , '<STR_LIT>' , win , '<STR_LIT>' , moves , '<STR_LIT>' ) ) <EOL> res = <NUM_LIT:1> - self . numMovesCoeff * ( moves - self . minmoves ) / ( self . maxmoves - self . minmoves ) <EOL> if win : <EOL> return res <EOL> else : <EOL> return - res <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> net1 = CaptureGameNetwork ( hsize = <NUM_LIT:1> ) <EOL> net2 = CaptureGameNetwork ( hsize = <NUM_LIT:1> ) <EOL> r = RelativeGomokuTask ( <NUM_LIT:7> , maxGames = <NUM_LIT:10> , useNetworks = True ) <EOL> print ( ( r ( net1 , net2 ) ) ) <EOL> print ( ( r ( net2 , net1 ) ) ) <EOL> print ( ( r . env ) ) <EOL> r . maxGames = <NUM_LIT:50> <EOL> print ( ( r ( net1 , net2 ) ) ) <EOL> print ( ( r ( net2 , net1 ) ) ) <EOL> print ( ( r . env ) ) </s>
