mutate function done?
parent
a6b906d9b3
commit
6547edc23e
|
|
@ -18,17 +18,20 @@ import utils
|
||||||
POPULATION_SIZE = 10
|
POPULATION_SIZE = 10
|
||||||
SELECTION_SIZE = (POPULATION_SIZE * 7) // 10 # 70% of population, rounded down for selection
|
SELECTION_SIZE = (POPULATION_SIZE * 7) // 10 # 70% of population, rounded down for selection
|
||||||
CROSSOVER_PAIR_SIZE = (POPULATION_SIZE - SELECTION_SIZE) // 2 # pairs needed for crossover
|
CROSSOVER_PAIR_SIZE = (POPULATION_SIZE - SELECTION_SIZE) // 2 # pairs needed for crossover
|
||||||
XOVER_POINT = 3
|
XOVER_POINT = 3 # 4th position
|
||||||
|
MUTATION_BITS = POPULATION_SIZE * 1
|
||||||
|
|
||||||
fitness = 0.01
|
fitness = 0.01
|
||||||
pop_grey = []
|
pop_grey = []
|
||||||
pop_bin = []
|
pop_bin = [] # 32 Bit Binary
|
||||||
pop_bin_params = []
|
pop_bin_params = []
|
||||||
pop_new = []
|
pop_new = [] # 32 Bit Grey-Code as String
|
||||||
|
|
||||||
e_func = lambda x: np.e**x
|
e_func = lambda x: np.e**x
|
||||||
|
|
||||||
def generate_random_population():
|
def generate_random_population():
|
||||||
|
""" Puts random 32 Bit binary strings into 4 * 8 Bit long params. """
|
||||||
|
# Random population array
|
||||||
for i in range(POPULATION_SIZE):
|
for i in range(POPULATION_SIZE):
|
||||||
pop_grey[i] = format(random.getrandbits(32), '32b')
|
pop_grey[i] = format(random.getrandbits(32), '32b')
|
||||||
pop_bin[i] = utils.grey_to_bin(pop_grey[i])
|
pop_bin[i] = utils.grey_to_bin(pop_grey[i])
|
||||||
|
|
@ -61,9 +64,10 @@ def eval_fitness(pop_bin_values):
|
||||||
|
|
||||||
return fitness_arr
|
return fitness_arr
|
||||||
|
|
||||||
def select(fitness_arr):
|
def select(population, fitness_arr):
|
||||||
sum_of_fitness = sum(fitness_arr)
|
sum_of_fitness = sum(fitness_arr)
|
||||||
while len(pop_new) < SELECTION_SIZE:
|
while len(population) < SELECTION_SIZE:
|
||||||
|
# Roulette logic
|
||||||
roulette_num = random.random()
|
roulette_num = random.random()
|
||||||
is_chosen = False
|
is_chosen = False
|
||||||
while not is_chosen:
|
while not is_chosen:
|
||||||
|
|
@ -71,8 +75,8 @@ def select(fitness_arr):
|
||||||
for i, fitness in enumerate(fitness_arr):
|
for i, fitness in enumerate(fitness_arr):
|
||||||
cumulative_p += fitness / sum_of_fitness
|
cumulative_p += fitness / sum_of_fitness
|
||||||
if roulette_num < cumulative_p:
|
if roulette_num < cumulative_p:
|
||||||
# Add the 32 Bit individual in grey code to pop_new
|
# Add the 32 Bit individual in grey code to population
|
||||||
pop_new.append(pop_grey[i])
|
population.append(pop_grey[i])
|
||||||
|
|
||||||
# Calc new sum of fitness
|
# Calc new sum of fitness
|
||||||
fitness_arr.pop(i)
|
fitness_arr.pop(i)
|
||||||
|
|
@ -81,14 +85,40 @@ def select(fitness_arr):
|
||||||
is_chosen = True # break while loop
|
is_chosen = True # break while loop
|
||||||
break # break for loop
|
break # break for loop
|
||||||
|
|
||||||
def xover():
|
def xover(population, xover_rate):
|
||||||
# calc how many pairs are possible with pop_new
|
"""Performs crossover on pairs of individuals from population."""
|
||||||
individual_a = pop_new[0]
|
offspring = []
|
||||||
individual_b = pop_new[1]
|
|
||||||
|
|
||||||
|
# Process pairs while we have enough individuals and haven't reached CROSSOVER_PAIR_SIZE
|
||||||
|
pair_count = 0
|
||||||
|
i = 0
|
||||||
|
while i < len(population) - 1 and pair_count < xover_rate:
|
||||||
|
parent_a = population[i]
|
||||||
|
parent_b = population[i + 1]
|
||||||
|
|
||||||
# get first three pairs in pop_new
|
# Create two new offspring by swapping parts at XOVER_POINT
|
||||||
# do the crossover
|
offspring_a = parent_a[:XOVER_POINT] + parent_b[XOVER_POINT:]
|
||||||
|
offspring_b = parent_b[:XOVER_POINT] + parent_a[XOVER_POINT:]
|
||||||
|
|
||||||
|
offspring.extend([offspring_a, offspring_b])
|
||||||
|
pair_count += 1
|
||||||
|
i += 2 # Move to next pair
|
||||||
|
|
||||||
|
return offspring
|
||||||
|
|
||||||
|
def mutate(population, mutation_rate):
|
||||||
|
"""Mutate random bits in the population with given mutation rate"""
|
||||||
|
for _ in range(mutation_rate):
|
||||||
|
# Select random individual and convert to list for efficient modification
|
||||||
|
random_num = random.randrange(POPULATION_SIZE)
|
||||||
|
bits = list(population[random_num])
|
||||||
|
|
||||||
|
# Flip random bit
|
||||||
|
bit_pos = random.randrange(32)
|
||||||
|
bits[bit_pos] = '1' if bits[bit_pos] == '0' else '0'
|
||||||
|
|
||||||
|
# Convert back to string and update population
|
||||||
|
population[random_num] = ''.join(bits)
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
pop_bin_values = generate_random_population(10)
|
pop_bin_values = generate_random_population(10)
|
||||||
|
|
@ -98,13 +128,16 @@ def main():
|
||||||
fitness_arr = eval_fitness(pop_bin_values)
|
fitness_arr = eval_fitness(pop_bin_values)
|
||||||
|
|
||||||
# Selection
|
# Selection
|
||||||
select(fitness_arr) # Alters pop_new
|
select(pop_new, fitness_arr) # Alters pop_new
|
||||||
|
|
||||||
# Crossover
|
# Crossover
|
||||||
|
offspring = xover(pop_new, CROSSOVER_PAIR_SIZE)
|
||||||
|
pop_new.extend(offspring) # .extend needed
|
||||||
|
|
||||||
# mutation
|
# Mutation
|
||||||
|
mutate(pop_new, MUTATION_BITS)
|
||||||
|
|
||||||
# pop_grey = pop_new
|
pop_grey = pop_new
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
|
|
||||||
|
|
@ -14,7 +14,7 @@ def bin_to_grey(binary):
|
||||||
return format(gray, f'0{len(binary)}b') # Convert back to binary string with same length
|
return format(gray, f'0{len(binary)}b') # Convert back to binary string with same length
|
||||||
|
|
||||||
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
def bin_to_param(binary, q_min = 0.0, q_max = 10.0):
|
||||||
"""Convert binary string to float parameter in range [q_min, q_max]"""
|
"""Convert one binary string to float parameter in range [q_min, q_max]"""
|
||||||
val = int(binary, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
val = int(binary, 2) / 25.5 * 10 # conversion to 0.0 - 10.0 float
|
||||||
# Scale to range [q_min, q_max]
|
# Scale to range [q_min, q_max]
|
||||||
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
q = q_min + ((q_max - q_min) / (2**len(binary))) * val
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue