Beispiel #1
0
 def get_weighted_values(self, weights):
     if len(weights) == len(CisTrans.values_options):
         self.values = [
             CisTrans.values_options[find_one_in_list(sum(weights), weights)] for i in range(len(self.positions))
         ]
     else:
         self.values = [choice(CisTrans.values_options) for i in range(len(self.positions))]
Beispiel #2
0
 def get_weighted_values(self, weights):
     if len(weights) == len(CisTrans.values_options):
         self.values = [
             CisTrans.values_options[find_one_in_list(
                 sum(weights), weights)] for i in range(len(self.positions))
         ]
     else:
         self.values = [
             choice(CisTrans.values_options)
             for i in range(len(self.positions))
         ]
Beispiel #3
0
    def get_weighted_values(self, weights):
        """Generate a random, but weighted value for each of the positions of
        the Torsion object.

        Args:
            weights (list): weights for all allowed values options, if it is
            not of the length of the values options, random values will be
            generated
        """
        if len(weights) == len(Torsion.values_options):
            self.values = [
                Torsion.values_options[find_one_in_list(sum(weights), weights)] for i in range(len(self.positions))
            ]
        else:
            self.values = [choice(Torsion.values_options) for i in range(len(self.positions))]
Beispiel #4
0
def mutation(list_for_mut, max_mutations, options, weights=None,
             periodic=False):
    """Mutate a list of values.

    Args(required):
        list_for_mut (list): list of values to be mutated
        max_mut: maximal number of changes to be made
        options: list of options for new values
    Args(optional):
        weights (list): weights for options
        periodic (bool)
    Returns:
        mutated list
    """

    if not isinstance(max_mutations, int):
        raise TypeError("The max. number of mutations needs to be an integer")
    if max_mutations < 0:
        raise ValueError("The max. number of mutations cannot be negative")
    mut_numb = random.randint(1, min(max_mutations, len(list_for_mut)))
    pos = random.sample(range(len(list_for_mut)), mut_numb)
    for p in pos:
        current_value = list_for_mut[p]
        banned = find_closest(current_value, options, periodic)
        cnt = 0
        while cnt < 100:
            if weights is None:
                new_value = random.sample(options, 1)[0]
            else:
                new_value = options[find_one_in_list(sum(weights), weights)]

            if len(banned) != len(options):
                if new_value not in banned:
                    list_for_mut[p] = new_value
                    break
                else:
                    cnt += 1
            else:
                # for very rare cases, i.e. if there are only two options
                # and the value is the mean of them!
                list_for_mut[p] = new_value
                break

    return list_for_mut
Beispiel #5
0
    def get_weighted_values(self, weights):
        """Generate a random, but weighted value for each of the positions of
        the Torsion object.

        Args:
            weights (list): weights for all allowed values options, if it is
            not of the length of the values options, random values will be
            generated
        """
        if len(weights) == len(Torsion.values_options):
            self.values = [
                Torsion.values_options[find_one_in_list(sum(weights), weights)]
                for i in range(len(self.positions))
            ]
        else:
            self.values = [
                choice(Torsion.values_options)
                for i in range(len(self.positions))
            ]