Example #1
0
class MyStack:
    def __init__(self):
        """
        Initialize your data structure here.
        """
        self.queue = Queue()

    def push(self, x: int) -> None:
        """
        Push element x onto stack.
        """
        self.queue.append(x)

    def pop(self) -> int:
        """
        Removes the element on top of the stack and returns that element.
        """
        return self.queue.get()

    def top(self) -> int:
        """
        Get the top element.
        """
        if self.queue:
            return self.queue[-1]
        return None

    def empty(self) -> bool:
        """
        Returns whether the stack is empty.
        """
        return len(self.queue) == 0
Example #2
0
 def print_keys(self):
     if not self.size:
         print('')
         return
     from queue import Queue
     q = Queue()
     q.append(self.head)
     count = 1
     print(self.head.key)
     while q.size:
         iteration = 0
         for _ in range(count):
             if iteration == 0:
                 count = 0 
             cur = q.popleft()
             if cur is None:
                 return
             if [cur.left, cur.right] == [None, None]:
                 continue
             for nei in [cur.left, cur.right]:
                 if nei is not None:
                     q.append(nei)
                     count += 1 
             iteration += 1
         tmp = q.first
         while tmp is not None:
             print(tmp.value.key, end="  ")
             tmp = tmp.next
         print()
class CallCenter:
    def __init__(self):
        self.active_calls = []
        self.waiting_calls = Queue()
        self.respondents = []
        self.free_respondents = Queue()
        self.managers = []
        self.directors = []

    def dispatch_call(self, call):
        '''dispatches a new call'''
        if len(self.free_respondents) == 0:
            self.waiting_calls.enqueue(call)
            return  # all respondents are currently busy, please wait

        self._dispatch_call(call)

    def escalate(self, call):
        '''escalates a call to the next employee level. can be because the employee is busy or
        not equipped to handle the call'''
        current_employee = call.employee
        next_employee = current_employee.boss

        if not next_employee.free:
            next_employee = next_employee.boss  # simplification: assume director is free

        call.employee = next_employee
        next_employee.free = False
        current_employee.free = True
        if current_employee.role == Role.respondent:
            self.free_respondents.append(current_employee)

    def call_end_receiver(self, call):
        '''listens for signal that call has ended'''
        self.active_calls.remove(call)
        call.employee.free = True

    def employee_free_receiver(self, employee):
        '''listens for signal that employee has become free'''
        self.free_respondents.append(employee)
        next_call = self.waiting_calls.pop()
        if next_call:
            self._dispatch_call(next_call)

    def _dispatch_call(self, call):
        if call.employee:
            return  # the call is already dispatched

        free_respondent = self.free_respondents.pop()
        call.employee = free_respondent
        free_respondent.free = False
        call.start()
        self.active_calls.append(call)
Example #4
0
    def get(self, request):
        # получаем все девайсы из бд
        devices = Device.objects.all()
        devices_dict = dict()

        # формируем инвентори и записываем в файл для норнира
        for device in devices:
            devices_dict[device.name] = dict()
            devices_dict[device.name]['hostname'] = device.ip_address
        with open(f"lldp_map/inventory/devices.yaml", 'w') as f:
            yaml.dump(devices_dict, f)

        # переменная для сбора инфы из lldp
        links = Queue()
        # запускаем норнир
        with InitNornir(config_file=f"lldp_map/inventory/config.yaml") as nr:
            # логин/пароль из переменных среды оси
            nr.inventory.defaults.username = os.getenv('DEVNET_USERNAME')
            nr.inventory.defaults.password = os.getenv('DEVNET_PASSWORD')
            # список всех хостов из инвентори
            devices_list = [host.name for host in nr.inventory.hosts.values()]
            # ALL_CONNECTIONS определяет все линки выводить или только между устройствами в БД
            nr.run(task=gather_lldp,
                   devices_list=devices_list,
                   links=links,
                   all_connections=ALL_CONNECTIONS)

        # убираем дубли записей
        unique_links = set(list(links.queue))
        # предыдущий файл топологии переименоываем в topology-old.json
        subprocess.run([
            "mv", "lldp_map/static/topology.json",
            "lldp_map/static/topology-old.json"
        ])
        # записываем актуальную топологию
        with open("lldp_map/static/topology.json", "w") as data_file:
            json.dump(dict(unique_links), data_file, indent=2)

        # строим граф
        build(unique_links)
        # делаем записи линков для отображения в текствовом виде на странице
        links = list()
        for item in unique_links:
            links.append(
                f"{item[0].split()[0]} {item[0].split()[1]} <--> {item[1].split()[1]} {item[1].split()[0]} "
            )

        # рендерим страницу topology.html с картинкой топологии и текстом
        return render(request, 'topology.html', {
            'links': links,
        })
def serve(arrive_time, serve_time):
    # define variables for arrival time and service time (you may change these later if you want)
    # create a new queue
    a = Queue()

    # define variables to keep track of the current time and service time (initialise them to 0)

    has_served_time = 0

    # other required variables
    customer_id = 1

    # this variable controls the access of a customer in the service area

    # it should be turned to True when the customer needs to be served; it remains False otherwise
    lock = False

    # simulation will run for 60 time units (you may change it later if you want)
    for i in range(1, 121):
        # if the customer has not arrived... increment time unit by 1
        if i % arrive_time == 0:  # <your check goes here># increment time based upon the variable# otherwise say that the customer with some unique ID has arrived
            print("The customer with id " + str(customer_id) + " has arrived" +
                  '\t' + str(i))

            # add customer to queue
            a.append(customer_id)
            # change the customer ID
            customer_id += 1
            # open the lock to the service area
            lock = True

        # if the customer has been served
        if lock == True and has_served_time == serve_time:
            b = a.serve()
            if a.count == 0:
                has_served_time = 0  #
            else:
                has_served_time = 1  # in this minute, next customer is served
                # remove customer from queue and print which customer was served

            print("The customer with id " + str(b) + " has been served" +
                  '\t' + str(i))
            # reset service time

            # lock the area
            if a.count == 0:
                lock = False
        # otherwise increment service time
        elif lock == True:
            has_served_time += 1
Example #6
0
class MyQueue(object):
    def __init__(self):
        self.value = Queue()

    def peek(self):  #Enqueue element into the end of the queue.
        self.value.append(object)

    def pop(self):  #dequeue the element at the front of the queue
        tempStack = []
        while self.value.isEmpty() != 1:
            top = self.value.pop()
            tempStack.append(top)

        tempStack.pop

        while tempStack.isEmpty() != 1:
            top = tempStack.value.pop()
            self.value.append(top)

    def put(self, value):  #Print the element at the front of the queue.
        tempStack = []
        while self.value.isEmpty() != 1:
            top = self.value.pop()
            tempStack.append(top)

        frontElement = tempStack.pop()  #pop the element to be printed out
        print(frontElement)  #print the first element from the queue
        self.value.append(frontElement)  #put the element back into the 'queue'

        while tempStack.isEmpty() != 1:
            top = tempStack.value.pop()
            self.value.append(top)
Example #7
0
    def largestValues(self, root: TreeNode):
        if root == None:
            return
        q = Queue(maxsize=0)
        q.put([(root, 0)])
        levels = {}
        while len(q) != 0:
            ele, level = q.get()
            if level not in levels: levels[level] = []
            levels[level].append(ele.val)

            if ele.left is not None: q.append((ele.left, level + 1))
            if ele.right is not None: q.append((ele.right, level + 1))

        return [max(levels[level]) for level in levels.keys()]
Example #8
0
class TaskScheduler:
    def __init__(self):
        self._task_queue = Queue()

    def new_task(self, task):
        self._task_queue.append(task)

    def run(self):
        while self._task_queue:
            task = self._task_queue.popleft()
            try:
                print(next(task))
                self._task_queue.append(task)
            except StopIteration:
                pass
Example #9
0
    def test_append(self):
        Q = Queue()

        Q.append(2)
        self.assertEqual(Q.first.value, 2)
        self.assertEqual(Q.last.value, 2)

        self.assertEqual(Q.first.prev, None)
        self.assertEqual(Q.last.next, None)
        self.assertEqual(str(Q), '[2]')

        Q.addleft(3)
        self.assertEqual(Q.first.value, 3)
        self.assertEqual(Q.last.value, 2)
        self.assertEqual(str(Q), '[3, 2]')
Example #10
0
 def print_in_line(self):
     if not self.size:
         print('')
         return
     from stack import Stack
     from queue import Queue
     q = Queue()
     q.append(self.head)
     A = Stack()
     A.append(self.head.key)
     while q.size:
         cur = q.popleft()
         for nei in [cur.left, cur.right]:
             if nei is not None:
                 q.append(nei)
                 A.append(nei.key)
     return A.arr
Example #11
0
def breadth_first_search(problem):
    """[Figure 3.11]"""
    node = Node(problem.initial)
    if problem.goal_test(node.state):
        return node
    frontier = Queue()
    #frontier=list()
    frontier.append(node)
    explored = set()
    while frontier:
        node = frontier.pop()
        explored.add(node.state)
        for child in node.expand(problem):
            if child.state not in explored and child.state not in frontier.data:
                #if child.state not in explored and child.state not in frontier:
                if problem.goal_test(child.state): return child
                frontier.append(child)

    return None
Example #12
0
	def scrape_websites(self):
		if not self.file:
			print 'no input file. set input file'
			return

		urls_file = open(self.file)
		urls = Queue()

		for url in urls_file.readlines():
			urls.append(url)

		# set of crawled urls
		processed_urls = set()

		# email result set
		emails = set()

		# iterate through urls
		while len(urls):
			# 
			url = urls.popleft()
			processed_urls.add(url)

			# get base url and path to resolve relative links
			parts = urlsplit(url)
			base_url = '{0.scheme}://{0.netloc}'.format(parts)
			path = url[:url.rfind('/')+1] if '/' in parts.path else url

			# get url content
			try:
				response = requests.get(url)
			except (requests.exceptions.MissingSchema, requests.exceptions.ConnectionError):
				# ignore pages with errors
				continue

			# regexp for emails
			new_emails = set(re.findall(r'[a-z0-9\.\-+_]+@[a-z0-9\.\-+_]+\.[a-z]+', response.text, re.I))
			emails.update(new_emails)

		print emails
		return emails
Example #13
0
File: bfs.py Project: Kozik149/SISE
class Bfs:
    def __init__(self, order=()):
        self.order = order
        self.to_search = Queue().queue
        self.searched = Queue().queue
        self.solution = []
        self.solved = False

    def steps(self, root):
        self.to_search.append(root)
        while (len(self.to_search) > 0 and self.solved is False):
            current_vertex = self.to_search.popleft()
            self.searched.append(current_vertex)
            if (current_vertex.goalCheck()):
                self.solved = True
                self.solution = Helper().track(current_vertex)
                break
            current_vertex.make_children(self.order)
            for i in range(len(current_vertex.children)):  #TODO
                if (current_vertex.children[i] not in self.to_search
                        and current_vertex.children[i] not in self.searched):
                    self.to_search.append(current_vertex.children[i])

        WorkWithFile.visited = len(self.searched)
        WorkWithFile.processed = len(self.searched) + len(self.to_search)
        WorkWithFile.deepest = self.solution[0].depth
        return self.solution
    def BFS(self, initial_state):
        start_node = Node(initial_state, None, None, 0)
        Node.num_processed = 0
        start = time.time()  ##
        if start_node.goal_test().all():
            return start_node.find_solution()

        frontier = Queue()  ## List Stack
        frontier.append(start_node)
        explored = set()
        while not (len(frontier) == 0):
            if BFS.searchCanceled:
                print("canceled!")
                return
            self.max_stored = max(self.max_stored,
                                  len(frontier) + len(explored))  ##
            node = frontier.popleft()
            if node.goal_test().all():
                print("***************GOAL STATE FOUND*******************")
                print("\n")
                print(node.display())
                BFS.pathCost = node.depth  ##
                BFS.maxStoredNodes = self.max_stored  ##
                BFS.numProcessedNodes = node.num_processed  ##
                BFS.timeTaken = time.time() - start  ##
                return node.find_solution()

            explored.add(tuple(node.state))
            children = node.generate_child()
            for child in children:
                if BFS.searchCanceled:
                    print("canceled!")
                    return
                if tuple(child.state) not in explored:
                    frontier.append(child)
                    explored.add(tuple(child.state))

        return
Example #15
0
def parse_input(to_q):
    input = read_input("day22.txt")
    if to_q:
        plyr1, plyr2 = Queue(), Queue()
    else:
        plyr1, plyr2 = [], []
    one = True
    for line in input:
        if "Player" in line:
            continue
        if line == "\n":
            one = False
            continue
        if one:
            if to_q:
                plyr1.put(int(line))
            else:
                plyr1.append(int(line))
        else:
            if to_q:
                plyr2.put(int(line))
            else:
                plyr2.append(int(line))
    return plyr1, plyr2
    def reverseLevelOrder(self, root):
        if root:
            another_queue = Queue()
            queue = Queue()
            queue.enqueue(root)

            while (len(queue) > 0):

                root = queue.dequeue()
                another_queue.append(root)

                if (root.right):
                    queue.append(root.right)

                if (root.left):
                    queue.append(root.left)

            while (len(another_queue) > 0):
                root = another_queue.pop()
                print(root.data),
Example #17
0
print(queue.pop(0))
print(queue.pop(0))
print(queue.pop(0))

print("\nQueue after removing elements")
print(queue)

# QUEUE.DEQUE

from collections import deque

# Initializing a queue
q = deque()

# Adding elements to a queue
q.append('a')
q.append('b')
q.append('c')

print("Initial queue")
print(q)

# Removing elements from a queue
print("\nElements dequeued from the queue")
print(q.popleft())
print(q.popleft())
print(q.popleft())

print("\nQueue after removing elements")
print(q)
Example #18
0
class Crawler(object):
    """A web crawler that processes links in a given website, recursively
    following all links on that site. This crawler supports parallel execution.
    Crawling is done by calling crawl with a page parser that queues new tasks
    in this Crawler."""
    def __init__(self, site, timeout, parallel=False):
        self.site = site
        self.timeout = timeout
        self.parallel = parallel
        self.queued = set()  # set of URLs that have already been seen
        if parallel:
            # Synchronize access both to the set of seen URLs and the task queue
            self.queued_lock = Lock()
            self.queue = Queue()
        else:
            self.queue = []
        self.url_count = 0
        self.queue_url(site, site, None)

    def put_task(self, task):
        """Queue the given task in this Crawler."""
        if self.parallel:
            self.queue.put(task)
        else:
            self.queue.append(task)

    def get_task(self):
        """Retrieve a task from this Crawler. The caller should first check that
        tasks remain."""
        if self.parallel:
            return self.queue.get()
        else:
            return self.queue.pop()

    def task_done(self):
        """Inform the Crawler that a task has completed. This should be done
        every time a task is finished."""
        if self.parallel:
            self.queue.task_done()

    def all_done(self):
        """Check whether or not all tasks have completed."""
        if self.parallel:
            # No synchronization needed; unfinished_tasks will never hit 0
            # unless everything is done
            return self.queue.unfinished_tasks == 0
        else:
            return len(self.queue) == 0

    def unsynchronized_already_seen(self, url):
        """Check if a URL has already been seen, adding it to the set of seen
        URLs if not already there. Access to the set should be synchronized by
        the caller if necessary."""
        if not url or url in self.queued:
            return True
        self.queued.add(url)
        self.url_count += 1
        return False

    def already_seen(self, url):
        """Check if the given URL has already been seen. Locks access to the set
        of seen URLs if crawling is being done in parallel."""
        if self.parallel:
            with self.queued_lock:  # lock access to set
                return self.unsynchronized_already_seen(url)
        else:
            return self.unsynchronized_already_seen(url)

    def queue_url(self, url, base, parent):
        url = make_url(url, base)
        if self.already_seen(url):
            return

        read = url.startswith(self.site)
        print(url, self.site, read)
        index = url.rindex('/')
        page = url[index + 1:]
        index = page.rfind('.')
        if index >= 0:
            ext = page[index + 1:]
            # if there is an extension, and the file type is not html, pass
            if ext and ext != 'html' and ext != 'htm':
                read = False

        self.put_task((url, parent, read))

    def handle_url(self, url_info, parser):
        """Process the URL specified by url_info with the given parser. Messages
        produced by this method are intentionally unsynchronized."""
        url, parent, read = url_info
        print('handling:', url)

        # Request, but don't read the page
        try:
            response = requests.get(url, timeout=self.timeout)
        except (HTTPError, URLError, ConnectionError) as e:
            print('bad link in {0}: {1}'.format(parent, url))
            print('error:', e)
            return

        if not read:
            return

        # Now read the page and send data to the parser
        parser.reset_with_page(response.url)
        try:
            data = str(response.content)
            parser.feed(data)
        except Exception as e:
            print('error while reading {0}: {1}'.format(url, e))

    def crawl(self, parser):
        """Crawl the site with the given parser."""
        while not self.all_done():
            self.handle_url(self.get_task(), parser)
            self.task_done()
Example #19
0
class Crawler(object):
    """A web crawler that processes links in a given website, recursively
    following all links on that site. This crawler supports parallel execution.
    Crawling is done by calling crawl with a page parser that queues new tasks
    in this Crawler."""
    def __init__(self, site, timeout, parallel=False):
        self.site = site
        self.timeout = timeout
        self.parallel = parallel
        self.queued = set() # set of URLs that have already been seen
        if parallel:
            # Synchronize access both to the set of seen URLs and the task queue
            self.queued_lock = Lock()
            self.queue = Queue()
        else:
            self.queue = []
        self.url_count = 0
        self.queue_url(site, site, None)

    def put_task(self, task):
        """Queue the given task in this Crawler."""
        if self.parallel:
            self.queue.put(task)
        else:
            self.queue.append(task)

    def get_task(self):
        """Retrieve a task from this Crawler. The caller should first check that
        tasks remain."""
        if self.parallel:
            return self.queue.get()
        else:
            return self.queue.pop()

    def task_done(self):
        """Inform the Crawler that a task has completed. This should be done
        every time a task is finished."""
        if self.parallel:
            self.queue.task_done()

    def all_done(self):
        """Check whether or not all tasks have completed."""
        if self.parallel:
            # No synchronization needed; unfinished_tasks will never hit 0
            # unless everything is done
            return self.queue.unfinished_tasks == 0
        else:
            return len(self.queue) == 0

    def unsynchronized_already_seen(self, url):
        """Check if a URL has already been seen, adding it to the set of seen
        URLs if not already there. Access to the set should be synchronized by
        the caller if necessary."""
        if not url or url in self.queued:
            return True
        self.queued.add(url)
        self.url_count += 1
        return False

    def already_seen(self, url):
        """Check if the given URL has already been seen. Locks access to the set
        of seen URLs if crawling is being done in parallel."""
        if self.parallel:
            with self.queued_lock: # lock access to set
                return self.unsynchronized_already_seen(url)
        else:
            return self.unsynchronized_already_seen(url)

    def queue_url(self, url, base, parent):
        """Queue the givn URL for reading, if it hasn't been seen before."""
        url = make_url(url, base) # construct and/or simplify the URL
        if self.already_seen(url):
            return

        # Only read the page if it is on this site and is HTML
        read = url.startswith(self.site)
        index = url.rindex('/')
        page = url[index+1:]
        index = page.rfind('.')
        if index >= 0:
            ext = page[index+1:]
            if ext != 'html' and ext != 'htm':
                read = False

        # Safely queue a new task to process the URL
        self.put_task((url, parent, read))

    def handle_url(self, url_info, parser):
        """Process the URL specified by url_info with the given parser. Messages
        produced by this method are intentionally unsynchronized."""
        url, parent, read = url_info
        print('handling:', url)

        # Request, but don't read the page
        try:
            opened = urlopen(url, timeout=self.timeout)
        except (HTTPError, URLError, socket.timeout) as e:
            print('bad link in {0}: {1}'.format(parent, url))
            print('error:', e)
            return

        if not read:
            return

        # Now read the page and send data to the parser
        parser.reset_with_page(opened.geturl())
        try:
            data = opened.read().decode()
            parser.feed(data)
        except Exception as e:
            print('error while reading {0}: {1}'.format(url, e))

    def crawl(self, parser):
        """Crawl the site with the given parser."""
        while not self.all_done():
            self.handle_url(self.get_task(), parser)
            self.task_done()
Example #20
0
 def test_str(self):
     Q = Queue([3, 9, 0, 4])
     Q.append(2)
     self.assertEqual(str(Q), '[3, 9, 0, 4, 2]')
Example #21
0
while len(Q) > 0:
    Q.pop(0)
endlog()

log("queue.Queue 사용")
Q = Queue()
for i in range(SIZE):
    Q.put(i)

while not Q.empty():
    Q.get()
endlog()

log("collections.deque 사용")
Q = deque()
for i in range(SIZE):
    Q.append(i)

while len(Q) > 0:
    Q.popleft()
endlog()

log("List 인덱싱")
Q = myQ(SIZE)
for i in range(SIZE):
    Q.push(i)

while not Q.empty():
    Q.pop()
endlog()
class TextInputLoop(BackendLoop):
    def __init__(self,
                 backend_interface=(),
                 use_widget=False,
                 n_lines=20,
                 widget_name="Input:",
                 check_delay=.15,
                 text_height="450px"):
        """
        :param BackendInterface backend_interface:
        """
        self.n_lines = n_lines
        self.use_widget = use_widget

        # Connect interface
        if isinstance(backend_interface, Iterable):
            backend_interface = tuple(backend_interface)
        elif not isinstance(backend_interface, tuple):
            backend_interface = (backend_interface, )
        super().__init__(*backend_interface)

        # Fields
        self._current_loop_nr = self._start_time = self._str_lines = self._widget = None

        # If we are doing widgets
        if use_widget:
            self.check_delay = check_delay

            # Event for whether the main thread is ready to process data
            self.main_thread_ready = Event()
            self.main_thread_ready.set()

            # Make queues for sending and receiving data
            self.ch_in_queue = Queue()
            self.ch_out_queue = Queue()

            # Make checker for ensuring what data is processed
            self.checker = UpdateChecker(
                in_queue=self.ch_in_queue,
                out_queue=self.ch_out_queue,
                main_thread_ready=self.main_thread_ready,
                queue_timout=self.check_delay)
            self.checker.daemon = True
            self.checker.start()

            # Set a timer for checking output
            self.timer = Timer(check_delay, self.check_output)
            self.timer.start()

            self._widget_label = widgets.Label(
                value=widget_name,
                margin="0px 0px 0px 0px",
            )
            self._widget = widgets.Textarea(
                value='',
                placeholder='',
                description="",
                disabled=False,
                layout=dict(width="90%",
                            height=text_height,
                            margin="-3px 0px 0px 0px"),
            )
            self._widget.observe(self._widget_update, )

    def check_output(self):
        # If main-thread has been asked to process data
        if not self.main_thread_ready.is_set():
            # Get data
            text = self.ch_out_queue.get()

            # Check if text has changed (some event does not change the text)
            if text != self.current_str:

                # Break into lines and pass onto storage
                self._str_lines = []
                for line in text.split("\n"):
                    self._str_lines.append(line)

                # Loop nr
                self._current_loop_nr += 1

                # Update through interface
                self.interface_loop_step()

            # TODO: Stop-checks and finalizing missing

            # Main thread is once again ready for data
            self.main_thread_ready.set()

        # Set a time for checking for data
        self.timer = Timer(self.check_delay, self.check_output)
        self.timer.start()

    def _start(self):
        # Initialize
        if self.n_lines > 0 and not self.use_widget:
            self._str_lines = Queue()
        else:
            self._str_lines = []

        # Set time of start and loop nr
        self._start_time = time()
        self._current_loop_nr = 0

        # Initialize parts through interfaces
        self.interface_loop_initialization()

        # Run console
        if self.use_widget:
            self._widget_update()
        else:
            self._console_run()

    def _widget_update(self, _=None):
        # Get text from widget
        text = self._widget.value

        # Send to thread
        self.ch_in_queue.put(text)

    def _console_run(self):
        quit_flat = "$quit"

        text_indent = "\t\t"

        print("-" * 100)
        print(text_indent + "Enter/Paste your content.")
        print(text_indent + "To stop, try Ctrl-D or write: ")
        print(text_indent + "\t{}".format(quit_flat))
        print("-" * 100)

        running = True
        while running:
            try:
                lines = input()
            except (EOFError, KeyboardInterrupt):
                break

            # Check for quit
            if lines.strip() == quit_flat:
                break

            # Split into lines
            lines = lines.split("\n")

            # Store line
            for line in lines:
                if isinstance(self._str_lines, Queue):
                    self._str_lines.put(line)
                else:
                    self._str_lines.append(line)

            # If using queue remove excessive lines
            if isinstance(self._str_lines, Queue):
                while self._str_lines.qsize() > self.n_lines:
                    self._str_lines.get()

            # Loop nr
            self._current_loop_nr += 1

            # Update through interface
            self.interface_loop_step()

            # Check for end
            if self.interface_loop_stop_check():
                running = False

        # Finalize interfaces
        self.interface_finalize()

        # TODO: Also use interrupt-handler

    def start(self):
        super().start()
        self.display()

    # noinspection PyTypeChecker
    def display(self):
        display(self._widget_label)
        display(self._widget)

    @property
    def widget(self):
        return self._widget

    @property
    def current_str(self) -> str:
        return "\n".join(self.string_lines)

    @property
    def string_lines(self) -> list:
        if isinstance(self._str_lines, Queue):
            return list(self._str_lines.queue)
        return self._str_lines

    @property
    def current_loop_nr(self) -> int:
        return self._current_loop_nr

    @property
    def start_time(self) -> float:
        return self._start_time
class GraphAlgo:
    def __init__(self, problem):
        self.f = Queue()
        self.f.put(problem.initialState())
        self.e = []
        self.problem = problem
        self.pathCost = 0
        self.maxMemoryUsage = 0
        self.seenNodes = 0
        self.expandedNodes = 0
        self._depth = 0

    def showResult(self,
                   start,
                   pathCostNotCalculate=False,
                   bidirectionalNode=None
                   ):  #bidirectionalNode is for bidirectionalSearch
        path = []
        actions = []
        while start.parent != None:
            path.append(start.state)
            actions.append(start.parent['action'])
            start = start.parent['node']
        path.append(start.state)
        path.reverse()
        actions.reverse()
        if bidirectionalNode:
            del path[-1]
            while bidirectionalNode.parent != None:
                path.append(bidirectionalNode.state)
                actions.append(bidirectionalNode.parent['action'].invert())
                bidirectionalNode = bidirectionalNode.parent['node']
            path.append(bidirectionalNode.state)
        print('actions: ', actions)
        print('path: ', path)
        print('seen nodes: ', self.seenNodes)
        print('expanded nodes: ', self.expandedNodes)
        print('max memory usage: ', self.maxMemoryUsage)
        if pathCostNotCalculate:
            print('path cost: ', len(path))
        else:
            print('path cost: ', self.pathCost)

    def BFS(self):
        while True:
            if len(self.f.queue) == 0:
                print('f is empty!!!')
                return
            node = self.f.get()
            self.e.append(node)
            for action in self.problem.actions(node):
                child = self.problem.result(node, action)
                child.parent = {'node': node, 'action': action}
                if child.state not in [
                        x.state for x in self.f.queue
                ] and child.state not in [x.state for x in self.e]:
                    if self.problem.isGoal(child):
                        self.seenNodes = self.f.qsize() + len(self.e) + 1
                        self.expandedNodes = len(self.e)
                        self.maxMemoryUsage = self.f.qsize() + len(self.e)
                        self.showResult(child, True)
                        return
                    else:
                        self.f.put(child)

    def DFS(self):
        if not self.recursivDFS(self.f.get()):
            print('not found!!!')

    def recursivDFS(self, node):
        self.seenNodes += 1
        self._depth += 1
        self.maxMemoryUsage = max(self.maxMemoryUsage, self._depth)
        if self.problem.isGoal(node):
            self.pathCost = self._depth
            self.showResult(node)
            return True
        else:
            for action in self.problem.actions(node):
                child = self.problem.result(node, action)
                child.parent = {'node': node, 'action': action}
                result = self.recursivDFS(child)
                if result:
                    return True
            self._depth -= 1
            self.expandedNodes += 1
            return False

    def DFSGraphSearch(self):
        self.f = []
        self.f.append(self.problem.initialState())
        while True:
            if len(self.f) == 0:
                print('f is empty!!!')
                return
            node = self.f.pop()
            self.e.append(node)
            for action in self.problem.actions(node):
                child = self.problem.result(node, action)
                child.parent = {'node': node, 'action': action}
                if child.state not in [
                        x.state for x in self.f
                ] and child.state not in [x.state for x in self.e]:
                    if self.problem.isGoal(child):
                        self.seenNodes = len(self.f) + len(self.e) + 1
                        self.expandedNodes = len(self.e)
                        self.maxMemoryUsage = len(self.f) + len(self.e)
                        self.showResult(child, True)
                        return
                    else:
                        self.f.append(child)

    def depthLimitedSearch(self, limit):  #DFS with Depth-Limited-Search
        if not self.recursivDLS(self.f.get(), limit):
            print('not found!!!')

    def recursivDLS(self, node, limit):
        limit -= 1
        self.seenNodes += 1
        self._depth += 1
        self.maxMemoryUsage = max(self.maxMemoryUsage, self._depth)
        if self.problem.isGoal(node):
            self.pathCost = self._depth
            self.showResult(node)
            return True
        elif limit == 0:
            self._depth -= 1
            return False
        else:
            for action in self.problem.actions(node):
                child = self.problem.result(node, action)
                child.parent = {'node': node, 'action': action}
                result = self.recursivDLS(child, limit)
                if result:
                    return True
            self._depth -= 1
            self.expandedNodes += 1
            return False

    def iterativeDeepeningSearch(self):
        limit = 0
        node = self.f.get()
        while self.maxMemoryUsage >= limit:
            limit += 1
            if self.recursivDLS(node, limit):
                return
            self._depth = 0

    def bidirectionalSearch(self, goal):
        g = Queue()
        g.put(goal)
        h = []
        while True:
            if len(self.f.queue) == 0:
                print('f is empty!!!')
                return
            if len(g.queue) == 0:
                print('g is empty!!!')
                return
            subscribe = next(
                (node.state for node in self.f.queue
                 if node.state in [node.state for node in g.queue]), None)
            if (subscribe):
                self.seenNodes = self.f.qsize() + len(
                    self.e) + g.qsize() + len(h)
                self.expandedNodes = len(self.e) + len(h)
                self.maxMemoryUsage = self.f.qsize() + len(
                    self.e) + g.qsize() + len(h)
                self.showResult(
                    next(node for node in self.f.queue
                         if node.state == subscribe), True,
                    next(node for node in g.queue if node.state == subscribe))
                return

            node1 = self.f.get()
            self.e.append(node1)
            for action in self.problem.actions(node1):
                child1 = self.problem.result(node1, action)
                child1.parent = {'node': node1, 'action': action}
                if child1.state not in [
                        x.state for x in self.f.queue
                ] and child1.state not in [x.state for x in self.e]:
                    self.f.put(child1)

            subscribe = next(
                (node.state for node in self.f.queue
                 if node.state in [node.state for node in g.queue]), None)
            if (subscribe):
                self.seenNodes = self.f.qsize() + len(
                    self.e) + g.qsize() + len(h)
                self.expandedNodes = len(self.e) + len(h)
                self.maxMemoryUsage = self.f.qsize() + len(
                    self.e) + g.qsize() + len(h)
                self.showResult(
                    next(node for node in self.f.queue
                         if node.state == subscribe), True,
                    next(node for node in g.queue if node.state == subscribe))
                return

            node2 = g.get()
            h.append(node2)
            for action in self.problem.actions(node2):
                child2 = self.problem.result(node2, action)
                child2.parent = {'node': node2, 'action': action}
                if child2.state not in [
                        x.state for x in g.queue
                ] and child2.state not in [x.state for x in h]:
                    g.put(child2)

    def uniformCostSrearch(self):
        self.f = []
        heapq.heappush(self.f, (0, self.problem.initialState()))
        while True:
            if not self.f:
                print("f is empty!!!")
                return
            myTuple = heapq.heappop(self.f)
            node = myTuple[1]
            nodePathCost = myTuple[0]
            if self.problem.isGoal(node):
                self.seenNodes = len(self.f) + len(self.e) + 1
                self.expandedNodes = len(self.e)
                self.maxMemoryUsage = len(self.f) + len(self.e) + 1
                self.pathCost = nodePathCost
                self.showResult(node)
                return

            self.e.append(node)
            for action in self.problem.actions(node):
                child = self.problem.result(node, action)
                pathCost = nodePathCost + self.problem.cost(node, action)
                child.parent = {'node': node, 'action': action}
                if (child.state not in [
                        x[1].state for x in self.f
                ]) and (child.state not in [x.state for x in self.e]):
                    heapq.heappush(self.f, (pathCost, child))
                else:
                    temp = next(
                        (x for x in self.f
                         if (x[1].state == child.state and x[0] > pathCost)),
                        None)
                    if temp:
                        self.f.remove(temp)
                        heapq.heappush(self.f, (pathCost, child))

    def AStar(self):
        self.f = []
        heapq.heappush(self.f, (self.problem.h(
            self.problem.initialState()), self.problem.initialState()))
        while True:
            if not self.f:
                print("f is empty!!!")
                return
            myTuple = heapq.heappop(self.f)
            node = myTuple[1]
            nodePathCost = myTuple[0]
            if self.problem.isGoal(node):
                self.seenNodes = len(self.f) + len(self.e) + 1
                self.expandedNodes = len(self.e)
                self.maxMemoryUsage = len(self.f) + len(self.e) + 1
                self.pathCost = nodePathCost - self.problem.h(node)
                self.showResult(node)
                return

            self.e.append(node)
            for action in self.problem.actions(node):
                child = self.problem.result(node, action)
                pathCost = nodePathCost - self.problem.h(
                    node) + self.problem.cost(node,
                                              action) + self.problem.h(child)
                child.parent = {'node': node, 'action': action}
                if (child.state not in [
                        x[1].state for x in self.f
                ]) and (child.state not in [x.state for x in self.e]):
                    heapq.heappush(self.f, (pathCost, child))
                else:
                    temp = next(
                        (x for x in self.f
                         if (x[1].state == child.state and x[0] > pathCost)),
                        None)
                    if temp:
                        self.f.remove(temp)
                        heapq.heappush(self.f, (pathCost, child))
Example #24
0
#s.settimeout(5)

message, (addr, _) = s.recvfrom(1024)

f = open("responses/host" + hostId + ".txt", "a+")

f.write("got  message from " + str(addr) + "\n")

f.close()

message = pickle.loads(message)

if message.message == "currentTime" and message not in queue.read():

    queue.append(
        str(message.multicastSenderId) + ' ' + str(message.clockOfInitiator))

    if hostTree.index(int(hostId)) < hostTree.index(
            multicastInitiator) and hostTree.index(int(hostId)) != 0:

        childIP = '10.0.0.' + str(hostTree[hostTree.index(int(hostId)) - 1])

        parentIP = '10.0.0.' + str(hostTree[hostTree.index(int(hostId)) + 1])

        message = pickle.dumps(message)

        while True:

            s.settimeout(10)

            r = s.sendto(message, (childIP, port))
Example #25
0
def minu_otsing(kaart):
    # leia start, näiteks tuple kujul (x, y)

    start = (0, 0)
    length = len(kaart)
    width = len(kaart[0])

    for i in range(length):
        for j in range(width):
            if kaart[i][j] == "s":
                start = (i, j)
                break
    print("Start: ", start)

    frontier = Queue()
    frontier.put(start)
    came_from = {}
    came_from[start] = None
    path = Queue()
    teemant = (0, 0)
    graph = {}
    breaking = False
    while not frontier.empty():
        current = frontier.get()
        neighbors = [
            (current[0] + 1, current[1]), (current[0], current[1] - 1),
            (current[0], current[1] + 1), (current[0] - 1, current[1])
        ]

        set1 = {()}
        for next in neighbors:
            if next not in came_from and next[0] < length and next[
                    1] < width and next[0] > 0 and next[1] > 0:

                if kaart[next[0]][next[1]] is "D":
                    set1.add(next)
                    frontier.put(next)
                    came_from[next] = current
                    teemant = next
                    graph.update({current: set(set1)})
                    breaking = True
                elif kaart[next[0]][next[1]] == " " and breaking == False:
                    set1.add(next)
                    frontier.put(next)
                    came_from[next] = current
                    graph.update({current: set(set1)})

    curr = teemant
    path = []

    path.append(curr)
    while curr != start:
        last, curr1 = graph.popitem()
        graph.update({last: curr1})
        if last == start:
            curr = start
            path.append(start)
        if curr in curr1:
            path.append(last)
            curr = last
        last, curr1 = graph.popitem()

    return path
Example #26
0
q.put(3)
q.put(2)
print('LifoQueue队列:', q.queue)
print('取出第一个:', q.get())
print('队列:', q.queue)

from queue import PriorityQueue  # 优先队列,堆
q = PriorityQueue()
q.put((2, 'a'))  # 元组比较大小是从左到右进行比较
q.put((3, 'b'))
q.put((1, 'c'))
print('优先队列:', q.queue)
print('依次取出:')
while (not q.empty()):
    print('', q.get())

from collections import deque  # 双端队列
q = deque()
q.append(2)
q.append(3)
q.append(1)
print('双端队列:', q)
q.appendleft(4)
q.append(5)
print('左插4,右插5:', q)
q.rotate(2)
print('循环右移2次:', q)
print('取出最左端:', q.popleft())
print('取出最右端:', q.pop())
print('当前:', q)
    f.close()
    flag = True
    for k, v in book.items():
        if v == False:
            flag = False
            break

    if flag == False:
        continue

    break

#################################################################################################################################################################
queue.append(
    str(pickle.loads(message).multicastSenderId) + ' ' +
    str(pickle.loads(message).clockOfInitiator))
sender_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

host = '10.0.0.' + hostId

safeMessage = pickle.dumps(
    Message(multicastSenderId=hostId,
            message="safe",
            clockOfInitiator=clockOfInitiator))

for i in range(memberCount * 2):

    r1 = sender_socket.sendto(safeMessage, (neighborIPs[0], port))
    counter += 1
Example #28
0
layers = [1, d, 1]

weights = [50 * np.ones((d, 1)), np.array([[1.5, 1, 1.5]])]
biases = [50 * np.linspace(-0.2, -0.8, d), np.array([-2])]

weights = [50 * (2**d) * np.ones((d, 1)), d_inv_sigmoid(sigmoid(np.linspace([-d], [d], d).T))]
biases = [50 * (2**d) * -sigmoid(np.linspace(-d, d, d)), -np.array(np.sum(weights[1])/2)]
"""

parameters = (weights, biases)

net = Net(layers, parameters=parameters)

f = DiffAE(net)
queue = Queue()
queue.append(np.array([Interval(0, 1)]))

verified = interval_bisection(f, queue)
print(len(verified))

#print([item[0].mid() for item in verified])

y = np.zeros_like(x)

for i in range(N):

    out = net.feedforward(np.array([x[i]]))

    y[i] = out[0]
"""
i += 1
Example #29
0
    for j in range(m):

        output = encoder.feedforward( np.array([xx[i, j], yy[i, j]]) )

        zz[i, j] = output[0]
"""

# interval bisection and newton
f = DiffAE(net)

u = Interval(0, 1)
v = Interval(0, 1)
init = np.array([u, v])
queue = Queue()
queue.append(init)

verified = interval_bisection(f, queue)

print('number of verified intervals = %d' % len(verified))

fig, ax = plt.subplots(figsize=(24, 24))

#ax.set_xlim([0, 1])
#ax.set_ylim([0, 1])

rectangles(ax, verified)

# plot results
ax.scatter(data_x, data_y, s=marker_size / 4, c='b', label='input')
ax.scatter(xo, yo, s=marker_size / 4, c='g', label='output')
Example #30
0
 def push(self, *args, **kwargs):
     Queue.append(self, *args, **kwargs)
Example #31
0
class FIFOQueue:
    def __init__(self, multiprocess=False, is_deque=True, max_len=200):
        if multiprocess:
            is_deque = False

        self._queue = None
        self._size = 0  # qsize() might not be implemented for MP queue
        self._use_deque = is_deque
        self._use_mpqueue = multiprocess
        if self._use_deque:
            if max_len == 0:
                max_len = None
            self._queue = deque(maxlen=max_len)
        else:
            if max_len is None:
                max_len = 0
            if self._use_mpqueue:
                self._queue = MPQueue(maxsize=max_len)
            else:
                self._queue = Queue(maxsize=max_len)

    def clear(self):
        self._size = 0
        if self._use_deque:
            self._queue.clear()
        else:
            while self._queue.get() is not None:
                pass

    def size(self):
        if self._use_deque:
            return len(self._queue)

        try:
            return self._queue.qsize()
        except NotImplementedError:
            return self._size

    def put(self, x):
        if self._use_deque:
            self._queue.append(x)
        else:
            try:
                self._queue.put_nowait(x)
                self._size += 1
            except:
                return False
        return True

    def get(self):
        if self._use_deque:
            try:
                return self._queue.popleft()
            except:
                return None

        try:
            x = self._queue.get_nowait()
        except:
            return None

        self._size -= 1
        return x
Example #32
0
class Scheduler:
    def __init__(self, ids: int = 3):
        self._pool = {}
        for processor_id in range(ids):
            self._pool[processor_id] = Processor(processor_id)
        self._stack = Stack()
        self._queue = Queue()
        self._task_generator = Thread(target=self._generate_task)
        self._schedule_thread = Thread(target=self._schedule)
        self._is_running = True
        self._cur_task_id = 0

    def _generate_task(self):
        while self._is_running:
            task = Task(self._cur_task_id)
            self._cur_task_id += 1
            processor_id = choice(tuple(self._pool.keys()))
            self._queue.append((
                processor_id,
                task,
            ))
            print("Task {} generated for {}".format(repr(task), processor_id))
            sleep(randint(0, 5))

    def _schedule(self):
        while self._is_running:
            if self._stack.has_data():
                processor_id, task = self._stack.pop()
                print("Task {} popped from stack for processor {}".format(
                    repr(task), processor_id))
            elif self._queue.has_data():
                processor_id, task = self._queue.pop()
                print("Task {} popped from queue for processor {}".format(
                    repr(task), processor_id))

            if self._pool[processor_id].is_running():
                print("Task {} for processor {} pushed to stack".format(
                    repr(task), processor_id))
                self._stack.push((processor_id, task))
            else:
                self._pool[processor_id](task)

            sleep(1)

    def run(self):
        for _, processor in self._pool.items():
            processor.start()

        self._task_generator.start()
        self._schedule_thread.start()

    def stop(self):
        self._is_running = False

        for _, processor in self._pool.items():
            processor.stop()

        if self._task_generator.is_alive():
            self._task_generator.join()
        if self._schedule_thread.is_alive():
            self._schedule_thread.join()