Unconstrained optimization using Populational Methods (PSO and Genetics)
For constrained optimization there is a PSO implementation (see the examples)
using Pkg
Pkg.add("url=https://github.com/CodeLenz/LPop")First, define a function of
f(x) = (x[1]-3)^2 + (x[2]-5)^2with minimum at
The solution using PSO is
# Number of particles
Np = 20
# Number of iterations (epochs)
niter = 100
# Lower bounds
xl = zeros(2)
# Upper bounds
xu = 10*ones(2)
# Call the optimizer
x_opt = PSO(f,Np,niter,xl,xu)
2-element Vector{Float64}:
2.999999999994289
5.000000000002956
Solution using Genetic Algorithms
# Number of individuals
Np = 20
# Number of iterations (epochs)
ngera = 100
# Lower bounds
xl = zeros(2)
# Upper bounds
xu = 10*ones(2)
# Number of bits (per variable)
Nb = 6
# Mutation rate
mutrate = 2/100
# Type of replacement
replacement = "Elite"
# Call the optimizer
x_opt = Genetic(f,Np,ngera,Nb,mutrate,xl,xu,replacement)
The approach uses the procedure proposed by Deb (Comput. Methods Appl. Mech. Engrg. 186 (2000) 311±338). Lets solve the first example in this manuscript (converting the inequalities to <=0).
This is a hard problem, since the feasible region is only
# Objective function and constraints (<=0)
function f(x)
obj = (x[1]^2+x[2]-11)^2 + (x[1] + x[2]^2 -7)^2
g1 = -(4.84 - (x[1]-0.05)^2 -(x[2]-2.5)^2)
g2 = -(x[1]^2 + (x[2]-2.5)^2 -4.84 )
return [obj; g1; g2]
end
# Lower and upper side constraints
xl = [0.0 ; 0.0]
xu = [6.0 ; 6.0]
# Number of particles
NP = 100
# Number of iterations
niter = 100
# Solve the problem
x_opt, fg_opt = PSO_Deb(f,NP,niter,xl,xu)
The optimal constrained solution is
Using the standart coefficients (no tunning to this problem) and running the program 1000 times with 20 particles and 100 iterations gives 753 correct runs (75%). Using 100 particles gives 1000 correct runs (100%)
Paralelization
Other Selection and Replacement operators for GA
