def test_DTWs(self, DTWs): DRTWs = [] now_time = 0 cur_state = self.init_state for dtw in DTWs: if cur_state == self.sink_state: DRTWs.append(ResetTimedWord(dtw.action, dtw.time, True)) else: time = dtw.time + now_time new_LTW = TimedWord(dtw.action, time) for tran in self.trans: if tran.source == cur_state and tran.is_passing_tran( new_LTW): cur_state = tran.target if tran.reset: now_time = 0 reset = True else: now_time = time reset = False DRTWs.append( ResetTimedWord(dtw.action, dtw.time, reset)) break if cur_state in self.accept_states: value = 1 elif cur_state == self.sink_state: value = -1 else: value = 0 return DRTWs, value
def init_table_normal(actions, system): if system.init_state in system.accept_states: init_value = 1 else: init_value = 0 S = [Element([], [init_value], [[]])] R = [] E = [[]] tables = [ObsTable(S, R, E, parent=-1, reason="init")] for i in range(0, len(actions)): temp_tables = [] for table in tables: new_DTWs = [TimedWord(actions[i], 0)] # 同样也是LTWs outputs = system.test_DTWs_normal(new_DTWs, True) if outputs[-1] == -1: # now at sink guesses = [True] else: guesses = [True, False] for guess in guesses: new_LRTWs = [ ResetTimedWord(new_DTWs[0].action, new_DTWs[0].time, guess) ] new_element = Element(new_LRTWs, [outputs[-1]], [[]]) temp_R = table.R + [new_element] new_table = ObsTable(deepcopy(S), deepcopy(temp_R), deepcopy(E), parent=-1, reason="init") temp_tables.append(new_table) tables = temp_tables return tables
def is_LRTWs_value_right(LRTWs, realValue, system): if not LRTWs: if system.init_state in system.accept_states: value = 1 else: value = 0 else: now_time = 0 cur_state = system.init_state for lrtw in LRTWs: if lrtw.time < now_time: cur_state = system.sink_state if not lrtw.reset: return False else: LRTW = ResetTimedWord(lrtw.action, lrtw.time, lrtw.reset) flag, cur_state, reset = is_passing_tran( LRTW, cur_state, system) if not flag: return False if reset: now_time = 0 else: now_time = lrtw.time if cur_state in system.accept_states: value = 1 elif cur_state == system.sink_state: value = -1 else: value = 0 if value != realValue: return False return True
def make_closed(closed_move, actions, table, system): element = closed_move[0] table.S.append(element) table.R.remove(element) element_list = [] for i in range(0, len(actions)): action_element_list = [] if element.values[0] == -1: # 原先已经是无效状态了,再加一个LTW还是无效状态 new_LRTWs = element.LRTWs + [ResetTimedWord(actions[i], 0, True)] new_Element = fill_sink_row(new_LRTWs, table.E) action_element_list.append(new_Element) else: new_LRTWs = element.LRTWs + [ResetTimedWord(actions[i], 0, True)] if is_LRTWs_valid(new_LRTWs): DTWs = LRTW_to_DTW(new_LRTWs) outputs = system.test_DTWs_normal(DTWs, True) system.mq_num -= 1 if outputs[-1] == -1: system.mq_num += 1 new_Element = fill_sink_row(new_LRTWs, table.E) action_element_list.append(new_Element) else: guesses = [True, False] for guess in guesses: new_LRTWs = element.LRTWs + [ ResetTimedWord(actions[i], 0, guess) ] new_Element_list = fill_guess_row( new_LRTWs, table.E, system) action_element_list += new_Element_list else: # make closed 的时候是允许无效的情况存在的 new_Element = fill_sink_row(new_LRTWs, table.E) action_element_list.append(new_Element) element_list.append(action_element_list) table_list = [] situations = [situation for situation in product(*element_list)] for situation in situations: temp_table = ObsTable(deepcopy(table.S), deepcopy(table.R) + deepcopy(list(situation)), deepcopy(table.E), parent=table.table_id, reason="make_closed") if check_table_consistent(temp_table): table_list.append(temp_table) return table_list
def test_DTWs(self, DTWs): self.test_num += 1 tuple_DTWs = tuple(DTWs) if tuple_DTWs in self.cache: return self.cache[tuple_DTWs][0], self.cache[tuple_DTWs][1] self.test_num_cache += 1 DRTWs = [] outputs = [] now_time = 0 cur_state = self.init_state for dtw in DTWs: self.action_num += 1 time = dtw.time + now_time new_LTW = TimedWord(dtw.action, time) flag = False for tran in self.trans: if tran.source == cur_state and tran.is_passing_tran(new_LTW): flag = True cur_state = tran.target if tran.reset: now_time = 0 reset = True else: now_time = time reset = False DRTWs.append(ResetTimedWord(dtw.action, dtw.time, reset)) if cur_state in self.accept_states: outputs.append(1) else: outputs.append(0) break if not flag: DRTWs.append(ResetTimedWord(dtw.action, dtw.time, True)) outputs.append(-1) break # 补全 len_diff = len(DTWs) - len(DRTWs) if len_diff != 0: temp = DTWs[len(DRTWs):] for i in temp: DRTWs.append(ResetTimedWord(i.action, i.time, True)) outputs.append(-1) self.cache[tuple_DTWs] = [DRTWs, outputs] return DRTWs, outputs
def normalize(trace): new_trace = [] for i in trace: if math.modf(float(i.time))[0] == 0.0: time = math.modf(float(i.time))[1] else: time = math.modf(float(i.time))[1] + 0.5 new_trace.append(ResetTimedWord(i.action, time, i.reset)) return new_trace
def test_LTWs(self, LTWs): self.mq_num += 1 if not LTWs: if self.init_state in self.accept_states: value = 1 else: value = 0 return [], value else: LRTWs = [] value = None now_time = 0 cur_state = self.init_state for ltw in LTWs: if ltw.time < now_time: value = -1 LRTWs.append(ResetTimedWord(ltw.action, ltw.time, True)) break else: DTW = TimedWord(ltw.action, ltw.time - now_time) cur_state, value, reset = self.test_DTW( DTW, now_time, cur_state) if reset: LRTWs.append(ResetTimedWord(ltw.action, ltw.time, True)) now_time = 0 else: LRTWs.append( ResetTimedWord(ltw.action, ltw.time, False)) now_time = ltw.time if value == -1: break # 补全 len_diff = len(LTWs) - len(LRTWs) if len_diff != 0: temp = LTWs[len(LRTWs):] for i in temp: LRTWs.append(ResetTimedWord(i.action, i.time, True)) return LRTWs, value
def test_DTWs(self, DTWs): self.test_num += 1 DRTWs = [] value = None now_time = 0 cur_state = self.init_state for dtw in DTWs: time = dtw.time + now_time new_LTW = TimedWord(dtw.action, time) flag = False for tran in self.trans: if tran.source == cur_state and tran.is_passing_tran(new_LTW): flag = True cur_state = tran.target if tran.reset: now_time = 0 reset = True else: now_time = time reset = False DRTWs.append(ResetTimedWord(dtw.action, dtw.time, reset)) break if not flag: DRTWs.append(ResetTimedWord(dtw.action, dtw.time, True)) value = -1 break # 补全 len_diff = len(DTWs) - len(DRTWs) if len_diff != 0: temp = DTWs[len(DRTWs):] for i in temp: DRTWs.append(ResetTimedWord(i.action, i.time, True)) if value != -1: if cur_state in self.accept_states: value = 1 else: value = 0 return DRTWs, value
def is_LRTWs_right(LRTWs, system): if not LRTWs: return True else: now_time = 0 cur_state = system.init_state for lrtw in LRTWs: if lrtw.time < now_time: cur_state = system.sink_state if not lrtw.reset: return False else: LRTW = ResetTimedWord(lrtw.action, lrtw.time, lrtw.reset) flag, cur_state, reset = is_passing_tran( LRTW, cur_state, system) if not flag: return False if reset: now_time = 0 else: now_time = lrtw.time return True
def combine_LRTWS_with_DTWs(DTWs, reset): DRTWs = [] for i in range(len(DTWs)): DRTWs.append(ResetTimedWord(DTWs[i].action, DTWs[i].time, reset[i])) return DRTW_to_LRTW(DRTWs)
def combine_LRTWs_with_LTWs(LTWs, reset): LRTWs = [] for i in range(len(LTWs)): LRTWs.append(ResetTimedWord(LTWs[i].action, LTWs[i].time, reset[i])) return LRTWs