Particle swarm optimization in fortran.
The repository opti contains a particle swarm optimizer (PSO) written in fortran. In the future more optimizer are planed.
For the PSO the user can chose between two topologies. Either the traditional star topology or an random adaptive neighberhood which is better for exploration of parameter spaces. This is useful when many parameters have to be optimized.
The project structured as a fpm (fortran package manager) project. Thus, the project can be used by adding the following two lines to the toml file:
[dependencies] \
opti = { git=”https://github.com/Konrad1991/opti"}
The project contains only one subroutine called optimizer. It accepts the following parameter:
interface
function fct (inp, problem_size) result(out)
implicit none
integer, intent(in) :: problem_size
real(8), intent(in), dimension(problem_size) :: inp
real(8) :: out
end function fct
end interface
module objectivefct
contains
function f(inp, problem_size) result(out)
!! This function defines a multidimensional rosenbrock function
!! For problem_size = 3 Minimum at (1,1,1)
!! For 4 <= problem_size <= 7 local minima near (-1, 1, ...,1)
implicit none
integer, intent(in) :: problem_size
real(8), intent(in), dimension(problem_size) :: inp
real(8) :: out
integer :: i
out = 0.0
do i = 1, (problem_size -1)
out = out + 100.0*(inp(i+1) - inp(i)**2)**2 + (1 - inp(i) )**2
end do
end function
end module
program example
use objectivefct
use psomod
implicit none
integer:: n_swarm, n_generations, n_params
real(8) :: lb(3), ub(3)
real(8) :: desired_error
real(8) :: result(3)
n_swarm = 40
n_generations = 10000
n_params = 3
lb = -10
ub = 10
desired_error = 0.00001
call optimizer(n_swarm, n_generations, n_params, lb, ub, desired_error, f, result)
print*, result
end program example