def simulatedAnnealing(originalBoard, prob, iters = 1000, numMoves = 40): # hill climbing, but picks worse move with given probability bestScore = 0 bestPath = None for i in xrange(iters): board = deepcopy(originalBoard) row, col, color = randomStart(board) path = [(row, col)] for x in xrange(numMoves): random_num = random.random() noDecreasingMove = False if random_num < prob: # pick random move that decreases score moves = decreasingMoves(board, row, col, color) if len(moves) == 0: noDecreasingMove = True break move = random.choice(moves)[0] if random_num >= prob or noDecreasingMove is True: # pick best move move, score = bestMove(board, row, col, color) if move is None: print 'THIS IS NOT SUPPOSED TO HAPPEN' newRow, newCol = move board[row][col] = board[newRow][newCol] board[newRow][newCol] = color path.append(move) row = newRow col = newCol score = scoreBoard(board) if score > bestScore: bestScore = score bestPath = path return (bestPath, bestScore)
def simulatedAnnealing(originalBoard, prob, iters=1000, numMoves=40): # hill climbing, but picks worse move with given probability bestScore = 0 bestPath = None for i in xrange(iters): board = deepcopy(originalBoard) row, col, color = randomStart(board) path = [(row, col)] for x in xrange(numMoves): random_num = random.random() noDecreasingMove = False if random_num < prob: # pick random move that decreases score moves = decreasingMoves(board, row, col, color) if len(moves) == 0: noDecreasingMove = True break move = random.choice(moves)[0] if random_num >= prob or noDecreasingMove is True: # pick best move move, score = bestMove(board, row, col, color) if move is None: print 'THIS IS NOT SUPPOSED TO HAPPEN' newRow, newCol = move board[row][col] = board[newRow][newCol] board[newRow][newCol] = color path.append(move) row = newRow col = newCol score = scoreBoard(board) if score > bestScore: bestScore = score bestPath = path return (bestPath, bestScore)
def randomHillClimbing(originalBoard, numMoves = 40): # picks random nondecreasing move at each state - less greedy board = deepcopy(originalBoard) row, col, color = randomStart(board) path = [(row, col)] for x in xrange(numMoves): moves = nondecreasingMoves(board, row, col, color) if len(moves) == 0: # score can only decrease - stop break move, score = random.choice(moves) newRow, newCol = move board[row][col] = board[newRow][newCol] board[newRow][newCol] = color path.append(move) row = newRow col = newCol return path, scoreBoard(board)
def randomHillClimbing(originalBoard, numMoves=40): # picks random nondecreasing move at each state - less greedy board = deepcopy(originalBoard) row, col, color = randomStart(board) path = [(row, col)] for x in xrange(numMoves): moves = nondecreasingMoves(board, row, col, color) if len(moves) == 0: # score can only decrease - stop break move, score = random.choice(moves) newRow, newCol = move board[row][col] = board[newRow][newCol] board[newRow][newCol] = color path.append(move) row = newRow col = newCol return path, scoreBoard(board)
def steepestAscentHillClimbing(originalBoard, numMoves = 40): # pick best nondecreasing move at each state - greedy board = deepcopy(originalBoard) row, col, color = randomStart(board) path = [(row, col)] for x in xrange(numMoves): move, score = bestNondecreasingMove(board, row, col, color) if move is None: # score can only decrease - stop break if len(path) > 1 and move == path[len(path) - 2]: # looping -> stop break newRow, newCol = move board[row][col] = board[newRow][newCol] board[newRow][newCol] = color path.append(move) row = newRow col = newCol return path, scoreBoard(board)
def steepestAscentHillClimbing(originalBoard, numMoves=40): # pick best nondecreasing move at each state - greedy board = deepcopy(originalBoard) row, col, color = randomStart(board) path = [(row, col)] for x in xrange(numMoves): move, score = bestNondecreasingMove(board, row, col, color) if move is None: # score can only decrease - stop break if len(path) > 1 and move == path[len(path) - 2]: # looping -> stop break newRow, newCol = move board[row][col] = board[newRow][newCol] board[newRow][newCol] = color path.append(move) row = newRow col = newCol return path, scoreBoard(board)