def monte_play(game_state): ans=0.0 numRuns = 10 if(game_state.maxValue() >= 16): numRuns = 20 elif(game_state.maxValue() >= 32): numRuns = 50 elif(game_state.maxValue() >= 256): numRuns = 75 elif(game_state.maxValue() >= 512): numRuns = 120 for i in range(numRuns): # print(i) tmp=TwentyFortyEight() tmp.cells=game_state.cells tmp.score=game_state.score count = 0 while(tmp.canMove()): # count += 1 # print(count) dir=random.choice([1,2,3,4]) tmp.move(dir) tmp.new_tile() #print(game_state.cells) ans+=tmp.score1() ans /= numRuns return ans
def run_2048_new_tile_test(): """ Checks if new_tile() creates a new tile of either number 2 with 90% possibility or a 4 with 10% possibility. """ twos = 0 fours = 0 NUM_COUNTS = 1000 for count in range(NUM_COUNTS): o_game_board = TwentyFortyEight(2, 2) o_game_board.new_tile() twos += sum([row.count(2) for row in o_game_board.arr_grid]) fours += sum([row.count(4) for row in o_game_board.arr_grid]) print "twos: %d / %d - %4.1f percent" % (twos, NUM_COUNTS, (twos * 100.0) / NUM_COUNTS) print "fours: %d / %d - %4.1f percent" % (fours, NUM_COUNTS, (fours * 100.0) / NUM_COUNTS)
def run_2048_reset_test(): """ Test for reset function """ print "Running test suite for reset function" suite = TestSuite() o_game_board = TwentyFortyEight(4, 4) arr_board = o_game_board.reset() i_test_count = 1 for i_index_y in range(4): for i_index_x in range(4): suite.run_test(arr_board[i_index_y][i_index_x], 0, "Test # " + str(i_test_count)) i_test_count += 1 suite.report_results() print o_game_board o_game_board.get_open_tile()
def main(): #the memory usage before the start of the current Python process process = psutil.Process(os.getpid()) startMem = process.memory_info().rss / 10**9, 'GB' hiddenLayers = [40] nReplays = 0 nIterations = 20 epsilon = 0.8 epsilonDecayFactor = 0.99 nReplays = 0 toh = TOH() tfe = TwentyFortyEight() #Start training TOH print("TOWERS OF HANOI:") startTohTrain = time.time() qnet, outcomes, samples = Qnet.trainQnet(100, hiddenLayers, nIterations, nReplays, epsilon, epsilonDecayFactor, toh) endTohTrain = time.time() - startTohTrain print("TOTAL TIME TO TRAIN TOWERS OF HANOI: ", endTohTrain) #Start testing TOH #startTohTest = time.time() #endTohTest = time.time() - startTohTest #Start training TwentyFortyEight print() print("TWENTY FORTY EIGHT:") startTfeTrain = time.time() qnet, outcomes, samples = Qnet.trainQnet(100, hiddenLayers, nIterations, nReplays, epsilon, epsilonDecayFactor, tfe) endTfeTrain = time.time() - startTfeTrain print("TOTAL TIME TO TRAIN TWENTY FORTY EIGHT: ", endTfeTrain) #Start testing TFE #startTfeTest = time.time() #endTfeTest = time.time() - startTfeTest #the memory usage after the process completes endMem = process.memory_info().rss / 10**9, 'GB' totalMem = endMem[0] - startMem[0] print() print("TOTAL MEMORY USED BY PROGRAM IN GB: ", totalMem)
def run_2048_move_test(): """ Checks if the grid has been redrawn correctly after a move command """ # suite = TestSuite() o_game_board = TwentyFortyEight(4, 6) o_game_board.set_tile(0, 0, 2) o_game_board.set_tile(1, 0, 2) o_game_board.set_tile(1, 3, 2) o_game_board.set_tile(3, 3, 4) print o_game_board o_game_board.move(2) # o_game_board.move(3) # o_game_board.move(1) print o_game_board row1 = o_game_board.arr_grid[0].count(4) row2 = o_game_board.arr_grid[1].count(0) if row1 != 1: print "row1 != 4 test failed" # print row1 if row2 != 2: print "row2 != 0 test failed"
# dir=minimax_alpha_beta(x,7) # print(dir) # print("Score:"+str(x.score)+"\t Max Tile:"+str(x.maxValue())) # x.move(dir) # x.new_tile() # # print("GAME ENDs") # # print(x.maxValue()) # print("Score:"+str(x.score)+"\t Max Tile:"+str(x.maxValue())) # # x.__str__() # # print(x.get_available_moves()) occ = np.zeros(16) for i in range(500): x=TwentyFortyEight() x.make_tables() # x.print_tables() # print("Generated Tables") x.new_tile() # avail_moves = x.get_available_moves() while(True): # x.__str__() # print("-----------------------") temp = x.vectorize_state(); tempx = np.zeros((1, 256)) for j in range(16): tempx[0, j*16 + temp[j]] = 1 probs=y.eval(feed_dict={x1: tempx})[0] # print(probs)
def __init__(self): self.S = TwentyFortyEight() self.S.make_tables() self.score = 0 self.S.new_tile() print("New Episode")
class Environment(object): def __init__(self): self.S = TwentyFortyEight() self.S.make_tables() self.score = 0 self.S.new_tile() print("New Episode") def reset(self): self.S.score = 0 self.S.cells = 0 self.score = 0 self.S.new_tile() return self.S.vectorize_state() def step(self, action): score_prev = self.S.score1() cells = self.S.cells self.S.move(action + 1) r = self.S.score1() - score_prev self.score = self.S.score1() if not cells == self.S.cells: self.S.new_tile() if not self.S.canMove(): return self.S.vectorize_state(), r, True return self.S.vectorize_state(), r, False def seed(self, a): return