Exemple #1
0
    def create_offsprings(self, bridge_1_v_id, bridge_2_v_id):

        # create the empty offsprings
        offspring_1 = DNA(Settings.INPUT_SHAPE, Settings.OUTPUT_SHAPE)
        offspring_2 = DNA(Settings.INPUT_SHAPE, Settings.OUTPUT_SHAPE)

        # populate the input and output attributes
        offspring_1.input_vertex_id = self.parent_1.input_vertex_id
        offspring_1.output_vertex_id = self.parent_2.output_vertex_id

        offspring_2.input_vertex_id = self.parent_2.input_vertex_id
        offspring_2.output_vertex_id = self.parent_1.output_vertex_id

        # populate the vertices and edges dictionaries until reaching the bridge vertices (included)
        self.grow_offspring(
            offspring_1, self.parent_1.vertices[self.parent_1.input_vertex_id],
            bridge_1_v_id)
        self.grow_offspring(
            offspring_2, self.parent_2.vertices[self.parent_2.input_vertex_id],
            bridge_2_v_id)

        # swap the edges_out of the two bridges
        swap_tmp = deepcopy(offspring_1.vertices[bridge_1_v_id].edges_out)
        offspring_1.vertices[bridge_1_v_id].edges_out = deepcopy(
            offspring_2.vertices[bridge_2_v_id].edges_out)
        offspring_2.vertices[bridge_2_v_id].edges_out = swap_tmp

        for e in offspring_1.vertices[bridge_1_v_id].edges_out:
            e.from_vertex = offspring_1.vertices[bridge_1_v_id]

        for e in offspring_2.vertices[bridge_2_v_id].edges_out:
            e.from_vertex = offspring_2.vertices[bridge_2_v_id]

        # swap the mutable_out attribute of the bridges for the case when only one bridge is the buffer
        swap_tmp = offspring_1.vertices[bridge_1_v_id].mutable_out
        offspring_1.vertices[bridge_1_v_id].mutable_out = offspring_2.vertices[
            bridge_2_v_id].mutable_out
        offspring_2.vertices[bridge_2_v_id].mutable_out = swap_tmp

        # populate the rest of the offsprings
        self.grow_offspring(offspring_1, offspring_1.vertices[bridge_1_v_id],
                            -1)
        self.grow_offspring(offspring_2, offspring_2.vertices[bridge_2_v_id],
                            -1)

        # # create a fusion of the two parents
        # parents_fusion = DNA(Settings.INPUT_SHAPE, Settings.OUTPUT_SHAPE)
        # parents_fusion.vertices.update(deepcopy(self.parent_1.vertices))
        # parents_fusion.vertices.update(deepcopy(self.parent_2.vertices))
        # parents_fusion.edges.update(deepcopy(self.parent_1.edges))
        # parents_fusion.edges.update(deepcopy(self.parent_2.edges))

        # # swap the edges_out of the two bridges
        # swap_tmp = deepcopy(parents_fusion.vertices[bridge_1_v_id].edges_out)
        # parents_fusion.vertices[bridge_1_v_id].edges_out = deepcopy(parents_fusion.vertices[bridge_2_v_id].edges_out)
        # parents_fusion.vertices[bridge_2_v_id].edges_out = swap_tmp

        # for e in parents_fusion.vertices[bridge_1_v_id].edges_out:
        #     e.from_vertex = parents_fusion.vertices[bridge_1_v_id]

        # for e in parents_fusion.vertices[bridge_2_v_id].edges_out:
        #     e.from_vertex = parents_fusion.vertices[bridge_2_v_id]

        # # swap the mutable_out attribute of the bridges for the case when only one bridge is the buffer
        # swap_tmp = parents_fusion.vertices[bridge_1_v_id].mutable_out
        # parents_fusion.vertices[bridge_1_v_id].mutable_out = parents_fusion.vertices[bridge_2_v_id].mutable_out
        # parents_fusion.vertices[bridge_2_v_id].mutable_out = swap_tmp

        # # grow the offsprings by walking the two graphs from their respective input
        # offspring_1 = self.grow_offspring(parents_fusion, self.parent_1.input_vertex_id, self.parent_2.output_vertex_id)
        # offspring_2 = self.grow_offspring(parents_fusion, self.parent_2.input_vertex_id, self.parent_1.output_vertex_id)

        # print("**************************")
        # print("parent 1 : input_id " + str(self.parent_1.input_vertex_id) + " output_id " + str(self.parent_1.output_vertex_id))
        # print("parent 2 : input_id " + str(self.parent_2.input_vertex_id) + " output_id " + str(self.parent_2.output_vertex_id))
        # print("offspring 1 : input_id " + str(offspring_1.input_vertex_id) + " output_id " + str(offspring_1.output_vertex_id))
        # print("offspring 2 : input_id " + str(offspring_2.input_vertex_id) + " output_id " + str(offspring_2.output_vertex_id))
        # print("**************************")

        return (offspring_1, offspring_2)