Memetic Algorithm in Python

Hello everyone, I’m going to show you and test my Memetic Algorithm in Python. This is my innovative version of Memetic Algorithm for global optimization, which has local search and multi-start mechanisms.

Memetic Algorithm (MA) is a combination of an evolutionary search based optimization algorithm with the problem-specific local search to balance the exploration and exploitation of the algorithm to enhance the solution quality (Deng and Wang 2017).

The main benefit of MA is to combine a global search algorithm such as GA, PSO, ACO, etc. with a local search algorithm to improve the optimal solution (Decerle, Grunder et al. 2018).

Recently, MA has been proven to be powerful and effective in solving complex large-scale optimization problems (Pereira, Ritt et al. 2018; Gong, Deng et al. 2019; Zhou, Benlic et al. 2020).

Here is the general structure of my Memetic Algorithm:

Here is the test function to validate the performance of the proposed Memetic Algorithm:

Let’s see how my Memetic Algorithm works:

For more videos like this, check my YouTube channel here.

import numpy as np
import random
import math
 
def objective_function(pop):
    fitness = np.zeros(pop.shape[0])
    for i in range(pop.shape[0]):
        x=pop[i]
        A = 10
        fitness[i] = 10e6-(A*2 + x[0]**2 - A * np.cos(2 * math.pi * x[0]) + x[1]**2 - A * np.cos(2 * math.pi * x[1]))
    return fitness
 
def selection(pop, fitness, pop_size):
    next_generation = np.zeros((pop_size, pop.shape[1]))
    elite = np.argmax(fitness)
    next_generation[0] = pop[elite]  # keep the best
    fitness = np.delete(fitness,elite)
    pop = np.delete(pop,elite,axis=0)
    P = [f/sum(fitness) for f in fitness]  # selection probability
    index = list(range(pop.shape[0]))
    index_selected = np.random.choice(index, size=pop_size-1, replace=False, p=P)
    s = 0
    for j in range(pop_size-1):
        next_generation[j+1] = pop[index_selected[s]]
        s +=1
    return next_generation
 
def crossover(pop, crossover_rate):
    offspring = np.zeros((crossover_rate, pop.shape[1]))
    for i in range(int(crossover_rate/2)):
        r1=random.randint(0, pop.shape[0]-1)
        r2 = random.randint(0, pop.shape[0]-1)
        while r1 == r2:
            r1 = random.randint(0, pop.shape[0]-1)
            r2 = random.randint(0, pop.shape[0]-1)
        cutting_point = random.randint(1, pop.shape[1] - 1)
        offspring[2*i, 0:cutting_point] = pop[r1, 0:cutting_point]
        offspring[2*i, cutting_point:] = pop[r2, cutting_point:]
        offspring[2*i+1, 0:cutting_point] = pop[r2, 0:cutting_point]
        offspring[2*i+1, cutting_point:] = pop[r1, cutting_point:]
    return offspring
 
def mutation(pop, mutation_rate):
    offspring = np.zeros((mutation_rate, pop.shape[1]))
    for i in range(int(mutation_rate/2)):
        r1=random.randint(0, pop.shape[0]-1)
        r2 = random.randint(0, pop.shape[0]-1)
        while r1 == r2:
            r1 = random.randint(0, pop.shape[0]-1)
            r2 = random.randint(0, pop.shape[0]-1)
        cutting_point = random.randint(0, pop.shape[1]-1)
        offspring[2*i] = pop[r1]
        offspring[2*i,cutting_point] = pop[r2,cutting_point]
        offspring[2*i+1] = pop[r2]
        offspring[2*i+1, cutting_point] = pop[r1, cutting_point]
    return offspring
 
def local_search(pop, fitness, lower_bounds, upper_bounds, step_size, rate):
    index = np.argmax(fitness)
    offspring = np.zeros((rate*2*pop.shape[1], pop.shape[1]))
    for r in range(rate):
        offspring1 = np.zeros((pop.shape[1], pop.shape[1]))
.
.
.
Sorry! This is only a half of the code.

Notice: It would take you from 1 to 3 hours to re-type the Python code yourself; or with just €3.99 (the cost of a cup of coffee), you can download/copy the whole Python code within 2 minutes. It’s your choice to make.

Original price is €9.99 but today it’s only €3.99 (save €6 today – available for a limited time only)

Download the whole Python code here (Membership Code ID: 017)

No need to build the Python code from scratch because it’s very time-consuming. My idol, Jim Rohn, once said: “Time is more value than money. You can get more money, but you cannot get more time”. If you think this code can be used in your research/teaching work, you should download it and then customize/modify/apply it to your work, without any obligation of citing the original source if you don’t want. However, redistribution (i.e., downloading the code/script here and then making it available on another site on the Internet) is strictly prohibited.

If you have any question or problem, please contact Dr. Panda by email: learnwithpanda2018@gmail.com

Thank you very much and good luck with your research!

4 Replies to “Memetic Algorithm in Python”

Leave a Reply

Your email address will not be published. Required fields are marked *