def test_infinite_stream2(self): wcdtool_path, wcdtool_testcase_subpath = config_parser.parse_config( "config.ini") solution_checker = SolutionChecker(wcdtool_path, wcdtool_testcase_subpath, 20) n1 = Node('ES1') n2 = Node('SW1') n3 = Node('ES2') stream1 = Stream('tt1', 7500, 300, 100, 1, [n1, n2, n3]) stream3 = Stream('tt3', 3000, 300, 100, 1, [n1, n2, n3]) stream2 = Stream('tt2', 3000, 100, 100, 2, [n1, n2, n3]) streams = {'tt1': stream1, 'tt2': stream2, 'tt3': stream3} sw1 = Switch('SW1') sw1.associate_stream_to_queue(stream1.uid, stream1.sending_time, stream1.period, stream1.priority, 'ES2') sw1.associate_stream_to_queue(stream3.uid, stream3.sending_time, stream3.period, stream3.priority, 'ES2') sw1.associate_stream_to_queue(stream2.uid, stream2.sending_time, stream2.period, stream2.priority, 'ES2') switches = {'SW1': sw1} tc = TC(switches, streams, 'TC5') initial_solution = create_initial_solution(tc) is_valid, is_feasible, exceeding_percentages, wcds, infinite_streams = solution_checker.check_solution( initial_solution) self.assertEqual(False, is_feasible) self.assertEqual(True, is_valid) self.assertEqual(True, stream2.uid in infinite_streams) self.assertEqual( True, stream1.uid not in infinite_streams and stream3.uid not in infinite_streams)
def test_dq_optimizer_simple(self): wcdtool_path, wcdtool_testcase_subpath = config_parser.parse_config( "config.ini") solution_checker = SolutionChecker(wcdtool_path, wcdtool_testcase_subpath, 20) n1 = Node('ES1') n2 = Node('SW1') n3 = Node('ES2') stream1 = Stream('tt1', 1500, 100, 100, 0, [n1, n2, n3]) streams = {'tt1': stream1} sw1 = Switch('SW1') sw1.associate_stream_to_queue(stream1.uid, stream1.sending_time, stream1.period, stream1.priority, 'ES2') p = sw1.output_ports['ES2'] p.set_window(stream1.priority, 0, 24, 100) switches = {'SW1': sw1} solution = TC(switches, streams, 'TC1') is_valid, is_feasible, exceeding_percentages, wcds, infinite_streams = solution_checker.check_solution( solution) optimized_solution = optimize_ports_for_stream(solution, stream1, solution_checker) is_valid, is_feasible, exceeding_percentages, wcds, infinite_streams = solution_checker.check_solution( optimized_solution) self.assertEqual(True, stream1.deadline > float(wcds[stream1.uid])) self.assertEqual(True, stream1.deadline < float(wcds[stream1.uid]) + 1)
def insert_head(self, value): """""" # inserting at head x = Node(value) x.child = self.head if self.head: self.head.parent = x self.head = x x.parent = None
def push_front(self, value): """""" if not self.head: self.head = Node(value) self.tail = self.head self.size = 1 else: self.head.set_parent(value) self.head = self.head.parent self.size += 1
def push_back(self, value): """""" if not self.head: self.head = Node(value) self.tail = self.head self.size = 1 else: self.tail.set_child(value) self.tail = self.tail.child self.size += 1
def test_dq_optimizer_simple_multi_streams_same_port(self): wcdtool_path, wcdtool_testcase_subpath = config_parser.parse_config( "config.ini") solution_checker = SolutionChecker(wcdtool_path, wcdtool_testcase_subpath, 20) n1 = Node('ES1') n2 = Node('SW1') n3 = Node('SW2') n4 = Node('ES2') stream1 = Stream('tt1', 1500, 300, 100, 0, [n1, n2, n3, n4]) stream2 = Stream('tt2', 1500, 100, 100, 3, [n1, n3, n4]) streams = {'tt1': stream1, 'tt2': stream2} sw1 = Switch('SW1') sw1.associate_stream_to_queue(stream1.uid, stream1.sending_time, stream1.period, stream1.priority, 'SW2') p = sw1.output_ports['SW2'] p.set_window(stream1.priority, 0, 24, 100) sw2 = Switch('SW2') sw2.associate_stream_to_queue(stream1.uid, stream1.sending_time, stream1.period, stream1.priority, 'ES2') sw2.associate_stream_to_queue(stream2.uid, stream2.sending_time, stream2.period, stream2.priority, 'ES2') p = sw2.output_ports['ES2'] p.set_window(stream1.priority, 0, 24, 100) p.set_window(stream2.priority, 24, 24, 100) switches = {'SW1': sw1, 'SW2': sw2} solution = TC(switches, streams, 'TC3') is_valid, is_feasible, exceeding_percentages, wcds, infinite_streams = solution_checker.check_solution( solution) optimized_solution = optimize_ports_for_stream(solution, stream1, solution_checker) optimized_solution = optimize_ports_for_stream(solution, stream2, solution_checker) is_valid, is_feasible, exceeding_percentages, wcds, infinite_streams = solution_checker.check_solution( optimized_solution) self.assertEqual(True, is_feasible) self.assertEqual( True, optimized_solution.switches['SW2'].output_ports['ES2']. get_hyperperiod() == 49)
def push(self, value): """ standard push function updates self.tail afterwards :param value: :return: """ if self.max == self.size: raise StackOverflow() if not self.tail: self.tail = Node(value) self.head = self.tail else: self.tail.set_child(value) self.tail = self.tail.child self.size += 1
def read_file(problem_name: str, verbose: bool) -> tuple: """ Read both .tsp and .opt.tour files from a TSPLIB problem. :param problem_name: TSP name from TSPLIB repository :param verbose: True for printing on log file, False otherwise. Default value is False. :return: a list, named 'tsp_nodes', containing all nodes specified inside .tsp file and a tour, named 'optimal_tour', which contains the sequence given as optimal path for the current TSP. """ tsp_file_path = Path.cwd() / 'TSPLIB_instances' / problem_name if verbose: info(' Reading %s.tsp file', problem_name) with open(tsp_file_path / (problem_name + '.tsp'), encoding='utf-8') as tsp_file: tsp_lines = tsp_file.readlines() tsp_nodes = list() for node in tsp_lines[6:-1]: node_info = node.strip().split(' ') tsp_nodes.append( Node(int(node_info[0]), int(float(node_info[1])), int(float(node_info[2])))) if verbose: info(' %s.tsp EOF reached\n', problem_name) info(' Reading %s.opt.tour file', problem_name) with open(tsp_file_path / (problem_name + '.opt.tour'), encoding='utf-8') as opt_file: opt_lines = opt_file.readlines() optimal_tour = Tour() if len(opt_lines) != 0: for opt_node in opt_lines[6:-2]: optimal_tour.path.append(int(opt_node)) optimal_tour.path.append(optimal_tour.path[0]) if verbose: info(' %s.opt.tour EOF reached\n', problem_name) return tsp_nodes, optimal_tour
def add(self, item): current = self.head previous = None stop = False while current != None and not stop: if current.getData() > item: stop = True else: previous = current current = current.getNext() temp = Node(item) if previous == None: temp.setNext(self.head) self.head = temp else: temp.setNext(current) previous.setNext(temp)
def add(self, item): temp = Node(item) temp.setNext(self.head) self.head = temp
class Stack(Container): """ as we are simulating static arrays in Python, will alter the implementation of top by making it a pointer to the top element as opposed to the index of the top element in the base array will use size to hold the number of entries instead self.root = None """ def __init__(self, size): self.max = size super().__init__() # will not use self.head at all def push(self, value): """ standard push function updates self.tail afterwards :param value: :return: """ if self.max == self.size: raise StackOverflow() if not self.tail: self.tail = Node(value) self.head = self.tail else: self.tail.set_child(value) self.tail = self.tail.child self.size += 1 def pop(self): """ standard pop function updates self.tail and returns the popped value :return: """ return_value = self.tail.value if self.tail else None if not self.tail: assert self.size == 0 and not self.head raise StackUnderflow() elif self.size == 1: self.tail = self.head = None else: self.tail = self.tail.parent self.tail.child = None self.size -= 1 return return_value def empty(self): """ returns bool(stack is empty?) :return: """ return bool(self.tail)
def push(self, num: int): node = Node(num) node.parent = self._tail self.addNode(node)
class List(Container): def push_back(self, value): """""" if not self.head: self.head = Node(value) self.tail = self.head self.size = 1 else: self.tail.set_child(value) self.tail = self.tail.child self.size += 1 def push_front(self, value): """""" if not self.head: self.head = Node(value) self.tail = self.head self.size = 1 else: self.head.set_parent(value) self.head = self.head.parent self.size += 1 def insert(self, index, value): """""" # inserting at specified index pointer = self.head assert index < self.size for i in range(index): pointer = pointer.child if pointer != self.head: temp = pointer.parent temp.set_child(value) temp.child.child = pointer pointer.parent = temp.child self.size += 1 else: self.push_front(value) def insert_head(self, value): """""" # inserting at head x = Node(value) x.child = self.head if self.head: self.head.parent = x self.head = x x.parent = None def swap(self, index, value): """""" pointer = self.head assert index < self.size for i in range(index): pointer = pointer.child pointer.value = value def delete(self, index): """""" # deleting at specified index # NOT O(1) # worst case: O(n) pointer = self.head assert index < self.size for i in range(index): pointer = pointer.child if pointer != self.head: temp = pointer.parent if pointer: temp.child = pointer.child else: temp.child = None else: self.head = pointer.child self.size -= 1 def at(self, index): """""" pointer = self.head assert index < self.size for i in range(index): pointer = pointer.child return pointer.value def search(self, value): """""" x = self.head # while x != NIL and x.key != k while x and x.value != value: x = x.child return x
def add(self, num: int) -> None: node = Node(num) self.addNode(node)
def parse_testcase(test_case_path: str, wcdtool_path: str, wcdtool_testcase_path: str): """ Args: test_case_path (str): Path to .streams file wcdtool_path (str): Path to wcdtool executable wcdtool_testcase_path (str): Path to wcdtool testcase folder Returns: TestCase object """ ##### 1. Setup ##### # Data Structures streams = {} # Map: Stream Name -> Stream switches = {} # Map: Switch Name -> Switch _nodes = {} # Map: Node Name -> Node _routes = {} # Map: VLS Name -> Route (Ordered List of Nodes) # Files stream_file = open(test_case_path, 'r') vls_file = open(test_case_path[:-7] + 'vls', 'r') # remove ".streams" ending and add ".vls" instead # Determine test case name from path tc_name = os.path.splitext(os.path.basename(test_case_path))[0] # removes file ending and dot ##### 2. Copying ##### # Copy over stream and vls file for later use with wcd tool if not os.path.exists("{}\\in\\".format(wcdtool_path + wcdtool_testcase_path + tc_name)): os.makedirs("{}\\in\\".format(wcdtool_path + wcdtool_testcase_path + tc_name), exist_ok=True) if not os.path.exists("{}\\out\\".format(wcdtool_path + wcdtool_testcase_path + tc_name)): os.makedirs("{}\\out\\".format(wcdtool_path + wcdtool_testcase_path + tc_name), exist_ok=True) copyfile(stream_file.name, "{}\\in\\{}".format(wcdtool_path + wcdtool_testcase_path + tc_name, 'msg.txt')) copyfile(vls_file.name, "{}\\in\\{}".format(wcdtool_path + wcdtool_testcase_path + tc_name, 'vls.txt')) ##### 3. Parsing ##### # Parsing .vls file for line in vls_file: if not line.startswith('#'): # TODO: allow more chars m = re.findall(r'([a-zA-Z0-9_]+)\s?,\s?([a-zA-Z0-9_]+)', line) if m is not None: _routes[line.split(' ')[0]] = [] # Create Route with name of vl r = _routes[line.split(' ')[0]] first = True for tpl in m: # For each link create nodes if not existent yet n1_name = tpl[0] n2_name = tpl[1] if n1_name not in _nodes: _nodes[n1_name] = Node(n1_name) if _nodes[n1_name].type == 'SW': switches[n1_name] = Switch(n1_name) if n2_name not in _nodes: _nodes[n2_name] = Node(n2_name) if _nodes[n2_name].type == 'SW': switches[n2_name] = Switch(n2_name) # Add nodes to route if first: # first link -> add both nodes r.append(_nodes[n1_name]) r.append(_nodes[n2_name]) first = False else: # add second node r.append(_nodes[n2_name]) # Add output port to first node if it is a switch if n1_name in switches.keys(): switches[n1_name].add_outputport_to(n2_name) # Parsing .streams file for line in stream_file: if not line.startswith('#'): m = re.search(r'([^\s,]+),\s?(\d+),\s?(\d+),\s?([^\s,]+),\s?([^\s,]+),\s?(\d+),\s?(\d+)', line) if m is not None: if m.group(5) != 'TT': print('Warning. Unknow traffic class {} found. Will assume TT'.format(m.group(5))) s = Stream(m.group(1), int(m.group(2)), int(m.group(3)), int(m.group(7)), int(m.group(6)), _routes[m.group(4)]) streams[m.group(1)] = s # Add to list # Associate Stream with each switch on route (skip end systems) i = 1 for node in s.route[1:-1]: switches[node.uid].associate_stream_to_queue(s.uid, s.sending_time, s.period, s.priority, s.route[i + 1].uid) i += 1 ##### 4. Return TestCase ##### return TestCase(switches, streams, tc_name, len(_nodes)-len(switches))