Example #1
0
 def genotype_factory(
     self,
     regressor_chromosome=None,
     n_estimators: int = None,
 ):
     return OrderedDict({
         "base_estimator":
         regressor_chromosome if regressor_chromosome is not None else
         DecisionTreeRegressorChromosome(),
         "n_estimators":
         RandPoissonChromosome(
             value=n_estimators if n_estimators is not None else 50,
             min_val=2,
             max_val=None,
             output_dtype=int),
         "max_samples":
         RandGaussChromosome(
             value=rand_real(),
             min_val=0.1,
             max_val=1.,
         ),
         "max_features":
         RandGaussChromosome(value=rand_real(), min_val=0.1, max_val=1.0),
         "warm_start":
         RandUniformBooleanChromosome()
     })
Example #2
0
 def __init__(self, dimensions: int):
     super(AckleyChromosome, self).__init__(
         length=dimensions,
         fixed=True,
         generator=RandGaussChromosome(value=rand_real(min_val=-32.768,
                                                       max_val=32.768),
                                       min_val=-32.768,
                                       max_val=32.768,
                                       rounding=3,
                                       output_dtype=np.float64))
 def __init__(self, dimensions: int):
     super(RastriginChromosome, self).__init__(
         length=dimensions,
         fixed=True,
         generator=RandGaussChromosome(value=rand_real(min_val=-5.12,
                                                       max_val=5.12),
                                       min_val=-5.12,
                                       max_val=5.12,
                                       rounding=2,
                                       output_dtype=np.float64))
    def mutate(self, mutation_probability: float, **kwargs):
        """Mutates the genotype
        
        Args:
            mutation_probability (float): The likelihood of change
        """

        for identifier, generator in self.__blueprint.items():
            if rand_real() < mutation_probability:
                self.__genotype[identifier] = generator.generate(**kwargs)

        self.__genotype.update(self.__fixed_genotype)

        return self.__genotype
Example #5
0
 def genotype_factory(self,
                      max_depth: int = None,
                      learning_rate: float = None,
                      n_estimators: int = None):
     return OrderedDict({
         "max_depth":
         RandPoissonChromosome(
             value=max_depth if max_depth is not None else rand_int(2, 16),
             min_val=2,
             max_val=None,
             rounding=None,
             output_dtype=int),
         "learning_rate":
         RandGaussChromosome(
             value=learning_rate if learning_rate is not None else 0.1,
             min_val=0.01,
             max_val=0.99,
             rounding=2),
         "n_estimators":
         RandPoissonChromosome(value=n_estimators if n_estimators
                               is not None else rand_int(2, 128),
                               min_val=2,
                               max_val=None,
                               output_dtype=int),
         "objective":
         RandOptionsChromosome(
             options=["reg:linear", "reg:gamma", "reg:tweedie"]),
         "booster":
         RandOptionsChromosome(options=["gbtree", "gblinear", "dart"]),
         "base_score":
         RandGaussChromosome(value=0.5,
                             min_val=0.01,
                             max_val=0.99,
                             rounding=3),
         "gamma":
         RandGaussChromosome(value=rand_real(),
                             min_val=0.01,
                             max_val=0.99,
                             rounding=3)
     })
Example #6
0
 def genotype_factory(self,
                      n_estimators: int = None,
                      learning_rate: float = None,
                      regressor_chromosome=None):
     return OrderedDict({
         "base_estimator":
         regressor_chromosome if regressor_chromosome is not None else
         DecisionTreeRegressorChromosome(),
         "n_estimators":
         RandPoissonChromosome(
             value=n_estimators if n_estimators is not None else 50,
             min_val=2,
             max_val=None,
             output_dtype=int),
         "learning_rate":
         RandGaussChromosome(value=learning_rate
                             if learning_rate is not None else rand_real(),
                             min_val=0.01,
                             max_val=1.0,
                             rounding=3,
                             output_dtype=np.float32),
         "loss":
         RandOptionsChromosome(options=["linear", "square", "exponential"])
     })
    def mutate(self, mutation_probability: float, **kwargs):
        """ The mutation function for the array_chromosome

        When mutating the ArrayChromosome we use a number of techniques to
        modify the contents.

        1. Iterate through each entry in the array and with some probability
           change the value based on the generator
        2. Then select pairs of positions at random with some probability and swap
           their values
        3. If fixed is set to False then:
           a) Attempt to add an item to the array with some probability
           b) Attempt to remove an item from the array with some probability

        Args:
            mutation_probability: The likelihood that we will change the array
            **kwargs:

        Returns:

        """

        # 1. Attempt to mutate each value
        for key, val in self.genotype.items():
            if rand_real() < mutation_probability:
                self.genotype[key] = self.__generator.generate(**kwargs)

        # 2. Attempt to swap positions of the array
        keys: List[str or int] = list(self.genotype.keys())
        if len(keys) > 1:

            # select the number of items to modify in the list
            items_to_select: int = rand_int(2, len(keys))

            selected: List[str or int] = rand_options(keys, items_to_select)

            shuffled: List[np.int64] = copy.copy(selected)

            np.random.shuffle(shuffled)

            for i, key in enumerate(selected):

                if rand_real() < mutation_probability:
                    self.genotype[selected[i]], self.genotype[
                        shuffled[i]] = self.genotype[
                            shuffled[i]], self.genotype[selected[i]]

        # TODO: Sensibly define how to insert/update an OrderedDict
        # 3. Attempt to add and remove items to the array
        # if self.__fixed is False:
        #
        #     # Add
        #     num_positions = rand_int(1, len(keys))
        #
        #     # create a temporary placeholder to update the OrderedDict
        #     temp_phenotype = list(self.genotype.values())
        #
        #     for i in range(num_positions):
        #
        #         if rand_real() < mutation_probability:
        #             position = rand_int(0, len(self.genotype.keys()))
        #             temp_phenotype.insert(position, self.__generator.generate(**kwargs))
        #
        #     # Remove

        self.phenotype = list(self.genotype.values())

        return self.phenotype