コード例 #1
0
    def run(self, coderack, slipnet, workspace):
        if workspace.has_null_replacement():
            return # Fizzle

        changed_objects = []
        for obj in workspace.initial_string.get_objects():
            if obj.is_changed:
                changed_objects.append(obj)

        if len(changed_objects) > 1:
            print "Can't solve problems with more than one letter changed."
            # FIXME: Too harsh.
            sys.exit()

        if not changed_objects:
            return workspace.propose_rule(None, None, None, None)

        i_object = changed_objects[0]
        m_object = i_object.replacement.object2

        if not i_object.correspondence:
            i_descriptions = i_object.rule_initial_string_descriptions()
        else:
            correspondence_slippages = i_object.correspondence.slippages()
            i_descriptions = []
            for description in i_object.rule_initial_string_descriptions():
                applied = description.apply_slippages(i_object,
                                                      correspondence_slippages)
                relevant = i_object.correspondence.object2.relevant_descriptions()
                if applied in relevant:
                    i_descriptions.append(description)
        if not i_descriptions:
            return # Fizzle

        depths = [d.conceptual_depth() for d in i_descriptions]
        i_probabilities = workspace.temperature_adjusted_values(depths)
        i_description = toolbox.weighted_select(i_probabilities, i_descriptions)

        m_descriptions = m_object.extrinsic_descriptions + \
                         m_object.rule_modified_string_descriptions()
        if not m_descriptions:
            return # Fizzle

        depths = [d.conceptual_depth() for d in m_descriptions]
        m_probabilities = workspace.temperature_adjusted_values(depths)
        m_description = toolbox.weighted_select(m_probabilities, m_descriptions)

        if isinstance(m_description, ExtrinsicDescription):
            related_descriptor = slipnet.get_related_node(i_description.descriptor,
                                                        m_description.relation)
            if related_descriptor:
                for description in m_object.descriptions:
                    if description.descriptor == related_descriptor:
                        m_description = description
                        break

        return workspace.propose_rule(i_object, i_description,
                                      m_object, m_description)
コード例 #2
0
    def run(self, coderack, slipnet, workspace):
        if workspace.has_null_replacement():
            return # Fizzle

        changed_objects = []
        for obj in workspace.initial_string.get_objects():
            if obj.is_changed:
                changed_objects.append(obj)

        if len(changed_objects) > 1:
            print("Can't solve problems with more than one letter changed.")
            # FIXME: Too harsh, especially for graphical clients.
            sys.exit()

        if not changed_objects:
            return workspace.propose_rule(None, None, None, None)

        i_object = changed_objects[0]
        m_object = i_object.replacement.object2

        if not i_object.correspondence:
            i_descriptions = i_object.rule_initial_string_descriptions()
        else:
            correspondence_slippages = i_object.correspondence.slippages()
            i_descriptions = []
            for description in i_object.rule_initial_string_descriptions():
                applied = description.apply_slippages(i_object,
                                                      correspondence_slippages)
                relevant = i_object.correspondence.object2.relevant_descriptions()
                if applied in relevant:
                    i_descriptions.append(description)
        if not i_descriptions:
            return # Fizzle

        depths = [d.conceptual_depth() for d in i_descriptions]
        i_probabilities = workspace.temperature_adjusted_values(depths)
        i_description = toolbox.weighted_select(i_probabilities, i_descriptions)

        m_descriptions = m_object.extrinsic_descriptions + \
                         m_object.rule_modified_string_descriptions()
        if not m_descriptions:
            return # Fizzle

        depths = [d.conceptual_depth() for d in m_descriptions]
        m_probabilities = workspace.temperature_adjusted_values(depths)
        m_description = toolbox.weighted_select(m_probabilities, m_descriptions)

        if isinstance(m_description, ExtrinsicDescription):
            related_descriptor = slipnet.get_related_node(i_description.descriptor,
                                                          m_description.relation)
            if related_descriptor:
                for description in m_object.descriptions:
                    if description.descriptor == related_descriptor:
                        m_description = description
                        break

        return workspace.propose_rule(i_object, i_description,
                                      m_object, m_description)
コード例 #3
0
 def choose_relevant_distinguishing_description_by_conceptual_depth(self):
     """Return a relevant, distinguishing description probabilistically
     based on the descriptor's conceptual depth."""
     relevant_descriptions = self.relevant_distinguishing_descriptions()
     if relevant_descriptions:
         depths = [d.conceptual_depth() for d in relevant_descriptions]
         return toolbox.weighted_select(depths, relevant_descriptions)
コード例 #4
0
 def choose_relevant_distinguishing_description_by_conceptual_depth(self):
     """Return a relevant, distinguishing description probabilistically
     based on the descriptor's conceptual depth."""
     relevant_descriptions = self.relevant_distinguishing_descriptions()
     if relevant_descriptions:
         depths = [d.conceptual_depth() for d in relevant_descriptions]
         return toolbox.weighted_select(depths, relevant_descriptions)
コード例 #5
0
 def choose_relevant_description_by_activation(self):
     """Return a relevant description probabilistically based on the
     descriptor's activation."""
     relevant_descriptions = self.relevant_descriptions()
     if relevant_descriptions:
         descriptors = [d.descriptor for d in relevant_descriptions]
         activations = [d.activation for d in descriptors]
         return toolbox.weighted_select(activations, relevant_descriptions)
コード例 #6
0
 def choose_relevant_description_by_activation(self):
     """Return a relevant description probabilistically based on the
     descriptor's activation."""
     relevant_descriptions = self.relevant_descriptions()
     if relevant_descriptions:
         descriptors = [d.descriptor for d in relevant_descriptions]
         activations = [d.activation for d in descriptors]
         return toolbox.weighted_select(activations, relevant_descriptions)
コード例 #7
0
ファイル: __init__.py プロジェクト: codeaudit/copycat
 def choose(self):
     """Choose a codelet from the coderack."""
     if self.is_empty():
         return None
     urgencies = [bin.urgency_sum(self.temperature) for bin in self.bins]
     bin = toolbox.weighted_select(urgencies, self.bins)
     self.time += 1
     self.last_chosen = bin.choose()
     return self.last_chosen
コード例 #8
0
ファイル: __init__.py プロジェクト: ajhager/copycat
 def choose(self):
     """Choose a codelet from the coderack."""
     if self.is_empty():
         return None
     urgencies = [bin.urgency_sum(self.temperature) for bin in self.bins]
     pbin = toolbox.weighted_select(urgencies, self.bins)
     self.time += 1
     self.last_chosen = pbin.choose()
     return self.last_chosen
コード例 #9
0
 def get_random_leftmost_object(self):
     """Return a random leftmost object from the string."""
     leftmost_objects = []
     category = self.slipnet.plato_string_position_category
     for obj in self.get_objects():
         if obj.get_descriptor(category) == self.slipnet.plato_leftmost:
             leftmost_objects.append(obj)
     if leftmost_objects:
         values = [obj.relative_importance for obj in leftmost_objects]
         return toolbox.weighted_select(values, leftmost_objects)
コード例 #10
0
ファイル: string.py プロジェクト: Alex-Linhares/copycat-1
 def get_random_leftmost_object(self):
     """Return a random leftmost object from the string."""
     leftmost_objects = []
     category = self.slipnet.plato_string_position_category
     for obj in self.get_objects():
         if obj.get_descriptor(category) == self.slipnet.plato_leftmost:
             leftmost_objects.append(obj)
     if leftmost_objects:
         values = [obj.relative_importance for obj in leftmost_objects]
         return toolbox.weighted_select(values, leftmost_objects)
コード例 #11
0
 def choose_left_neighbor(self):
     """Return one of the left neighbors of the bond chosen by salience."""
     if self.is_leftmost_in_string():
         return None
     left_neighbors = []
     for left_neighbor_object in self.left_object.all_left_neighbors():
         x = left_neighbor_object.string_number
         y = self.left_object.string_number
         possible_left_neighbor = self.string.left_right_bonds.get((x, y))
         if possible_left_neighbor != None:
             left_neighbors.append(possible_left_neighbor)
     saliences = [neighbor.salience() for neighbor in left_neighbors]
     return toolbox.weighted_select(saliences, left_neighbors)
コード例 #12
0
    def run(self, coderack, slipnet, workspace):
        description_type = self.arguments[0]

        obj = workspace.choose_object('total_salience')

        descriptors = description_type.get_possible_descriptors(obj)
        if descriptors == []:
            return # Fizzle

        activations = [descriptor.activation for descriptor in descriptors]
        descriptor = toolbox.weighted_select(activations, descriptors)

        return workspace.propose_description(obj, description_type, descriptor)
コード例 #13
0
ファイル: bond.py プロジェクト: ajhager/copycat
 def choose_right_neighbor(self):
     """Return one of the right neighbors of the bond chosen by salience."""
     if self.is_rightmost_in_string():
         return None
     right_neighbors = []
     for right_neighbor_object in self.right_object.all_right_neighbors():
         right1 = self.right_object.string_number
         right2 = right_neighbor_object.string_number
         possible_right_neighbor = self.string.left_right_bonds.get((right1, right2))
         if possible_right_neighbor != None:
             right_neighbors.append(possible_right_neighbor)
     saliences = [neighbor.salience() for neighbor in right_neighbors]
     return toolbox.weighted_select(saliences, right_neighbors)
コード例 #14
0
ファイル: bond.py プロジェクト: codeaudit/copycat
 def choose_right_neighbor(self):
     """Return one of the right neighbors of the bond chosen by salience."""
     if self.is_rightmost_in_string():
         return None
     right_neighbors = []
     for right_neighbor_object in self.right_object.all_right_neighbors():
         x = self.right_object.string_number
         y = right_neighbor_object.string_number
         possible_right_neighbor = self.string.left_right_bonds.get((x, y))
         if possible_right_neighbor != None:
             right_neighbors.append(possible_right_neighbor)
     saliences = [neighbor.salience() for neighbor in right_neighbors]
     return toolbox.weighted_select(saliences, right_neighbors)
コード例 #15
0
ファイル: bond.py プロジェクト: Alex-Linhares/copycat-1
 def choose_left_neighbor(self):
     """Return one of the left neighbors of the bond chosen by salience."""
     if self.is_leftmost_in_string():
         return None
     left_neighbors = []
     for left_neighbor_object in self.left_object.all_left_neighbors():
         left1 = left_neighbor_object.string_number
         left2 = self.left_object.string_number
         possible_left_neighbor = self.string.left_right_bonds.get(
             (left1, left2))
         if possible_left_neighbor != None:
             left_neighbors.append(possible_left_neighbor)
     saliences = [neighbor.salience() for neighbor in left_neighbors]
     return toolbox.weighted_select(saliences, left_neighbors)
コード例 #16
0
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]

        initial_string = workspace.initial_string
        target_string = workspace.target_string
        i_relevance = initial_string.local_bond_category_relevance(category)
        t_relevance = target_string.local_bond_category_relevance(category)
        i_unhappiness = initial_string.intra_string_unhappiness
        t_unhappiness = target_string.intra_string_unhappiness
        values = [
            round(toolbox.average(i_relevance, i_unhappiness)),
            round(toolbox.average(t_relevance, t_unhappiness))
        ]
        string = toolbox.weighted_select(values,
                                         [initial_string, target_string])

        obj = string.get_random_object('intra_string_salience')
        neighbor = obj.choose_neighbor()
        if neighbor == None:
            return  # Fizzle

        facet = workspace.choose_bond_facet(obj, neighbor)
        if facet == None:
            return  # Fizzle

        object_descriptor = obj.get_descriptor(facet)
        neighbor_descriptor = neighbor.get_descriptor(facet)
        if object_descriptor == None or neighbor_descriptor == None:
            return  # Fizzle

        if slipnet.get_bond_category(object_descriptor,
                                     neighbor_descriptor) == category:
            from_object = obj
            to_object = neighbor
            from_descriptor = object_descriptor
            to_descriptor = neighbor_descriptor
        elif slipnet.get_bond_category(neighbor_descriptor,
                                       object_descriptor) == category:
            from_object = neighbor
            to_object = obj
            from_descriptor = neighbor_descriptor
            to_descriptor = object_descriptor
        else:
            return  # Fizzle

        return workspace.propose_bond(from_object, to_object, category, facet,
                                      from_descriptor, to_descriptor)
コード例 #17
0
    def run(self, coderack, slipnet, workspace):
        obj = workspace.choose_object('total_salience')

        description = obj.choose_relevant_description_by_activation()
        if description == None:
            return # Fizzle
        descriptor = description.descriptor

        links = descriptor.similar_has_property_links()
        if links == []:
            return # Fizzle

        associations = [link.degree_of_association() for link in links]
        activations = [link.to_node.activation for link in links]
        choices = map(lambda a, b: a * b, associations, activations)
        prop = toolbox.weighted_select(choices, links).to_node
        
        return workspace.propose_description(obj, prop.category(), prop)
コード例 #18
0
ファイル: bond.py プロジェクト: ajhager/copycat
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]

        initial_string = workspace.initial_string
        target_string = workspace.target_string
        i_relevance = initial_string.local_bond_category_relevance(category)
        t_relevance = target_string.local_bond_category_relevance(category)
        i_unhappiness = initial_string.intra_string_unhappiness
        t_unhappiness = target_string.intra_string_unhappiness
        values = [round(toolbox.average(i_relevance, i_unhappiness)),
                  round(toolbox.average(t_relevance, t_unhappiness))]
        string = toolbox.weighted_select(values, [initial_string, target_string])

        obj = string.get_random_object('intra_string_salience')
        neighbor = obj.choose_neighbor()
        if neighbor is None:
            return # Fizzle

        facet = workspace.choose_bond_facet(obj, neighbor)
        if facet is None:
            return # Fizzle

        object_descriptor = obj.get_descriptor(facet)
        neighbor_descriptor = neighbor.get_descriptor(facet)
        if object_descriptor is None or neighbor_descriptor is None:
            return # Fizzle

        if slipnet.get_bond_category(object_descriptor,
                                     neighbor_descriptor) == category:
            from_object = obj
            to_object = neighbor
            from_descriptor = object_descriptor
            to_descriptor = neighbor_descriptor
        elif slipnet.get_bond_category(neighbor_descriptor,
                                       object_descriptor) == category:
            from_object = neighbor
            to_object = obj
            from_descriptor = neighbor_descriptor
            to_descriptor = object_descriptor
        else:
            return # Fizzle

        return workspace.propose_bond(from_object, to_object, category, facet,
                                      from_descriptor, to_descriptor)
コード例 #19
0
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]

        initial_string = workspace.initial_string
        target_string = workspace.target_string
        i_relevance = initial_string.local_direction_category_relevance(
            category)
        t_relevance = target_string.local_direction_category_relevance(
            category)
        i_unhappiness = initial_string.intra_string_unhappiness
        t_unhappiness = target_string.intra_string_unhappiness
        values = [
            round(toolbox.average(i_relevance, i_unhappiness)),
            round(toolbox.average(t_relevance, t_unhappiness))
        ]
        string = toolbox.weighted_select(values,
                                         [initial_string, target_string])

        obj = string.get_random_object('intra_string_salience')
        if category == slipnet.plato_left:
            neighbor = obj.choose_left_neighbor()
        elif category == slipnet.plato_right:
            neighbor = obj.choose_right_neighbor()
        if neighbor == None:
            return  # Fizzle

        facet = workspace.choose_bond_facet(obj, neighbor)
        if facet == None:
            return  # Fizzle

        object_descriptor = obj.get_descriptor(facet)
        neighbor_descriptor = neighbor.get_descriptor(facet)
        if object_descriptor == None or neighbor_descriptor == None:
            return  # Fizzle

        bond_category = slipnet.get_bond_category(object_descriptor,
                                                  neighbor_descriptor)
        if bond_category == None or not bond_category.directed:
            return  # Fizzle

        return workspace.propose_bond(obj, neighbor, bond_category, facet,
                                      object_descriptor, neighbor_descriptor)
コード例 #20
0
ファイル: __init__.py プロジェクト: codeaudit/copycat
    def post(self, codelet, urgency):
        """Post a codelet to the coderack.

        If the coderack is already at its maximum size, remove a codelet to make
        room for the new one. The bin to post to is a function of the numver of
        bins and the urgency of the codelet passed in."""
        removed_codelet = None
        codelets = self.codelets()
        if len(codelets) == self.max_codelets:
            probabilities = [self.remove_probability(c) for c in codelets]
            removed_codelet = toolbox.weighted_select(probabilities, codelets)
            removed_codelet.bin.remove(removed_codelet)

        if urgency >= 100:
            bin = self.extremely_high_bin
        else:
            index = int((len(self.bins) * urgency) / 100.0)
            bin = self.bins[index]
        bin.add(codelet)
        codelet.timestamp = self.time

        return removed_codelet
コード例 #21
0
ファイル: __init__.py プロジェクト: ajhager/copycat
    def post(self, codelet, urgency):
        """Post a codelet to the coderack.

        If the coderack is already at its maximum size, remove a codelet to make
        room for the new one. The bin to post to is a function of the numver of
        bins and the urgency of the codelet passed in."""
        removed_codelet = None
        codelets = self.codelets()
        if len(codelets) == self.max_codelets:
            probabilities = [self.remove_probability(c) for c in codelets]
            removed_codelet = toolbox.weighted_select(probabilities, codelets)
            removed_codelet.bin.remove(removed_codelet)

        if urgency >= 100:
            pbin = self.extremely_high_bin
        else:
            index = int((len(self.bins) * urgency) / 100.0)
            pbin = self.bins[index]
        pbin.add(codelet)
        codelet.timestamp = self.time

        return removed_codelet
コード例 #22
0
ファイル: bond.py プロジェクト: ajhager/copycat
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]

        initial_string = workspace.initial_string
        target_string = workspace.target_string
        i_relevance = initial_string.local_direction_category_relevance(category)
        t_relevance = target_string.local_direction_category_relevance(category)
        i_unhappiness = initial_string.intra_string_unhappiness
        t_unhappiness = target_string.intra_string_unhappiness
        values = [round(toolbox.average(i_relevance, i_unhappiness)),
                  round(toolbox.average(t_relevance, t_unhappiness))]
        string = toolbox.weighted_select(values, [initial_string, target_string])

        obj = string.get_random_object('intra_string_salience')
        if category == slipnet.plato_left:
            neighbor = obj.choose_left_neighbor()
        elif category == slipnet.plato_right:
            neighbor = obj.choose_right_neighbor()
        if neighbor is None:
            return # Fizzle

        facet = workspace.choose_bond_facet(obj, neighbor)
        if facet is None:
            return # Fizzle

        object_descriptor = obj.get_descriptor(facet)
        neighbor_descriptor = neighbor.get_descriptor(facet)
        if object_descriptor is None or neighbor_descriptor is None:
            return # Fizzle

        bond_category = slipnet.get_bond_category(object_descriptor,
                                                  neighbor_descriptor)
        if bond_category is None or not bond_category.directed:
            return # Fizzle

        return workspace.propose_bond(obj, neighbor,
                                      bond_category, facet,
                                      object_descriptor, neighbor_descriptor)
コード例 #23
0
 def choose_neighbor(self):
     """Choose a neighbor probabilistically using intra string salience."""
     neighbors = self.all_neighbors()
     saliences = [obj.intra_string_salience for obj in neighbors]
     return toolbox.weighted_select(saliences, neighbors)
コード例 #24
0
ファイル: group.py プロジェクト: ajhager/copycat
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]
        bond_category = slipnet.get_related_node(category,
                                                 slipnet.plato_bond_category)

        i_string = workspace.initial_string
        i_relevance = i_string.local_bond_category_relevance(bond_category)
        i_unhappiness = i_string.intra_string_unhappiness
        t_string = workspace.target_string
        t_relevance = t_string.local_bond_category_relevance(bond_category)
        t_unhappiness = t_string.intra_string_unhappiness
        weights = [round(toolbox.average(i_relevance, i_unhappiness)),
                   round(toolbox.average(t_relevance, t_unhappiness))]
        choices = [i_string, t_string]
        string = toolbox.weighted_select(weights, choices)

        obj = string.get_random_object('intra_string_salience')
        if obj.spans_whole_string():
            return # Fizzle

        if obj.is_leftmost_in_string():
            direction = slipnet.plato_right
        elif obj.is_rightmost_in_string():
            direction = slipnet.plato_left
        else:
            activations = [slipnet.plato_left.activation, slipnet.plato_right.activation]
            choices = [slipnet.plato_left, slipnet.plato_right]
            direction = toolbox.weighted_select(activations, choices)

        number = toolbox.weighted_index(string.bonds_to_scan_distribution)

        if direction == slipnet.plato_left:
            bond = obj.left_bond
        else:
            bond = obj.right_bond

        if bond is None or bond.bond_category != bond_category:
            if obj.type_name == 'group':
                return # Fizzle
            objects = [obj]
            bonds = []
            if category == slipnet.plato_sameness_group:
                single_letter_group_direction = None
            else:
                choices = [slipnet.plato_left, slipnet.plato_right]
                weights = [node.local_descriptor_support(string, slipnet.plato_group) \
                            for node in choices]
                index = toolbox.weighted_index(weights)
                single_letter_group_direction = choices[index]
                single_letter_group = Group(workspace, string, category,
                                            single_letter_group_direction,
                                            obj, obj, objects, bonds)

                probability = single_letter_group.single_letter_group_probability()
                if toolbox.flip_coin(probability):
                    return workspace.propose_group(objects, bonds, category,
                                                   single_letter_group_direction)
            return # Fizzle

        direction_category = bond.direction_category
        facet = bond.bond_facet
        opposite_bond_category = slipnet.get_related_node(bond_category,
                                                          slipnet.plato_opposite)
        if direction_category:
            opposite_direction_category = slipnet.get_related_node(direction_category,
                                                                   slipnet.plato_opposite)
        else:
            opposite_direction_category = None

        objects = [bond.left_object, bond.right_object]
        bonds = [bond]
        next_bond = bond
        for _ in range(2, number):
            bond_to_add = None
            if direction == slipnet.plato_left:
                next_bond = next_bond.choose_left_neighbor()
                if next_bond is None:
                    break
                else:
                    next_object = next_bond.left_object
            else:
                next_bond = next_bond.choose_right_neighbor()
                if next_bond is None:
                    break
                else:
                    next_object = next_bond.right_object

            if next_bond is None:
                bond_to_add = None
            elif all([next_bond.bond_category == bond_category,
                      next_bond.direction_category == direction_category,
                      next_bond.bond_facet == facet]):
                bond_to_add = next_bond
            elif all([next_bond.bond_category == opposite_bond_category,
                      next_bond.direction_category == opposite_direction_category,
                      next_bond.bond_facet == facet]):
                bond_to_add = next_bond.flipped_version()

            if bond_to_add:
                objects.append(next_object)
                bonds.append(bond_to_add)
            else:
                break

        return workspace.propose_group(objects, bonds, category, direction_category)
コード例 #25
0
 def choose_neighbor(self):
     """Choose a neighbor probabilistically using intra string salience."""
     neighbors = self.all_neighbors()
     saliences = [obj.intra_string_salience for obj in neighbors]
     return toolbox.weighted_select(saliences, neighbors)
コード例 #26
0
 def choose_right_neighbor(self):
     """Choose a right neighbor probabilistically based on intra string
     salience."""
     neighbors = self.all_right_neighbors()
     values = [obj.intra_string_salience for obj in neighbors]
     return toolbox.weighted_select(values, neighbors)
コード例 #27
0
    def run(self, coderack, slipnet, workspace):
        flip_obj2 = False

        object1 = workspace.initial_string.get_random_object(
            'relative_importance')

        object1_description = object1.choose_relevant_distinguishing_description_by_conceptual_depth(
        )
        if not object1_description:
            return  # Fizzle
        object1_descriptor = object1_description.descriptor

        object2_descriptor = object1_descriptor
        for slippage in workspace.slippages():
            if slippage.descriptor1 == object1_descriptor:
                object2_descriptor = slippage.descriptor2
                break

        object2_candidates = []
        for obj in workspace.target_string.get_objects():
            for description in obj.relevant_descriptions():
                if description.descriptor == object2_descriptor:
                    object2_candidates.append(obj)
        if not object2_candidates:
            return  # Fizzle

        weights = [obj.inter_string_salience for obj in object2_candidates]
        object2 = toolbox.weighted_select(weights, object2_candidates)

        if object1.spans_whole_string() != object2.spans_whole_string():
            return  # Fizzle

        mappings = workspace.get_concept_mappings(
            object1, object2, object1.relevant_descriptions(),
            object2.relevant_descriptions())
        if not mappings:
            return

        possible = False
        for mapping in mappings:
            probability = mapping.slippability() / 100.0
            probability = workspace.temperature_adjusted_probability(
                probability)
            if toolbox.flip_coin(probability):
                possible = True
                break
        if not possible:
            return  # Fizzle

        distinguished_mappings = [m for m in mappings if m.is_distinguishing()]
        if not distinguished_mappings:
            return  # Fizzle

        possible_opposite_mappings = []
        for mapping in distinguished_mappings:
            description_type = mapping.description_type1
            if description_type != slipnet.plato_string_position_category and \
               description_type != slipnet.plato_bond_facet:
                possible_opposite_mappings.append(mapping)

        opposite_descriptions = [m.description_type1 for m in mappings]
        if all([
                object1.is_string_spanning_group(),
                object2.is_string_spanning_group(),
                not slipnet.plato_opposite.is_active(),
                slipnet.are_all_opposite_concept_mappings(
                    possible_opposite_mappings),
                slipnet.plato_direction_category in opposite_descriptions
        ]):
            old_object2_string_number = object2.string_number
            object2 = object2.flipped_version()
            object2.string_number = old_object2_string_number
            mappings = workspace.get_concept_mappings(
                object1, object2, object1.relevant_descriptions(),
                object2.relevant_descriptions())
            flip_obj2 = True

        return workspace.propose_correspondence(object1, object2, mappings,
                                                flip_obj2)
コード例 #28
0
ファイル: group.py プロジェクト: ajhager/copycat
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]

        # Choose a string based on local direction category relevance.
        i_string = workspace.initial_string
        i_relevance = i_string.local_direction_category_relevance(category)
        i_unhappiness = i_string.intra_string_unhappiness
        t_string = workspace.target_string
        t_relevance = t_string.local_direction_category_relevance(category)
        t_unhappiness = t_string.intra_string_unhappiness
        choices = [i_string, t_string]
        weights = [round(toolbox.average(i_relevance, i_unhappiness)),
                   round(toolbox.average(t_relevance, t_unhappiness))]
        string = toolbox.weighted_select(weights, choices)

        obj = string.get_random_object('intra_string_salience')
        if obj.spans_whole_string():
            return # Fizzle

        if obj.is_leftmost_in_string():
            direction = slipnet.plato_right
        elif obj.is_rightmost_in_string():
            direction = slipnet.plato_left
        else:
            choices = [slipnet.plato_left, slipnet.plato_right]
            activations = [slipnet.plato_left.activation,
                           slipnet.plato_right.activation]
            direction = toolbox.weighted_select(activations, choices)

        number = toolbox.weighted_index(string.bonds_to_scan_distribution)

        if direction == slipnet.plato_left:
            bond = obj.left_bond
        else:
            bond = obj.right_bond

        if not bond or bond.direction_category != category:
            return # Fizzle

        bond_category = bond.bond_category
        facet = bond.bond_facet

        opposite_bond_category = slipnet.get_related_node(bond_category,
                                                          slipnet.plato_opposite)
        opposite_category = slipnet.get_related_node(category,
                                                     slipnet.plato_opposite)

        objects = [bond.left_object, bond.right_object]
        bonds = [bond]
        next_bond = bond
        for _ in range(2, number):
            bond_to_add = None
            if direction == slipnet.plato_left:
                next_bond = next_bond.choose_left_neighbor()
                if not next_bond:
                    break
                else:
                    next_object = next_bond.left_object
            else:
                next_bond = next_bond.choose_right_neighbor()
                if not next_bond:
                    break
                else:
                    next_object = next_bond.right_object

            if not next_bond:
                bond_to_add = None
            elif (next_bond.bond_category == bond_category) and \
                 (next_bond.direction_category == category) and \
                 (next_bond.bond_facet == facet):
                bond_to_add = next_bond
            elif (next_bond.bond_category == opposite_bond_category) and \
                 (next_bond.direction_category == opposite_category) and \
                 (next_bond.bond_facet == facet):
                bond_to_add = next_bond.flipped_version()

            if bond_to_add:
                objects.append(next_object)
                bonds.append(bond_to_add)
            else:
                break

        group_category = slipnet.get_related_node(bond_category,
                                                  slipnet.plato_group_category)

        return workspace.propose_group(objects, bonds, group_category, category)
コード例 #29
0
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]
        bond_category = slipnet.get_related_node(category,
                                               slipnet.plato_bond_category)

        i_string = workspace.initial_string
        i_relevance = i_string.local_bond_category_relevance(bond_category)
        i_unhappiness = i_string.intra_string_unhappiness
        t_string = workspace.target_string
        t_relevance = t_string.local_bond_category_relevance(bond_category)
        t_unhappiness = t_string.intra_string_unhappiness
        weights = [round(toolbox.average(i_relevance, i_unhappiness)),
                   round(toolbox.average(t_relevance, t_unhappiness))]
        choices = [i_string, t_string]
        string = toolbox.weighted_select(weights, choices)

        obj = string.get_random_object('intra_string_salience')
        if obj.spans_whole_string():
            return # Fizzle

        if obj.is_leftmost_in_string():
            direction = slipnet.plato_right
        elif obj.is_rightmost_in_string():
            direction = slipnet.plato_left
        else:
            activations = [slipnet.plato_left.activation, slipnet.plato_right.activation]
            choices = [slipnet.plato_left, slipnet.plato_right]
            direction = toolbox.weighted_select(activations, choices) 

        number = toolbox.weighted_index(string.bonds_to_scan_distribution)

        if direction == slipnet.plato_left:
            bond = obj.left_bond
        else:
            bond = obj.right_bond

        if bond == None or bond.bond_category != bond_category:
            if obj.type_name == 'group':
                return # Fizzle
            objects = [obj]
            bonds = []
            if category == slipnet.plato_sameness_group:
                single_letter_group_direction = None
            else:
                choices = [slipnet.plato_left, slipnet.plato_right]
                weights = [node.local_descriptor_support(string, slipnet.plato_group) \
                            for node in choices]
                index = toolbox.weighted_index(weights)
                single_letter_group_direction = choices[index]
                single_letter_group = Group(workspace, string, category,
                                            single_letter_group_direction,
                                            obj, obj, objects, bonds)

                probability = single_letter_group.single_letter_group_probability()
                if toolbox.flip_coin(probability):
                    return workspace.propose_group(objects, bonds, category,
                                                   single_letter_group_direction)
            return # Fizzle
        
        direction_category = bond.direction_category
        facet = bond.bond_facet
        opposite_bond_category = slipnet.get_related_node(bond_category, 
                                                        slipnet.plato_opposite)
        if direction_category:
            opposite_direction_category = slipnet.get_related_node(direction_category,
                                                                 slipnet.plato_opposite)
        else:
            opposite_direction_category = None

        objects = [bond.left_object, bond.right_object]
        bonds = [bond]
        next_bond = bond
        for i in range(2, number):
            bond_to_add = None
            if direction == slipnet.plato_left:
                next_bond = next_bond.choose_left_neighbor()
                if next_bond == None:
                    break
                else:
                    next_object = next_bond.left_object
            else:
                next_bond = next_bond.choose_right_neighbor()
                if next_bond == None:
                    break
                else:
                    next_object = next_bond.right_object

            if next_bond == None:
                bond_to_add = None
            elif all([next_bond.bond_category == bond_category,
                      next_bond.direction_category == direction_category,
                      next_bond.bond_facet == facet]):
                bond_to_add = next_bond
            elif all([next_bond.bond_category == opposite_bond_category,
                      next_bond.direction_category == opposite_direction_category,
                      next_bond.bond_facet == facet]):
                bond_to_add = next_bond.flipped_version()

            if bond_to_add:
                objects.append(next_object)
                bonds.append(bond_to_add)
            else:
                break

        return workspace.propose_group(objects, bonds, category, direction_category)
コード例 #30
0
ファイル: correspondence.py プロジェクト: ajhager/copycat
    def run(self, coderack, slipnet, workspace):
        flip_obj2 = False

        object1 = workspace.initial_string.get_random_object("relative_importance")

        object1_description = object1.choose_relevant_distinguishing_description_by_conceptual_depth()
        if not object1_description:
            return  # Fizzle
        object1_descriptor = object1_description.descriptor

        object2_descriptor = object1_descriptor
        for slippage in workspace.slippages():
            if slippage.descriptor1 == object1_descriptor:
                object2_descriptor = slippage.descriptor2
                break

        object2_candidates = []
        for obj in workspace.target_string.get_objects():
            for description in obj.relevant_descriptions():
                if description.descriptor == object2_descriptor:
                    object2_candidates.append(obj)
        if not object2_candidates:
            return  # Fizzle

        weights = [obj.inter_string_salience for obj in object2_candidates]
        object2 = toolbox.weighted_select(weights, object2_candidates)

        if object1.spans_whole_string() != object2.spans_whole_string():
            return  # Fizzle

        mappings = workspace.get_concept_mappings(
            object1, object2, object1.relevant_descriptions(), object2.relevant_descriptions()
        )
        if not mappings:
            return

        possible = False
        for mapping in mappings:
            probability = mapping.slippability() / 100.0
            probability = workspace.temperature_adjusted_probability(probability)
            if toolbox.flip_coin(probability):
                possible = True
                break
        if not possible:
            return  # Fizzle

        distinguished_mappings = [m for m in mappings if m.is_distinguishing()]
        if not distinguished_mappings:
            return  # Fizzle

        possible_opposite_mappings = []
        for mapping in distinguished_mappings:
            description_type = mapping.description_type1
            if (
                description_type != slipnet.plato_string_position_category
                and description_type != slipnet.plato_bond_facet
            ):
                possible_opposite_mappings.append(mapping)

        opposite_descriptions = [m.description_type1 for m in mappings]
        if all(
            [
                object1.is_string_spanning_group(),
                object2.is_string_spanning_group(),
                not slipnet.plato_opposite.is_active(),
                slipnet.are_all_opposite_concept_mappings(possible_opposite_mappings),
                slipnet.plato_direction_category in opposite_descriptions,
            ]
        ):
            old_object2_string_number = object2.string_number
            object2 = object2.flipped_version()
            object2.string_number = old_object2_string_number
            mappings = workspace.get_concept_mappings(
                object1, object2, object1.relevant_descriptions(), object2.relevant_descriptions()
            )
            flip_obj2 = True

        return workspace.propose_correspondence(object1, object2, mappings, flip_obj2)
コード例 #31
0
 def choose_right_neighbor(self):
     """Choose a right neighbor probabilistically based on intra string
     salience."""
     neighbors = self.all_right_neighbors()
     values = [obj.intra_string_salience for obj in neighbors]
     return toolbox.weighted_select(values, neighbors)
コード例 #32
0
 def choose(self):
     """Return a number 0-100 based on the probabilities."""
     values = [self.probabilities[i] for i in sorted(self.probabilities)]
     return toolbox.weighted_select(values, sorted(self.probabilities))
コード例 #33
0
    def run(self, coderack, slipnet, workspace):
        category = self.arguments[0]

        # Choose a string based on local direction category relevance.
        i_string = workspace.initial_string
        i_relevance = i_string.local_direction_category_relevance(category)
        i_unhappiness = i_string.intra_string_unhappiness
        t_string = workspace.target_string
        t_relevance = t_string.local_direction_category_relevance(category)
        t_unhappiness = t_string.intra_string_unhappiness
        choices = [i_string, t_string]
        weights = [round(toolbox.average(i_relevance, i_unhappiness)),
                   round(toolbox.average(t_relevance, t_unhappiness))]
        string = toolbox.weighted_select(weights, choices)

        obj = string.get_random_object('intra_string_salience')
        if obj.spans_whole_string():
            return # Fizzle

        if obj.is_leftmost_in_string():
            direction = slipnet.plato_right
        elif obj.is_rightmost_in_string():
            direction = slipnet.plato_left
        else:
            choices = [slipnet.plato_left, slipnet.plato_right]
            activations = [slipnet.plato_left.activation,
                           slipnet.plato_right.activation]
            direction = toolbox.weighted_select(activations, choices)

        number = toolbox.weighted_index(string.bonds_to_scan_distribution)

        if direction == slipnet.plato_left:
            bond = obj.left_bond
        else:
            bond = obj.right_bond

        if not bond or bond.direction_category != category:
            return # Fizzle

        bond_category = bond.bond_category
        facet = bond.bond_facet

        opposite_bond_category = slipnet.get_related_node(bond_category,
                                                        slipnet.plato_opposite)
        opposite_category = slipnet.get_related_node(category,
                                                   slipnet.plato_opposite)

        objects = [bond.left_object, bond.right_object]
        bonds = [bond]
        next_bond = bond
        for i in range(2, number):
            bond_to_add = None
            if direction == slipnet.plato_left:
                next_bond = next_bond.choose_left_neighbor()
                if not next_bond:
                    break
                else:
                    next_object = next_bond.left_object
            else:
                next_bond = next_bond.choose_right_neighbor()
                if not next_bond:
                    break
                else:
                    next_object = next_bond.right_object

            if not next_bond:
                bond_to_add = None
            elif (next_bond.bond_category == bond_category) and \
                 (next_bond.direction_category == category) and \
                 (next_bond.bond_facet == facet):
                bond_to_add = next_bond
            elif (next_bond.bond_category == opposite_bond_category) and \
                 (next_bond.direction_category == opposite_category) and \
                 (next_bond.bond_facet == facet):
                bond_to_add = next_bond.flipped_version()

            if bond_to_add:
                objects.append(next_object)
                bonds.append(bond_to_add)
            else:
                break

        group_category = slipnet.get_related_node(bond_category,
                                                slipnet.plato_group_category)

        return workspace.propose_group(objects, bonds, group_category, category)