diff --git a/src/EasyGA.py b/src/EasyGA.py index f17d6ba..92baaac 100644 --- a/src/EasyGA.py +++ b/src/EasyGA.py @@ -38,21 +38,17 @@ class GA: #assuming domain if string (strings can never be range) for x in range(len(self.gene_input)): - if isinstance(gene_input[x], int): + if isinstance(gene_input[x], list) == False: self.gene_input[x] = [self.gene_input[x], self.gene_input[x]] - 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[x] = "domain" - break - elif y == (len(self.gene_input[x]) -1): - self.gene_input_type[x] = "range" - else: - if isinstance(gene_input[x], str): + if self.gene_input_type[x] == None: #If it hasn't been hard-set by the user + for y in range(len(self.gene_input[x])): + if isinstance(gene_input[x][y], str): self.gene_input_type[x] = "domain" - else: + break + elif isinstance(gene_input[x][y], float): + self.gene_input_type[x] = "float-range" + elif y == (len(self.gene_input[x]) -1 and self.gene_input_type[x] != "float-range"): self.gene_input_type[x] = "range" diff --git a/src/initialization/gene_function/gene_random.py b/src/initialization/gene_function/gene_random.py index d3e16dd..af2cdd1 100644 --- a/src/initialization/gene_function/gene_random.py +++ b/src/initialization/gene_function/gene_random.py @@ -10,18 +10,13 @@ def check_values(low,high): assert high != 0, "High value can not be zero" def random_gene(gene_input, gene_input_type, gene_index): - created_gene = None #Determining if single range/domain or index-dependent - if isinstance(gene_input[0], list): - if gene_input_type[gene_index] == "range": - created_gene = random.randint(gene_input[gene_index][0], gene_input[gene_index][1]) - elif gene_input_type[gene_index] == "domain": - created_gene = random.choice(gene_input[gene_index]) - else: - if gene_input_type[gene_index] == "range": - created_gene = random.randint(gene_input[0], gene_input[1]) - elif gene_input_type[gene_index] == "domain": - created_gene = random.choice(gene_input) + if gene_input_type[gene_index] == "range": + created_gene = random.randint(gene_input[gene_index][0], gene_input[gene_index][1]) + elif gene_input_type[gene_index] == "domain": + created_gene = random.choice(gene_input[gene_index]) + elif gene_input_type[gene_index] == "float-range": + created_gene = random.uniform(gene_input[gene_index][0], gene_input[gene_index][1]) return created_gene diff --git a/src/new_initialization_method_testing.py b/src/new_initialization_method_testing.py index 8f4e3b7..07b1a09 100644 --- a/src/new_initialization_method_testing.py +++ b/src/new_initialization_method_testing.py @@ -1,11 +1,19 @@ import EasyGA import random +#1. GA should take in range for gene input +#2. GA should take in index-dependent range for gene input +#3. GA should take in domain input +#4. GA should take in index-dependent domain for gene input +#5. GA should accept mix of range and domain for gene input + + # Create the Genetic algorithm ga = EasyGA.GA() -test_range_two = [["left", "right"],[22,35],5,[22,"up"]] -ga.gene_input_type[2] = "domain" -ga.initialize(test_range_two) +test_gene_input = [["left", "right"],[1,100],[5.0,10],[22,"up"]] +#ga.gene_input_type[1] = "domain" +#ga.gene_input_type[1] = "float-range" +ga.initialize(test_gene_input) ga.population.print_all()