def __connect_non_conflict_links(self, non_conflict_links):
     # 2. Others, evaluate the weighted average of truth value between
     # the duplicate links.
     for merged_atom in self.ret:
         for links in non_conflict_links:
             # array elements are same except original node information.
             link_key_sample = links[0]
             eq_link.key_to_link(self.a, link_key_sample, merged_atom, get_weighted_tv(links))
Beispiel #2
0
    def __connect_links(self, best_interaction_information_link):
        """Connect selected links to new blend atom.

        Args:
            best_interaction_information_link: Selected link set.
            :param best_interaction_information_link: list[EqualLinkKey]
        """
        for link in best_interaction_information_link:
            eq_link.key_to_link(self.a, link, self.ret[0], link.tv)
Beispiel #3
0
 def __connect_non_conflict_links(self, non_conflict_links):
     # 2. Others, evaluate the weighted average of truth value between
     # the duplicate links.
     for merged_atom in self.ret:
         for links in non_conflict_links:
             # array elements are same except original node information.
             link_key_sample = links[0]
             eq_link.key_to_link(self.a, link_key_sample, merged_atom,
                                 get_weighted_tv(links))
    def __connect_links(self, best_interaction_information_link):
        """Connect selected links to new blend atom.

        Args:
            best_interaction_information_link: Selected link set.
            :param best_interaction_information_link: list[EqualLinkKey]
        """
        for link in best_interaction_information_link:
            eq_link.key_to_link(self.a, link, self.ret[0], link.tv)
Beispiel #5
0
    def __connect_links(self, non_conflict_links, non_duplicate_links):
        """Connect the every links to new blend.

        Args:
            conflict_links: Conflicted link tuples list.
            non_duplicate_links: Non-duplicated links list.
            :param non_conflict_links: list[EqualLinkKey]
            :param non_duplicate_links: list[EqualLinkKey]
        """
        for i, merged_atom in enumerate(self.ret):
            # Just copy.
            for link in non_conflict_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            for link in non_duplicate_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
    def __connect_links(self, non_conflict_links, non_duplicate_links):
        """Connect the every links to new blend.

        Args:
            conflict_links: Conflicted link tuples list.
            non_duplicate_links: Non-duplicated links list.
            :param non_conflict_links: list[EqualLinkKey]
            :param non_duplicate_links: list[EqualLinkKey]
        """
        for i, merged_atom in enumerate(self.ret):
            # Just copy.
            for link in non_conflict_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            for link in non_duplicate_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
    def __generate_information_probability(
            self,
            related_node_target_links
    ):
        """Calculate probabilities with target links.

        It creates the entropy and probabilities from related nodes in
        whole AtomSpace.

        Args:
            related_node_target_links: Target link tuples in related node list
            :param related_node_target_links: list[list[EqualLinkKey]]
        Returns:
            The max value of n_gram.
            :rtype : int
        """
        log.debug(
            "ConnectConflictInteractionInformation: Calculating probabilities "
            "(Total: " + str(len(related_node_target_links)) + ")"
        )

        current_ratio = 0
        # Register the every link in related nodes to provider.
        for i, target_equal_link in enumerate(related_node_target_links):
            current_ratio = self.__print_progress(
                "ConnectConflictInteractionInformation:PROB:",
                current_ratio, i, len(related_node_target_links), 30
            )
            # TODO: To prevent freeze during probability generation,
            # user can limit the max value of calculation.
            max_repeat_length = self.provider.n_gram \
                if 0 < self.provider.n_gram < len(target_equal_link) \
                else len(target_equal_link)

            # Make n-gram data in each related node.
            # The provider with high n-gram will provides more correct data,
            # but speed will going slower rapidly.
            # (0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ... (1, 1, 1)
            cartesian_binary_iterator = \
                itertools.product([False, True], repeat=max_repeat_length)

            for viable_case_binary in enumerate(cartesian_binary_iterator):
                gram_data = list()
                for j, selector in enumerate(viable_case_binary):
                    # Make each gram data.
                    if selector:
                        gram_data.append(eq_link.key_to_link(
                            self.a,
                            target_equal_link[j],
                            self.ret[0],
                            target_equal_link[j].tv
                        ))
                # Register the generated gram_data.
                self.provider.add_one_rawdata_count(
                    [data for data in gram_data if data is not None], 1
                )

        # Update provider's statistic data.
        PyProbabilityAtom().calculate_probabilities(self.provider)
        PyEntropyAtom().calculate_entropies(self.provider)
    def __connect_conflict_links(self, conflict_links):
        # 1-b. Prepare cartesian product iterator.
        # if number of conflict_links is 3, this iterator produces:
        # (0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ... (1, 1, 1)
        cartesian_binary_iterator = \
            itertools.product([0, 1], repeat=len(conflict_links))

        # 1-c. Connect to each viable atoms.
        for i, viable_case_binary in enumerate(cartesian_binary_iterator):
            for j, selector in enumerate(viable_case_binary):
                eq_link.key_to_link(
                    self.a,
                    conflict_links[j][selector],
                    self.ret[i],
                    conflict_links[j][selector].tv
                )
    def __connect_links(self, conflict_link_cases, non_conflict_links,
                        non_duplicate_links):
        """Connect the randomly selected links from each conflict link case to
        new blend atom.

        Args:
            conflict_link_cases: conflicted links list.
            non_conflict_link_cases: non-conflicted links list.
            non_duplicate_link_cases: non-duplicated links list.
            :param conflict_link_cases: list[list[EqualLinkKey]]
            :param non_conflict_links: list[EqualLinkKey]
            :param non_conflict_links: list[EqualLinkKey]
        """
        for i, merged_atom in enumerate(self.ret):
            # 1. Choose one link randomly in each conflict link set.
            for conflict_link_case in conflict_link_cases:
                link = random.choice(conflict_link_case)
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 2. Others, connect link with weighted average of truth value
            # between the duplicate links.
            for link in non_conflict_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 3. Just copy.
            for link in non_duplicate_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
    def __connect_links(self, conflict_link_cases, non_conflict_links,
                        non_duplicate_links):
        """Connect the randomly selected links from each conflict link case to
        new blend atom.

        Args:
            conflict_link_cases: conflicted links list.
            non_conflict_link_cases: non-conflicted links list.
            non_duplicate_link_cases: non-duplicated links list.
            :param conflict_link_cases: list[list[EqualLinkKey]]
            :param non_conflict_links: list[EqualLinkKey]
            :param non_conflict_links: list[EqualLinkKey]
        """
        for i, merged_atom in enumerate(self.ret):
            # 1. Choose one link randomly in each conflict link set.
            for conflict_link_case in conflict_link_cases:
                link = random.choice(conflict_link_case)
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 2. Others, connect link with weighted average of truth value
            # between the duplicate links.
            for link in non_conflict_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 3. Just copy.
            for link in non_duplicate_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
Beispiel #11
0
    def __generate_information_probability(self, related_node_target_links):
        """Calculate probabilities with target links.

        It creates the entropy and probabilities from related nodes in
        whole AtomSpace.

        Args:
            related_node_target_links: Target link tuples in related node list
            :param related_node_target_links: list[list[EqualLinkKey]]
        Returns:
            The max value of n_gram.
            :rtype : int
        """
        log.debug(
            "ConnectConflictInteractionInformation: Calculating probabilities "
            "(Total: " + str(len(related_node_target_links)) + ")")

        current_ratio = 0
        # Register the every link in related nodes to provider.
        for i, target_equal_link in enumerate(related_node_target_links):
            current_ratio = self.__print_progress(
                "ConnectConflictInteractionInformation:PROB:", current_ratio,
                i, len(related_node_target_links), 30)
            # TODO: To prevent freeze during probability generation,
            # user can limit the max value of calculation.
            max_repeat_length = self.provider.n_gram \
                if 0 < self.provider.n_gram < len(target_equal_link) \
                else len(target_equal_link)

            # Make n-gram data in each related node.
            # The provider with high n-gram will provides more correct data,
            # but speed will going slower rapidly.
            # (0, 0, 0), (0, 0, 1), (0, 1, 0), (0, 1, 1), ... (1, 1, 1)
            cartesian_binary_iterator = \
                itertools.product([False, True], repeat=max_repeat_length)

            for viable_case_binary in enumerate(cartesian_binary_iterator):
                gram_data = list()
                for j, selector in enumerate(viable_case_binary):
                    # Make each gram data.
                    if selector:
                        gram_data.append(
                            eq_link.key_to_link(self.a, target_equal_link[j],
                                                self.ret[0],
                                                target_equal_link[j].tv))
                # Register the generated gram_data.
                self.provider.add_one_rawdata_count(
                    [data for data in gram_data if data is not None], 1)

        # Update provider's statistic data.
        PyProbabilityAtom().calculate_probabilities(self.provider)
        PyEntropyAtom().calculate_entropies(self.provider)
    def __connect_links(self, conflict_link_cases, non_conflict_links, non_duplicate_links):
        """Connect the every viable cases of links to new blends.

        Args:
            conflict_link_cases: conflicted links list.
            non_conflict_link_cases: non-conflicted links list.
            non_duplicate_link_cases: non-duplicated links list.
            :param conflict_link_cases: list[list[EqualLinkKey]]
            :param non_conflict_links: list[EqualLinkKey]
            :param non_conflict_links: list[EqualLinkKey]
        """
        for i, merged_atom in enumerate(self.ret):
            # 1. Connect to each viable atoms.
            for conflict_link_case in conflict_link_cases:
                for link in conflict_link_case:
                    eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 2. Others, connect link with weighted average of truth value
            # between the duplicate links.
            for link in non_conflict_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 3. Just copy.
            for link in non_duplicate_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
    def __connect_links(self, conflict_link_cases, non_conflict_links,
                        non_duplicate_links):
        """Connect the every viable cases of links to new blends.

        Args:
            conflict_link_cases: conflicted links list.
            non_conflict_link_cases: non-conflicted links list.
            non_duplicate_link_cases: non-duplicated links list.
            :param conflict_link_cases: list[list[EqualLinkKey]]
            :param non_conflict_links: list[EqualLinkKey]
            :param non_conflict_links: list[EqualLinkKey]
        """
        for i, merged_atom in enumerate(self.ret):
            # 1. Connect to each viable atoms.
            for conflict_link_case in conflict_link_cases:
                for link in conflict_link_case:
                    eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 2. Others, connect link with weighted average of truth value
            # between the duplicate links.
            for link in non_conflict_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
            # 3. Just copy.
            for link in non_duplicate_links:
                eq_link.key_to_link(self.a, link, merged_atom, link.tv)
Beispiel #14
0
    def __evaluate_interaction_information(
        self,
        decided_atoms,
        conflict_link_cases,
        non_conflict_link_cases,
        non_duplicate_link_cases,
    ):
        """Evaluate interaction information value for each available conflict
        link cases, and returns one link set has maximum information value.

        Args:
            decided_atoms: The source atoms to make new atom.
            conflict_link_cases: Conflicted link tuples list.
            non_conflict_link_cases: Non-conflict links list.
            non_duplicate_link_cases: Non-duplicated links list.
            :param decided_atoms: list[Atom]
            :param conflict_link_cases: list[list[EqualLinkKey]]
            :param non_conflict_link_cases: list[EqualLinkKey]
            :param non_duplicate_link_cases: list[EqualLinkKey]
        Returns:
            A link set has maximum interaction information value.
            :rtype: list[EqualLinkKey]
        """
        result_list = list()

        inheritance_nodes = list()
        for decided_atom in decided_atoms:
            inheritance_nodes += find_inheritance_nodes(self.a, decided_atom)

        # TODO: To prevent freeze during interaction information generation,
        # user can limit the max value of calculation.
        max_repeat_length = self.evaluate_n_gram_limit \
            if self.evaluate_n_gram_limit < self.provider.n_gram \
            else self.provider.n_gram

        log.debug("ConnectConflictInteractionInformation: " +
                  "Calculating interaction information " + "(Total: " +
                  str(len(conflict_link_cases)) + ")")

        current_ratio = 0
        for i, conflict_link in enumerate(conflict_link_cases):
            current_ratio = self.__print_progress(
                "ConnectConflictInteractionInformation:II:", current_ratio, i,
                len(conflict_link_cases), 25)
            merged_links = list()
            merged_links.extend(non_conflict_link_cases)
            merged_links.extend(non_duplicate_link_cases)
            merged_links.extend(conflict_link)

            # Calculate n-gram data in each available link cases.
            # The provider with high n-gram will provides more correct data,
            # but speed will going slower rapidly.
            filtered_merged_links = \
                map(lambda x:
                    eq_link.key_to_link(self.a, x, self.ret[0], x.tv),
                    filter(lambda x:
                           # TODO: Currently connector excludes
                           # an InheritanceLink to get valuable(funny) result.
                           self.a[x.h].out[0] not in inheritance_nodes and
                           # Manually throw away the links have low strength.
                           (x.tv.mean > self.inter_info_strength_above_limit),
                           merged_links
                           )
                    )

            if len(filtered_merged_links) is 0:
                continue

            interaction_information = PyInteractionInformationAtom(). \
                calculate_interaction_information(
                filtered_merged_links,
                self.provider,
                max_repeat_length
            )

            result_list.append({
                "merged_links":
                merged_links,
                "filtered_merged_links":
                filtered_merged_links,
                "interaction_information":
                interaction_information
            })

        if len(result_list) < 1:
            self.last_status = blending_status.EMPTY_RESULT
            return []

        result_list = sorted(result_list,
                             key=(lambda x: x["interaction_information"]),
                             reverse=True)

        self.__make_result_log(result_list, reverse=False)
        # self.__make_result_log(result_list, reverse=True)

        return result_list[0]['merged_links']
    def __evaluate_interaction_information(
            self,
            decided_atoms,
            conflict_link_cases,
            non_conflict_link_cases,
            non_duplicate_link_cases,
    ):
        """Evaluate interaction information value for each available conflict
        link cases, and returns one link set has maximum information value.

        Args:
            decided_atoms: The source atoms to make new atom.
            conflict_link_cases: Conflicted link tuples list.
            non_conflict_link_cases: Non-conflict links list.
            non_duplicate_link_cases: Non-duplicated links list.
            :param decided_atoms: list[Atom]
            :param conflict_link_cases: list[list[EqualLinkKey]]
            :param non_conflict_link_cases: list[EqualLinkKey]
            :param non_duplicate_link_cases: list[EqualLinkKey]
        Returns:
            A link set has maximum interaction information value.
            :rtype: list[EqualLinkKey]
        """
        result_list = list()

        inheritance_nodes = list()
        for decided_atom in decided_atoms:
            inheritance_nodes += find_inheritance_nodes(self.a, decided_atom)

        # TODO: To prevent freeze during interaction information generation,
        # user can limit the max value of calculation.
        max_repeat_length = self.evaluate_n_gram_limit \
            if self.evaluate_n_gram_limit < self.provider.n_gram \
            else self.provider.n_gram

        log.debug(
            "ConnectConflictInteractionInformation: " +
            "Calculating interaction information " +
            "(Total: " + str(len(conflict_link_cases)) + ")"
        )

        current_ratio = 0
        for i, conflict_link in enumerate(conflict_link_cases):
            current_ratio = self.__print_progress(
                "ConnectConflictInteractionInformation:II:",
                current_ratio, i, len(conflict_link_cases), 25
            )
            merged_links = list()
            merged_links.extend(non_conflict_link_cases)
            merged_links.extend(non_duplicate_link_cases)
            merged_links.extend(conflict_link)

            # Calculate n-gram data in each available link cases.
            # The provider with high n-gram will provides more correct data,
            # but speed will going slower rapidly.
            filtered_merged_links = \
                map(lambda x:
                    eq_link.key_to_link(self.a, x, self.ret[0], x.tv),
                    filter(lambda x:
                           # TODO: Currently connector excludes
                           # an InheritanceLink to get valuable(funny) result.
                           self.a[x.h].out[0] not in inheritance_nodes and
                           # Manually throw away the links have low strength.
                           (x.tv.mean > self.inter_info_strength_above_limit),
                           merged_links
                           )
                    )

            if len(filtered_merged_links) is 0:
                continue

            interaction_information = PyInteractionInformationAtom(). \
                calculate_interaction_information(
                filtered_merged_links,
                self.provider,
                max_repeat_length
            )

            result_list.append({
                "merged_links": merged_links,
                "filtered_merged_links": filtered_merged_links,
                "interaction_information": interaction_information
            })

        if len(result_list) < 1:
            self.last_status = blending_status.EMPTY_RESULT
            return []

        result_list = sorted(
            result_list,
            key=(lambda x: x["interaction_information"]),
            reverse=True
        )

        self.__make_result_log(result_list, reverse=False)
        # self.__make_result_log(result_list, reverse=True)

        return result_list[0]['merged_links']
Beispiel #16
0
 def __connect_conflict_links(self, conflict_links):
     # 1. Choose one link randomly in each conflict link set.
     for merged_atom in self.ret:
         for links in conflict_links:
             link_key = random.choice(links)
             eq_link.key_to_link(self.a, link_key, merged_atom, link_key.tv)
 def __connect_non_duplicate_links(self, non_duplicate_links):
     # Just copy.
     for links in non_duplicate_links:
         for link in links:
             for merged_atom in self.ret:
                 eq_link.key_to_link(self.a, link, merged_atom, link.tv)
Beispiel #18
0
 def __connect_non_duplicate_links(self, non_duplicate_links):
     # Just copy.
     for merged_atom in self.ret:
         for links in non_duplicate_links:
             for link in links:
                 eq_link.key_to_link(self.a, link, merged_atom, link.tv)
 def __connect_conflict_links(self, conflict_links):
     # 1. Choose one link randomly in each conflict link set.
     for merged_atom in self.ret:
         for links in conflict_links:
             link_key = random.choice(links)
             eq_link.key_to_link(self.a, link_key, merged_atom, link_key.tv)