def improvementMAST(agent, target, r, c, prevr, prevc, thresh): # check if recommended same location if (r == prevr and c == prevc): agent.searchCell(r, c, target) ret_r, ret_c = ba3(agent, r, c) return ret_r, ret_c # generate path path = pathwalkMAST(prevr, prevc, r, c) # highest probability path pathHigh = findHighestinRoomMAST(agent, path, thresh) # walkthrough the path i = 0 found = False # indicator variable ret_r = r ret_c = c for item in path: # walk through path if (item in pathHigh): # check if in pathhigh if (not agent.searchCell(item[0], item[1], target)): # search ret_r, ret_c = ba3(agent, item[0], item[1]) else: found = True break i += 1 numActions((prevr, prevc), path[i - 1], agent) if (not found): return (ret_r, ret_c) else: return (-1, -1)
def improvedMAMTWithClue(agent, target, thresh=0.2): highest = 0 r = c = 0 for row in agent.map: for cell in row: cell.score = cell.probability * (1 - cell.falseNegativeProbability) # dist = float(manhattanDistance((r,c), (i,j))) # agent.map[i][j].score = dist / agent.map[i][j].score if cell.score > highest: highest = cell.score r, c = cell.row, cell.col while (agent.hasFoundTarget == False): prevr = r prevc = c searchResult = agent.searchCell(r, c, target) # search cell withinFive = targetInRange(agent, target, r, c) # check if within 5 if searchResult == False: # if not found target.move() r, c = ba3(agent, r, c) # normalize board if not withinFive: # if outside # find best score in cells outside minScore, r, c = minOutRange(agent, prevr, prevc) numActions((prevr, prevc), (r, c), agent) # increment actions continue else: # if within 5 # find best score in cells within 5 minScore, r, c = minInRange(agent, prevr, prevc) r, c = improvementMAMT(agent, target, r, c, prevr, prevc) # run improvement if (r == -1 and c == -1): break return agent.numMoves
def improvedMAMTWithClue(agent, target): highest = 0 r = c = 0 for row in agent.map: for cell in row: cell.score = cell.probability * \ (1 - cell.falseNegativeProbability) if cell.score > highest: highest = cell.score r, c = cell.row, cell.col while agent.hasFoundTarget == False: minScore = agent.dim**4 prevr = r prevc = c numChecks = int(agent.map[r][c].falseNegativeProbability * 10.0) for check in range(numChecks): searchResult = agent.searchCell(r, c, target) #print('searched cell: ', (prevr,prevc)) #print('Target Position: ', target.position) if searchResult == False: target.move() withinFive = targetInRange(agent, target, prevr, prevc) #print('target in range? ', withinFive) scale = 1 - agent.map[r][c].probability + \ agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability agent.map[r][c].probability = agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability i = 0 while i < agent.dim: j = 0 while j < agent.dim: if prevr == i and prevc == j: j += 1 continue # probabilities/scores are updated here agent.map[i][j].probability = agent.map[i][ j].probability / scale agent.map[i][j].score = agent.map[i][j].probability * \ (1 - agent.map[i][j].falseNegativeProbability) dist = manhattanDistance((prevr, prevc), (i, j)) agent.map[i][j].score = (1 + float(dist)) / ( agent.map[i][j].probability * (1 - agent.map[i][j].falseNegativeProbability)) j = j + 1 i = i + 1 if check == numChecks - 1: if withinFive: minScore, r, c = minInRange(agent, prevr, prevc) else: minScore, r, c = minOutRange(agent, prevr, prevc) numActions((prevr, prevc), (r, c), agent) else: break return agent.numMoves
def improvedMAMTWithoutClue(agent, target): highest = 0 r = c = 0 for row in agent.map: for cell in row: cell.score = cell.probability * (1 - cell.falseNegativeProbability) # dist = float(manhattanDistance((r,c), (i,j))) # agent.map[i][j].score = dist / agent.map[i][j].score if cell.score > highest: highest = cell.score r, c = cell.row, cell.col while (agent.hasFoundTarget == False): minScore = agent.dim**4 prevr = r prevc = c numChecks = int(agent.map[r][c].falseNegativeProbability * 10.0) for check in range(0, numChecks): searchResult = agent.searchCell(r, c, target) if searchResult == False: target.move() scale = 1 - agent.map[r][c].probability + \ agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability agent.map[r][c].probability = agent.map[r][c].falseNegativeProbability * \ agent.map[r][c].probability i = 0 while i < agent.dim: j = 0 while j < agent.dim: if i == prevr and j == prevc: j += 1 continue agent.map[i][j].probability = agent.map[i][ j].probability / scale dist = manhattanDistance((prevr, prevc), (i, j)) agent.map[i][j].score = (1 + float(dist)) / ( agent.map[i][j].probability * (1 - agent.map[i][j].falseNegativeProbability)) if check == numChecks - 1 and minScore > agent.map[i][ j].score: minScore = agent.map[i][j].score r = i c = j j = j + 1 i = i + 1 if check == numChecks - 1: numActions((prevr, prevc), (r, c), agent) else: break return agent.numMoves
def rule2MAST(agent, target): highest = 0 r = c = 0 # Set the initial scores for row in agent.map: for cell in row: cell.score = cell.probability * (1 - cell.falseNegativeProbability) if cell.score > highest: highest = cell.score r, c = cell.row, cell.col while (agent.hasFoundTarget == False): maxP = 0 prevr = r prevc = c searchResult = agent.searchCell(r, c, target) if searchResult == False: # Calculate the scaling factor scale = 1 - agent.map[r][c].probability + \ agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability # Update the belief state of the searched cell agent.map[r][c].probability = agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability i = 0 while i < agent.dim: j = 0 while j < agent.dim: if i == prevr and j == prevc: j += 1 continue # Update the beleif state and score of all cells agent.map[i][ j].probability = agent.map[i][j].probability / scale agent.map[i][j].score = agent.map[i][j].probability * \ (1 - agent.map[i][j].falseNegativeProbability) # Keep strack of the highest scored cell for the next search if agent.map[i][j].score > maxP: maxP = agent.map[i][j].score numActions((r, c), (i, j), agent) r = i c = j j += 1 i += 1 return agent.numMoves
def rule3MAST(agent, target): highest = 0 r = c = 0 # the initializing of the first probabilities (no score here) for row in agent.map: for cell in row: cell.score = cell.probability * (1 - cell.falseNegativeProbability) if cell.score > highest: highest = cell.score r, c = cell.row, cell.col # while the target has not been found, keep searching cells while (agent.hasFoundTarget == False): minScore = agent.dim**4 prevr = r prevc = c searchResult = agent.searchCell(r, c, target) if searchResult == False: # update the probabilties of the cell we just searched scale = 1 - agent.map[r][c].probability + \ agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability agent.map[r][c].probability = agent.map[r][c].falseNegativeProbability * \ agent.map[r][c].probability i = 0 while i < agent.dim: j = 0 while j < agent.dim: # if we reach the same cell that we already updated, skip, as the distance will be zero and this will always be the lowest scored cell if i == prevr and j == prevc: j += 1 continue # update the probabilties of each of the other cells in the board agent.map[i][ j].probability = agent.map[i][j].probability / scale # update the score of each of the other cells in the board. dist = manhattanDistance((prevr, prevc), (i, j)) agent.map[i][j].score = (1 + float(dist)) / ( agent.map[i][j].probability * (1 - agent.map[i][j].falseNegativeProbability)) if minScore > agent.map[i][j].score: minScore = agent.map[i][j].score r = i c = j j = j + 1 i = i + 1 # imcrement number of actions between the previous cell and the one we have just explored numActions((prevr, prevc), (r, c), agent) return agent.numMoves
def rule1MAST(agent, target): r, c = (rnd.randint(0, agent.dim - 1), rnd.randint(0, agent.dim - 1)) while (agent.hasFoundTarget == False): maxBelief = 0 searchResult = agent.searchCell(r, c, target) prevr = r prevc = c if searchResult == False: # Calculate the scaling factor scale = 1 - agent.map[r][c].probability + \ agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability # Update the probability of the searched cell agent.map[r][c].probability = agent.map[r][c].probability * \ agent.map[r][c].falseNegativeProbability i = 0 while i < agent.dim: j = 0 while j < agent.dim: if i == prevr and j == prevc: j += 1 continue # Update the probabilities for the rest of the cells in the map agent.map[i][ j].probability = agent.map[i][j].probability / scale # Keep track of the highest probability cell in the map if agent.map[i][j].probability > maxBelief: maxBelief = agent.map[i][j].probability r = i c = j j += 1 i += 1 # Update the number of moves taken based on the distance to the searched cell numActions((prevr, prevc), (r, c), agent) return agent.numMoves
def improvementMAMT(agent, target, r, c, prevr, prevc, thresh=0.2): pathHighs = [] # check if recommended same location if (r == prevr and c == prevc): agent.searchCell(r, c, target) ret_r, ret_c = ba3(agent, r, c) return ret_r, ret_c # generate path path1, path2 = pathwalkMAMT(prevr, prevc, r, c) if path1 == path2: path = path1 pathHighs = findHighestinRoomMAMT(agent, path, thresh) elif (path1 and path2): # highest probability path path1probs, pathHigh1 = findHighestinRoomMAMT(agent, path1, thresh) path2probs, pathHigh2 = findHighestinRoomMAMT(agent, path2, thresh) # check this conditional because of the whole 1 - prob thing for the queue if path1probs[0] <= path2probs[0]: path = path1 pathHighs = pathHigh1 else: path = path2 pathHighs = pathHigh2 elif (path1 and not path2): path = path1 pathHighs = pathHigh1 elif (not path1 and path2): path = path2 pathHighs = pathHigh2 else: return (r, c) # walkthrough the path i = 0 found = False # indicator variable ret_r = r ret_c = c inMH = True for item in path: # walk through path if (item in pathHighs): # check if in pathhigh if (not agent.searchCell(item[0], item[1], target)): # search ret_r, ret_c = ba3(agent, item[0], item[1]) # boolean if target is on in range inMH = targetInRange(agent, target, r, c) if not inMH: # we know that it is within 6 of the original spot, so pick out of those minScore, ret_r, ret_c = minInRange(agent, prevr, prevc, f=at6) i += 1 break else: found = True break i += 1 numActions((prevr, prevc), path[i - 1], agent) if (not found): return (ret_r, ret_c) else: return (-1, -1)