示例#1
0
def main():
    global edges, edges2, edges3, edges4
    if args.distances is None:
        generate_random_graph()
    else:
        parse_distances()

    bf_load_graph(graph)
    print("Using BruteForce algorithm")
    edges = brute_force()
    edges[0] = edges[0][:-1]

    hm_load_graph(graph)
    print("Using HeuristicMax algorithm")
    edges2 = heuristic("MAX")
    edges2[0] = edges2[0][:-1]

    hm_load_graph(graph)
    print("Using HeuristicMin algorithm")
    edges3 = heuristic("MIN")
    edges3[0] = edges3[0][:-1]

    hm_load_graph(graph)
    print("Using HeuristicRandom algorithm")
    edges4 = heuristic("RANDOM")
    edges4[0] = edges4[0][:-1]

    url = "http://0.0.0.0:8000"
    webbrowser.open_new_tab(url)
    app.run(port=8000, host="0.0.0.0")
def main():
    global edges, edges2, edges3, edges4
    if args.distances is None:
        generate_random_graph()
    else:
        parse_distances()

    bf_load_graph(graph)
    print("Using BruteForce algorithm")
    edges = brute_force()
    edges[0] = edges[0][:-1]

    hm_load_graph(graph)
    print("Using HeuristicMax algorithm")
    edges2 = heuristic("MAX")
    edges2[0] = edges2[0][:-1]

    hm_load_graph(graph)
    print("Using HeuristicMin algorithm")
    edges3 = heuristic("MIN")
    edges3[0] = edges3[0][:-1]

    hm_load_graph(graph)
    print("Using HeuristicRandom algorithm")
    edges4 = heuristic("RANDOM")
    edges4[0] = edges4[0][:-1]

    url = "http://0.0.0.0:8000"
    webbrowser.open_new_tab(url)
    app.run(port=8000, host="0.0.0.0")
示例#3
0
def astar(state, heuristic):
	
	# For experiments:
	#start = time.time()
	
	# Null parent is a fake parent for the starting node.
	null_parent = Node(0,0,0,0)
	node = Node(state, null_parent, 0, heuristic(state))
	
	frontier = Queue.PriorityQueue()
	frontier.put(node)
	
	explored = []
	count = 0
	
	# Loop through the frontier until it is empty or a solution is found.
	while not frontier.empty():
		count += 1
		node = frontier.get()
		
		# For experiments:
		#print "Node f value:"
		#print node.g + node.h
		
		state = node.state
		if state == solution():
			
			# For experiments:
			#end = time.time()
			#print heuristic
			#print "COUNT"
			#print count
			#print "DEPTH OF SOLN"
			#print node.g
			#print "ASTAR time"
			#print end - start
			
			return getpath(node)
		
		explored.append(state)
		
		# Expand possible new nodes and put on the frontier if state hasn't yet
		# been explored
		blank = blankSquare(state)
		neighbors_list = neighbors(blank)
		for neighbor in neighbors_list:
			new_state = moveBlank(state, neighbor)
			if new_state not in explored:
				new_node = Node(new_state, node, node.g+1, heuristic(new_state))
				frontier.put(new_node)
	
	# Failure node is needed for comparison.
	return Node("failure", 0, 0, 0)
示例#4
0
文件: connectFour.py 项目: MJC598/AI
def min_value(action_list, board, player_char, player_win, opp_win, opp_char,
              increment):
    temp_board = deepcopy(board)
    total_nodes = 0
    value_list = []
    if player_char == 'X':
        player = 'p1'
        if increment == 1:
            value_list = []
            for action in action_list:
                # print('hey, im in the min_value but p1')
                value, nodes = heuristic(
                    action[0], action[1],
                    update_board(temp_board, player, action), player_char,
                    opp_char, player_win, opp_win)
                value_list.append(tuple((value, action)))
                total_nodes += nodes
            return min(value_list), total_nodes
        else:
            value_list = []
            for action in action_list:
                mval, n = max_value(action_list,
                                    update_board(temp_board, player, action),
                                    player_char, player_win, opp_win, opp_char,
                                    increment + 1)
                value_list.append(tuple((mval, action)))
                total_nodes += n
            return min(value_list), total_nodes
    else:
        player = 'p2'
        if increment == 3:
            value_list = []
            for action in action_list:
                value, nodes = heuristic(
                    action[0], action[1],
                    update_board(temp_board, player, action), player_char,
                    opp_char, player_win, opp_win)
                value_list.append(tuple((value, action)))
                total_nodes += nodes
            return min(value_list), total_nodes
        else:
            value_list = []
            for action in action_list:
                mval, n = max_value(action_list,
                                    update_board(temp_board, player, action),
                                    player_char, player_win, opp_win, opp_char,
                                    increment + 1)
                value_list.append(tuple((mval, action)))
                total_nodes += n
            return min(value_list), total_nodes
示例#5
0
def DFS(startCost, pos, costLimit, pathSoFar, map, boxPlaced,width):
        """
        returns (solution-sequence or None, new cost limit)
        """
 
        minimumCost = startCost + heuristic(pos, map, boxPlaced) #evaluate the cost function to compare it the the cost limit

        if minimumCost > costLimit: #if cost to hight kill the branch
            return (None, minimumCost)
        if problemSolved(map): #if solution founded return it
            printing.printMap(map,width)
            return (pathSoFar, costLimit)
 
        nextCostLimit = Infinity
	
        accesArea = accessibleArea(map,pos,width)#build the access area map

        #check if the configuration was obtained earlier and if yes, depending on expectedQuality
        #check if the new path to reach the configuration is better than the previous one. if not kill the branch
        for i,accMap in enumerate(accesAreaList):
                if accMap == accesArea:
                    if costOfAccesAreaList[i] < minimumCost or random.randint(1,100) > expectedQuality :
                        return(None, badMove)#bad move return bad move as cost value to avoid problems with the cost limit update
                        #because since the search stoped before being stoped by the cost limit, the curent cost is lower thant the cost limit

                        #if the new configuration was better and was kept, remove the previous form the lists 
                        del(accesAreaList[i])
                        del(costOfAccesAreaList[i])
                
        accesAreaList.append(accesArea)#add the new configuration to the lists
        costOfAccesAreaList.append(minimumCost)
	
        
        for boxPos in boxReachable(accesArea, map,width):#for all reachable boxes
            for posToMove in successors(boxPos,width): #for all possible moves
                #posToMove is the position of the agent when it moves the box
                if accesArea[posToMove] != 1:
                    continue #if pos to move can not be reached try another position
                
                newStartCost = startCost + moveCost
                (asPlaceBox, newMap) = moveBox (map , boxPos , posToMove,width)#move the box
		        
                if newMap == None:#if the move is not allowed
                    continue # try another one
                        
                        #call the function with the new configuration 
                (solution, newCostLimit) = DFS(newStartCost, boxPos, costLimit, pathSoFar + [posToMove,boxPos], newMap, boxPlaced+asPlaceBox,width)
                if solution != None:#if a sulution is founded, stop the search
                    return (solution, newCostLimit)
                nextCostLimit = min(nextCostLimit, newCostLimit)

        return (None, nextCostLimit)#if no solution founded return the minimum cost returned
示例#6
0
def aStarSearch(task, heuristic, useHelpfulAction=False, noDel=False):
    extendedPathsCount = 0

    root = SearchNode(task.initial_state, None, None, 0)
    open_set = PriorityQueue()
    open_set.push(root, 0)

    state_cost = {task.initial_state: 0}

    while not open_set.isEmpty():
        pop_node = open_set.pop()
        pop_state = pop_node.state

        # Extend only if the cost of the node is the cheapest found so far.
        # Otherwise ignore, since we've found a better one. 
        if state_cost[pop_state] == pop_node.cost:
            extendedPathsCount += 1

            if task.goal_reached(pop_state):
                print "Finished searching. Found a solution. "
                return (extendedPathsCount, pop_node.cost, pop_node.path(), pop_state)

            relaxedPlan = None
            if useHelpfulAction:
                relaxedPlan = heuristic.getRelaxedPlan(SearchNode(pop_state, None, None, 0))

            for op, succ_state in task.get_successor_states(pop_state, noDel):
                # If we're using helpful actions, ignore op that is not in the helpful actions
                if useHelpfulAction:
                    if relaxedPlan and not op.name in relaxedPlan:
                        #print str(op.name) + " not in " +  str(relaxedPlan)
                        continue

                # Assume each action (operation) has cost of one
                succ_node = SearchNode(succ_state, pop_node, op, 1)
                h = heuristic(succ_node)
                #print "\nHeuristic values for " + ', '.join(map(format_string, succ_node.state)) + " is " + str(h)

                if h == float('inf'):
                    # Don't bother with states that can't reach the goal anyway
                    continue

                old_succ_cost = state_cost.get(succ_state, float("inf"))
                if succ_node.cost < old_succ_cost:
                    # Found a cheaper state or never saw this state before
                    open_set.push(succ_node, succ_node.cost + h)
                    state_cost[succ_state] = succ_node.cost

    print "No more operations left. Cannot solve the task"
    # (extendedPathsCount, cost, path, final_state)
    return (extendedPathsCount, None, None, None)
示例#7
0
    def search_map(map, heuristic, epsilon=1, message_queue=None):
        # Create a map for checking if a block is in queue
        check_map = utilities.Utilities.create_matrix(map.size, -1)
        check_map[map.start.x][map.start.y] = 0

        # Run algorithm
        path_found = False
        open_list = []
        queue = PriorityQueue()
        # Lock user input
        if message_queue != None:
            message_queue.put_nowait(message.Message(action="LOCK"))
        # Clear path
        if message_queue != None:
            message_queue.put_nowait(message.Message(action="CLEAR"))
        node = SearchNode(map.start, None)
        queue.push(node, heuristic(map.start, map.end))
        while not queue.empty():
            node = queue.pop()

            # Request drawing
            if message_queue != None:
                pop_message = message.Message(action="PUSH",
                                              param=grid.Grid.POP_ID)
                pop_message.x = node.data.position.x
                pop_message.y = node.data.position.y
                message_queue.put_nowait(pop_message)

            node = node.data
            x = node.position.x
            y = node.position.y
            if not map.is_valid(x, y) or map.is_wall(x, y):
                break
            open_list.append(node)

            # If current node is end, stop
            if x == map.end.x and y == map.end.y:
                path_found = True
                break

            g_value = check_map[x][y] + 1
            dxdyrange = [
                (-1, -1),
                (-1, 0),
                (-1, 1),
                (0, 1),
                (1, 1),
                (1, 0),
                (1, -1),
                (0, -1),
            ]
            for (dx, dy) in dxdyrange:
                tempx = x + dx
                tempy = y + dy
                # Child is a wall or not valid
                if not map.is_valid(tempx, tempy) or map.is_wall(tempx, tempy):
                    continue
                # Child is already in queue and has smaller g(x)
                if check_map[tempx][tempy] != -1 and check_map[tempx][
                        tempy] <= g_value:
                    continue

                # Otherwise, add child to queue
                check_map[tempx][tempy] = g_value
                child_pos = search_map.Position(tempx, tempy)
                f_value = g_value + heuristic(child_pos, map.end) * epsilon
                queue.push(SearchNode(child_pos, node), f_value)

                # Request drawing
                if message_queue != None:
                    in_queue_message = message.Message(
                        action="PUSH", param=grid.Grid.IN_QUEUE_ID)
                    in_queue_message.x = tempx
                    in_queue_message.y = tempy
                    message_queue.put_nowait(in_queue_message)

        return map, open_list, path_found
示例#8
0
    episode_data['total_reward'] = total_reward
    episode_data['info'] = info
    episode_data['all_steps'] = episode_steps
    return episode_data


# Harvest batch
def get_batch(net, steps_in_episode, batch_size, cpus):
    batch = []
    for eps_idx in range(batch_size):
        batch.append(play_episode(net, steps_in_episode))
    return batch


heuristic()


def get_batch_heuristic():
    pass


def filter_batch(batch, percentile):
    # Extract the total_reward field
    batch_rewards = [episode['total_reward'] for episode in batch]
    cutoff = np.percentile(batch_rewards, percentile, interpolation='higher')

    elite_batch = []
    for episode, batch_reward in zip(batch, batch_rewards):
        if batch_reward >= cutoff:
            elite_batch.append(episode)
示例#9
0
文件: bot2.py 项目: darkf/quizclown
	if ready[0]:
		line = s.recv(1024)

		words = line.split(" ")

		if words[0]=='PING':
			s.send("PONG "+word[1]+"\r\n")
			
		if len(words)>2 and words[1]=='PRIVMSG':
			quote = line.split(":")[2]
			quote = quote.split("\r\n")[0]
	
			user = words[0].split(":")[1]
			user = user.split("!")[0]
	
			heur = heuristic(ans[qnums[qid]])
			full_ans = plain_question(ans[qnums[qid]])

			if good_enough(quote, full_ans) or (heur != "" and good_enough(quote, heur)):
				bot_say("%s got it right in %d seconds" % (user, time.time()-question_time))
				
				if user not in scores:
					scores[user] = 0
	
				scores[user] += 1
	
				qid += 1
				waiting = 2
				timeout = time.time() + 5
	
			if quote=="quizclown":
示例#10
0
文件: main.py 项目: matheusota/MO648
            solutionStatistics = modelSimple(i, R, 100, True)
            statisticsList.append(solutionStatistics)
            numberOfUsers.append(i)

        createGraph(numberOfUsers, statisticsList)

    elif mode == "-best_effort":
        bestEffort(50, R, 10, False)
    elif mode == "-simple":
        modelSimple(50, R, 10, False)
    elif mode == "-simple2":
        modelSimple2(50, R, 10, False)
    elif mode == "-round":
        modelRound(50, R, 10, False)
    elif mode == "-roundprob":
        modelRoundProb(50, R, 10, False)
    elif mode == "-spp":
        modelSPP(50, R, 10, False)
    elif mode == "-reshuffle":
        reshuffle(50, R, 10, False)
    elif mode == "-quadratic":
        modelQuadratic(50, R, 10, False)
    elif mode == "-best":
        modelBest(35, R, 1, False)
    elif mode == "-best_round":
        modelBestRound(50, R, 10, False)
    elif mode == "-brute_force":
        bruteForce(50, R, 10, False)
    elif mode == "-heuristic":
        heuristic(50, R, 10, False)
示例#11
0
def branch_and_bound(g):
    """
        Branch and bound
        cria um fila de prioridades, onde os primeiros a serem executados sao os branchs com menos vertices a serem verificados
        executa a heuristica feita para receber uma solucao inial
        melhor_solucao = solucao_inicial
        define o lower_bound como a metade da melhor solucao
        adciona ((cobertura,vertices que podem ser escolhidos,vertices no grafo,lower_bound),prioridade)
        enquanto a fila nao estiver vazia
            sub_problema = fila.pop()
            se sub_problema_cobertura forma uma cobertura
                se sub_problema_cobertura melhor que melhor solucao
                    melhor solucao = sub_problema_cobertura
            senao
                v = seleciona_melhor_vertice
                sub_problema_cobertura U v
                se solucao_atual + solucao_resto_do_grafo/2 < lower_bound
                    adiciona sub_problema_alterado a fila
                remove v do sub_problema_cobertura
                remove v dos vertices que ainda podem ser utilizados
                se vertices que ainda podem ser utilizados formam uma cobertura
                    adciona sub_problema_alterado a fila
        retorna melhor_solucao
    """
    start = time.time()
    my_pq = q.PriorityQueue()  #lista de prioridade
    start_solution = convert(heuristic_cover(g))
    start_lower = math.floor(len(start_solution) / 2)

    my_pq.put(  #adicionar o novo elemento
        (
            None,  #cover
            tuple(g.nodes()),  #vertices que podem ser utilizados
            tuple(g.nodes()),  #vertices que estao no grafo
            start_lower  #limite inicial
        ),
        nx.number_of_nodes(g)  #prioridade
    )

    best = (len(start_solution), start_solution)

    print(
        str(best[1]) + " - Start solution: %d - Time: %.2f" %
        (best[0], time.time() - start))

    itt = 0

    sub_problem = None
    sub_p_cover = None
    left_vertex = None
    left_graph = None
    this_lower_bound = None

    while not my_pq.empty():
        itt += 1
        #pega a maior prioridade (menor numeor de vertices restante)
        sub_problem = my_pq.get()

        if (sub_problem[0] == None):
            sub_p_cover = []
        else:
            sub_p_cover = list(sub_problem[0])
        left_vertex = list(sub_problem[1])
        left_graph = g.subgraph(list(sub_problem[2]))
        this_lower_bound = sub_problem[3]

        random.seed()
        best_pick = random.choice(left_vertex)
        new_cover_with_v = list(sub_p_cover)
        new_cover_with_v.append(best_pick)
        new_left_possibles = modify_graph(left_graph, best_pick)
        new_total_possibles = set(left_vertex).intersection(new_left_possibles)
        new_left_graph = g.subgraph(new_left_possibles)

        #Se nao tiver mais nos a serem pesquisados
        if nx.number_of_nodes(new_left_graph) == 0:
            #se a achada foi melhor que a atual
            if len(new_cover_with_v) < best[0]:
                best = (len(new_cover_with_v), new_cover_with_v)
                print(
                    str(best[1]) + " - New solution: %d - Time: %.2f" %
                    (best[0], time.time() - start))
        else:
            #se ainda houver elementos a serem testados
            if len(new_left_possibles) > 0:

                cost = len(new_cover_with_v) + math.floor(
                    heuristic(new_left_graph))
                if cost < best[0]:
                    my_pq.put(
                        (tuple(new_cover_with_v), tuple(new_total_possibles),
                         tuple(new_left_possibles), cost),
                        nx.number_of_nodes(new_left_graph))

        cover_without_v = list(sub_p_cover)
        new_left_possibles_without_v = list(left_vertex)
        new_left_possibles_without_v.remove(best_pick)

        #Verifica se ainda e viavel
        if len(new_left_possibles_without_v) > 0 and still_coverable(
                left_graph, best_pick, new_left_possibles_without_v):
            if this_lower_bound < best[0]:
                my_pq.put((tuple(cover_without_v),
                           tuple(new_left_possibles_without_v),
                           tuple(sub_problem[2]), this_lower_bound),
                          nx.number_of_nodes(left_graph))

    print(
        str(best[1]) + " - BEST solution: %d - Time: %.2f" %
        (best[0], time.time() - start))
    return best