Example #1
0
 def mergeKLists(self, lists):
     """
     :type lists: List[ListNode]
     :rtype: ListNode
     """
     if not lists:
         return None
     st = PriorityQueue()
     for x in lists:
         if x:
             st.push(x, x.val)
     if st.size() == 0:
         return None
         
     head = tail = None
     while st.size() != 0:
         ptr = st.pop()
         if ptr.next:
             st.push(ptr.next, ptr.next.val)
         ptr.next = None
         
         if ptr == None:
             continue
         if tail == None:
             head = ptr
         else:
             tail.next = ptr
         tail = ptr
         tail.next = None
     return head
Example #2
0
def best_first_search(problem, f):
    "Search nodes with minimum f(node) value first."
    frontier = PriorityQueue([Node.Node(problem.initial)], key=f)
    reached = {}
    while frontier:
        node = frontier.pop()
        if problem.is_goal(node.state):
            return node, len(frontier)
        for child in Node.expand(problem, node):
            s = child.state
            if s not in reached or child.path_cost < reached[s].path_cost:
                reached[s] = child
                frontier.add(child)
    return failure, None
Example #3
0
	def mergeKLists(self, lists):
		"""
		:type lists: List[ListNode]
		:rtype: ListNode
		"""
		pq = PriorityQueue()
		dummy = ListNode(0)
		curr = dummy
		for node in lists:
			if node:
				pq.add((curr, curr.val))
		while pq:
			node = pq.pop()
			curr.next = ListNode(node.val)
			curr = curr.next
			pq.add((node.next, node.next.val))
Example #4
0
def djikstra(start,end,edge_weight):

    path_weight = {node: float('inf') if node != start else 0 for node in adj_list}
    previous    = {node: None for node in adj_list}
    pqueue      = PriorityQueue()
    pqueue.put((0,start))

    while not pqueue.empty():
        node = pqueue.pop()[1]
        for child in node.children:
            if path_weight[node] + edge_weight[(node,child)] < path_weight[child]:
                path_weight[child] = path_weight[node] + edge_weight[(node,child)]
                # TODO update pqueue path_weight of child
                try:
                    pqueue.get(child)
                except:
                    # do nothing
                pqueue.put((path_weight[child],child))
                previous[child] = node
Example #5
0
    def mergeKLists(self, lists):
        """
        :type lists: List[ListNode]
        :rtype: ListNode
        """
        from Queue import PriorityQueue
        if not lists:
            return None
        pq = PriorityQueue()
        for lst in lists:
            if lst:
                pq.put((lst.val, lst))

        dummy = cur = ListNode(None)
        while not pq.empty():
            v, nd = pq.pop()
            cur.next = nd
            cur = cur.next
            if nd.next:
                pq.put((nd.next.val, nd.next))
        return dummy.next
Example #6
0
class Solver:
    def __init__(self):
        self.total_iters = 0
        self.queue = None
        self.visited = None
        self.visited_directions = None
        self.puzzle_state = None
        self.heuristic = None
        self.method = None
        self.methods = {
            'bfs': self.bfs,
            'dfs': self.dfs,
            'id_dfs': self.id_dfs,
            'a_star': self.a_star
        }

    def search(self, method):
        self.reset_puzzle(method)

        start = dt.datetime.now()
        search_outcome = self.methods[method]()
        end = dt.datetime.now()

        if self.puzzle_state:
            moves = self.puzzle_state.moves
            directions = self.puzzle_state.directions if len(self.puzzle_state.directions) < 50 else []
        else:
            moves = None
            directions = None

        result = SearchResult(
                search_outcome,
                self.total_iters,
                len(self.queue),
                moves,
                directions,
                end - start
        )

        return result

    def log_state(self):
        if self.total_iters % 20000 == 0:
            stdout.write(".")
            stdout.flush()

    def reset_puzzle(self, method):
        self.total_iters = 0
        self.puzzle_state = BlocksworldPuzzle(setup.generate_puzzle())
        self.visited = set()
        self.visited_directions = {}

        if method == 'bfs':
            self.queue = deque([self.puzzle_state])
        elif method == 'dfs':
            self.queue = [self.puzzle_state]
            self.visited = {}
        elif method == 'id_dfs':
            self.queue = [self.puzzle_state]
            self.visited = {}
        elif method == 'a_star':
            self.queue = PriorityQueue()
            self.queue.put((0, self.puzzle_state))
            self.heuristic = lambda heur: heur.total_manhattan_distance()

    # Breadth-first search.
    # FIFO queue -- Double Ended Queue(DEQUEUE)
    # GET - left | ADD - right.
    def bfs(self):
        while len(self.queue) > 0:
            self.total_iters += 1
            self.log_state()
            self.visited_directions = {}

            # Get nearest(more shallow state.
            self.puzzle_state = self.queue.popleft()
            # Add current state to visited ones
            self.visited.add(self.puzzle_state.puzzle_hash())
            self.visited_directions[self.puzzle_state.puzzle_hash()] = self.puzzle_state.directions

            if self.puzzle_state.is_goal_state():
                return True

            def is_visited(state, visited):
                return state.puzzle_hash() in visited

            # Queue extend with not visited
            valid_children = []
            for child_state in self.puzzle_state.get_children():
                if not is_visited(child_state, self.visited):
                    valid_children.append(child_state)
            self.queue.extend(valid_children)

        # No result.
        return False

    # Depth First Search.
    # Not optimal solution.
    # It can go on infinitely.
    # LIFO Queue --> python list()
    def dfs(self):
        self.queue = [self.puzzle_state]
        # Visited : {state_hash:state_depth}
        self.visited = {}

        while len(self.queue) > 0:
            # Stopping in 1000000 iterations in case of infinite loop
            if self.total_iters > 1000000:
                return False
            self.total_iters += 1
            self.log_state()

            # Get the state that is deepest
            self.puzzle_state = self.queue.pop()  # Get deepest state.
            # Add current state in visited.
            self.visited[self.puzzle_state.puzzle_hash()] = self.puzzle_state.moves
            self.visited_directions[self.puzzle_state.puzzle_hash()] = self.puzzle_state.directions

            if self.puzzle_state.is_goal_state():
                return True

            def is_visited(child, visited):
                return child.puzzle_hash() in visited

            valid_children = []
            for child_state in self.puzzle_state.get_children():
                if not is_visited(child_state, self.visited) \
                        or self.visited[child_state.puzzle_hash()] > child_state.moves:
                    valid_children.append(child_state)
            self.queue.extend(valid_children)

        # No result
        return False

    # Iterative deepening depth-first search.
    # The solution is more optimal than DFS.
    # It is faster than BFS
    # It can go on infinitely, like DFS.
    # LIFO Queue --> python list()
    def id_dfs(self):
        start_state = self.puzzle_state

        for depth in itertools.count():
            self.queue = [start_state]
            self.visited = {}

            while len(self.queue) > 0:
                self.total_iters += 1
                self.log_state()

                # Get the state that is the deepest.
                self.puzzle_state = self.queue.pop()
                # Add current state to visited ones.
                self.visited[self.puzzle_state.puzzle_hash()] = self.puzzle_state.moves
                self.visited_directions[self.puzzle_state.puzzle_hash()] = self.puzzle_state.directions

                if self.puzzle_state.is_goal_state():
                    return True

                def is_visited(child, visited):
                    return child.puzzle_hash() in visited

                if self.puzzle_state.moves < depth:
                    valid_children = []
                    for child_state in self.puzzle_state.get_children():
                        if not is_visited(child_state, self.visited) \
                                or self.visited[child_state.puzzle_hash()] > child_state.moves:
                            valid_children.append(child_state)
                    self.queue.extend(valid_children)

        return False

    # A* search.
    # Most optimal solution time-wise.
    # QUEUE: PRIORITY QUEUE(when _get() is used, it gets the item with the minimum value first)
    # ORDER: Heuristic(Total Manhattan Distance + number of state moves)
    def a_star(self):
        # Total_moves = total so far + total to be made
        def total_moves(puzzle_state, heuristic):
            return puzzle_state.moves + heuristic(self.puzzle_state)

        def state_cost_tuple(puzzle_state, heuristic):
            return total_moves(puzzle_state, heuristic), puzzle_state

        def valid_children(puzzle_state, visited):
            children = []
            for child_state in puzzle_state.get_children():
                if child_state.puzzle_hash() not in visited:
                    children.append(child_state)
            return children

        while not self.queue.empty():
            self.total_iters += 1
            self.log_state()

            # Get state with lowest value(moves left)
            self.puzzle_state = self.queue.get()[1]
            # Add state to visited
            self.visited.add(self.puzzle_state.puzzle_hash())
            self.visited_directions[self.puzzle_state.puzzle_hash()] = self.puzzle_state.directions

            if self.puzzle_state.is_goal_state():
                self.queue = self.queue.queue
                return True

            for valid_child in valid_children(self.puzzle_state, self.visited):
                self.queue.put(state_cost_tuple(valid_child, self.heuristic))

        # No result
        self.queue = self.queue.queue
        return False
Example #7
0
class Solver:
    def __init__(self):
        self.total_iters = 0
        self.queue = None
        self.visited = None
        self.visited_directions = None
        self.puzzle_state = None
        self.heuristic = None
        self.method = None
        self.methods = {
            'bfs': self.bfs,
            'dfs': self.dfs,
            'id_dfs': self.id_dfs,
            'a_star': self.a_star
        }

    def search(self, method):
        self.reset_puzzle(method)

        start = dt.datetime.now()
        search_outcome = self.methods[method]()
        end = dt.datetime.now()

        if self.puzzle_state:
            moves = self.puzzle_state.moves
            directions = self.puzzle_state.directions if len(
                self.puzzle_state.directions) < 50 else []
        else:
            moves = None
            directions = None

        result = SearchResult(search_outcome, self.total_iters,
                              len(self.queue), moves, directions, end - start)

        return result

    def log_state(self):
        if self.total_iters % 20000 == 0:
            stdout.write(".")
            stdout.flush()

    def reset_puzzle(self, method):
        self.total_iters = 0
        self.puzzle_state = BlocksworldPuzzle(setup.generate_puzzle())
        self.visited = set()
        self.visited_directions = {}

        if method == 'bfs':
            self.queue = deque([self.puzzle_state])
        elif method == 'dfs':
            self.queue = [self.puzzle_state]
            self.visited = {}
        elif method == 'id_dfs':
            self.queue = [self.puzzle_state]
            self.visited = {}
        elif method == 'a_star':
            self.queue = PriorityQueue()
            self.queue.put((0, self.puzzle_state))
            self.heuristic = lambda heur: heur.total_manhattan_distance()

    # Breadth-first search.
    # FIFO queue -- Double Ended Queue(DEQUEUE)
    # GET - left | ADD - right.
    def bfs(self):
        while len(self.queue) > 0:
            self.total_iters += 1
            self.log_state()
            self.visited_directions = {}

            # Get nearest(more shallow state.
            self.puzzle_state = self.queue.popleft()
            # Add current state to visited ones
            self.visited.add(self.puzzle_state.puzzle_hash())
            self.visited_directions[
                self.puzzle_state.puzzle_hash()] = self.puzzle_state.directions

            if self.puzzle_state.is_goal_state():
                return True

            def is_visited(state, visited):
                return state.puzzle_hash() in visited

            # Queue extend with not visited
            valid_children = []
            for child_state in self.puzzle_state.get_children():
                if not is_visited(child_state, self.visited):
                    valid_children.append(child_state)
            self.queue.extend(valid_children)

        # No result.
        return False

    # Depth First Search.
    # Not optimal solution.
    # It can go on infinitely.
    # LIFO Queue --> python list()
    def dfs(self):
        self.queue = [self.puzzle_state]
        # Visited : {state_hash:state_depth}
        self.visited = {}

        while len(self.queue) > 0:
            # Stopping in 1000000 iterations in case of infinite loop
            if self.total_iters > 1000000:
                return False
            self.total_iters += 1
            self.log_state()

            # Get the state that is deepest
            self.puzzle_state = self.queue.pop()  # Get deepest state.
            # Add current state in visited.
            self.visited[
                self.puzzle_state.puzzle_hash()] = self.puzzle_state.moves
            self.visited_directions[
                self.puzzle_state.puzzle_hash()] = self.puzzle_state.directions

            if self.puzzle_state.is_goal_state():
                return True

            def is_visited(child, visited):
                return child.puzzle_hash() in visited

            valid_children = []
            for child_state in self.puzzle_state.get_children():
                if not is_visited(child_state, self.visited) \
                        or self.visited[child_state.puzzle_hash()] > child_state.moves:
                    valid_children.append(child_state)
            self.queue.extend(valid_children)

        # No result
        return False

    # Iterative deepening depth-first search.
    # The solution is more optimal than DFS.
    # It is faster than BFS
    # It can go on infinitely, like DFS.
    # LIFO Queue --> python list()
    def id_dfs(self):
        start_state = self.puzzle_state

        for depth in itertools.count():
            self.queue = [start_state]
            self.visited = {}

            while len(self.queue) > 0:
                self.total_iters += 1
                self.log_state()

                # Get the state that is the deepest.
                self.puzzle_state = self.queue.pop()
                # Add current state to visited ones.
                self.visited[
                    self.puzzle_state.puzzle_hash()] = self.puzzle_state.moves
                self.visited_directions[self.puzzle_state.puzzle_hash(
                )] = self.puzzle_state.directions

                if self.puzzle_state.is_goal_state():
                    return True

                def is_visited(child, visited):
                    return child.puzzle_hash() in visited

                if self.puzzle_state.moves < depth:
                    valid_children = []
                    for child_state in self.puzzle_state.get_children():
                        if not is_visited(child_state, self.visited) \
                                or self.visited[child_state.puzzle_hash()] > child_state.moves:
                            valid_children.append(child_state)
                    self.queue.extend(valid_children)

        return False

    # A* search.
    # Most optimal solution time-wise.
    # QUEUE: PRIORITY QUEUE(when _get() is used, it gets the item with the minimum value first)
    # ORDER: Heuristic(Total Manhattan Distance + number of state moves)
    def a_star(self):
        # Total_moves = total so far + total to be made
        def total_moves(puzzle_state, heuristic):
            return puzzle_state.moves + heuristic(self.puzzle_state)

        def state_cost_tuple(puzzle_state, heuristic):
            return total_moves(puzzle_state, heuristic), puzzle_state

        def valid_children(puzzle_state, visited):
            children = []
            for child_state in puzzle_state.get_children():
                if child_state.puzzle_hash() not in visited:
                    children.append(child_state)
            return children

        while not self.queue.empty():
            self.total_iters += 1
            self.log_state()

            # Get state with lowest value(moves left)
            self.puzzle_state = self.queue.get()[1]
            # Add state to visited
            self.visited.add(self.puzzle_state.puzzle_hash())
            self.visited_directions[
                self.puzzle_state.puzzle_hash()] = self.puzzle_state.directions

            if self.puzzle_state.is_goal_state():
                self.queue = self.queue.queue
                return True

            for valid_child in valid_children(self.puzzle_state, self.visited):
                self.queue.put(state_cost_tuple(valid_child, self.heuristic))

        # No result
        self.queue = self.queue.queue
        return False
Example #8
0
    b =[5,2,9,4]
    a = PriorityQueue()
    heapq.heappush(b, 3)
    heapq.heapify(b) #once heapify, then the heap will maintain the order forever
    print heapq.heappop(b)
    print heapq.heappop(b)
    heapq.heappush(b,1)
    print b[0]
    print b[1]
    print len(b)

    print("*******************")

    nums = [1, 8, 2, 23, 7, -4, 18, 23, 42, 37, 2]
    print heapq.nlargest(3, nums)
    print nums

    a = deque([])
    a.append(6)
    a.append(5)
    a.append(4)
    print a
    print a.pop()
    print a.popleft()
    print a[0]
    print len(a)




Example #9
0
class Simulator(object):
    def __init__(self,
            stats = None,
            render = None) :
        self.waiting = PriorityQueue()
        self.server = Server()
        self.active = None
        self.events = EventList()
        self.clock = 0
        self.tasks = []
        self.until = 0

        # Statistics
        if stats is not None:
            self.stats = True
            self.stats_file = open(stats, "wb")
            self.stats_writer = csv.writer(self.stats_file, delimiter='|', lineterminator='\n')
            self.write_headers()
        else:
            self.stats = False

        # Rendering
        if render is not None:
            self.render = True
            self.render_file = render
            self.render_data = dict()
        else:
            self.render = False

    def update(self):
        """
        Update simulator state after any event.
        """
        # Which instance to execute ?
        self.selectInstance()

        # If there is no instance, nothing to do
        if self.active is None:
            return
        else:
            # Compute finish event
            self.events.put(self.active.event(self.clock))

            # Compute server event
            self.events.put(self.server.event(self.clock))

    def advance(self, time):
        """
        Advance the simulator to the given time.
        """
        if self.active is not None:
            self.active.advance(self.clock, time)

        self.server.advance(self.clock, time)

        self.clock = time

    def selectInstance(self):
        """
        Select (set the variables) the instance to execute on the simulator
        according to the state of various entities.
        """
        if self.server.state != Server.WAITING:
            if self.waiting.isEmpty():
                self.active = None
            else:
                self.active = self.waiting.first()
        elif self.waiting.isEmpty():
            self.active = self.server.activate()
        else:
            if self.waiting.first().priority > self.server.priority:
                self.active = self.waiting.first()
            else:
                self.active = self.server.activate()

    def reactArrival(self, event):
        # Put the new instance on its waiting list
        if event.instance.type == Instance.SOFT:
            self.server.put(event.instance)
        elif event.instance.type == Instance.HARD:
            self.waiting.put(event.instance, event.instance.priority)

        # Compute the next arrival
        self.events.put(event.instance.task.nextArrival(event.time))

    def reactFinish(self, event):
        # Remove the instance from its waiting list
        if event.instance.type == Instance.SOFT:
            self.server.pop()
        elif event.instance.type == Instance.HARD:
            self.waiting.pop()

        # Compute statistics
        if self.stats :
            event.instance.statistics()
            self.write_instance(event.instance)

        # Compute rendering
        if self.render :
            task = event.instance.task
            if not task.id in self.render_data:
                self.render_data[task.id] = dict()
                self.render_data[task.id]["executed"] = list()
                self.render_data[task.id]["arrivals"] = list()

            for t in event.instance.executed:
                self.render_data[task.id]["executed"].append(t)
            self.render_data[task.id]["arrivals"].append(event.instance.arrival)

    def reactSuspend(self, event):
        # Suspend the server
        self.server.suspend()

    def reactRefill(self, event):
        # Refill the server
        self.server.refill()
        self.events.put(self.server.nextRefill(event.time))

    def read(self, filename):
        """
        Load a configuration from a file.
        """
        r = ReadConfig()
        r.read(filename)

        self.server = r.server
        self.tasks = r.tasks

    def init(self, until = -1):
        """
        Initialize the server to run until the given time.
        If -1 is given, run until the LCM of periodic tasks.
        """

        logging.debug("Initialization until " + str(until))

        if (until == -1):
            until = PeriodicTask.lcm(self.tasks)

        for t in self.tasks:
            self.events.put(t.nextArrival(0));

        self.events.put(self.server.nextRefill(0))

        self.until = until

    def run(self):
        """
        Execute the simulation.
        """
        next = self.events.next()
        while next.time < self.until:
            self.advance(next.time)

            logging.debug(str(self.clock) + ": Event " + next.type + " "
                          + (next.instance.task.id
                              if next.instance is not None else ""))

            if next.type == Event.ARRIVAL:
                self.reactArrival(next)
            elif next.type == Event.FINISH:
                self.reactFinish(next)
            elif next.type == Event.SUSPEND:
                self.reactSuspend(next)
            elif next.type == Event.REFILL:
                self.reactRefill(next)

            self.update()
            next = self.events.next()

        if self.stats:
            self.write_footers()

        if self.render:
            self.rendering()

    def write_headers(self):
        """
        Write the results to a file.
        """
        col = 6

        # Write some comments on the head of the file
        width = 9*(col + 1)

        self.stats_file.write("Simulator results")

        today = date.today()
        self.stats_file.write(("Date : " +
                    str(today.day) + "/" +
                    str(today.month) + "/" +
                    str(today.year) + "\n").rjust(width - 17))
        self.stats_file.write("\n")

        # Write the table headers
        self.stats_writer.writerow(["id"[0:col].center(col),
                         "type"[0:col].center(col),
                         "arrival"[0:col].center(col),
                         "start"[0:col].center(col),
                         "finish"[0:col].center(col),
                         "computation"[0:col].center(col),
                         "idle"[0:col].center(col),
                         "deadline"[0:col].center(col),
                         "TTD"[0:col].center(col)])

    def write_instance(self, instance):
        col = 20
        self.stats_writer.writerow([instance.task.id[0:col].rjust(col),
                         instance.type[0:col].rjust(col),
                         str(instance.arrival)[0:col].rjust(col),
                         str(instance.start)[0:col].rjust(col),
                         str(instance.finish)[0:col].rjust(col),
                         str(instance.computation)[0:col].rjust(col),
                         str(instance.idle)[0:col].rjust(col),
                         str(instance.deadline)[0:col].rjust(col),
                         str(instance.time_to_deadline)[0:col].rjust(col)])

    def write_footers(self):
        col = 6
        width = 9*(col + 1)
        self.stats_file.write(("Runtime : " + str(self.clock) + "\n").rjust(width))

        self.stats_file.write("Server : " +
                    str(self.server.__class__.__name__))
        self.stats_file.write("\n")

        if isinstance(self.server, PollingServer):
            self.stats_file.write("Capacity : " +
                        str(self.server.capacity) +
                        " Period : " +
                        str(self.server.period) + "\n")
        self.stats_file.close()



    def rendering(self):
        """
        Render the graph of execution.
        """

        plt.clf()

        plt.subplot('311')
        y_pos = np.arange(len(self.render_data))

        i = 0
        for key, data in self.render_data.iteritems():
            for time in data["executed"]:
                plt.barh(i-0.5, time[1] - time[0], left = time[0], height=1.0)
            for arrival in data["arrivals"]:
                plt.plot(arrival, i, "r+")
            i += 1

        plt.yticks(y_pos, [key for key in self.render_data])

        #print self.server.stats["ctimes"]

        plt.subplot('312')
        plt.plot(self.server.stats["ctimes"], self.server.stats["cvalues"])

        plt.subplot('313')
        plt.plot(self.server.stats["itimes"], self.server.stats["ivalues"])
        plt.savefig(self.render_file)