diff --git a/src/EasyGA.py b/src/EasyGA.py index 75b7986..9f367b8 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -21,6 +21,9 @@ class GA: self.gene_function_impl = random_gene self.gene_input = [] self.gene_input_type = [] #What if user gives two numbers (i.e. [1,100]) but wants to pick between the two (domain)? + while len(self.gene_input_type) != self.chromosome_length: + self.gene_input_type.append(None) + # Set the GA Configuration self.initialization_impl = random_initialization #self.mutation_impl = PerGeneMutation(Mutation_rate) @@ -34,26 +37,24 @@ class GA: self.gene_input = gene_input #assuming domain if string (strings can never be range) - if self.gene_input_type == []: - for x in range(len(self.gene_input)): + for x in range(len(self.gene_input)): + if self.gene_input_type[x] == None: if (isinstance(self.gene_input[x], list)): for y in range(len(self.gene_input[x])): if isinstance(gene_input[x][y], str): - self.gene_input_type.append("domain") + self.gene_input_type[x] = "domain" break elif y == (len(self.gene_input[x]) -1): - self.gene_input_type.append("range") + self.gene_input_type[x] = "range" else: if isinstance(gene_input[x], str): - self.gene_input_type.append("domain") + self.gene_input_type[x] = "domain" else: if isinstance(gene_input[x], int): self.gene_input[x] = [self.gene_input[x], self.gene_input[x]] - self.gene_input_type.append("range") + self.gene_input_type[x] = "range" - #If length doesn't correspond to chromosome, update here - while len(self.gene_input_type) != self.chromosome_length: - self.gene_input_type.append(self.gene_input_type[len(self.gene_input_type)-1]) + # Create the first population self.population = self.initialization_impl( diff --git a/src/new_initialization_method_testing.py b/src/new_initialization_method_testing.py index 8687dd0..2cbe4d2 100644 --- a/src/new_initialization_method_testing.py +++ b/src/new_initialization_method_testing.py @@ -3,9 +3,10 @@ import random # Create the Genetic algorithm ga = EasyGA.GA() -test_range_two = [["left", "right"],[22,88],5,[22,"up"]] +test_range_two = [["left", "right"],[22,35],5,[22,"up"]] +ga.gene_input_type[1] = "domain" ga.initialize(test_range_two) -ga.population.print_all() +ga.population.print_all()