def AStarSearch(problem, heuristic): from game import Directions import util import searchNode #print "Start:", problem.getStartState() #print "Is the start a goal?", problem.isGoalState(problem.getStartState()) #print "Start's successors:", problem.getSuccessors(problem.getStartState()) closedSet=set() fringe=util.PriorityQueue() node= searchNode.searchNode(tuple([problem.getStartState(),"START",0]),None,heuristic(problem.getStartState(),problem)) fringe.push(node,node.AStarCost()) foundGoal=False while not foundGoal: currentNode=fringe.pop() if(problem.isGoalState(currentNode.currentState[0])): foundGoal=True print('goal!!!!') break #print("Current: ",currentNode.currentState) #print ("umm successors: ", problem.getSuccessors(currentNode.currentState[0])) #closedSet.add(currentNode.currentState[0]) if currentNode.currentState[0] not in closedSet: closedSet.add(currentNode.currentState[0]) for successors in problem.getSuccessors(currentNode.currentState[0]): #print("Successor: ",successors) #if successors[0] not in closedSet: #closedSet.add(successors[0]) #print(closedSet) newNode=searchNode.searchNode(successors,currentNode,heuristic(successors[0],problem)) fringe.push(newNode,newNode.AStarCost()) #raw_input("Press Enter to continue...") return currentNode.GetDirections()
def BFSPriorityQueue(problem): from game import Directions import util import searchNode import Postgres #myPG = Postgres.Postgres() #print "Start:", problem.getStartState() #print "Is the start a goal?", problem.isGoalState(problem.getStartState()) #print "Start's successors:", problem.getSuccessors(problem.getStartState()) closedSet = set() fringe = util.PriorityQueue() node = searchNode.searchNode(tuple([problem.getStartState(), "START", 0])) fringe.push(node, node.currentDepth) foundGoal = False while not foundGoal: currentNode = fringe.pop() if (problem.isGoalState(currentNode.currentState[0])): foundGoal = True print('goal!!!!') break #print("Current: ",currentNode.currentState) #print ("umm successors: ", problem.getSuccessors(currentNode.currentState[0])) #closedSet.add(currentNode.currentState[0]) if currentNode.currentState[0] not in closedSet: closedSet.add(currentNode.currentState[0]) for successors in problem.getSuccessors( currentNode.currentState[0]): #print("Successor: ",successors) #if successors[0] not in closedSet: #closedSet.add(successors[0]) #print(closedSet) #myPG.WriteState(successors[0]) newNode = searchNode.searchNode(successors, currentNode) fringe.push(newNode, newNode.currentDepth) #raw_input("Press Enter to continue...") return currentNode.GetDirections()
def DFSStack(problem): from game import Directions import util import searchNode #print "Start:", problem.getStartState() #print "Is the start a goal?", problem.isGoalState(problem.getStartState()) #print "Start's successors:", problem.getSuccessors(problem.getStartState()) closedSet = set() fringe = util.Stack() node = searchNode.searchNode(tuple([problem.getStartState(), "START", 0])) fringe.push(node) foundGoal = False while not foundGoal: currentNode = fringe.pop() if (problem.isGoalState(currentNode.currentState[0])): foundGoal = True #print('goal!!!!') break #print("Current: ",currentNode.currentState) #print ("umm successors: ", problem.getSuccessors(currentNode.currentState[0])) #closedSet.add(currentNode.currentState[0]) if currentNode.currentState[0] not in closedSet: closedSet.add(currentNode.currentState[0]) for successors in problem.getSuccessors( currentNode.currentState[0]): #print("Successor: ",successors) #Just undid if successors[0] not in closedSet: #Just undid closedSet.add(successors[0]) #print(closedSet) newNode = searchNode.searchNode(successors, currentNode) fringe.push(newNode) #raw_input("Press Enter to continue...") return currentNode.GetDirections()
def search(client): nameFilter = bayesFilter("name") descriptionFilter = bayesFilter("description") recentlyUsedList = {} APIUseCount = 0 searchList = [] #加入根节点 准备搜索 root_uid = client.get.account__get_uid() root_info = client.get.users__show(uid=root_uid["uid"]) searchList.append(searchNode(root_info, ())) fileptr = file("../debugInfo/consoleInfo.dat", "w") invalidName = file("../debugInfo/invalidName.dat", "w") validName = file("../debugInfo/validName.dat", "w") invalidDescription= file("../debugInfo/invalidDescription.dat", "w") validDescription= file("../debugInfo/validDescription.dat", "w") count = 0; userList = [] idList = set() successFlag = True # 用来记录最近搜索的节点, 如果长期7一个森林里面游荡,强行请出.. recent_search = [] try: while(len(userList) < 5000 and len(searchList)): #Schedule function1 # topNodenum = schedule2(searchList, recentlyUsedList) topNodenum = schedule1(searchList) topNode = searchList[topNodenum] del searchList[topNodenum] recent_search.append(topNode) # debug 信息 count +=1 print "len(searchList): ", len(searchList) s = json.dumps(topNode.getParentList(), encoding="UTF-8", ensure_ascii=False) fileptr.write(str(count) + " "+ topNode.getName() + " " + str(topNode.getValue())+" "+str(topNode.getGradeInfo()) + " " + s.encode("utf-8") + "\n") print str(count) + " "+ topNode.getName() + " " + str(topNode.getValue()) + " " + s.encode("utf-8") # ============================================================================= parentList = topNode.getParentList() #保证只搜索4层之内的东西 if len(parentList) <=3: friends = client.get.friendships__friends(count=200, uid=topNode.getUid()) APIUseCount +=1 print "APIUseCount", APIUseCount topNode.setFriendsList(friends["users"]) for eachitem in friends["users"]: # 满足搜索条件 if searchOrNot(eachitem, idList, nameFilter, descriptionFilter, invalidName, invalidDescription, validName, validDescription): tempList = list(parentList) tempList.append(topNode.getName()) tempNode = searchNode(eachitem, tempList) #evaluate the tempnode value, the bigger, the better tempNode.evaluate() #Insert the node into the search list according to the evaluated value; insertOrderly(searchList,tempNode) insertOrderly(userList,tempNode) idList.add(eachitem["id"]) except Exception as ex: successFlag = False userListptr = file("../debugInfo/UserlistPickle", "w") searchListptr = file("../debugInfo/searchlistPickle", "w") cPickle.dump(userList, userListptr) cPickle.dump(searchList, userListptr) print_searchList(userList, "../debugInfo/userList") print "Error!!!" print "APIUseCount", APIUseCount print "======================================" print ex if successFlag: userListptr = file("../debugInfo/UserlistPickle", "w") cPickle.dump(userList, userListptr) print_searchList(userList, "../debugInfo/userList") print "APIUseCount", APIUseCount