修改实验一,通过自动打分程序

master
huangjielun 2 years ago
parent 9418256a0b
commit 831eed19c4

@ -87,69 +87,85 @@ def depthFirstSearch(problem):
print "Start's successors:", problem.getSuccessors(problem.getStartState())
"""
"*** YOUR CODE HERE ***"
# start state
startState = problem.getStartState()
# have exit
exitState = []
#use stack to dfs
States = util.Stack()
States.push((startState,[]))
#repeat a circle to confirm if it is successful
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
State,Actions = States.pop()
if State not in exitState:
exitState.append(State)
Successor = problem.getSuccessors(State)
for Node in Successor:
Coordinates = Node[0]
Direction = Node[1]
if not Coordinates in exitState:
States.push((Coordinates,Actions + [Direction]))
startState = Coordinates
return Actions + [Direction]
visited_node = []
myStack= util.Stack()
actions = []
s = problem.getStartState()
if problem.isGoalState(s):
return actions
myStack.push((s, actions))
while not myStack.isEmpty():
state = myStack.pop()
if state[0] in visited_node:
continue
visited_node.append(state[0])
actions = state[1]
if (problem.isGoalState(state[0])):
return actions
for successor in problem.getSuccessors(state[0]):
child_state = successor[0]
action = successor[1]
sub_action = list(actions)
if not child_state in visited_node:
sub_action.append(action)
myStack.push((child_state, sub_action))
return actions
util.raiseNotDefined()
def breadthFirstSearch(problem):
"""Search the shallowest nodes in the search tree first."""
"*** YOUR CODE HERE ***"
startState = problem.getStartState()
exitState = []
States = util.Queue()
States.push((startState,[]))
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
State, Actions = States.pop()
if State not in exitState:
Successor = problem.getSuccessors(State)
exitState.append(State)
for Node in Successor:
Coordinates = Node[0]
Direction = Node[1]
if Coordinates not in exitState:
States.push((Coordinates,Actions+[Direction]))
startState = Coordinates
return Actions + [Direction]
visited_node = []
myQueue = util.Queue()
actions = []
s = problem.getStartState()
if problem.isGoalState(s):
return actions
myQueue.push((s, actions))
while not myQueue.isEmpty():
state = myQueue.pop()
if state[0] in visited_node:
continue
visited_node.append(state[0])
actions = state[1]
if (problem.isGoalState(state[0])):
return actions
for successor in problem.getSuccessors(state[0]):
child_state = successor[0]
action = successor[1]
sub_action = list(actions)
if not child_state in visited_node:
sub_action.append(action)
myQueue.push((child_state, sub_action))
return actions
util.raiseNotDefined()
def uniformCostSearch(problem):
"""Search the node of least total cost first."""
"*** YOUR CODE HERE ***"
startState = problem.getStartState()
exitState = []
States = util.PriorityQueue()
States.push((startState, []),0)
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
State, Actions = States.pop()
if State not in exitState:
Successor = problem.getSuccessors(State)
exitState.append(State)
for Node in Successor:
Coordinates = Node[0]
Direction = Node[1]
if Coordinates not in exitState:
newAction = Actions + [Direction]
States.push((Coordinates, newAction),problem.getCostOfActions(newAction))
startState = Coordinates
return Actions + [Direction]
visited_node = []
mypriorityQueue = util.PriorityQueue()
actions = []
s = problem.getStartState()
if problem.isGoalState(s):
return actions
mypriorityQueue.push((s, actions), 0)
while not mypriorityQueue.isEmpty():
state = mypriorityQueue.pop()
if state[0] in visited_node:
continue
visited_node.append(state[0])
actions = state[1]
if (problem.isGoalState(state[0])):
return actions
for successor in problem.getSuccessors(state[0]):
child_state = successor[0]
action = successor[1]
sub_action = list(actions)
if not child_state in visited_node:
sub_action.append(action)
mypriorityQueue.push((child_state, sub_action), problem.getCostOfActions(sub_action))
return actions
util.raiseNotDefined()
def nullHeuristic(state, problem=None):
@ -162,25 +178,29 @@ def nullHeuristic(state, problem=None):
def aStarSearch(problem, heuristic=nullHeuristic):
"""Search the node that has the lowest combined cost and heuristic first."""
"*** YOUR CODE HERE ***"
startState = problem.getStartState()
exitState = []
States = util.PriorityQueue()
h_n = heuristic(startState,problem)
States.push((startState, []), h_n)
while (not States.isEmpty()) and (not problem.isGoalState(startState)):
State, Actions = States.pop()
if State not in exitState:
Successor = problem.getSuccessors(State)
exitState.append(State)
for Node in Successor:
Coordinates = Node[0]
Direction = Node[1]
if Coordinates not in exitState:
newAction = Actions + [Direction]
newCost = problem.getCostOfActions(newAction) + heuristic(Coordinates,problem)
States.push((Coordinates, newAction), newCost)
startState = Coordinates
return Actions + [Direction]
visited_node = []
mypriorityQueue = util.PriorityQueue()
actions = []
s = problem.getStartState()
if problem.isGoalState(s):
return actions
mypriorityQueue.push((s, actions), 0)
while not mypriorityQueue.isEmpty():
state = mypriorityQueue.pop()
if state[0] in visited_node:
continue
visited_node.append(state[0])
actions = state[1]
if (problem.isGoalState(state[0])):
return actions
for successor in problem.getSuccessors(state[0]):
child_state = successor[0]
action = successor[1]
sub_action = list(actions)
if not child_state in visited_node:
sub_action.append(action)
mypriorityQueue.push((child_state, sub_action),heuristic(child_state, problem) + problem.getCostOfActions(sub_action))
return actions
util.raiseNotDefined()

Binary file not shown.

@ -297,8 +297,7 @@ class CornersProblem(search.SearchProblem):
space)
"""
"*** YOUR CODE HERE ***"
allCorners = (False,False,False,False)
startState = (self.startingPosition,allCorners)
startState = (self.startingPosition,[])
return startState
util.raiseNotDefined()
@ -307,8 +306,14 @@ class CornersProblem(search.SearchProblem):
Returns whether this search state is a goal state of the problem.
"""
"*** YOUR CODE HERE ***"
Corners = state[1]
return Corners[0] and Corners[1] and Corners[2] and Corners[3]
result = state[1]
if state[0] in self.corners:
if state[0] not in result:
result.append(state[0])
if (len(result) == 4):
return True
else:
return False
util.raiseNotDefined()
def getSuccessors(self, state):
@ -332,31 +337,16 @@ class CornersProblem(search.SearchProblem):
# hitsWall = self.walls[nextx][nexty]
"*** YOUR CODE HERE ***"
x,y = state[0]
Corners = state[1]
Corners_0 = Corners[0]
Corners_1 = Corners[1]
Corners_2 = Corners[2]
Corners_3 = Corners[3]
dx , dy = Actions.directionToVector(action)
x, y = state[0]
dx, dy = Actions.directionToVector(action)
nextx, nexty = int(x + dx), int(y + dy)
hitsWall = self.walls[nextx][nexty]
newCorners = []
newState = (nextx,nexty)
if not hitsWall:
if newState in self.corners:
if newState == (1,1):
newCorners = [True,Corners_1,Corners_2,Corners_3]
elif newState == (1,self.top):
newCorners = [Corners_0,True,Corners_2,Corners_3]
elif newState == (self.right,1):
newCorners = [Corners_0,Corners_1,True,Corners_3]
elif newState == (self.right,self.top):
newCorners = [Corners_0,Corners_1,Corners_2,True]
successor =((newState,newCorners),action,1)
else:
successor = ((newState,Corners),action,1)
successors.append(successor)
visited = list(state[1])
if not self.walls[nextx][nexty]:
nextState = (nextx, nexty)
cost = 1
if nextState in self.corners and nextState not in visited:
visited.append(nextState)
successors.append(((nextState, visited), action, cost))
self._expanded += 1 # DO NOT CHANGE
return successors
@ -391,37 +381,28 @@ def cornersHeuristic(state, problem):
walls = problem.walls # These are the walls of the maze, as a Grid (game.py)
"*** YOUR CODE HERE ***"
currentPosition = state[0]
stateCorners = state[1]
top = walls.height - 2
right = walls.width - 2
Node = []
visited = state[1]
now_state = state[0]
Heuristic = 0
last = []
if (problem.isGoalState(state)):
return 0
for i in corners:
if i == (1,1):
if not stateCorners[0]:
Node.append(i)
elif i == (1,top):
if not stateCorners[1]:
Node.append(i)
elif i == (right,top):
if not stateCorners[2]:
Node.append(i)
elif i == (right,1):
if not stateCorners[3]:
Node.append(i)
cost = 0
position = currentPosition
while len(Node) > 0:
distArr = []
for i in range(0,len(Node)):
dist = util.manhattanDistance(position,Node[i])
distArr.append(dist)
miniDist = min(distArr)
cost += miniDist
miniDist_I = distArr.index(miniDist)
position = Node[miniDist_I]
del Node[miniDist_I]
return cost # Default to trivial solution
if i not in visited:
last.append(i)
pos = now_state
cost = 999999
while len(last) != 0:
for i in last:
if cost > (abs(pos[0] - i[0]) + abs(pos[1] - i[1])):
min_con = i
cost = (abs(pos[0] - i[0]) + abs(pos[1] - i[1]))
Heuristic += cost
pos = min_con
cost = 999999
last.remove(min_con)
return Heuristic
class AStarCornersAgent(SearchAgent):
"A SearchAgent for FoodSearchProblem using A* and your foodHeuristic"
@ -515,32 +496,30 @@ def foodHeuristic(state, problem):
"""
position, foodGrid = state
"*** YOUR CODE HERE ***"
hvalue = 0
foodAvailable = []
totalDistance = 0
for i in range(0,foodGrid.width):
for j in range(0,foodGrid.height):
if foodGrid[i][j] == True:
foodLocation = (i,j)
foodAvailable.append(foodLocation)
if len(foodAvailable) == 0:
return 0
maxDistance = ((0,0),(0,0),0)
for currentFood in foodAvailable:
for selectFood in foodAvailable:
if currentFood == selectFood:
pass
else:
distance = util.manhattanDistance(currentFood,selectFood)
if maxDistance[2] < distance:
maxDistance = (currentFood,selectFood,distance)
if maxDistance[0] == (0,0) and maxDistance[1] == (0,0):
hvalue = util.manhattanDistance(position,foodAvailable[0])
else:
dis_1 = util.manhattanDistance(position,maxDistance[0])
dis_2 = util.manhattanDistance(position,maxDistance[1])
hvalue = min(dis_1,dis_2) + maxDistance[2]
return hvalue
lsFoodGrid = foodGrid.asList()
last = list(lsFoodGrid)
Heuristic = 0
cost = 0
max_con = position
for i in last:
if cost < (abs(position[0] - i[0]) + abs(position[1] - i[1])):
max_con = i
cost = (abs(position[0] - i[0]) + abs(position[1] - i[1]))
Heuristic = cost
diff = position[0] - max_con[0]
count = 0
for i in last:
if diff > 0:
if position[0] < i[0]:
count += 1
if diff < 0:
if position[0] > i[0]:
count += 1
if diff == 0:
if position[0] != i[0]:
count += 1
return Heuristic + count
class ClosestDotSearchAgent(SearchAgent):
"Search for all food using a sequence of searches"

Binary file not shown.
Loading…
Cancel
Save