def test_mutated_tree(self): for _ in range(100): tree1 = pt_create.apply(random.randint(10, 35)) tree2 = copy.deepcopy(tree1) sub1, sub2 = pt_mutate.change_node_operator(tree2, None) com_res = pt_compare.apply(tree1, tree2) self.assertEqual(str(sub1), str(com_res.subtree1)) self.assertEqual(str(sub2), str(com_res.subtree2)) tree2 = copy.deepcopy(tree1) sub1, sub2 = pt_mutate.add_new_node(tree2, None) com_res = pt_compare.apply(tree1, tree2) if sub1.operator == Operator.LOOP and sub1.children[ 1].operator is not None: self.assertEqual(str(sub1.children[1]), str(com_res.subtree1)) self.assertEqual(str(sub2.children[1]), str(com_res.subtree2)) else: self.assertEqual(str(sub1), str(com_res.subtree1)) self.assertEqual(str(sub2), str(com_res.subtree2)) tree2 = copy.deepcopy(tree1) sub1, sub2 = pt_mutate.remove_node(tree2, None) com_res = pt_compare.apply(tree1, tree2) if (len(sub1.children) == 2 and (sub1.parent is None or (sub2.operator is not None and (sub1.parent is not None and sub2.operator != sub1.parent.operator)) or sub2.operator == Operator.LOOP)) or (len(sub1.children) > 2): self.assertEqual(str(sub1), str(com_res.subtree1)) self.assertEqual(str(sub2), str(com_res.subtree2)) else: self.assertEqual(str(sub1.parent), str(com_res.subtree1)) self.assertEqual(str(sub2.parent), str(com_res.subtree2))
def compute_run_time(): no_tree, no_event, pro, mutated_num = 10, 10, 0.9, 5 tree1 = [pt_create.apply(random.randint(11, 15)) for _ in range(no_tree)] m_tree1 = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree1] log1 = [log_create.apply(tree, no_event, pro) for tree in tree1] tree2 = [pt_create.apply(random.randint(16, 20)) for _ in range(no_tree)] m_tree2 = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree2] log2 = [log_create.apply(tree, no_event, pro) for tree in tree2] tree3 = [pt_create.apply(random.randint(21, 25)) for _ in range(no_tree)] m_tree3 = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree3] log3 = [log_create.apply(tree, no_event, pro) for tree in tree3] tree4 = [pt_create.apply(random.randint(26, 30)) for _ in range(no_tree)] log4 = [log_create.apply(tree, no_event, pro) for tree in tree4] m_tree4 = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree4] # tree5 = [pt_create.apply(random.randint(31, 33)) for _ in range(no_tree)] # log5 = [log_create.apply(tree, no_event, pro) for tree in tree5] # m_tree5 = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree5] # tree6 = [pt_create.apply(random.randint(34, 35)) for _ in range(no_tree)] # log6 = [log_create.apply(tree, no_event, pro) for tree in tree6] # m_tree6 = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree6] l_without_lock = list( map( lambda tree_log: avg_runtime_without_lock(tree_log[0], tree_log[ 1], tree_log[2]), [(tree1, m_tree1, log1), (tree2, m_tree2, log2), (tree3, m_tree3, log3), (tree4, m_tree4, log4)])) # , (tree5, m_tree5, log5), (tree6, m_tree6, log6) l_with_lock = list( map( lambda tree_log: avg_runtime_with_lock(tree_log[0], tree_log[ 1], tree_log[2]), [(tree1, m_tree1, log1), (tree2, m_tree2, log2), (tree3, m_tree3, log3), (tree4, m_tree4, log4)])) # , (tree5, m_tree5, log5), (tree6, m_tree6, log6) plot_histogram_lock_runtime(l_without_lock, l_with_lock)
def create_pts(): pts = list() for node_num in PT_RANGE: trees = pd.DataFrame(columns=['tree', '#node', 'depth', 'root-op']) for i in range(pt_num): tree = pt_create.apply(random.randint(node_num[0], node_num[1])) num_nodes = pt_number.apply(tree, 'D') trees.loc[i] = [ str(tree), num_nodes, pt_mani_utils.pt_depth(tree), str(tree.operator) ] pts.append(trees) return pts
def test_random_create_tree(self): for _ in range(100): tree1 = pt_create.apply(random.randint(10, 35)) tree2 = pt_utils.parse(str(tree1)) q1, q2 = list(), list() q1.append(tree1) q2.append(tree2) while len(q1) != 0: node1 = q1.pop(0) node2 = q2.pop(0) self.assertEqual(str(node1.parent), str(node2.parent)) self.assertEqual(str(node1.children), str(node2.children)) for i in range(len(node1.children)): q1.append(node1.children[i]) q2.append(node2.children[i])
def pt_write_to_table(tab, mutate_tabs, num, l, u, mutate_num, mutate_level): for _ in range(num): row = tab.row # TODO: 一定时间内没有找到 no_node = random.randint(l, u) tree = pt_create.apply(no_node) info = [ no_node, " " + str(tree), pt_mani_utils.leaves_number(tree), pt_mani_utils.pt_depth(tree) ] for i, mutate_tab in enumerate(mutate_tabs): for m_tree in uniq_mutate_tree(tree, mutate_num, mutate_level[i]): mutate_tree_write_to_table(mutate_tab, m_tree, [row] + info) excel_utils.write_row_to_table(tab.table, row, info)
def creat_non_fitting_based_on_tree2(file, name, node_num, parameters): num = [ "node", "best_worst_cost", "optimal_cost", "optimal_time", "repaired_cost", "repaired_time", "scope_repair_cost", "scope_repair_time", "grade1", "grade2" ] tree_num, mutated_num, log_num, non_fit_pro = 25, 1, 1, 0.9 tree = [ pt_create.apply(random.randint(node_num[0], node_num[1])) for _ in range(tree_num) ] m_tree = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree] log = [[ log_create.apply(m_tree, log_num, non_fit_pro) for m_tree in m_tr4 ] for m_tr4 in m_tree] for i in range(len(tree)): for j in range(len(m_tree[0])): result = compute_cost_and_time(tree[i], m_tree[i][j], log[i][j], parameters) print_tree_align_compare(result)
def creat_non_fitting_based_on_tree1(file, name, node_num): num = [ "node", "best_worst_cost", "optimal_cost", "optimal_time", "repaired_cost", "repaired_time", "scope_repair_cost", "scope_repair_time", "grade1", "grade2" ] tree_num, mutated_num, log_num, non_fit_pro = 5, 1, 5, 0.9 tree = [ pt_create.apply(random.randint(node_num[0], node_num[1])) for _ in range(tree_num) ] m_tree = [[pt_mutate.apply(tree) for _ in range(mutated_num)] for tree in tree] log = [log_create.apply(tree, log_num, non_fit_pro) for tree in tree] for i in range(len(tree)): node_num = pt_mani_utils.non_none_leaves_number(tree[i]) for j in range(len(m_tree[0])): param1 = {'ret_tuple_as_trans_desc': True, 'COMPARE_OPTION': 1} result = compute_cost_and_time(tree[i], m_tree[i][j], log[i], param1) print_tree_align_compare(result) result["node"] = node_num
def test_random_create_tree_node(self): for _ in range(100): no_num = random.randint(10, 35) tree = pt_create.apply(no_num) index_num = pt_number.apply(tree, 'D', 1) self.assertEqual(no_num, index_num)