Improve memetic algorithm
This commit is contained in:
parent
9aeff47bb1
commit
7a8ec91550
|
@ -67,9 +67,8 @@ def explore_neighbourhood(element, n, data, max_iterations=100000):
|
|||
return neighbour
|
||||
|
||||
|
||||
def local_search(n, m, data):
|
||||
first_solution = get_first_random_solution(n, m, data)
|
||||
def local_search(first_solution, n, data):
|
||||
best_solution = explore_neighbourhood(
|
||||
element=first_solution, n=n, data=data, max_iterations=100
|
||||
element=first_solution, n=n, data=data, max_iterations=50
|
||||
)
|
||||
return best_solution
|
||||
|
|
|
@ -1,9 +1,33 @@
|
|||
from genetic_algorithm import *
|
||||
from local_search import local_search
|
||||
from copy import deepcopy
|
||||
|
||||
|
||||
def run_local_search(n, m, data, individual):
|
||||
pass
|
||||
def get_best_indices(n, population):
|
||||
select_population = deepcopy(population)
|
||||
best_elements = []
|
||||
for _ in range(n):
|
||||
best_index, _ = get_best_elements(select_population)
|
||||
best_elements.append(best_index)
|
||||
select_population.pop(best_index)
|
||||
return best_elements
|
||||
|
||||
|
||||
def run_local_search(n, data, population, mode, probability=0.1):
|
||||
new_population = []
|
||||
if mode == "all":
|
||||
for individual in population:
|
||||
new_population.append(local_search(individual, n, data))
|
||||
elif mode == "random":
|
||||
expected_individuals = len(population) * probability
|
||||
for _ in range(expected_individuals):
|
||||
random_individual = population[randint(len(population))]
|
||||
new_population.append(local_search(random_individual, n, data))
|
||||
else:
|
||||
expected_individuals = len(population) * probability
|
||||
best_indexes = get_best_indices(n=expected_individuals, population=population)
|
||||
for element in best_indexes:
|
||||
new_population.append(local_search(population[element], n, data))
|
||||
|
||||
|
||||
def memetic_algorithm(n, m, data, hybridation, max_iterations=100000):
|
||||
|
@ -12,9 +36,9 @@ def memetic_algorithm(n, m, data, hybridation, max_iterations=100000):
|
|||
for i in range(max_iterations):
|
||||
if i % 10 == 0:
|
||||
best_index, _ = get_best_elements(population)
|
||||
run_local_search(n, m, data, individual=population[best_index])
|
||||
run_local_search(n, data, population, mode=hybridation)
|
||||
parents = select_parents(population, n, mode="stationary")
|
||||
offspring = crossover(mode="uniform", parents=parents, m=m)
|
||||
offspring = crossover(mode="position", parents=parents, m=m)
|
||||
offspring = mutate(offspring, n, data)
|
||||
population = replace_population(population, offspring, mode="stationary")
|
||||
population = evaluate_population(population, data)
|
||||
|
|
Loading…
Reference in New Issue