def equivalence_query_normal(max_time_value, teacher, hypothesis, prev_ctx=None): """Normal teacher """ if prev_ctx is not None: for ctx in prev_ctx: teacher_res = teacher.is_accepted_delay(ctx.tws) hypothesis_res = hypothesis.is_accepted_delay(ctx.tws) if teacher_res != hypothesis_res and hypothesis_res != -2: return False, ctx flag_pos, w_pos = ota_inclusion(max_time_value, hypothesis, teacher) if flag_pos == False: dtw_pos = findDelayTimedwords(w_pos, 's', teacher.sigma) ctx_pos = Element(dtw_pos, []) return False, ctx_pos else: flag_neg, w_neg = ota_inclusion(max_time_value, teacher, hypothesis) if flag_neg == False: dtw_neg = findDelayTimedwords(w_neg, 's', teacher.sigma) ctx_neg = Element(dtw_neg, []) return False, ctx_neg else: return True, None
def test_is_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] for s in new_S: self.assertEqual(AA.is_accepted_delay(dRTWs_to_lRTWs(s.tws)), s.value[0]) for r in new_R: self.assertEqual(AA.is_accepted_delay(dRTWs_to_lRTWs(r.tws)), r.value[0]) new_E = [] T5 = OTATable(new_S, new_R, new_E, parent=-1, reason="test") # T5.show() print("-----------is consistent----------------") flag, new_a, new_e_index, i, j, reset_i, reset_j = T5.is_consistent() self.assertEqual(flag, False) self.assertEqual(new_a, [ResetTimedword('b', 2, True)]) self.assertEqual(new_e_index, 0) self.assertEqual(i, 0) self.assertEqual(j, 6) self.assertEqual(reset_i, True)
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 equivalence_query(max_time_value, teacher, hypothesis): """Smart teacher """ flag_pos, w_pos = ota_inclusion(max_time_value, hypothesis, teacher) if flag_pos == False: #drtw_pos = findDelayRTWs(w_pos, 's', teacher) drtw_pos = dTWs_to_dRTWs(w_pos, 's', teacher) ctx_pos = Element(drtw_pos, []) return False, ctx_pos else: flag_neg, w_neg = ota_inclusion(max_time_value, teacher, hypothesis) if flag_neg == False: #drtw_neg = findDelayRTWs(w_neg, 's', teacher) drtw_neg = dTWs_to_dRTWs(w_neg, 's', teacher) ctx_neg = Element(drtw_neg, []) return False, ctx_neg else: return True, None
def ltw_to_dtw(ltw): dtw = [] for i in range(len(ltw)): if i == 0 or reset[i - 1]: dtw.append(Timedword(ltw[i].action, ltw[i].time)) else: dtw.append( Timedword(ltw[i].action, round1(ltw[i].time - ltw[i - 1].time))) return Element(dtw, [])
def test_is_closed(self): A = buildOTA('example6.json', 's') AA = buildAssistantOTA(A, 's') # Assist #max_time_value = AA.max_time_value() sigma = AA.sigma T1_tables = init_table_normal(sigma, AA) self.assertEqual(len(T1_tables), 1) #print("--------------------------------------------------") flag_closed, new_S, new_R, move = T1_tables[0].is_closed() self.assertEqual(flag_closed, False) self.assertEqual(new_S, [ Element([], [0], []), Element([ResetTimedword('a', 0, True)], [-1], []) ]) self.assertEqual(new_R, [ Element([ResetTimedword('b', 0, True)], [-1], []), Element([ResetTimedword('c', 0, True)], [-1], []) ]) self.assertEqual(move, Element([ResetTimedword('a', 0, True)], [-1], []))
def sampleGeneration(inputs, upperGuard, stateNum, length=None): """Generate a sample.""" sample = [] if length is None: length = random.randint(1, stateNum) for i in range(length): input = inputs[random.randint(0, len(inputs) - 1)] time = random.randint(0, upperGuard * 2 + 1) if time % 2 == 0: time = time // 2 else: time = time // 2 + 0.1 temp = Timedword(input, time) sample.append(temp) return Element(sample, [])
def equivalence_query(max_time_value, teacher, hypothesis): """ """ flag_pos, w_pos = ota_inclusion(max_time_value, hypothesis, teacher) if flag_pos == False: #drtw_pos = findDelayRTWs(w_pos, 's', teacher) drtw_pos = dTWs_to_dRTWs(w_pos, 's', teacher) ctx_pos = Element(drtw_pos, []) return False, ctx_pos else: flag_neg, w_neg = ota_inclusion(max_time_value, teacher, hypothesis) if flag_neg == False: #drtw_neg = findDelayRTWs(w_neg, 's', teacher) drtw_neg = dTWs_to_dRTWs(w_neg, 's', teacher) ctx_neg = Element(drtw_neg, []) return False, ctx_neg else: return True, None # def dRTWs_to_lRTWs(delay_resettimedwords): # """Given a delay reset-timedwords, return the local reset-timedwords. # """ # reset = False # current_clock_valuation = 0 # local_resettimedwords = [] # for drtw in delay_resettimedwords: # if reset == False: # current_clock_valuation = current_clock_valuation + drtw.time # else: # current_clock_valuation = drtw.time # local_resettimedwords.append(ResetTimedword(drtw.action,current_clock_valuation,drtw.reset)) # reset = drtw.reset # return local_resettimedwords # def equivalence_main(): # experiments_path = os.getcwd()+"/experiments/" # A, _ = buildOTA(experiments_path+'example3.json', 's') # AA = buildAssistantOTA(A, 's') # max_time_value = AA.max_time_value() # # H, _ = buildOTA('example2_1.json', 'q') # # HH = buildAssistantOTA(H, 'q') # H, _ = buildOTA(experiments_path+'example3_1.json', 'q') # HH = buildAssistantOTA(H, 'q') # AA.show() # print("------------------------------") # HH.show() # print("------------------------------") # # H.show() # # flag, ctx = equivalence_query(max_time_value,AA,HH) # # print(flag) # # print("------------------------------") # # #print(ctx.show()) # # print("------------------------------") # flag_pos, w_pos = ota_inclusion(max_time_value, HH, AA) # print(flag_pos) # # flag_neg, w_neg = ota_inclusion(max_time_value, AA, HH) # # print(flag_neg) # print(w_pos.show()) # print("--------------------------------------------") # path1 = findpath(w_pos,'s',AA.sigma) # for lw in path1: # print(lw.show(),lw.action) # print("----------------------------------------------") # dtw = findDelayTimedwords(w_pos,'s',AA.sigma) # print(dtw) # # # print("----------------------------------------------") # # # drtw = dTWs_to_dRTWs(w_pos,'s',AA) # # # print(drtw) # # # #flag, ctx = equivalence_query(max_time_value,AA,HH) # # # #print(flag) # # # print("------------------------------") # # # #print(ctx.show()) # if __name__=='__main__': # equivalence_main()
def sampleGeneration_valid(teacher, upperGuard, length): """Generate a sample adapted to the given teacher.""" # First produce a path (as a list of transitions) in the OTA path = [] current_state = teacher.initstate_name for i in range(length): edges = [] for tran in teacher.trans: if current_state == tran.source: edges.append(tran) edge = random.choice(edges) path.append(edge) current_state = edge.target # Next, figure out (double of) the minimum and maximum logical time. min_time, max_time = [], [] for tran in path: assert len(tran.constraints) == 1 min_time.append(min_constraint_double(tran.constraints[0])) max_time.append(max_constraint_double(tran.constraints[0], upperGuard)) # For each transition, maintain a mapping from logical time to the # number of choices. weight = dict() for i in reversed(range(length)): tran = path[i] mn, mx = min_time[i], max_time[i] weight[i] = dict() if i == length - 1 or tran.reset: for j in range(mn, mx + 1): weight[i][j] = 1 else: for j in range(mn, mx + 1): weight[i][j] = 0 for k, w in weight[i + 1].items(): if k >= j: weight[i][j] += w # Now sample according to the weights double_times = [] cur_time = 0 for i in range(length): start_time = max(min_time[i], cur_time) distr = [] for j in range(start_time, max_time[i] + 1): distr.append(weight[i][j]) if sum(distr) == 0: return None # sampling failed cur_time = sampleDistribution(distr) + start_time double_times.append(cur_time) if path[i].reset: cur_time = 0 # Finally, change doubled time to fractions. ltw = [] for i in range(length): if double_times[i] % 2 == 0: time = double_times[i] // 2 else: time = double_times[i] // 2 + 0.1 ltw.append(Timedword(path[i].label, time)) # Convert logical-timed word to delayed-timed word. dtw = [] for i in range(length): if i == 0 or path[i - 1].reset: dtw.append(Timedword(path[i].label, ltw[i].time)) else: dtw.append(Timedword(path[i].label, ltw[i].time - ltw[i - 1].time)) return Element(dtw, [])