# Function Optimization using Genetic Algorithm and Particle Swarm Optimization-Results and Comparison

In this article some experiments and their results are discussed for minimization of Rastrigin function, a famous mathematical function used in Optimization Techniques evaluation. The experiments are performed on certain setup, parameters and system. This has been performed using Genetic Algorithm (GA) and Particle Swarm Optimization (PSO). The fitness function is the given function to be optimized. This article just pays focus on the way experiments are to be performed and analyzed in students lab and by mentors teaching Machine Learning results do not play a role in benchmarking or changing existing theories. It is just for elaboration for the purpose of academicians and students to learn the art of optimizing function with the help of GA and PSO.

This function is given by

The function is usually evaluated on xi ∈ [-5.12, 5.12], for all i = 1, …, n . This has been tried on 5 different values of n between 10 and 100 as follows. Taking N as the size of the chromosome for GA and PSO implementation and population size of 10.

The Matlab implementation of fitness function is

The following table shows the experimental results.

The general analysis of results is that PSO is performing better than GA here given the same number of iterations. Though GA solutions are gradually decreasing while PSO solutions are oscillating.  Within PSO the c1=3, c2=3 seems to perform best, c1=1 and c2=2 seem to perform the worst in achieving global minimum. More details and comments below.

Num of chromosomes : 10

One point crossover GA., with 5% mutation rate.

Maximum  number of iterations: 500.

## Graphical Visualization of Results

Command for plots:

### Here is plot for N=80

Green: PSO1: with parameters : c1=1,c2=2 : Oscillates and convergence not as fast as other parameters.

Blue: PSO2 : with parameters : c1=3,c2=3  :  Oscillates and reaches towards minima.

Red: PSO3 : with parameters : c1=3,c2=2  : Oscillates.  Reaches towards minima

Black: PSO4 : with parameters :  c1=2,c2=3 : Oscillates.  Reaches  towards minima

Dots : GA, still decreasing and is more stable though global minimum is not less than PSO.

And comparing GA and PSO , GA seems not to oscillates and be stable though its minimum not attained as fast.

PSO1 with c1=1,c2=2, seem to not be as fast in attaining minimum as compared to other parameters which are almost equivalent in attaining minimum. Here is separate result of c1=1, c2=2.

Plot of the three competing models is as follows:

## Results for N=60

Green: PSO1: with parameters: c1=1,c2=2: Oscillates and convergence not as fast as other parameters.

Blue: PSO2 : with parameters : c1=3,c2=3  :  Oscillates and reaches towards minima.

Red: PSO3 : with parameters : c1=3,c2=2  : Oscillates.  Reaches towards minima

Black: PSO4 : with parameters :  c1=2,c2=3 : Oscillates.  Reaches  towards minima

Dots: GA, still decreasing and is more stable though global minimum is not less than PSO.

And comparing GA and PSO , GA seems not to oscillates and be stable though its minimum not attained as fast.

Plot of three competing models as follows

PSO1 with c1=1,c2=2, seem to not be as fast in attaining minimum as compared to other parameters which are almost equivalent in attaining minimum. Here is separate result of c1=1, c2=2.

## Results for N=40

Green: PSO1: with parameters : c1=1,c2=2 : Oscillates and convergence not as fast as other parameters.

Blue: PSO2 : with parameters : c1=3,c2=3  :  Oscillates and reaches towards minima.

Red: PSO3 : with parameters : c1=3,c2=2  : Oscillates.  Reaches towards minima

Black: PSO4 : with parameters :  c1=2,c2=3 : Oscillates.  Reaches  towards minima

Dots: GA, still decreasing and is more stable though global minimum is not less than pso.

And comparing GA and PSO , GA seems not to oscillates so much and be stable though its minimum not attained as fast.

PSO1 with c1=1,c2=2, seem to not be as fast in attaining minimum as compared to other parameters which are almost equivalent in attaining minimum. Here is separate result of c1=1, c2=2.

## Results for N=20

Green: PSO1: with parameters : c1=1,c2=2 : Oscillates and convergence not as fast as other parameters.

Blue: PSO2 : with parameters : c1=3,c2=3  :  Oscillates and reaches towards minima.

Red: PSO3 : with parameters : c1=3,c2=2  : Oscillates.  Reaches towards minima

Black: PSO4 : with parameters :  c1=2,c2=3 : Oscillates.  Reaches  towards minima

Dots: GA, still decreasing and is more stable though global minimum is not less than PSO.

And comparing GA and PSO , GA seems not to oscillates and be stable though its minimum not attained as fast.

PSO1 with c1=1,c2=2, seem to not be as fast in attaining minimum as compared to other parameters which are almost equivalent in attaining minimum. Here is separate result of c1=1, c2=2.

## Results for N=11

Green: PSO1: with parameters : c1=1,c2=2 : Oscillates and convergence not as fast as other parameters.

Blue: PSO2 : with parameters : c1=3,c2=3  :  Oscillates and reaches towards minima.

Red: PSO3 : with parameters : c1=3,c2=2  : Oscillates.  Reaches towards minima

Black: PSO4 : with parameters :  c1=2,c2=3 : Oscillates.  Reaches  towards minima

Dots: GA, still decreasing and is more stable though global minimum is not less than PSO

And comparing GA and PSO , GA seems not to oscillates and be stable though its minimum not attained as fast.

PSO1 with c1=1,c2=2, seem to not be as fast in attaining minimum as compared to other parameters which are almost equivalent in attaining minimum. Here is separate result of c1=1, c2=2.

The experiments are performed on certain setup, parameters and system. The general impression and analysis are given at end of each experiment conducted. Changing parameters, the mutations functions, population to be performed for creating new chromosomes in case of GA drastically effects the results obtained. In a similar manner for PSO several parameters, initializations effect the results apart from C1 and C2 experimented above. The results are not benchmarks, they are elaborated for experimental setups in labs, and to be followed by mentors teaching Machine Learning Lab Work. For benchmark results look into peer-reviewed research papers.