示例#1
0
    def mutate(self, cluster: Cluster, *args, **kwargs) -> Cluster:
        """Applies small random rotations to a all initial_molecules in the cluster"""
        successful = False

        while not successful:

            for particle in range(len(cluster.molecules)):
                theta = self.random(self.step_size)
                cluster.molecules[particle].rotate(axis=random_axis(),
                                                   theta=theta)

            successful = self.check_cluster(cluster)
        return cluster
示例#2
0
    def mutate(self, old_cluster: Cluster, *args, **kwargs) -> Cluster:
        """Generates a new cluster and returns it.

        Args:
            old_cluster: Cluster object, required, The template for the cluster
            *args: unused
            **kwargs: unused

        Returns:
            A new cluster with new molecular positions and orientations

        """
        max_step = self.box_length or len(old_cluster.molecules)**(5. / 4)

        # new_cluster = copy.deepcopy(old_cluster)
        #
        # base_molecules = copy.deepcopy(old_cluster.molecules)
        #
        # new_cluster.molecules = []
        #
        # for m in base_molecules:
        #     m.center()
        #     vec = np.random.uniform(low=0.1, high=max_step, size=3)
        #     m.translate(vec)
        #
        #     m.rotate(random_axis(), np.random.uniform(0, 2 * np.pi))
        #
        #     new_cluster.molecules.append(m)
        #
        # return new_cluster

        new_molecules = [
            Molecule(coordinates=copy.deepcopy(m.coordinates),
                     particle_names=m.particle_names)
            for m in old_cluster.molecules
        ]

        for m in new_molecules:

            m.center()

            vec = np.random.uniform(low=0.1, high=max_step, size=3)
            m.translate(vec)

            m.rotate(random_axis(), np.random.uniform(0, 2 * np.pi))

        new_cluster = Cluster(molecules=new_molecules)
        return new_cluster
示例#3
0
    def mutate(self, cluster: Cluster, *args, **kwargs) -> Cluster:
        """Applies a rotation to a single member of a cluster

        Args:
            cluster (Cluster): required, the cluster to be mutated
        """
        successful = False

        while not successful:
            selection = np.random.randint(len(cluster.molecules))

            # Step length is distributed according to the distribution selected at __init__
            theta = self.random(self.step_size)

            cluster.molecules[selection].rotate(axis=random_axis(),
                                                theta=theta)
            successful = self.check_cluster(cluster)
        return cluster
示例#4
0
    def mutate(self, cluster: Cluster, *args, **kwargs) -> Cluster:
        """Applies rotations to several members of a cluster

        Args:
            cluster (Cluster): required, the cluster to be mutated
        """
        successful = False

        while not successful:
            selection = np.random.randint(len(cluster.molecules),
                                          size=np.random.randint(
                                              len(cluster.molecules)))

            for particle in selection:

                theta = self.random(self.step_size)

                cluster.molecules[particle].rotate(axis=random_axis(),
                                                   theta=theta)

            successful = self.check_cluster(cluster)

        return cluster
示例#5
0
    def prepare_parents(self, parents: List[Cluster]) -> List[Cluster]:
        """Shuffles, centers and rotates the parent clusters

        Args:
            parents: List[Clusters], required, parents for mating.

        Returns:
            List[Clusters] shuffled, centered and rotated clusters, Note: order is not preserved.

        """
        # Shuffle the order of the parents
        np.random.shuffle(parents)

        prepared_parents = []

        # Center and then apply uniformly random rotations to each parent
        for parent in parents:
            axis = random_axis()
            parent.center()
            parent.rotate(axis, np.random.uniform(2.0 * np.pi))

            prepared_parents.append(copy.deepcopy(parent))

        return prepared_parents