def runCrossEntropyMethod(initMeanV, initSdV): n = 30 rho = 0.1 k = int(rho * n) meanV = initMeanV sdV = initSdV for i in xrange(11, 101): meanV, sdV = runCrossEntropyIteration(n, k, meanV, sdV) print strftime("%Y-%m-%d %H:%M:%S", gmtime()) print i, 'mean', meanV print i, 'sd', sdV if i % 10 == 0: game.writeWeights('weights_ce{}.tetris'.format(i), meanV) game.writeWeights('sd_ce{}.tetris'.format(i), sdV)
if successor.isWin(): successor = None elif successor.isLose(): # reward = -100 successor = None output.append((successor, prob, reward)) return output def discount(self): return self.tetris.discount() if __name__ == "__main__": # weights = TetrisGame.readWeights('weightsThiery.tetris') #weights = [0.10000000000000001, 0.5, -44.892192895842715, -88.868713917352991, -9.0845044328524125, 99.48473353935913, -0.10000000000000001, -0.10000000000000001, -46.3552758541178784, -44.894981251780582, -49.903753991739844, -48.776916252514777, -56.009249265806467, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] # weights = [random.random() for _ in xrange(22)] weights = TetrisGame.readWeights('weights_ql400.tetris') # evaluator = AdvancedEvaluator(weights) evaluator = AdhocEvaluator(weights) baseSeq = [0,1,2,3,4,5,6]*100 # random.seed(21) random.shuffle(baseSeq); # state: (board, new piece) # action: (newGrid, reward) def featureExtractor(state, action): newState = state.generateSuccessor(0, action) output = evaluator.featureExtractor(newState) return output model = TetrisGameMDP(TetrisMDP(), ExpectimaxTetrisAgent(0, 1, evaluator), FinitePieceGenerator(baseSeq))
print 'std', std print '5%', percent5 print '95%', percent95 return mean, std, percent5, percent95 def printArr(arr): print '==========' for a in arr: print a if __name__ == '__main__': # weightFile = 'weights_ql500.tetris' results_lines = [] results_scores = [] for i in xrange(9,21): weightFile = 'weights_ce{}.tetris'.format(i) weights = game.readWeights(weightFile) linesV, scoresV = simulateGame(weights, 10) print '===lines===' results_lines.append(analyze(linesV)) print '===scores===' results_scores.append(analyze(scoresV)) results1 = zip(*results_lines) results2 = zip(*results_scores) print len(results1), len(results2) for result in results1: printArr(result) for result in results2: printArr(result)
pygame.display.set_caption("Tetris") clock = pygame.time.Clock() # Create game_board surface for the tetris grid game_board_size = [i * tetris_pixel_size for i in (10, 24)] surface_board = pygame.Surface(game_board_size) surface_saved = pygame.Surface((4 * tetris_pixel_size, 4 * tetris_pixel_size)) surface_upcoming = pygame.Surface(((2 * tetris_pixel_size), ((4 * tetris_pixel_size * 5) + upcoming_tetris_display_gap * 5))) surface_upcoming.fill((0, 100, 100)) # Create font for text text_font = pygame.font.SysFont('Comic Sans MS', 16) # Create tetris game object tetrisGame = TetrisGame.TetrisGame() run = True while run: # Get and process events, including keypress for event in pygame.event.get(): if event.type == pygame.QUIT: run = False if event.type == pygame.KEYDOWN: # this method of getting keypresses does not repeat if event.key == pygame.K_DOWN: tetrisGame.move_drop_soft(1) if event.key == pygame.K_UP: tetrisGame.move_rotate(1) if event.key == pygame.K_RIGHT: tetrisGame.move_horizontal(1)