def __init__(self, genotype, strategy, strategy_parameters, random_seed=None): """Initialize the Individual. :param genotype: the genotype of the individual :param strategy: the strategy chosen to reproduce. See the Strategy enum for more information :param strategy_parameters: the parameters required for the given strategy, as a list """ self.genotype = genotype self.length = len(genotype) self.random_seed = random_seed self.random = random_with_seed(self.random_seed) self.fitness = None self.strategy = strategy self.strategy_parameters = strategy_parameters if not isinstance(strategy, Strategy): raise ValueError( "Provided strategy parameter was not an instance of Strategy.") if strategy == Strategy.SINGLE_VARIANCE and len( strategy_parameters) == 1: self.reproduce = self._reproduce_single_variance elif strategy == Strategy.MULTIPLE_VARIANCE and len( strategy_parameters) == self.length: self.reproduce = self._reproduce_multiple_variance elif strategy == Strategy.FULL_VARIANCE and len( strategy_parameters) == self.length * (self.length + 1) / 2: self.reproduce = self._reproduce_full_variance else: raise ValueError( "The length of the strategy parameters was not correct.")
def __init__(self, fitness_function, individual_length, warm_start=None, generations=100, population_size=30, num_children=1, mean=0, std=1, maximize=False, strategy=Strategy.SINGLE_VARIANCE, random_seed=None, reporter=None): """Initializes an EvoPy instance. :param fitness_function: the fitness function on which the individuals are evaluated :param individual_length: the length of each individual :param warm_start: the individual to start from :param generations: the number of generations to execute :param population_size: the population size of each generation :param num_children: the number of children generated per parent individual :param mean: the mean for sampling the random offsets of the initial population :param std: the standard deviation for sampling the random offsets of the initial population :param maximize: whether the fitness function should be maximized or minimized :param strategy: the strategy used to generate offspring by individuals. For more information, check the Strategy enum :param random_seed: the seed to use for the random number generator :param reporter: callback to be invoked at each generation with a ProgressReport as argument """ self.fitness_function = fitness_function self.individual_length = individual_length self.warm_start = np.zeros(self.individual_length) if warm_start is None else warm_start self.generations = generations self.population_size = population_size self.num_children = num_children self.mean = mean self.std = std self.maximize = maximize self.strategy = strategy self.random_seed = random_seed self.random = random_with_seed(self.random_seed) self.reporter = reporter
def random_integer_seed_test(): """Test if integers are correctly used.""" random = random_with_seed(42) assert random.randint(100) == 51
def random_invalid_seed_test(): """Test if an error is raised when an incorrect parameter is supplied.""" random_with_seed(4.0)
def random_none_seed_test(): """Test if none is given the original random is used.""" np.random.seed(42) random = random_with_seed(None) assert random.randint(100) == 51
def random_state_seed_test(): """Test if states are correctly used.""" random = random_with_seed(np.random.RandomState(42)) assert random.randint(100) == 51