The genetic algorithm is an excellent evolutionary algorithm for the optimization process. There are several modules that can handle the optimization process via genetic algorithm, but most of them require the knowledge of object-oriented programming and difficult to understand for beginners. Fortunately, there is an easy python module which can handle continuous, combinatorial and mixed optimization problems with continuous, discrete, and mixed variables. It provides an easy implementation of genetic-algorithm (GA) in Python. The package is developed by Rayan(Mohammad) Solgi.
You have to install this package via pip or conda(if you are a conda) package manager. An example of pip installer code is given below:
pip install geneticalgorithm
Solving a simple optimization problem
Let, we have to minimize this function
f (x,y,z)=xy-z
where 2<x<6.5 : 3<y<8, and 3<z<5
It is certain that the minimum value will be 2*3-5=1. Now let's see how to implement it via this pakage.
Impleentation of geneticalgorithm package
Genetic algorithm package needs numpy array function in its different arguments. So, numpy will be imported first followed by geneticalgorithm
import numpy as np
from geneticalgorithm import geneticalgorithm as ga
Now, lets set up the variable array and the function. The function will receive a variable array, will multiply the first two elements, and subtract the third element.
def f(x):
return x[0]*x[1]-x[2]
Now we have to define our variable boundaries and variable dimensions. Also let us assume that we want real number for the values of x, and integer for y and z. It can be defined as below:
varbound=np.array([[2,6],[3,8],[3,5]])
vartype=np.array([['real'],['int'],['int']])
Now we have to set up the important parameters like population size, iteration, crossover cize etc.
algorithm_param = {'max_num_iteration': 3000,\
'population_size':500,\
'mutation_probability':0.1,\
'elit_ratio': 0.01,\
'crossover_probability': 0.8,\
'parents_portion': 0.3,\
'crossover_type':'uniform',\
'max_iteration_without_improv':None}
Here:
- max_num_iteration: Can be None or any value you want.
- population_size: determines the number of trial solutions in each iteration. The default value is 100
- crossover_type: can be uniform, one_point,two_point
- elit_ratio: determines the number of elites in the population. The default value is 0.01 (i.e. 1 percent). For example when population size is 100 and elit_ratio is 0.01 then there is one elite in the population. If this parameter is set to be zero then geneticalgorithm implements a standard genetic algorithm instead of elitist GA.
- parents_portion: the portion of population filled by the members of the previous generation (aka parents); default is 0.3 (i.e. 30 percent of population)
- max_iteration_without_improv: if the algorithms does not improve the objective function over the number of successive iterations determined by this parameter, then geneticalgorithm stops and report the best found solution before the max_num_iterations to be met. The default value is None
Finally we can get up the ga function to get the result
model=ga(function=f,dimension=3,variable_type_mixed=vartype,variable_boundaries=varbound,algorithm_parameters=algorithm_param)
model.run()
which yields the result
The best solution found: [2.00019667 3. 5. ] Objective function: 1.0005900088155135
And a graph of iteration
It is to be noted that, genetic algorithm always yields the minimum solution. To get the maximum solution, put a minus sign before the function in the function section.
You can find the full code here
Comments
Post a Comment