Clearified some code and how to use i.
This commit is contained in:
39
README.md
39
README.md
@ -24,7 +24,11 @@ import EasyGA
|
|||||||
def user_gene_function():
|
def user_gene_function():
|
||||||
return random.randint(1, 100)
|
return random.randint(1, 100)
|
||||||
|
|
||||||
# Standard user size requirements:
|
# The user defined Fitness Function
|
||||||
|
def user_fitness_function():
|
||||||
|
pass
|
||||||
|
|
||||||
|
# Standard user size requirements
|
||||||
Population_size = 10
|
Population_size = 10
|
||||||
Chromosome_length = 10
|
Chromosome_length = 10
|
||||||
|
|
||||||
@ -32,10 +36,41 @@ Chromosome_length = 10
|
|||||||
ga = EasyGA.GA(Population_size, Chromosome_length,user_gene_function)
|
ga = EasyGA.GA(Population_size, Chromosome_length,user_gene_function)
|
||||||
ga.initialize()
|
ga.initialize()
|
||||||
|
|
||||||
# Looking at the first chromosome in the population:
|
# Looking to print the first Chromosome
|
||||||
ga.population.chromosomes[0].print_chromosome()
|
ga.population.chromosomes[0].print_chromosome()
|
||||||
|
|
||||||
|
# Looking to print one gene in chromosome 0
|
||||||
|
ga.population.chromosomes[0].genes[0].print_value()
|
||||||
|
|
||||||
|
# Looking to get the data of a chromosome
|
||||||
|
my_chromosome = ga.population.chromosomes[0].get_chromosome()
|
||||||
|
print(f"my_chromosome: {my_chromosome}")
|
||||||
|
# Looking to get the data of one gene in the chromosome
|
||||||
|
my_gene = ga.population.chromosomes[0].genes[0].get_value()
|
||||||
|
print(f"my_gene: {my_gene}")
|
||||||
|
|
||||||
```
|
```
|
||||||
|
## Ouput
|
||||||
|
```Python
|
||||||
|
[99],[30],[59],[77],[68],[57],[14],[92],[85],[27]
|
||||||
|
|
||||||
|
99
|
||||||
|
|
||||||
|
my_chromosome: [<EasyGA.gene object at 0x7fb5642d4860>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4898>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4908>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d49e8>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4b00>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4ba8>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4b70>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4c88>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4cc0>,
|
||||||
|
<EasyGA.gene object at 0x7fb5642d4cf8>]
|
||||||
|
|
||||||
|
my_gene: 99
|
||||||
|
|
||||||
|
```
|
||||||
|
|
||||||
|
|
||||||
# Developing EasyGA:
|
# Developing EasyGA:
|
||||||
Download the repository to some folder - If you never used git. Look up a youtube tutorial. It will all make sense.
|
Download the repository to some folder - If you never used git. Look up a youtube tutorial. It will all make sense.
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
# Defult packages for GA functionality
|
||||||
from initialization.random_initialization import random_initialization
|
from initialization.random_initialization import random_initialization
|
||||||
|
|
||||||
def check_gene(value):
|
def check_gene(value):
|
||||||
@ -8,42 +9,50 @@ def check_gene(value):
|
|||||||
## Your main structure
|
## Your main structure
|
||||||
class gene:
|
class gene:
|
||||||
# Defults
|
# Defults
|
||||||
# fitness = double , value = Anything
|
# fitness = Anything, value = Anything
|
||||||
def __init__(self, value):
|
def __init__(self, value):
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
self.value = check_gene(value)
|
self.value = check_gene(value)
|
||||||
|
|
||||||
def get_fitness(self):
|
|
||||||
return self.fitness
|
|
||||||
|
|
||||||
def set_fitness(self,fitness):
|
def set_fitness(self,fitness):
|
||||||
self.fitness = fitness
|
self.fitness = fitness
|
||||||
|
|
||||||
|
def get_fitness(self):
|
||||||
|
return self.fitness
|
||||||
|
|
||||||
def get_value(self):
|
def get_value(self):
|
||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
def print_value(self):
|
||||||
|
print(self.value)
|
||||||
|
|
||||||
|
def print_fitness(self):
|
||||||
|
print(self.fitness)
|
||||||
|
|
||||||
class chromosome:
|
class chromosome:
|
||||||
# Defults
|
# Defults
|
||||||
# fitness = double, genes = [gene,gene,gene,etc]
|
# fitness = Anything, genes = [gene,gene,gene,etc]
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.fitness = None
|
self.fitness = None
|
||||||
self.genes = []
|
self.genes = []
|
||||||
|
|
||||||
|
def add_gene(self,gene):
|
||||||
|
self.genes.append(gene)
|
||||||
|
|
||||||
def get_fitness(self):
|
def get_fitness(self):
|
||||||
return self.score
|
return self.score
|
||||||
|
|
||||||
def add_gene(self,gene):
|
def get_chromosome(self):
|
||||||
self.genes.append(gene)
|
return self.genes
|
||||||
|
|
||||||
def print_chromosome(self):
|
def print_chromosome(self):
|
||||||
for i in range(len(self.genes)):
|
for i in range(len(self.genes)):
|
||||||
# Print the gene one by one.
|
# Print the gene one by one.
|
||||||
if(i == len(self.genes) - 1):
|
if(i == len(self.genes) - 1):
|
||||||
print(f"[{self.genes[i].get_value()}]", end = '')
|
print(f"[{self.genes[i].get_value()}]")
|
||||||
else:
|
else:
|
||||||
print(f"[{self.genes[i].get_value()}],", end = '')
|
print(f"[{self.genes[i].get_value()}],", end = '')
|
||||||
|
|
||||||
|
|
||||||
class population:
|
class population:
|
||||||
# population = [chromosome,chromosome,etc]
|
# population = [chromosome,chromosome,etc]
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|||||||
@ -5,6 +5,10 @@ import EasyGA
|
|||||||
def user_gene_function():
|
def user_gene_function():
|
||||||
return random.randint(1, 100)
|
return random.randint(1, 100)
|
||||||
|
|
||||||
|
# The user defined Fitness Function
|
||||||
|
def user_fitness_function():
|
||||||
|
pass
|
||||||
|
|
||||||
# Standard user size requirements
|
# Standard user size requirements
|
||||||
Population_size = 10
|
Population_size = 10
|
||||||
Chromosome_length = 10
|
Chromosome_length = 10
|
||||||
@ -13,5 +17,15 @@ Chromosome_length = 10
|
|||||||
ga = EasyGA.GA(Population_size, Chromosome_length,user_gene_function)
|
ga = EasyGA.GA(Population_size, Chromosome_length,user_gene_function)
|
||||||
ga.initialize()
|
ga.initialize()
|
||||||
|
|
||||||
# Looking at the first chromosome in the population
|
# Looking to print the first Chromosome
|
||||||
ga.population.chromosomes[0].print_chromosome()
|
ga.population.chromosomes[0].print_chromosome()
|
||||||
|
|
||||||
|
# Looking to print one gene in chromosome 0
|
||||||
|
ga.population.chromosomes[0].genes[0].print_value()
|
||||||
|
|
||||||
|
# Looking to get the data of a chromosome
|
||||||
|
my_chromosome = ga.population.chromosomes[0].get_chromosome()
|
||||||
|
print(f"my_chromosome: {my_chromosome}")
|
||||||
|
# Looking to get the data of one gene in the chromosome
|
||||||
|
my_gene = ga.population.chromosomes[0].genes[0].get_value()
|
||||||
|
print(f"my_gene: {my_gene}")
|
||||||
|
|||||||
Reference in New Issue
Block a user