{"id":30729361,"url":"https://github.com/nathanrooy/particle-swarm-optimization","last_synced_at":"2025-09-03T15:07:52.422Z","repository":{"id":54830022,"uuid":"67048123","full_name":"nathanrooy/particle-swarm-optimization","owner":"nathanrooy","description":"Learn about particle swarm optimization (PSO) through Python!","archived":false,"fork":false,"pushed_at":"2021-01-26T20:23:20.000Z","size":35,"stargazers_count":278,"open_issues_count":0,"forks_count":121,"subscribers_count":8,"default_branch":"master","last_synced_at":"2025-08-31T00:52:39.538Z","etag":null,"topics":["minimization","optimization","particle-swarm-optimization","pso","python","tutorial"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/nathanrooy.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-08-31T15:06:07.000Z","updated_at":"2025-08-28T06:30:25.000Z","dependencies_parsed_at":"2022-08-14T04:10:22.859Z","dependency_job_id":null,"html_url":"https://github.com/nathanrooy/particle-swarm-optimization","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/nathanrooy/particle-swarm-optimization","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanrooy%2Fparticle-swarm-optimization","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanrooy%2Fparticle-swarm-optimization/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanrooy%2Fparticle-swarm-optimization/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanrooy%2Fparticle-swarm-optimization/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/nathanrooy","download_url":"https://codeload.github.com/nathanrooy/particle-swarm-optimization/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/nathanrooy%2Fparticle-swarm-optimization/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":273460842,"owners_count":25109815,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-09-03T02:00:09.631Z","response_time":76,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["minimization","optimization","particle-swarm-optimization","pso","python","tutorial"],"created_at":"2025-09-03T15:07:48.184Z","updated_at":"2025-09-03T15:07:52.412Z","avatar_url":"https://github.com/nathanrooy.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Particle Swarm Optimization with Python\n[![gh-actions-ci](https://img.shields.io/github/workflow/status/nathanrooy/particle-swarm-optimization/ci?style=flat-square)](https://github.com/nathanrooy/particle-swarm-optimization/actions?query=workflow%3Aci)\n[![GitHub license](https://img.shields.io/github/license/nathanrooy/particle-swarm-optimization?style=flat-square)](https://github.com/nathanrooy/particle-swarm-optimization/blob/master/LICENSE)\n[![codecov](https://img.shields.io/codecov/c/github/nathanrooy/particle-swarm-optimization.svg?style=flat-square)](https://codecov.io/gh/nathanrooy/particle-swarm-optimization)\n\n\u003ca target=\"_blank\" href=\"https://en.wikipedia.org/wiki/Particle_swarm_optimization\"\u003eParticle swarm optimization\u003c/a\u003e (PSO) is amazing and I created a series of tutorials that cover the topic using Python. The first (pso-simple) is comprised of a bare bones implementation and is useful for anyone new to PSO and looking to get a good understanding of how it works. The tutorial can be found here: https://nathanrooy.github.io/posts/2016-08-17/simple-particle-swarm-optimization-with-python/\n\nThe second version (pso-advanced) is still a work in progress...\n\n\n## Installation\nYou can either download/clone this repo and use as is, or you can pip install it with the following command:\n```sh\npip install git+https://github.com/nathanrooy/particle-swarm-optimization\n```\n\n## Usage\n### particle swarm optimization - simple\nOnce you have completed the installation, usage is similar to that of other common optimization frameworks.\n```py\n\u003e\u003e\u003e from pso import pso_simple\n```\nNext, you need to specify a cost fucntion. I included the sphere function for example purposes, but you'll probably end up using your own.\n```py\n\u003e\u003e\u003e from pso.cost_functions import sphere\n```\nNext, let's specify some bounds and an initial starting location:\n```py\n\u003e\u003e\u003e initial=[5,5]               # initial starting location [x1,x2...]\n\u003e\u003e\u003e bounds=[(-10,10),(-10,10)]  # input bounds [(x1_min,x1_max),(x2_min,x2_max)...]\n```\nLastly, lets minimize this thing!\n```py\n\u003e\u003e\u003e pso_simple.minimize(sphere, initial, bounds, num_particles=15, maxiter=30, verbose=True)\n```\nThe output of which should look like this:\n```py\niter:    0, best solution:  -1.000000\niter:    1, best solution:  50.000000\niter:    2, best solution:  44.186379\niter:    3, best solution:  37.884043\niter:    4, best solution:  26.485279\niter:    5, best solution:  15.552986\niter:    6, best solution:   8.098333\niter:    7, best solution:   2.697282\niter:    8, best solution:   0.514359\niter:    9, best solution:   0.111682\niter:   10, best solution:   0.010832\niter:   11, best solution:   0.002607\niter:   12, best solution:   0.002607\niter:   13, best solution:   0.002607\niter:   14, best solution:   0.000507\niter:   15, best solution:   0.000507\niter:   16, best solution:   0.000507\niter:   17, best solution:   0.000507\niter:   18, best solution:   0.000507\niter:   19, best solution:   0.000507\niter:   20, best solution:   0.000507\niter:   21, best solution:   0.000415\niter:   22, best solution:   0.000268\niter:   23, best solution:   0.000194\niter:   24, best solution:   0.000064\niter:   25, best solution:   0.000064\niter:   26, best solution:   0.000018\niter:   27, best solution:   0.000013\niter:   28, best solution:   0.000001\niter:   29, best solution:   0.000001\n\nFINAL SOLUTION:\n   \u003e [0.0010272779593734505, -0.00023254128511081273]\n   \u003e 1.109375455095469e-06\n```\n\n### particle swarm optimization - advanced\n(coming soon...)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathanrooy%2Fparticle-swarm-optimization","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fnathanrooy%2Fparticle-swarm-optimization","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fnathanrooy%2Fparticle-swarm-optimization/lists"}