def get_random_object(self, method=None): """Return a random object from the string.""" if method: objects = self.get_objects() values = [getattr(obj, method) for obj in objects] values = self.workspace.temperature_adjusted_values(values) return objects[toolbox.weighted_index(values)] return random.choice(self.get_objects())
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)
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)
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)
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)