Пример #1
0
def fill(LRTWs, E, reset, system):
    # 调用该函数的时候,要保证LRTWs是有效的
    values = []
    for i in range(len(E)):
        new_LRTWs = LRTWs + combine_LRTWs_with_LTWs(E[i], reset[i])
        if is_LRTWs_valid(new_LRTWs):
            DTWs = LRTW_to_DTW(new_LRTWs)
            outputs = system.test_DTWs_normal(DTWs, True)
            if outputs[-1] == -1:
                if not check_guessed_to_sink(LRTW_to_DRTW(new_LRTWs), system):
                    return False, None
            values.append(outputs[-1])
        else:
            if not is_combined_LRTWs_valid(new_LRTWs):
                return False, None
            else:
                valid_LRTWs = get_valid_LRTWs(new_LRTWs)
                DTWs = LRTW_to_DTW(valid_LRTWs)
                outputs = system.test_DTWs_normal(DTWs, True)
                if outputs[-1] == -1:
                    return False, None
                values.append(-1)
    return True, Element(LRTWs, values, reset)
Пример #2
0
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
Пример #3
0
def deal_ctx(table, ctx, system):
    table_elements = [s for s in table.S] + [r for r in table.R]
    S_R_LRTWs = [s.LRTWs for s in table.S] + [r.LRTWs for r in table.R]

    temp_list = []
    for i in range(len(ctx)):
        outputs = system.test_DTWs_normal(ctx[:i + 1])
        if outputs[-1] == -1:
            temp_list.append([True])
        else:
            temp_list.append([True, False])
    resets_guess = [list(situation) for situation in product(*temp_list)]

    table_list = []
    for resets in resets_guess:
        LRTWs = combine_LRTWS_with_DTWs(ctx, resets)
        if check_guessed_reset(LRTWs, table_elements):
            element_list = []
            for j in range(len(LRTWs)):
                cur_LRTWs_elements = []
                temp_LRTWs = LRTWs[:j + 1]
                if temp_LRTWs in S_R_LRTWs:
                    continue
                if system.test_DTWs_normal(LRTW_to_DTW(temp_LRTWs),
                                           True)[-1] == -1:
                    system.mq_num -= 1
                    new_Element = fill_sink_row(temp_LRTWs, table.E)
                    cur_LRTWs_elements.append(new_Element)
                else:
                    new_Element_list = fill_guess_row(temp_LRTWs, table.E,
                                                      system)
                    cur_LRTWs_elements += new_Element_list
                element_list.append(cur_LRTWs_elements)
            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="add_ctx")
                if check_table_consistent(temp_table):
                    table_list.append(temp_table)
    return table_list
Пример #4
0
def make_consistent(prefix_LTWs, e_index, reset_i, reset_j, index_i, index_j,
                    table, system):
    table_elements = [s for s in table.S] + [r for r in table.R]
    new_e_LTWs = prefix_LTWs + table.E[e_index]
    table.E.append(new_e_LTWs)

    resets = []
    for i in range(len(table_elements)):
        select = []
        if i == index_i:
            select.append(reset_i)
        elif i == index_j:
            select.append(reset_j)
        else:
            if table_elements[i].values[0] == -1:
                select.append([True] * len(new_e_LTWs))
            else:
                temp_list = [[True, False]] * len(new_e_LTWs)
                select = [list(situation) for situation in product(*temp_list)]
                # 再一次进行过滤,去除无效的猜测
                temp_select = []
                for temp in select:
                    new_LRTWs = table_elements[
                        i].LRTWs + combine_LRTWs_with_LTWs(new_e_LTWs, temp)
                    if is_combined_LRTWs_valid(new_LRTWs):
                        temp_select.append(temp)
                select = temp_select
        resets.append(select)
    resets_list = [list(temp) for temp in product(*resets)]

    tables = []
    for reset_temp in resets_list:
        flag = True
        temp_table = ObsTable(deepcopy(table.S),
                              deepcopy(table.R),
                              deepcopy(table.E),
                              parent=table.table_id,
                              reason="make_consistent")
        for i in range(len(temp_table.S)):
            new_LRTWs = temp_table.S[i].LRTWs + combine_LRTWs_with_LTWs(
                new_e_LTWs, reset_temp[i])
            if temp_table.S[i].values[0] != -1:
                if is_LRTWs_valid(new_LRTWs):
                    DTWs = LRTW_to_DTW(new_LRTWs)
                    outputs = system.test_DTWs_normal(DTWs, True)
                    if outputs[-1] == -1:
                        if not check_guessed_to_sink(LRTW_to_DRTW(new_LRTWs),
                                                     system):
                            flag = False
                            break
                    temp_table.S[i].values.append(outputs[-1])
                    temp_table.S[i].suffixes_resets.append(reset_temp[i])
                else:
                    valid_LRTWs = get_valid_LRTWs(new_LRTWs)
                    DTWs = LRTW_to_DTW(valid_LRTWs)
                    outputs = system.test_DTWs_normal(DTWs, True)
                    if outputs[-1] == -1:
                        flag = False
                        break
                    temp_table.S[i].values.append(-1)
                    temp_table.S[i].suffixes_resets.append(reset_temp[i])
            else:
                temp_table.S[i].values.append(-1)
                temp_table.S[i].suffixes_resets.append(reset_temp[i])
        if flag:
            s_length = len(temp_table.S)
            for j in range(len(temp_table.R)):
                new_LRTWs = temp_table.R[j].LRTWs + combine_LRTWs_with_LTWs(
                    new_e_LTWs, reset_temp[s_length + j])
                if temp_table.R[j].values[0] != -1:
                    if is_LRTWs_valid(new_LRTWs):
                        DTWs = LRTW_to_DTW(new_LRTWs)
                        outputs = system.test_DTWs_normal(DTWs, True)
                        if outputs[-1] == -1:
                            if not check_guessed_to_sink(
                                    LRTW_to_DRTW(new_LRTWs), system):
                                flag = False
                                break
                        temp_table.R[j].values.append(outputs[-1])
                        temp_table.R[j].suffixes_resets.append(
                            reset_temp[s_length + j])
                    else:
                        valid_LRTWs = get_valid_LRTWs(new_LRTWs)
                        DTWs = LRTW_to_DTW(valid_LRTWs)
                        outputs = system.test_DTWs_normal(DTWs, True)
                        if outputs[-1] == -1:
                            flag = False
                            break
                        temp_table.R[j].values.append(-1)
                        temp_table.R[j].suffixes_resets.append(
                            reset_temp[s_length + j])
                else:
                    temp_table.R[j].values.append(-1)
                    temp_table.R[j].suffixes_resets.append(
                        reset_temp[s_length + j])
        if flag and check_table_consistent(temp_table):
            tables.append(temp_table)
    return tables