Exemplo n.º 1
0
def traverse(graph,
             start,
             acc,
             before_node=noop,
             after_node=noop,
             on_edge=noop,
             state=None):
    if state is None:
        state = TraversalState(graph.nodes(), acc)

    time = 0
    remaining_nodes = PriorityQueue()
    remaining_nodes.insert(start, priority(time))

    while not remaining_nodes.is_empty() and not state.finished:
        current_node = remaining_nodes.peek_highest()

        if state.fresh(current_node):
            state.visit(current_node, time)
            before_node(graph, current_node, state)

            for neighbor in graph.neighbors(current_node):
                if state.fresh(neighbor):
                    time += 1
                    remaining_nodes.insert(neighbor, priority(time))
        elif state.visited(current_node):
            time += 1
            state.process(current_node, time)
            after_node(graph, current_node, state)
            remaining_nodes.extract_highest()
        else:  # PROCESSED
            remaining_nodes.extract_highest()

    return state
Exemplo n.º 2
0
def getWay(start, end):
    """ Знаходить найкоротший шлях, між двома заданими вершинами графа

    @param start: початкова вершина
    @param end: кінцева вершина
    @return: список вершин шляху або порожній список, якщо шляху між вершинами не існує.
    """
    sources = {start: -1}
    distances = [inf for _ in range(n)]
    distances[start] = 0
    pq = PriorityQueue()
    pq.insert(start, 0)
    while not pq.empty():
        i = pq.extractMinimum()
        if i == end:
            break
        for j in graph[i]:
            if distances[i] + graph[i][j] < distances[j]:
                distances[j] = distances[i] + graph[i][j]
                sources[j] = i
                if j in pq:
                    pq.updatePriority(j, distances[j])
                else:
                    pq.insert(j, distances[j])
    if distances[end] == inf:
        return []
    path = [end]
    while end != start:
        end = sources[end]
        path.append(end)
    path.reverse()
    return path
Exemplo n.º 3
0
def main():

    tasks = Dispenser(why_not)
    time = 0

    cmd, args = get_cmd( COMMANDS )
    while cmd != QUIT_CMD:
        if cmd == TICK_CMD:
            time += 1
            if not tasks.isEmpty():
                current = tasks.peek()
                current.time_left -= 1
                if current.time_left == 0:
                    print( "\nTask '" + current.name + \
                           "' completed at time " + str( time ) + "." )
                    tasks.remove()
                    if not tasks.isEmpty():
                        print( "New task is '" + tasks.peek().name + "'." )
                    else:
                        print( "Nothing else to do." )
            else:
                print( "Nothing to do." )
        elif cmd == ADD_CMD:
            new_task = Task( args[ 0 ], int( args[ 1 ] ) )
            tasks.insert( new_task )
            print( "\nAdded. Current task is '" + tasks.peek().name + \
                   "'." )
        else:
            assert True, "PROGRAM ERROR"
        cmd, args = get_cmd( COMMANDS )

    print( "\nTerminating the simulation." )
Exemplo n.º 4
0
def main():
    tiger = Golfer('Tiger Woods', 61)
    phil = Golfer('Phil Mickelson', 72)
    hal = Golfer('Hal sutton', 69)

    pq = PriorityQueue()
    for g in [tiger, phil, hal]:
        pq.insert(g)

    pq.printQueue()
    print()
    while not pq.empty():
        print(pq.remove())
Exemplo n.º 5
0
class PriorityQueueTest(unittest.TestCase):
    def setUp(self):
        self.p_queue = PriorityQueue()

    def test(self):
        elements = []
        for i in range(1000):
            x = random()
            elements.append(x)
            self.p_queue.insert(x)
        elements.sort(reverse=True)
        for elem in elements:
            self.assertEqual(elem, self.p_queue.pop())
Exemplo n.º 6
0
    def getActions(self, start, final):
        print(
            "*****************************************************************************************************"
        )
        print("Cel: " + str(final))
        actions = []
        explored = []
        currentState = start
        currentState.priority = 99
        fringe = PriorityQueue()
        fringe.insert(currentState)

        while not self.testGoal(currentState, final):
            currentState = fringe.delete(currentState, self.mapElements)
            print("Zmieniamy obecny stan")
            print(str(currentState.position), str(currentState.rotation))

            explored.append(currentState)

            if self.testGoal(currentState, final):
                ns = currentState
                while ns.parent is not None:
                    actions = [ns.action] + actions
                    ns = ns.parent
                return actions

            #explored.append(currentState)
            for j in self.getSuccessors(currentState):
                print("Sprawdzam dany następnik:")
                x = copy.deepcopy(j[1])
                x.action = j[0]
                x.parent = copy.deepcopy(currentState)
                x.priority = self.getPriority(x, final)
                print(str(x.position), str(x.rotation), "priorytet: ",
                      str(x.priority))
                if not self.stateValueExists(
                        x, explored) and not self.stateValueExists(
                            x, fringe.queue):
                    fringe.insert(x)
                    print(
                        "Dodano następnik, nie było go ani we fringe ani w explored"
                    )
                elif self.stateValueExists(x, fringe.queue):
                    print("Następnik był we fringe")
                    for i in fringe.queue:
                        if x.position == i.position and x.rotation == i.rotation and x.priority < i.priority:
                            i = x  #copy.deepcopy(x)
            print(
                "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
            )
Exemplo n.º 7
0
    def replan(self, location, heading):
        """
        :type location: Position
        :param location: The current location of the robot
        :type heading: Direction
        :param heading: The current direction the robot is pointing
        """
        open_nodes = PriorityQueue(lifo=False)
        closed_nodes = set()
        came_from = {}  # The key is the end state, the value is a tuple of g, move, and start state

        initial_state = (location, heading)
        g = 0
        h = self.heuristic.get_value(initial_state)
        f = g + h
        open_nodes.insert(f, initial_state)
        came_from[initial_state] = (g, None, None)
        current_state = initial_state
        while len(open_nodes) > 0:
            while current_state in closed_nodes:
                current_state = open_nodes.pop_min()

            closed_nodes.add(current_state)

            if self.heuristic.get_value(current_state) == 0:
                pol = self.create_policy(current_state, initial_state, came_from)
                return self.policy

            moves = self.get_possible_moves(current_state)
            for move in moves:
                new_state = self.get_move_result(current_state, move)
                if new_state in closed_nodes:
                    continue
                g = came_from[current_state][0] + self.get_cost(move)
                h = self.heuristic.get_value(new_state)
                f = g + h
                if new_state not in open_nodes:
                    open_nodes.insert(f, new_state)
                elif new_state in came_from and f >= open_nodes.get_priority(new_state):
                    continue
                came_from[new_state] = (g, move, current_state)
                open_nodes.update_priority(new_state,f)

        return "No path to goal!"
Exemplo n.º 8
0
def find_path_using_a_star(a, b, is_open):
    """Find a path from a to b using a uniform-step-cost version of the A* algorithm"""
    def successor(p_):
        for move in UNIT_MOVES:
            pp = p_ + move
            if is_open(pp):
                yield pp

    def traceback():
        moves = []
        p_ = b
        pp = predecessor(p_)
        while pp:
            moves.append(p_ - pp)
            p_ = pp
            pp = predecessor(p_)
        moves.reverse()
        return moves

    def predecessor(p_):
        return node_dict[p_][0]

    def g(p_):
        return node_dict[p_][1]

    def h(p_):
        return (p_ - b).l1_norm

    pq = PriorityQueue(a, h(a))
    node_dict = {a: (None, 0)}  # dict contents are (predecessor, g)
    while pq:
        p0 = pq.pop()
        if p0 == b:
            return traceback()
        g1 = g(p0) + 1
        for p in successor(p0):
            f = g1 + h(p)
            if p not in node_dict:  # or f < existing_f:  Not necessary since nodes are on a uniform-cost grid
                pq.insert(p, f)
                node_dict[p] = p0, g1
    return None
Exemplo n.º 9
0
def main():
    initial_map = open_map()

    #Calculate width and height of map
    width = get_width(initial_map)
    height = get_height(initial_map)
    #Find start and end nodes
    start, end = find_coords(initial_map, width, height)
    frontier = PriorityQueue()
    #Add the start node to the priority queue, with a heuristic value of 0, and no parent
    frontier.insert(start, 0, None)
    visited_queue = PriorityQueue()

    #Execute the search algorithm
    best_first(frontier, initial_map, visited_queue, width, height, start, end)

    #Find the path
    path = find_path(visited_queue, initial_map, width, height, start, end)

    #Print the path on the map
    print_map(initial_map, path, width, height, start, end)
Exemplo n.º 10
0
def get_MST(connections, n):
    if n <= 1:
        return []

    graph = defaultdict(set)
    pq = PriorityQueue()
    INT_MAX = 2**32 - 1
    map = dict()
    result = []

    for u, v, w in connections:
        graph[u].add((v, w))
        graph[v].add((u, w))
        if not pq.find(u):
            pq.insert(u, INT_MAX)
        if not pq.find(v):
            pq.insert(v, INT_MAX)

    pq.update_priority(connections[0][0], 0)

    while pq.size() > 0:
        curr_node = pq.extract_min()
        if curr_node in map:
            result.append(map[curr_node])
            del map[curr_node]

        for nhbr in graph[curr_node]:
            node = nhbr[0]
            if pq.find(node):
                if pq.get_priority(node) > nhbr[1]:
                    pq.update_priority(node, nhbr[1])
                    map[node] = [curr_node, node]

    if len(result) == n - 1:
        return result
    else:
        return -1
Exemplo n.º 11
0
class HeapSort:
    # T(N) = NlogN
    # D(N) = 1
    # unstable
    __data_list = []
    __pq = PriorityQueue()

    def set_data(self, data_tup):
        # set data like [(0, 1), (index, value)]
        self.__data_list = data_tup
        self.__pq = PriorityQueue()

    def set_list_data(self, data_list):
        # set data like [1, 2, 3, 5, 4, value]
        data_tup = [(x, y)
                    for (x, y) in zip(range(0, len(data_list)), data_list)]
        self.__data_list = data_tup

    def get_sorted_tup(self):
        if len(self.__data_list) == 0:
            raise Exception("get_sorted_tup:尚未初始化数据")
        else:
            self.__sort()
            return self.__data_list

    def get_sorted_list(self):
        return [x[1] for x in self.get_sorted_tup()]

    def set_order(self, is_asc=True):
        self.__pq.set_order(is_asc)

    def __sort(self):
        for i in self.__data_list:
            self.__pq.insert(i)
        for i in range(0, len(self.__data_list)):
            self.__data_list[i] = self.__pq.pop()
Exemplo n.º 12
0
class UCS(Algorithm):

    def __init__(self, initial_vertex):
        super().__init__(Graph(initial_vertex))
        self.inistial_vertex = initial_vertex
        self.frontier = PriorityQueue()
        self.frontier.insert(initial_vertex)
        self.explored = []

    def execute(self):
        while True:
            if self.frontier.is_empty():
                return False
            lowest_cost_rubik = self.frontier.delete()
            if self.goal_test(lowest_cost_rubik):
                return True
            self.explored.append(lowest_cost_rubik)
            children = lowest_cost_rubik.children_nodes()
            self.update_number_of_nodes_info_after_expanding(len(children))
            for child_rubik in children:
                child_rubik.path_cost = lowest_cost_rubik.path_cost + 1
                if not self.visited(child_rubik):
                    print(child_rubik.path_cost)
                    self.frontier.insert(child_rubik)
                else:
                    self.nodes_in_memory -= 1
                    for rubik_in_queue in self.frontier.queue:
                        if rubik_in_queue.equal(child_rubik):
                            if rubik_in_queue.path_cost > child_rubik.path_cost:
                                self.frontier.queue.remove(rubik_in_queue)
                                self.frontier.insert(child_rubik)
                            else:
                                break

    def visited(self, rubik):
        if self.inside_queue(self.frontier.queue, rubik):
            return True
        if self.inside_queue(self.explored, rubik):
            return True
        return False
Exemplo n.º 13
0
def a_star(initial_state,
           max_cost=1000,
           max_time=30 * 60,
           max_states=50000,
           progress_report=None):
    """
An implementation of the A* search algorithm.
See https://www.redblobgames.com/pathfinding/a-star/introduction.html
    initial_state   an object supporting the following methods:
        cost        the cost associated with reaching this state from the initial state
        neighbors   an iterator returning neighboring states
        is_goal     returns True if the state is a goal state
        heuristic   returns an underestimate of the number of moves to reach a goal state
    max_cost        search terminates if an underestimate of the cost exceeds max_cost
    max_time        search terminates if the search time exceeds max_time
    max_states      search terminates if the number of states visited exceeds max_states
    progress_report (progress_fn, progress_interval)
        progress_fn a function called every progress_interval seconds.
        Arguments passed to progress_fn are (current_state, states_reached, priority_queue, elapsed_time).
        If progress_fn returns non-None, the search is terminated.

    Returns (solution_state, solution_info)
"""
    frontier = PriorityQueue(initial_state, 0)
    states_reached = [initial_state]
    time0 = time.time()
    termination_condition = ""
    if progress_report:
        progress_fn, progress_interval = progress_report
        progress_update_time = time0
    else:
        progress_fn, progress_interval = None, None
        progress_update_time = math.inf

    while not frontier.empty():
        current = frontier.pop()
        if current.is_goal():
            return current, f"cost-optimal solution (cost = {current.cost()}) found in {eng(time.time() - time0, 2)}s" \
                          + f" after examining {len(states_reached)} states."
        # These checks could be in the next_state loop
        if time.time() - time0 > max_time:
            termination_condition += f"Time limit ({max_time}s) exceeded."
        if current.cost() > max_cost:
            termination_condition += f"Max cost ({max_cost}) exceeded."
        if len(states_reached) > max_states:
            termination_condition += f"Max states ({max_states}) exceeded."
        if termination_condition:
            return None, termination_condition
        if time.time() > progress_update_time:
            return_value = progress_fn(current, states_reached, frontier,
                                       time.time() - time0)
            if return_value is not None:
                msg = f"halted after {eng(time.time() - time0, 2)}s after examining {len(states_reached)} states. "
                if type(return_value) is str:
                    msg += return_value
                return None, msg
            progress_update_time = time.time() + progress_interval

        for next_state in current.neighbors():
            next_added = False
            try:
                i = states_reached.index(next_state)
                if states_reached[i].cost() > next_state.cost():
                    states_reached[i] = next_state
                    next_added = True
            except ValueError:
                states_reached.append(next_state)
                next_added = True
            if next_added:
                h = next_state.heuristic()
                # if h is 0:
                #     print(f'Found a solution with cost = {next_state.cost()} (possibly non-optimal).')
                priority = next_state.cost() + h
                frontier.insert(next_state, priority)
    return None, "No solution."
Exemplo n.º 14
0
    def test1():
        from PriorityQueue import PriorityQueue
        import random
        print("test1")
        ITERATIONS = 10000
        que = PriorityQueue()
        heap = BinomialHeap()
        length = 0
        for i in range(ITERATIONS):
            if i % 300 == 0:
                print("Progress: {:.0%}".format(float(i) / ITERATIONS))
            op = random.randint(0, 99)

            if op < 1:  # Clear
                heap.check_structure()
                for j in range(length):
                    if heap.dequeue() != que.extractMin():
                        raise AssertionError()
                if not que.empty():
                    raise AssertionError()
                length = 0

            elif op < 2:  # Peek
                heap.check_structure()
                if length > 0:
                    val = que.extractMin()
                    if heap.peek() != val:
                        raise AssertionError()
                    que.insert(val)

            elif op < 60:  # Add
                n = random.randint(1, 100)
                for j in range(n):
                    val = random.randint(0, 9999)
                    que.insert(val)
                    heap.enqueue(val)
                length += n

            elif op < 70:  # Merge
                n = random.randint(1, 100)
                temp = BinomialHeap()
                for j in range(n):
                    val = random.randint(0, 9999)
                    que.insert(val)
                    temp.enqueue(val)
                heap.merge(temp)
                if len(temp) != 0:
                    raise AssertionError()
                length += n

            elif op < 100:  # Remove
                n = min(random.randint(1, 100), length)
                for j in range(n):
                    if heap.dequeue() != que.extractMin():
                        raise AssertionError()
                length -= n

            else:
                raise AssertionError()

            if len(heap) != length:
                raise AssertionError()

        print("Test passed")
Exemplo n.º 15
0
class DijkstraSP:
    # Notice : In this alg., p2p-distance should be great than zero!
    __distTo = []
    __edgeTo = []
    __start = None
    __graph = None
    __pq = None

    def __init__(self, graph: Graph):
        # V is the sum of point in graph
        # dependency: Graph.class
        self.__graph = graph

    def init(self):
        self.__distTo = [float('inf') for _ in range(0, self.__graph.getV())]
        self.__edgeTo = [None for _ in range(0, self.__graph.getV())]
        self.__pq = PriorityQueue()

    def set_start(self, start_point):
        self.init()
        self.__start = start_point
        self.__distTo[start_point[0]] = 0.0
        self.__pq.insert((start_point, 0.0))
        while self.__pq.size() != 0:
            self.__relax(self.__pq.pop()[0])

    def __relax(self, point):
        # $point should like (point_index, point_info)
        # print("--", point)
        for edge in self.__graph.adj(point):
            w = edge.to()
            # print(edge)
            if self.__distTo[w[0]] > (self.__distTo[point[0]] + edge.weight()):
                self.__distTo[w[0]] = self.__distTo[point[0]] + edge.weight()
                self.__edgeTo[w[0]] = edge
                new_point = (w, edge.weight())
                # print(new_point, self.__pq.get_pq())
                if self.__pq.contains(w):
                    self.__pq.change(w, new_point)
                else:
                    self.__pq.insert(new_point)

    def have_path_to(self, target):
        # $target should like (point_index, point_info)
        return self.__distTo[target[0]] < float('inf')

    def path_to(self, target):
        # $target should like (point_index, point_info)
        paths = []
        if self.have_path_to(target):
            edge = self.__edgeTo[target[0]]
            paths.append(edge)
            while True:
                edge = self.__edgeTo[edge.fr()[0]]
                if edge is None:
                    break
                paths.append(edge)
        paths.reverse()
        return paths

    def min_dist_to(self, target):
        # $target should like (point_index, point_info)
        return self.__distTo[target[0]]
Exemplo n.º 16
0
class ExperienceReplay():
    def __init__(self,
                 param_set,
                 buffer_size=200000,
                 buffer_type='Qnetwork',
                 mem_priority=True,
                 general=False):
        """Initialize the storage containers and parameters relevant for experience replay.
		
		Arguments:
		param_set --  dictionary of parameters which must contain:
			PER_alpha -- hyperparameter governing how much prioritization is used
			PER_beta_zero -- importance sampling parameter initial value
			bnn_start -- number of timesteps before sample will be drawn; i.e the minimum partition size (necessary if buffer_type=='BNN')
			dqn_start -- same as dqn_start (necessary if buffer_type=='Qnetwork')
			episode_count -- number of episodes
			instance_count -- number of instances
			max_task_examples -- maximum number of timesteps per episode
			ddqn_batch_size -- minibatch size for DQN updates (necessary if buffer_type=='Qnetwork')
			bnn_batch_size -- minibatch size for BNN updates (necessary if buffer_type=='BNN')
			num_strata_samples -- number of samples to be drawn from each strata in the prioritized replay buffer
			general_num_partitions -- number of partitions for general experience buffer
			instance_num_partitions -- number of partitions for instance experience buffer

		Keyword arguments:
		buffer_size -- maximum capacity of the experience buffer (default: 200000)
		buffer_type -- string indicating whether experience replay is for training a DQN or a BNN (either 'Qnetwork' or 'BNN'; default: 'Qnetwork')
		mem_priority -- boolean indicating whether the experience replay should be prioritized (default: True)
		general -- boolean indicating if the experience replay is for collecting experiences over multiple instances or a single (default: False)
		"""
        # Extract/Set relevant parameters
        self.mem_priority = mem_priority
        self.alpha = param_set['PER_alpha']
        self.beta_zero = param_set['PER_beta_zero']
        self.capacity = buffer_size
        self.is_full = False
        self.index = 0  # Index number in priority queue where next transition should be inserted
        self.size = 0  # Current size of experience replay buffer
        if buffer_type == 'Qnetwork':
            self.num_init_train = param_set['dqn_start']
            self.tot_steps = param_set['episode_count'] * param_set[
                'max_task_examples']
            self.batch_size = param_set['ddqn_batch_size']
        elif buffer_type == 'BNN':
            self.num_init_train = param_set['bnn_start']
            self.tot_steps = (
                param_set['episode_count'] *
                param_set['instance_count']) * param_set['max_task_examples']
            self.batch_size = param_set['bnn_batch_size']
        self.beta_grad = (1 - self.beta_zero) / (self.tot_steps -
                                                 self.num_init_train)
        self.num_strata_samples = param_set['num_strata_samples']
        # Note: at least one partition must be completely filled in order for the sampling procedure to work
        self.num_partitions = self.capacity / (1.0 * self.num_init_train)
        # Initialize experience buffer
        self.exp_buffer = []
        # Initialize rank priority distributions and stratified sampling cutoffs if needed
        if self.mem_priority:
            # Initialize Priority Queue (will be implemented as a binary heap)
            self.pq = PriorityQueue(capacity=buffer_size)
            self.distributions = {}
            partition_num = 1
            partition_division = self.capacity / self.num_partitions
            for n in np.arange(partition_division, self.capacity + 0.1,
                               partition_division):
                # Set up power-law PDF and CDF
                distribution = {}
                distribution['pdf'] = np.power(np.linspace(1, n, n),
                                               -1 * self.alpha)
                pdf_sum = np.sum(distribution['pdf'])
                distribution['pdf'] = distribution['pdf'] / float(
                    pdf_sum)  # Normalise PDF
                cdf = np.cumsum(distribution['pdf'])
                # Set up strata for stratified sampling (transitions will have varying TD-error magnitudes)
                distribution['strata_ends'] = np.zeros(self.batch_size + 1)
                distribution['strata_ends'][0] = 0  # First index is 0 (+1)
                distribution['strata_ends'][
                    self.batch_size] = n  # Last index is n
                # Use linear search to find strata indices
                stratum = 1.0 / self.batch_size
                index = 0
                for s in range(1, self.batch_size):
                    if cdf[index] >= stratum:
                        index += 1
                    while cdf[index] < stratum:
                        index = index + 1
                    distribution['strata_ends'][s] = index
                    stratum = stratum + 1.0 / self.batch_size  # Set condition for next stratum
                # Store distribution
                self.distributions[partition_num] = distribution
                partition_num = partition_num + 1

    def get_size(self):
        """Return the number of elements in the experience buffer."""
        return self.size

    def store(self, exp, priority=None):
        """Stores a single transition tuple in the experience buffer.

		Arguments:
		exp -- numpy array containg single transition

		Keyword arguments:
		priority -- priority for this transition; if None, then maximum priority is used (default: None)
		"""
        # Increment size, circling back to begninning if memory limit is reached
        self.size = np.min((self.size + 1, self.capacity))
        if self.index >= self.capacity:
            self.is_full = True
            self.index = 0
        # If prioritized replay, update priority queue
        if self.mem_priority:
            if priority is None:
                # Store with max priority
                priority = self.pq.find_max() or 1
            # Add to priority queue
            if self.is_full:

                self.pq.update_by_val(self.index, priority, self.index)
            else:
                self.pq.insert(priority, self.index)
        # Add experience to buffer
        if self.is_full:
            self.exp_buffer[self.index] = exp
        else:
            self.exp_buffer.append(exp)
        self.index += 1

    def add(self, exp_list):
        """Store observed transitions.

		Arguments:
		exp_list -- list of transitions (each is a numpy array)
		"""
        # loop over experiences and store each one
        for trans_idx in range(len(exp_list)):
            self.store(exp_list[trans_idx])

    def add_with_priorities(self, exp_list, priorities_list):
        """Store observed transitions and associated priorities.
		
		Arguments:
		exp_list -- list of transitions (each is a numpy array)
		priorities_list -- list of priorities (one for each transition)
		"""
        for trans_idx in range(len(exp_list)):
            self.store(exp_list[trans_idx], priorities_list[trans_idx])

    def sample(self, total_steps=0):
        """Sample from the experience buffer by rank prioritization if specified.
		Otherwise sampling is done uniformly.

		Keyword arguments:
		total_steps -- number of steps taken in experiment (default: 0)
		"""

        N = self.size
        num_samples = np.min(
            (self.batch_size * self.num_strata_samples, self.size))

        # Perform uniform sampling of experience buffer
        if not self.mem_priority:
            indices = npr.choice(range(N), replace=False, size=num_samples)
            exp_batch = np.array(self.exp_buffer)[indices]
            weights = np.ones(len(indices)) / (len(indices) * 1.0)
            return np.reshape(exp_batch, (num_samples, -1)), weights, indices
        # Perform prioritized sampling of experience buffer
        else:
            # Find the closest precomptued distribution by size
            dist_idx = math.floor(N / float(self.capacity) *
                                  self.num_partitions)
            distribution = self.distributions[int(dist_idx)]
            N = dist_idx * 100
            rank_indices_set = set()
            # Perform stratified sampling of priority queue
            for i_exp in range(num_samples)[::-1]:
                # To increase the training batch size we sample several times from each strata, repeated indices are eliminated
                rank_indices_set.add(
                    npr.randint(
                        distribution['strata_ends'][int(
                            i_exp / self.num_strata_samples)],
                        distribution['strata_ends'][
                            int(i_exp / self.num_strata_samples) + 1]))
            rank_indices = list(rank_indices_set)
            exp_indices = self.pq.get_values_by_val(rank_indices)
            exp_batch = [
                self.exp_buffer[int(exp_idx)] for exp_idx in exp_indices
            ]

            # Compute importance sampling weights
            beta = np.min([
                self.beta_zero +
                (total_steps - self.num_init_train - 1) * self.beta_grad, 1
            ])
            IS_weights = np.power(N * distribution['pdf'][rank_indices],
                                  -1 * beta)

            # Normalize IS_weights by maximum weight, guarantees that IS weights only scale downwards
            w_max = np.max(IS_weights)
            IS_weights = IS_weights / float(w_max)
            return np.reshape(exp_batch,
                              (len(exp_indices), -1)), IS_weights, exp_indices

    def rand_unif_sample(self, n):
        """Returns a random uniform sample of n experiences.
		
		Arguments:
		n -- number of transitions to sample
		"""
        indices = npr.choice(range(self.size), replace=False, size=n)
        exp_batch = np.array(self.exp_buffer)[indices]
        return np.reshape(exp_batch, (n, -1))

    def update_priorities(self, pq_insert_list):
        """Update priorities of sampled transitions.
		
		Arguments:
		pq_insert_list -- list containing [priority, exp_index] for each transition in the sample
		"""
        for i in range(len(pq_insert_list)):
            priority = pq_insert_list[i][0]
            exp_idx = pq_insert_list[i][1]
            self.pq.update_by_val(exp_idx, priority, exp_idx)
Exemplo n.º 17
0
from PriorityQueue import PriorityQueue
from SortedList import SortedList

pq = PriorityQueue([1, 2, 3, 4, 5, 3, 6])
pq.insert(7)
#pq.remove()
#pq.remove()
pq.sort()
pq.display()
Exemplo n.º 18
0
def Prim(g: Grafo):

    if (g.quantidadeVertices == 0):
        return None

    mst = Grafo()

    raiz = ModificacaoPrim(g)  # Modificação
    raiz = random.randint(
        0, (g.quantidadeVertices - 1))  # Vertice Aleatório (Desativar linha)

    print("Vertice Inicial: " + g.N[raiz] + "\n")

    mst.adiciona_vertice(g.N[raiz])

    ligacoesValidas = [0] * g.quantidadeVertices
    for i in range(g.quantidadeVertices):
        ligacoesValidas[i] = graphLibrary.grau(g, g.N[i])

    arestasDaFilaPrincipal = {}
    FilaPrincipal = PriorityQueue()

    aux = 0

    while (mst.quantidadeVertices != g.quantidadeVertices):

        for k in range(aux, len(mst.N)):
            Vertex = mst.N[k]
            idx = g.N.index(Vertex)
            if (ligacoesValidas[idx] == 0):
                continue
            for i in range(idx, len(g.M[idx])):
                if (g.M[idx][i] > 0):  # encontrei um vertice fora da arvore
                    v = g.N[i]  # Possivel novo vertice
                    if not (mst.existe_vertice(v)):
                        pesoArestaEncontrada = g.Mfila[idx][i].seeFist()
                        arestaEncontrada = g.N[idx] + '-' + g.N[i]
                        if pesoArestaEncontrada in arestasDaFilaPrincipal.keys(
                        ):
                            arestasDaFilaPrincipal[
                                pesoArestaEncontrada].append(arestaEncontrada)
                        else:
                            arestasDaFilaPrincipal[pesoArestaEncontrada] = [
                                arestaEncontrada
                            ]
                        FilaPrincipal.insert(pesoArestaEncontrada)

            idxfixo = idx
            while ((idx - 1) >= 0):
                if (g.M[idx - 1][idxfixo] > 0):
                    v = g.N[idx - 1]  # Possivel novo vertice
                    if not (mst.existe_vertice(v)):
                        pesoArestaEncontrada = g.Mfila[idx -
                                                       1][idxfixo].seeFist()
                        arestaEncontrada = g.N[idx - 1] + '-' + g.N[idxfixo]
                        if pesoArestaEncontrada in arestasDaFilaPrincipal.keys(
                        ):
                            arestasDaFilaPrincipal[
                                pesoArestaEncontrada].append(arestaEncontrada)
                        else:
                            arestasDaFilaPrincipal[pesoArestaEncontrada] = [
                                arestaEncontrada
                            ]
                        FilaPrincipal.insert(pesoArestaEncontrada)
                idx -= 1

        menorPesoAresta = FilaPrincipal.remove()
        novaAresta = arestasDaFilaPrincipal[menorPesoAresta].pop()

        v1 = novaAresta[0]
        v2 = novaAresta[-1]

        novoVertice = None
        if not mst.existe_vertice(v1):
            novoVertice = v1
            aux += 1
        if not mst.existe_vertice(v2):
            novoVertice = v2
            aux += 1

        if (novoVertice != None):
            mst.adiciona_vertice(novoVertice)
            mst.adiciona_aresta(novaAresta, menorPesoAresta)
            idxV1 = g.N.index(v1)
            idxV2 = g.N.index(v2)
            ligacoesValidas[idxV1] -= 1
            ligacoesValidas[idxV2] -= 1

    return mst