def rec(current_table): """If solution is found, return target. Else, return None.""" nonlocal t_number t_number += 1 cur_depth = current_table.effective_len() if t_number % 10 == 0: print(t_number, cur_depth) if cur_depth > depth: return None # First check if the table is closed flag_closed, new_S, new_R, move = current_table.is_closed() if not flag_closed: temp_tables = make_closed(new_S, new_R, move, current_table, sigma, AA) for table in temp_tables: target = rec(table) if target is not None: return target return None # If is closed, check if the table is consistent flag_consistent, new_a, new_e_index, reset_index_i, reset_index_j, reset_i, reset_j = current_table.is_consistent() if not flag_consistent: temp_tables = make_consistent(new_a, new_e_index, reset_index_i, reset_index_j, reset_i, reset_j, current_table, sigma, AA) for table in temp_tables: target = rec(table) if target is not None: return target return None # If prepared, check conversion to FA fa_flag, fa, sink_name = to_fa(current_table, t_number) if not fa_flag: return None # Can convert to FA: convert to OTA and test equivalence h = fa_to_ota(fa, sink_name, sigma, t_number) equivalent, ctx = equivalence_query_normal(max_time_value, AA, h, prev_ctx) # Add counterexample to prev list if not equivalent and ctx not in prev_ctx: prev_ctx.append(ctx) if not equivalent: temp_tables = add_ctx_normal(ctx.tws, current_table, AA) for table in temp_tables: target = rec(table) if target is not None: return target return None else: target = copy.deepcopy(h) return target
def test_make_consistent(self): A = buildOTA('example2.json', 's') AA = buildAssistantOTA(A, 's') # Assist max_time_value = AA.max_time_value() sigma = AA.sigma s1 = Element([], [0], []) s2 = Element([ResetTimedword('a', 0, True)], [-1], []) s3 = Element( [ResetTimedword('a', 1, False), ResetTimedword('b', 2, True)], [1], []) r1 = Element([ResetTimedword('b', 0, True)], [-1], []) r2 = Element( [ResetTimedword('a', 0, True), ResetTimedword('a', 0, True)], [-1], []) r3 = Element( [ResetTimedword('a', 0, True), ResetTimedword('b', 0, True)], [-1], []) r4 = Element([ResetTimedword('a', 1, False)], [0], []) r5 = Element([ ResetTimedword('a', 1, False), ResetTimedword('b', 2, True), ResetTimedword('a', 0, False) ], [0], []) r6 = Element([ ResetTimedword('a', 1, False), ResetTimedword('b', 2, True), ResetTimedword('b', 0, True) ], [-1], []) r7 = Element([ResetTimedword('b', 2, True)], [-1], []) new_S = [s1, s2, s3] new_R = [r1, r2, r3, r4, r5, r6, r7] new_E = [] T5 = OTATable(new_S, new_R, new_E, parent=-1, reason="test") # T5.show() print("-----------make consistent----------------") flag, new_a, new_e_index, i, j, reset_i, reset_j = T5.is_consistent() self.assertEqual(flag, False) tables = make_consistent(new_a, new_e_index, i, j, reset_i, reset_j, T5, sigma, AA) for tb in tables: S_U_R = [s for s in tb.S] + [r for r in tb.R] self.assertEqual(S_U_R[i].suffixes_resets[-1], [None]) self.assertEqual(S_U_R[j].suffixes_resets[-1], [None])
def main(): #print("------------------A-----------------") paras = sys.argv A, _ = buildOTA(paras[1], 's') #A,_ = buildOTA("example.json", 's') #A.show() #print("------------------Assist-----------------") AA = buildAssistantOTA(A, 's') #AA.show() #print("--------------max value---------------------") max_time_value = A.max_time_value() #print(max_time_value) #print("--------------all regions---------------------") #regions = get_regions(max_time_value) # for r in regions: # print(r.show()) print("**************Start to learn ...*******************") print("---------------initial table-------------------") sigma = AA.sigma T1 = init_table(sigma, AA) t_number = 1 print("Table " + str(t_number) + " is as follow.") T1.show() print("-----------------------------------------------") start = time.time() equivalent = False eq_total_time = 0 table = copy.deepcopy(T1) eq_number = 0 target = None while equivalent == False: prepared = table.is_prepared(AA) while prepared == False: flag_closed, new_S, new_R, move = table.is_closed() if flag_closed == False: temp = make_closed(new_S, new_R, move, table, sigma, AA) table = temp t_number = t_number + 1 print("Table " + str(t_number) + " is as follow.") table.show() print("--------------------------------------------------") flag_consistent, new_a, new_e_index = table.is_consistent() if flag_consistent == False: temp = make_consistent(new_a, new_e_index, table, sigma, AA) table = temp t_number = t_number + 1 print("Table " + str(t_number) + " is as follow.") table.show() print("--------------------------------------------------") flag_evi_closed, new_added = table.is_evidence_closed(AA) if flag_evi_closed == False: temp = make_evidence_closed(new_added, table, sigma, AA) table = temp t_number = t_number + 1 print("Table " + str(t_number) + " is as follow.") table.show() print("--------------------------------------------------") prepared = table.is_prepared(AA) fa, sink_name = to_fa(table, t_number) #print("---------------------------------------------") #fa.show() #print("---------------------------------------------") h = fa_to_ota(fa, sink_name, sigma, t_number) #h.show() #print("---------------------------------------------") target = copy.deepcopy(h) eq_start = time.time() equivalent, ctx = equivalence_query(max_time_value, AA, h) eq_end = time.time() eq_total_time = eq_total_time + eq_end - eq_start #print(ctx.show()) eq_number = eq_number + 1 if equivalent == False: temp = add_ctx(ctx.tws, table, AA) table = temp t_number = t_number + 1 print("Table " + str(t_number) + " is as follow.") table.show() print("--------------------------------------------------") end_learning = time.time() if target is None: print("Error! Learning Failed.") print("*******************Failed.***********************") else: print("Succeed! The learned OTA is as follows.") print("---------------------------------------------------") target.show() print("---------------------------------------------------") # print("Total time of learning: " + str(end-start)) # print("---------------------------------------------------") # print("Time intervals simplification...") # print() print("Removing the sink location...") print() print("The learned One-clock Timed Automtaton: ") print() target_without_sink = remove_sinklocation(target) end_removesink = time.time() target_without_sink.show() print("---------------------------------------------------") print("Total time of learning: " + str(end_learning - start)) #print("---------------------------------------------------") #print("Total time of equivalence queries: " + str(eq_total_time)) print("---------------------------------------------------") print("Total time of learning + simplifying: " + str(end_removesink - start)) print("---------------------------------------------------") print("The element number of S in the last table: " + str(len(table.S))) print("The element number of R in the last table: " + str(len(table.R))) print( "The element number of E in the last table (excluding the empty-word): " + str(len(table.E))) print("Total number of observation table: " + str(t_number)) print("Total number of membership query: " + str((len(table.S) + len(table.R)) * (len(table.E) + 1))) print("Total number of equivalence query: " + str(eq_number)) print("*******************Successful!***********************")
def random_one_step(current_table): """Find a random successor of the current table. Here a successor is defined to be the table after executing one make_closed, one make_consistent, or one add_ctx_normal on existing counterexamples. """ nonlocal t_number # First check if the table is closed flag_closed, new_S, new_R, move = current_table.is_closed() if not flag_closed: if debug_flag: print( "------------------make closed--------------------------") temp_tables = make_closed(new_S, new_R, move, current_table, sigma, AA) if len(temp_tables) > 0: return random.choice(temp_tables) else: return 'failed' # If is closed, check if the table is consistent flag_consistent, new_a, new_e_index, reset_index_i, reset_index_j, reset_i, reset_j = current_table.is_consistent( ) if not flag_consistent: if debug_flag: print( "------------------make consistent--------------------------" ) temp_tables = make_consistent(new_a, new_e_index, reset_index_i, reset_index_j, reset_i, reset_j, current_table, sigma, AA) if len(temp_tables) > 0: return random.choice(temp_tables) else: return 'failed' # If prepared, check conversion to FA t_number += 1 fa_flag, fa, sink_name = to_fa(current_table, t_number) if not fa_flag: return 'failed' # Can convert to FA: convert to OTA and test equivalence h = fa_to_ota(fa, sink_name, sigma, t_number) equivalent, ctx = True, None if prev_ctx is not None: for ctx in prev_ctx: teacher_res = AA.is_accepted_delay(ctx.tws) hypothesis_res = h.is_accepted_delay(ctx.tws) if teacher_res != hypothesis_res and hypothesis_res != -2: equivalent, ctx = False, ctx if equivalent: # If equivalent, the current table is a candidate return 'candidate' else: # Otherwise, add counterexample temp_tables = add_ctx_normal(ctx.tws, current_table, AA) if len(temp_tables) > 0: return random.choice(temp_tables) else: return 'failed'
def learn_ota(paras, debug_flag): A = buildOTA(paras, 's') AA = buildAssistantOTA(A, 's') max_time_value = A.max_time_value() print("**************Start to learn ...*******************") print("---------------initial table-------------------") sigma = AA.sigma need_to_explore = queue.PriorityQueue() for table in init_table_normal(sigma, AA): need_to_explore.put((table.effective_len(), table)) # List of existing counterexamples prev_ctx = [] # Current number of tables t_number = 0 start = time.time() eq_total_time = 0 eq_number = 0 target = None while True: if need_to_explore.qsize() == 0: break depth, current_table = need_to_explore.get() t_number = t_number + 1 if t_number % 1 == 0: print(t_number, need_to_explore.qsize(), current_table.effective_len()) if debug_flag: print("Table " + str(t_number) + " is as follow, %s has parent %s by %s" % (current_table.id, current_table.parent, current_table.reason)) current_table.show() print("--------------------------------------------------") # First check if the table is closed flag_closed, new_S, new_R, move = current_table.is_closed() if not flag_closed: if debug_flag: print("------------------make closed--------------------------") temp_tables = make_closed(new_S, new_R, move, current_table, sigma, AA) if len(temp_tables) > 0: for table in temp_tables: need_to_explore.put((table.effective_len(), table)) continue # If is closed, check if the table is consistent flag_consistent, new_a, new_e_index, reset_index_i, reset_index_j, reset_i, reset_j = current_table.is_consistent() if not flag_consistent: if debug_flag: print("------------------make consistent--------------------------") temp_tables = make_consistent(new_a, new_e_index, reset_index_i, reset_index_j, reset_i, reset_j, current_table, sigma, AA) if len(temp_tables) > 0: for table in temp_tables: need_to_explore.put((table.effective_len(), table)) continue # If prepared, check conversion to FA fa_flag, fa, sink_name = to_fa(current_table, t_number) if not fa_flag: continue # Can convert to FA: convert to OTA and test equivalence h = fa_to_ota(fa, sink_name, sigma, t_number) eq_start = time.time() # equivalent, ctx = equivalence_query_normal(max_time_value, AA, h, prev_ctx) equivalent, ctx = True, None if prev_ctx is not None: for ctx in prev_ctx: teacher_res = AA.is_accepted_delay(ctx.tws) hypothesis_res = h.is_accepted_delay(ctx.tws) if teacher_res != hypothesis_res and hypothesis_res != -2: equivalent, ctx = False, ctx ctx = minimizeCounterexample(AA, h, ctx) if equivalent: AA.equiv_query_num += 1 equivalent, ctx, _ = pac_equivalence_query(A, max_time_value, AA, h, AA.equiv_query_num, 0.001, 0.001) if not equivalent: # remove_sinklocation(copy.deepcopy(h)).show() print(ctx.tws) # print(ratio) # Add counterexample to prev list if not equivalent and ctx not in prev_ctx: prev_ctx.append(ctx) eq_end = time.time() eq_total_time = eq_total_time + eq_end - eq_start eq_number = eq_number + 1 if not equivalent: temp_tables = add_ctx_normal(ctx.tws, current_table, AA) if len(temp_tables) > 0: for table in temp_tables: need_to_explore.put((table.effective_len(), table)) else: target = copy.deepcopy(h) break end_learning = time.time() if target is None: print("---------------------------------------------------") print("Error! Learning Failed.") print("*******************Failed.***********************") return False else: print("---------------------------------------------------") print("Succeed! The learned OTA is as follows.") print("-------------Final table instance------------------") current_table.show() print("---------------Learned OTA-------------------------") target.show() print("---------------------------------------------------") print("Removing the sink location...") print() print("The learned One-clock Timed Automtaton: ") print() target_without_sink = remove_sinklocation(target) end_removesink = time.time() target_without_sink.show() print("---------------------------------------------------") print("Number of transitions in teacher: " + str(len(A.trans))) print("Total number of membership query: " + str(len(AA.membership_query))) # print("Total number of membership query (no-cache): " + str(AA.mem_query_num)) # print("Total number of equivalence query: " + str(len(prev_ctx) + 1)) print("Total number of equivalence query (no-cache): " + str(AA.equiv_query_num)) print("Total number of tests: " + str(AA.test_query_num)) print("Total number of tables explored: " + str(t_number)) # print("Total number of tables to explore: " + str(need_to_explore.qsize())) print("Number of locations in learned table: " + str(len(target_without_sink.locations))) print("Total time of learning: " + str(end_learning-start)) return target_without_sink