Add memetic algorithm prototype
This commit is contained in:
parent
ab4748d28e
commit
4e640ffc2d
|
@ -1,50 +1,22 @@
|
||||||
from numpy.random import choice, seed
|
from genetic_algorithm import *
|
||||||
|
from local_search import local_search
|
||||||
|
|
||||||
|
|
||||||
def get_first_random_solution(m, data):
|
def run_local_search(n, m, data, individual):
|
||||||
seed(42)
|
pass
|
||||||
random_indexes = choice(len(data.index), size=m, replace=False)
|
|
||||||
return data.loc[random_indexes]
|
|
||||||
|
|
||||||
|
|
||||||
def element_in_dataframe(solution, element):
|
def memetic_algorithm(n, m, data, hybridation, max_iterations=100000):
|
||||||
duplicates = solution.query(
|
population = [generate_individual(n, m, data) for _ in range(n)]
|
||||||
f"(source == {element.source} and destination == {element.destination}) or (source == {element.destination} and destination == {element.source})"
|
population = evaluate_population(population, data)
|
||||||
)
|
for i in range(max_iterations):
|
||||||
return not duplicates.empty
|
if i % 10 == 0:
|
||||||
|
best_index, _ = get_best_elements(population)
|
||||||
|
run_local_search(n, m, data, individual=population[best_index])
|
||||||
def replace_worst_element(previous, data):
|
parents = select_parents(population, n, mode="stationary")
|
||||||
solution = previous.copy()
|
offspring = crossover(mode="uniform", parents=parents, m=m)
|
||||||
worst_index = solution["distance"].astype(float).idxmin()
|
offspring = mutate(offspring, n, data)
|
||||||
random_element = data.sample().squeeze()
|
population = replace_population(population, offspring, mode="stationary")
|
||||||
while element_in_dataframe(solution=solution, element=random_element):
|
population = evaluate_population(population, data)
|
||||||
random_element = data.sample().squeeze()
|
best_index, _ = get_best_elements(population)
|
||||||
solution.loc[worst_index] = random_element
|
return population[best_index]
|
||||||
return solution, worst_index
|
|
||||||
|
|
||||||
|
|
||||||
def get_random_solution(previous, data):
|
|
||||||
solution, worst_index = replace_worst_element(previous, data)
|
|
||||||
previous_worst_distance = previous["distance"].loc[worst_index]
|
|
||||||
while solution.distance.loc[worst_index] <= previous_worst_distance:
|
|
||||||
solution, _ = replace_worst_element(previous=solution, data=data)
|
|
||||||
return solution
|
|
||||||
|
|
||||||
|
|
||||||
def explore_neighbourhood(element, data, max_iterations=100000):
|
|
||||||
neighbourhood = []
|
|
||||||
neighbourhood.append(element)
|
|
||||||
for _ in range(max_iterations):
|
|
||||||
previous_solution = neighbourhood[-1]
|
|
||||||
neighbour = get_random_solution(previous=previous_solution, data=data)
|
|
||||||
neighbourhood.append(neighbour)
|
|
||||||
return neighbour
|
|
||||||
|
|
||||||
|
|
||||||
def memetic_algorithm(m, data):
|
|
||||||
first_solution = get_first_random_solution(m=m, data=data)
|
|
||||||
best_solution = explore_neighbourhood(
|
|
||||||
element=first_solution, data=data, max_iterations=100
|
|
||||||
)
|
|
||||||
return best_solution
|
|
||||||
|
|
Loading…
Reference in New Issue