From f61cb7002e8b149b12866e6aac8a3f2fc7408cca Mon Sep 17 00:00:00 2001
From: coolneng <akasroua@gmail.com>
Date: Mon, 21 Jun 2021 18:22:31 +0200
Subject: [PATCH] Implement memetic algorithm

---
 src/local_search.py      |  5 ++---
 src/memetic_algorithm.py | 32 ++++++++++++++++++++++++++++----
 2 files changed, 30 insertions(+), 7 deletions(-)

diff --git a/src/local_search.py b/src/local_search.py
index 06f6834..79a6bfc 100644
--- a/src/local_search.py
+++ b/src/local_search.py
@@ -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
diff --git a/src/memetic_algorithm.py b/src/memetic_algorithm.py
index 20383c7..0308150 100644
--- a/src/memetic_algorithm.py
+++ b/src/memetic_algorithm.py
@@ -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)