def test5(): ''' hash items()''' root = LinesOfActionState(10, 50) succs = root.getSuccessors().items()[0][1].getSuccessors() dd = {} def count(d): k,v = d.popitem() r = v.getSuccessors() d[k]=v sum = 0 #for k in d.items(): # sum += 1 return sum def counts(): smax = -1 smin = 10000000 for _ in xrange(1000): sum = GTimeStatistics.measure_function(count,succs) smax = max(smax,sum) smin = min(smin,sum) return smax,smin smax,smin = GTimeStatistics.measure_function(counts) print GTimeStatistics print smax,smin
def test__measure_function(self): stat = TimeStatisticsClass() ev = CenterMassEvaluator() root_state = LinesOfActionState(15, 50) def f(the_state): ev.evaluate( the_state, WHITE, 10) for ss in root_state.getSuccessors().values(): stat.measure_function(f,ss) print stat.stats pass
def test__clean_up_if_need(self): o = TurnCache() current_state = LinesOfActionState(6, 50) succs = o.get_cached_value(current_state, LinesOfActionState.getSuccessors) self.assertEqual(succs,current_state.getSuccessors()) for s in succs.values(): o.get_cached_value(s, LinesOfActionState.getSuccessors) ns = filter(lambda s: s.blacks == 7, succs.values() )[0] self.assertEquals(o.checkers_left_table.keys(),[(8, 8), (8, 7)]) o.clean_up_if_need(current_state, ns) self.assertEquals(o.checkers_left_table.keys(),[ (8, 7)])
def test__get_cached_value(self): o = TurnCache() current_state = LinesOfActionState(6, 50) succs = o.get_cached_value(current_state, LinesOfActionState.getSuccessors) self.assertEqual(succs,current_state.getSuccessors()) for s in succs.values(): o.get_cached_value(s, LinesOfActionState.getSuccessors) t = o.checkers_left_table.has_key((8,7)) self.assertTrue(t) f = o.checkers_left_table.has_key((7,8)) self.assertFalse(f)
def cache_size_hit_dict_test(): log_name = create_new_file_name('.'.join(['cache_size_hit_dict_test', str(date.today()), 'data'])) log = open(log_name, 'w') d = {} agents = {} regular = LoaDEAgent() opt = LoaDEAgent() regular.pre_pre_pre_setup((False, False), 0) opt.pre_pre_pre_setup((False, True), 10000) state = LinesOfActionState(8, 100000) for cache_time in range(1,10,2): hit = 0 miss = 0 agents[WHITE] = opt agents[BLACK] = regular GameRunner(state, agents, cache_time, 1).run() hit += opt.alphaBetaAnyTime.cache_hit miss += opt.alphaBetaAnyTime.cache_miss d[cache_time] = (hit, miss, hit+miss, float(hit)/(hit+miss)) print '(hit, miss): ', (hit, miss, hit+miss, float(hit)/(hit+miss)) msg('cache_size_hit_dict = ' + str(d), log) print 'cache_size_hit_dict_test done! see results in log'
def cache_size_dict_test(): log_name = create_new_file_name('.'.join(['cache_size_dict_test', str(date.today()), 'data'])) log = open(log_name, 'w') d = {} agents = {} regular = LoaDEAgent() opt = LoaDEAgent() regular.pre_pre_pre_setup((False, False), 0) for cache_size in [10, 30, 100, 300, 1000, 3000, 10000, 30000, 100000]: total = 0 opt.pre_pre_pre_setup((False, True), cache_size) state = LinesOfActionState(8, 100000) agents[WHITE] = opt agents[BLACK] = regular winner = GameRunner(state, agents, 5, 1).run() if winner == WHITE: total += 1 winner = GameRunner(state, agents, 5, 1).run() if winner == WHITE: total += 1 agents[BLACK] = opt agents[WHITE] = regular winner = GameRunner(state, agents, 5, 1).run() if winner == BLACK: total += 1 winner = GameRunner(state, agents, 5, 1).run() if winner == BLACK: total += 1 d[cache_size] = total print 'total: ', total msg('cache_size_dict = ' + str(d), log) print 'cache_size_dict_test done! see results in log'
def reordering__borad_size_dict_test(): log_name = create_new_file_name('.'.join(['reordering__borad_size_dict_test', str(date.today()), 'data'])) log = open(log_name, 'w') d = {} agents = {} regular = LoaDEAgent() opt = LoaDEAgent() regular.pre_pre_pre_setup((False, False), 0) opt.pre_pre_pre_setup((True, False), 0) for board_size in range(8,12): total = 0 state = LinesOfActionState(board_size, 100000) agents[WHITE] = opt agents[BLACK] = regular winner = GameRunner(state, agents, 3, 1).run() if winner == WHITE: total += 1 winner = GameRunner(state, agents, 3, 1).run() if winner == WHITE: total += 1 agents[BLACK] = opt agents[WHITE] = regular winner = GameRunner(state, agents, 3, 1).run() if winner == BLACK: total += 1 winner = GameRunner(state, agents, 3, 1).run() if winner == BLACK: total += 1 d[board_size] = total print 'total: ', total msg('reordering__borad_size_dict = ' + str(d), log) print 'reordering__borad_size_dict_test done! see results in log'
def test__get_wkt(self): current_state = LinesOfActionState(6, 50) o = TurnCache() o.get_wkt(current_state, 'a', 'la', lambda: current_state.getWinner()) o.get_wkt(current_state, 'a', 'gw', current_state.getWinner) print o
def move(self, game_state): print game_state inp = raw_input( self.player + ' turn. Enter <row col action> (action is N/NE/E/SE/S/SW/W/NW/SPIN): ' ) row, col, action = inp.split() row = int(row) col = int(col) action = action.upper() res = [] if (action == 'SPIN'): res = SpinAction(row, col) else: direction_idx = DIRECTIONS.index(Direction(action, (0, 0))) res = MoveAction(row, col, DIRECTIONS[direction_idx]) return res def setup(self, player, game_state, turn_time_limit, setup_time_limit): self.player = player agents = {} agents[WHITE] = AlphaBetaAgent() agents[BLACK] = DummyAgent() state = LinesOfActionState(6, 50) winner = GameRunner(state, agents, 2, 1).run() print 'Winner:', winner