def analysis_iconditions_by_nonempty_isets_numbers(icondition_file,
                                                   min_ne_iset_number,
                                                   max_ne_iset_number):
    conditions = isu.load_iconditions_from_file(icondition_file)
    conditions = isu.parse_iconditions_ignore_singletons(conditions)
    ne_iset_conditions = dict()
    ne_iset_ids = dict()
    for i in range(min_ne_iset_number, max_ne_iset_number + 1):
        nc = list()
        ne_iset_conditions[i] = nc
        nc = list()
        ne_iset_ids[i] = nc

    for ic in conditions:
        number = isu.get_ne_iset_number(ic)
        if number == 0:
            continue
        ne_iset_conditions[number].append(ic)
        ids = isu.get_ne_iset_ids(ic)
        ne_iset_ids[number].append(ids)

    chains = list()
    for i in range(min_ne_iset_number + 1, max_ne_iset_number + 1):
        for ids in ne_iset_ids[i]:
            chain = find_parent_icondition(ne_iset_ids[i - 1], ids)
            if chain == -1:
                print("unchained iconditions non empty isets", ids)
            else:
                chains.append(chain)
                print(chain)

    for i in ne_iset_conditions:
        print(
            "non empty isets number %d, has %d iconditions, iset ids: " %
            (i, len(ne_iset_conditions[i])), ne_iset_ids[i])
Exemple #2
0
def search(k_size,
           m_size,
           n_size,
           is_use_extended_rules,
           is_check_valid_rule=False,
           lp_type="lpmln"):
    logging.info("basic isp searching start!")
    validator = ISetConditionValidator(
        is_use_extended_rules=is_use_extended_rules, lp_type=lp_type)
    rule_set_number = 3
    if is_use_extended_rules:
        rule_set_number = 4

    iset_number = isu.compute_iset_number_from_kmn(k_size, m_size, n_size,
                                                   rule_set_number)
    searching_space_size = 2**iset_number
    se_conditions = list()
    non_se_conditions = list()
    print_loop = 1000
    search_cnt = 0
    for sid in range(searching_space_size):
        # is_contain_valid, is_se_sat, condition = validator.validate_isets_kmn_program_from_icondition_id(
        #     sid, k_size, m_size, n_size, is_check_valid_rule=is_check_valid_rule)
        icondition = isu.construct_iset_condition_from_icondition_id(
            sid, iset_number)
        is_contain_valid, is_se_sat, condition = validator.validate_kmn_extended_iset_condition_return_isetcondition_str(
            icondition, k_size, m_size, n_size, is_check_valid_rule)

        if not is_contain_valid:
            if is_se_sat:
                se_conditions.append(condition)
            else:
                non_se_conditions.append(condition)
        search_cnt += 1
        if search_cnt % print_loop == 0:
            logging.info("search progress: %d / %d" %
                         (search_cnt, searching_space_size))

    logging.info("search progress: %d / %d" %
                 (search_cnt, searching_space_size))

    print(
        "%d-%d-%d problem: found %d SE-condition, found %d non-SE-condition" %
        (k_size, m_size, n_size, len(se_conditions), len(non_se_conditions)))

    print("\nSE-conditions (%d): " % len(se_conditions))
    for c in se_conditions:
        print("\t", c)

    print("\nnon-SE-conditions (%d): " % len(non_se_conditions))
    for c in non_se_conditions:
        print("\t", c)

    se_ic_file = config.get_isc_results_file_path(0, 1, 0, 1, 7)
    nse_ic_file = se_ic_file + ".nse"
    save_iconditions(se_ic_file, se_conditions)
    save_iconditions(nse_ic_file, non_se_conditions)

    return se_conditions, non_se_conditions
Exemple #3
0
    def generate_all_new_rules(self, rule):
        atoms = isu.get_universe(rule)
        new_rules = list()
        isets = isu.compute_isets_for_sets(rule, self.is_use_extended_rules)
        for at in atoms:
            del_set = {at}
            nr = list()
            for s in rule:
                nr.append(s.difference(del_set))
            new_rules.append(nr)

        return new_rules
def check_tuple(ne_isets, rule_number, is_use_extended_rules=False):
    rs = 3
    if is_use_extended_rules:
        rs = 4
    iset_number = 2**(rs * rule_number) - 1
    icondition = isu.construct_iset_condition_from_non_emtpy_iset_ids(
        ne_isets, iset_number)
    isets = isu.construct_isets_from_iset_condition(icondition,
                                                    is_use_extended_rules, 1)
    rules = isu.construct_rules_from_isets(isets, rule_number, rs)
    for r in rules:
        print(r)
 def validate_isets_kmn_program_from_icondition_id(
         self,
         icondition_id,
         k_size,
         m_size,
         n_size,
         is_check_valid_rule=True):
     iset_number = isu.compute_iset_number_from_kmn(k_size, m_size, n_size,
                                                    self.rule_set_size)
     icondition = isu.construct_iset_condition_from_icondition_id(
         icondition_id, iset_number)
     return self.validate_isets_kmn_program_from_iset_condition_return_str(
         icondition, k_size, m_size, n_size, is_check_valid_rule)
 def validate_isets_kmn_program_from_non_empty_ids_return_obj(
         self,
         non_empty_ids,
         k_size,
         m_size,
         n_size,
         is_check_valid_rule=True):
     iset_number = isu.compute_iset_number_from_kmn(k_size, m_size, n_size,
                                                    self.rule_set_size)
     icondition = isu.construct_iset_condition_from_non_emtpy_iset_ids(
         non_empty_ids, iset_number)
     return self.validate_isets_kmn_program_from_iset_condition_return_obj(
         icondition, k_size, m_size, n_size, is_check_valid_rule)
 def validate_kmn_extended_iset_condition_from_non_emtpy_iset_ids_return_icondition_str(
         self,
         non_emtpy_iset_ids,
         k_size,
         m_size,
         n_size,
         is_check_valid_rule=True):
     iset_number = isu.compute_iset_number_from_kmn(k_size, m_size, n_size,
                                                    self.rule_set_size)
     icondition = isu.construct_iset_condition_from_non_emtpy_iset_ids(
         non_emtpy_iset_ids, iset_number)
     return self.validate_kmn_extended_iset_condition_return_isetcondition_str(
         icondition, k_size, m_size, n_size, is_check_valid_rule)
def get_dnf_from_iconditions(file, ignore_isets):
    iconditions = isu.load_iconditions_from_file(file)
    iconditions = isu.parse_iconditions_ignore_singletons(iconditions)
    iset_size = len(iconditions[0])
    chars = ["a" + str(i) for i in range(iset_size)]
    symbol_chars = " ".join(chars)
    syms = symbols(symbol_chars)
    print(chars)
    left = false
    right = false
    for ic in iconditions:
        right = get_conjunction_formula(ic, syms, ignore_isets)
        left = Or(left, right)
    return left
    def get_validate_kmn_extended_isets_from_non_emtpy_iset_ids(
            self, non_emtpy_iset_ids, k_size, m_size, n_size):
        iset_number = isu.compute_iset_number_from_kmn(k_size, m_size, n_size,
                                                       self.rule_set_size)
        icondition = isu.construct_iset_condition_from_non_emtpy_iset_ids(
            non_emtpy_iset_ids, iset_number)
        isets = isu.construct_isets_from_iset_condition(
            icondition, self.is_use_extended_rules)

        all_kmn_isets = list()
        all_kmn_isets.append((-1, isets))
        for ne in non_emtpy_iset_ids:
            all_kmn_isets.append((ne, copy.deepcopy(isets)))

        return all_kmn_isets
    def check_se_relation_for_one_rule(self, rule, atom_universe):
        se_case_results = dict()

        ind_sets = isu.compute_isets_for_sets(rule)
        for key in ind_sets:
            iset = ind_sets[key]
            case_flag = self.get_case_by_intersection_sets(iset.intersect_sets)
            if case_flag not in se_case_results:
                se_case_results[case_flag] = list()

            if len(iset.members) > 1:
                iset_list = list(iset.members)
                del_at = set(iset_list[0:1])
                interpretation_atoms = atom_universe.difference(del_at)
                se_results = self.check_relations_for_one_rule_and_select_iset(
                    interpretation_atoms, del_at, rule, case_flag)
                se_case_results[case_flag].extend(se_results)

                # debug info
                # if case_flag == "100":
                #     print("\n\t rule: ", rule, "del atom: ", del_at)
                #     print("\t atoms: ", interpretation_atoms)
                #     print("\t iset: ", iset)
                #     print("se results: ", se_results)

        return se_case_results
def check_01_distributions_of_iconditions(file):
    iconditions = isu.load_iconditions_from_file(file)
    iconditions = isu.parse_iconditions_ignore_singletons(iconditions)
    iset_size = len(iconditions[0])
    zero_cnts = [0] * iset_size
    one_cnts = [0] * iset_size

    for col in range(iset_size):
        zero_c = 0
        one_c = 0
        for ic in iconditions:
            if ic[col] == 0:
                zero_c += 1
            elif ic[col] == 1:
                one_c += 1
        zero_cnts[col] = zero_c
        one_cnts[col] = one_c

    for col in range(iset_size):
        print("col-%d: zero = %d, one = %d, sum = %d" %
              (col, zero_cnts[col], one_cnts[col],
               zero_cnts[col] + one_cnts[col]))

    all_zero_cols = []
    all_ones_cols = []
    one_zero_eq_cols = []
    for col in range(iset_size):
        zero = zero_cnts[col]
        one = one_cnts[col]

        if one == 0:
            print("col-%d all zeros" % col)
            all_zero_cols.append(col)
        elif zero == 0:
            print("col-%d all ones" % col)
            all_ones_cols.append(col)
        elif one == zero:
            one_zero_eq_cols.append(col)

    print("all zeros cols ", all_zero_cols)
    print("all ones cols ", all_ones_cols)
    print("one zero eq cols", one_zero_eq_cols)
 def validate_isets_kmn_program_from_iset_condition_return_obj(
         self,
         icondition,
         k_size,
         m_size,
         n_size,
         is_check_valid_rule=True):
     isets = isu.construct_isets_from_iset_condition(
         icondition, self.is_use_extended_rules, iset_atom_number=1)
     is_contain_valid, is_se_sat = self.validate_isets_kmn_program(
         isets, k_size, m_size, n_size, is_check_valid_rule)
     icondition_obj = ISetCondition(icondition, list())
     return is_contain_valid, is_se_sat, icondition_obj
Exemple #13
0
    def parallel_validate_kmn_extended_icondition(cls, ne_isets, itask,
                                                  ht_validator):
        print("enter parallel validate ")
        is_use_extended_rules = itask.is_use_extended_rules
        icondition = isu.construct_iset_condition_from_non_emtpy_iset_ids(
            ne_isets, itask.iset_number)
        isets = isu.construct_isets_from_iset_condition(
            icondition, is_use_extended_rules)
        is_check_valid_rule = False
        singleton_iset_ids = list()
        validated_condition = ISetCondition(icondition, singleton_iset_ids)
        is_contain_valid, is_se_sat = ht_validator.validate_isets_kmn_program(
            isets, *itask.k_m_n, is_check_valid_rule)

        if not is_se_sat:
            return is_se_sat, validated_condition

        k_size = itask.k_m_n[0]
        m_size = itask.k_m_n[1]
        n_size = itask.k_m_n[2]
        ht_jobs = list()

        # manager = Manager()
        # singleton_results = manager.list()

        for sid in ne_isets:
            extended_isets = copy.deepcopy(isets)
            p = Process(target=cls.validate_extended_icondition,
                        args=(k_size, m_size, n_size, itask.lp_type,
                              itask.is_use_extended_rules, extended_isets,
                              sid + 1))  #, singleton_results))
            ht_jobs.append(p)
            p.start()

        for p in ht_jobs:
            p.join()

        # singleton_iset_ids.extend(singleton_results)
        return is_se_sat, validated_condition
    def check_se_relation_for_one_rule(self, rule, atom_universe):
        se_case_results = dict()
        ind_sets = isu.compute_isets_for_sets(rule, self.is_use_extended_rules)
        for key in ind_sets:
            iset = ind_sets[key]
            case_flag = self.get_case_by_intersection_sets(iset.intersect_sets)
            if case_flag not in se_case_results:
                se_case_results[case_flag] = list()

            iset_atoms = iset.members
            if len(iset_atoms) > 0:
                # if len(iset_atoms) == 0:
                add_at = set([-1])
                # interpretation_atoms = atom_universe.union(add_at)
                se_results = self.check_relations_for_one_rule_and_select_iset(
                    atom_universe, add_at, rule, case_flag)
                se_case_results[case_flag].extend(se_results)

        return se_case_results
    def validate_kmn_extended_icondition_return_isetcondition_obj(
            self,
            icondition,
            k_size,
            m_size,
            n_size,
            is_check_valid_rule=True):
        isets = isu.construct_isets_from_iset_condition(
            icondition, self.is_use_extended_rules)
        singleton_iset_ids = list()
        validated_condition = ISetCondition(icondition, singleton_iset_ids)
        is_contain_valid, is_se_sat = self.validate_isets_kmn_program(
            isets, k_size, m_size, n_size, is_check_valid_rule)

        if is_contain_valid or not is_se_sat:
            return is_contain_valid, is_se_sat, validated_condition

        non_empty_iset_keys = list()
        for key in isets:
            if len(isets[key].members) != 0:
                non_empty_iset_keys.append(key)
        new_atom = 0

        for nid in non_empty_iset_keys:
            extended_isets = copy.deepcopy(isets)
            extended_isets[nid].members.add(new_atom)
            is_contain_valid, is_se_sat = self.validate_isets_kmn_program(
                extended_isets,
                k_size,
                m_size,
                n_size,
                is_check_valid_rule=False)

            # TODO: nid 是实际的iset id, 而icondition中的集合id cid = iset_id + 1, 目的是为了从0开始索引,因此nid需要减一
            # TODO: the nid is real iset id, the id in icondition is iset_id - 1
            if not is_se_sat:
                singleton_iset_ids.append(nid - 1)

        is_se_sat = True
        return is_contain_valid, is_se_sat, validated_condition
    def validate_isets_kmn_program(self,
                                   isets,
                                   k_size,
                                   m_size,
                                   n_size,
                                   is_check_valid_rule=True):
        kmn = isu.construct_kmn_program_from_isets(isets, k_size, m_size,
                                                   n_size, self.rule_set_size)
        is_contain_valid_rule = False
        is_se_sat = False

        if is_check_valid_rule:
            for prg in kmn:
                if self.lp_se.is_contain_se_valid_rule(self.lp_se, prg):
                    is_contain_valid_rule = True
                    break

            if is_contain_valid_rule:
                return is_contain_valid_rule, is_se_sat

        is_se_sat = self.lp_se.se_check_kmn_program(*kmn)
        return is_contain_valid_rule, is_se_sat
    def check_one_rule_and_seint(self, rule, here, there):
        new_atom = self.atom_size + 1
        rule_isets = isu.compute_isets_for_sets(rule,
                                                self.is_use_extended_rules)
        results = dict()
        new_rules = self.generate_all_new_rules(rule)
        for key in new_rules:
            if key in results:
                tmp = results[key]
            else:
                tmp = self.generate_empty_table()
                results[key] = tmp

            new_rule = new_rules[key]

            sat_original_rule = self.lp_type.se_satisfy_rule(
                self.lp_type, here, there, rule)
            sat_new_rule = self.lp_type.se_satisfy_rule(
                self.lp_type, here, there, new_rule)
            tmp[int(sat_original_rule)].add(int(sat_new_rule))

        return results
def kmn_isc_task_worker(isc_config_file="isets-tasks.json", worker_name="", is_check_valid_rules=True, lp_type="lpmln", is_use_extended_rules=True):
    ISCFileTaskWorkerQueueManager.register("get_task_queue")
    ISCFileTaskWorkerQueueManager.register("get_result_queue")
    manager = ISCFileTaskWorkerQueueManager(address=(config.task_host, config.task_host_port),
                                            authkey=bytes(config.task_host_key, encoding="utf-8"))
    manager.connect()
    task_queue = manager.get_task_queue()
    result_queue = manager.get_result_queue()

    time_fmt = "%Y-%m-%d %H:%M:%S.%f"
    worker_host_name = config.worker_host_name
    msg_text = "task worker %s start!" % (worker_name)
    logging.info(msg_text)

    isc_tasks = isc_cfg.ISCTaskConfig(isc_config_file, is_use_extended_rules)
    isc_tasks = isc_tasks.isc_tasks
    processed_task_slices_number = 0

    while True:
        if not pathlib.Path(config.task_host_lock_file).exists():
            break

        if task_queue.empty():
            time.sleep(5)
            continue

        itask = task_queue.get()
        if itask[0] == kill_signal:
            msg_text = "%s:%s isc task worker terminate ..." % (worker_host_name, worker_name)
            logging.info(msg_text)
            break

        start_time = datetime.now()
        start_time_str = start_time.strftime(time_fmt)[:-3]

        isc_task_id = itask[0]
        it = isc_tasks[isc_task_id]
        k_size = it.k_m_n[0]
        m_size = it.k_m_n[1]
        n_size = it.k_m_n[2]
        unknown_iset_number = it.unknown_iset_number
        empty_iset_ids = it.empty_iset_ids

        task_details = itask[1]
        task_start = task_details[0]
        isc_begin = task_start.split(",")
        isc_begin = [int(s) for s in isc_begin]
        task_number = task_details[1]

        task_name = worker_name + ("-task-%d" % processed_task_slices_number)
        ne_number = len(isc_begin)

        msg_text = "%s: %d-%d-%d isc task: from %s length %d, nonempty iset number %d" % (
            task_name, k_size, m_size, n_size, task_start, task_number, ne_number)
        logging.info(msg_text)

        task_counter = CombinaryCounter(ne_number, unknown_iset_number)
        task_counter.reset_current_indicator(isc_begin)
        se_cdt_cnt = 0

        se_conditions_cache = list()
        validator = ISetConditionValidator(lp_type=lp_type, is_use_extended_rules=is_use_extended_rules)

        for i in range(task_number):
            task_idx = task_counter.get_current_indicator()
            non_ne_ids = isu.get_real_nonempty_iset_ids_from_partial_nonemtpy_iset_ids(task_idx, empty_iset_ids)

            is_contain_valid_rule, is_strongly_equivalent, condition = \
                validator.validate_kmn_extended_iset_condition_from_non_emtpy_iset_ids_return_icondition_str(
                    non_ne_ids, k_size, m_size, n_size, is_check_valid_rule=is_check_valid_rules)

            if not is_contain_valid_rule and is_strongly_equivalent:
                se_conditions_cache.append(condition)
                se_cdt_cnt += 1

        for sec in se_conditions_cache:
            result_queue.put((condition_signal, isc_task_id, sec))

        end_time = datetime.now()
        end_time_str = end_time.strftime(time_fmt)[:-3]
        msg_text = "%s, end %d-%d-%d isc tasks from %s length %d, start time %s, end time %s, find %d se conditions" % (
            task_name, k_size, m_size, n_size, task_start, task_number, start_time_str, end_time_str, se_cdt_cnt)
        logging.info(msg_text)
        result_queue.put((stat_signal, isc_task_id, task_number, (start_time, end_time)))
        processed_task_slices_number += 1

    logging.info("%s processes %d isc task slices" % (worker_name, processed_task_slices_number))
Exemple #19
0
def check_011_programs():
    program = [[{3}, set(), {2}], [{2, 3}, set(), {2}]]

    isets = isu.compute_isets_from_program(program, False)
    for s in isets:
        print(isets[s])