SciML / PSOGPU.jl

GPU accelerated Particle Swarm Optimization
MIT License
13 stars 1 forks source link

Start implementation of sync PSO on GPU #17

Closed utkarsh530 closed 10 months ago

utkarsh530 commented 10 months ago

MWE:

using CUDA, StaticArrays, PSOGPU, Setfield

using Base

import PSOGPU: init_particles, _update_particle_states!, calculate_loss!, PSOGBest

device!(2)

## Solving the rosenbrock problem

n = 3

lb = @SArray ones(Float32, n)

lb = -1*lb

ub = @SArray ones(Float32, n)

rosenbrock(x, p) = sum(p[2]* (x[i+1] - x[i]^2)^2 + (p[1] - x[i])^2 for i = 1:length(x) - 1)

x0 = @SArray zeros(Float32, n)
p = @SArray Float32[2.0, 100.0]

prob = OptimizationProblem(rosenbrock, x0, p; lb = lb, ub = ub)

n_particles = 1000

maxiters = 1000

gbest, particles = init_particles(prob, n_particles)

gpu_particles = cu(particles)

w = 0.7298f0
wdamp = 1.0f0

kernel1 = @cuda launch=false _update_particle_states!(prob, gpu_particles, gbest, w)

kernel2 = @cuda launch=false calculate_loss!(prob, gpu_particles)

kernel1(prob, gpu_particles, gbest, w)

kernel2(prob, gpu_particles)

for i in 1:maxiters

    kernel1(prob, gpu_particles, gbest, w)

    kernel2(prob, gpu_particles)

    best_particle = minimum(gpu_particles)
    gbest = PSOGBest(best_particle.best_position, best_particle.best_cost)
    @show gbest
    w = w * wdamp
end
utkarsh530 commented 10 months ago

@Vaibhavdixit02 somehow the global optima is not coming to be optimal

utkarsh530 commented 10 months ago

No worries, this has been resolved now.

utkarsh530 commented 10 months ago

Fixes #5

Vaibhavdixit02 commented 10 months ago

Lol, okay!