eckity.creators.ga_creators.simple_vector_creator

 1from eckity.creators.creator import Creator
 2from eckity.fitness.simple_fitness import SimpleFitness
 3from eckity.genetic_encodings.ga.bit_string_vector import BitStringVector
 4
 5
 6class GAVectorCreator(Creator):
 7	def __init__(self,
 8				 length=1,
 9				 gene_creator=None,
10				 bounds=(0.0, 1.0),
11				 vector_type=BitStringVector,
12				 fitness_type=SimpleFitness,
13				 events=None):
14		if events is None:
15			events = ["after_creation"]
16		super().__init__(events, fitness_type)
17
18		if gene_creator is None:
19			gene_creator = self.default_gene_creator
20		self.gene_creator = gene_creator
21
22		self.type = vector_type
23		self.length = length
24		self.bounds = bounds
25
26	def create_individuals(self, n_individuals, higher_is_better):
27		individuals = [self.type(length=self.length,
28								 bounds=self.bounds,
29								 fitness=self.fitness_type(higher_is_better=higher_is_better))
30					   for _ in range(n_individuals)]
31		for ind in individuals:
32			self.create_vector(ind)
33		self.created_individuals = individuals
34
35		return individuals
36
37	def create_vector(self, individual):
38		# vector = [self.gene_creator(individual.bounds[i % len(individual.bounds)]) for i in individual.size()]
39		vector = [self.gene_creator(individual, i) for i in range(self.length)]
40		individual.set_vector(vector)
41
42	@staticmethod
43	def default_gene_creator(individual, index):
44		return individual.get_random_number_in_bounds(index)
class GAVectorCreator(eckity.creators.creator.Creator):
 7class GAVectorCreator(Creator):
 8	def __init__(self,
 9				 length=1,
10				 gene_creator=None,
11				 bounds=(0.0, 1.0),
12				 vector_type=BitStringVector,
13				 fitness_type=SimpleFitness,
14				 events=None):
15		if events is None:
16			events = ["after_creation"]
17		super().__init__(events, fitness_type)
18
19		if gene_creator is None:
20			gene_creator = self.default_gene_creator
21		self.gene_creator = gene_creator
22
23		self.type = vector_type
24		self.length = length
25		self.bounds = bounds
26
27	def create_individuals(self, n_individuals, higher_is_better):
28		individuals = [self.type(length=self.length,
29								 bounds=self.bounds,
30								 fitness=self.fitness_type(higher_is_better=higher_is_better))
31					   for _ in range(n_individuals)]
32		for ind in individuals:
33			self.create_vector(ind)
34		self.created_individuals = individuals
35
36		return individuals
37
38	def create_vector(self, individual):
39		# vector = [self.gene_creator(individual.bounds[i % len(individual.bounds)]) for i in individual.size()]
40		vector = [self.gene_creator(individual, i) for i in range(self.length)]
41		individual.set_vector(vector)
42
43	@staticmethod
44	def default_gene_creator(individual, index):
45		return individual.get_random_number_in_bounds(index)
GAVectorCreator( length=1, gene_creator=None, bounds=(0.0, 1.0), vector_type=<class 'eckity.genetic_encodings.ga.bit_string_vector.BitStringVector'>, fitness_type=<class 'eckity.fitness.simple_fitness.SimpleFitness'>, events=None)
 8	def __init__(self,
 9				 length=1,
10				 gene_creator=None,
11				 bounds=(0.0, 1.0),
12				 vector_type=BitStringVector,
13				 fitness_type=SimpleFitness,
14				 events=None):
15		if events is None:
16			events = ["after_creation"]
17		super().__init__(events, fitness_type)
18
19		if gene_creator is None:
20			gene_creator = self.default_gene_creator
21		self.gene_creator = gene_creator
22
23		self.type = vector_type
24		self.length = length
25		self.bounds = bounds
gene_creator
type
length
bounds
def create_individuals(self, n_individuals, higher_is_better):
27	def create_individuals(self, n_individuals, higher_is_better):
28		individuals = [self.type(length=self.length,
29								 bounds=self.bounds,
30								 fitness=self.fitness_type(higher_is_better=higher_is_better))
31					   for _ in range(n_individuals)]
32		for ind in individuals:
33			self.create_vector(ind)
34		self.created_individuals = individuals
35
36		return individuals
def create_vector(self, individual):
38	def create_vector(self, individual):
39		# vector = [self.gene_creator(individual.bounds[i % len(individual.bounds)]) for i in individual.size()]
40		vector = [self.gene_creator(individual, i) for i in range(self.length)]
41		individual.set_vector(vector)
@staticmethod
def default_gene_creator(individual, index):
43	@staticmethod
44	def default_gene_creator(individual, index):
45		return individual.get_random_number_in_bounds(index)