
A python package for biomimetic optimization algorithms.
Explore the docs »
View Demo
·
Report Bug
·
Request Feature
Table of Contents
This package features a collection of biomimetic optimization algorithms and heuristics like particle swarm optimization, ant colony optimization, firefly algorithm, cuckoo search, bat algorithm, etc. It is designed to be easy to use and implement in your own projects. It is also designed to be easily extensible, so that you can add your own algorithms and heuristics to the package.
- Clone the repo
git clone https://github.com/adityaprakash-work/OptiHive.git
- pip
pip install git+https://github.com/adityaprakash-work/OptiHive.git
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
from optihive import pso as ohp
from optihive import utils as ohu
from optihive import benchmarks as ohb
from optihive import objectives as oho
# Objectivefunction will be given a dictionary of parameters, with the keys
# being the parameter names and the values being the parameter values. 'f' here
# is a dummy that directly acts on x and y
f = lambda x, y: (x-3.14)**2 + (y-2.72)**2 + np.sin(3*x+1.41) + np.sin(4*y-1.73)
# 'of' however acts on the expected dictionary by unpacking it.
of = lambda kwargs: f(**kwargs)
print(f"of(1, 2) = {of({'x': 1, 'y': 2})}")
of(1, 2) = 4.130187303206262
ss2d_htoof = {
"x": ("con", bucof.bounds_2d[: 2]),
"y": ("con", bucof.bounds_2d[2:]),
}
htoof = ohb.HolderTableObjective()
sot_hto = ohp.SwarmObjectiveTracker(
track_params=["x", "y"],
eager=False,
lazy_step=2,
)
print("True loss Contour")
htoof.plot2d()
for use_grad in [False, True]:
sot_hto = ohp.SwarmObjectiveTracker(
track_params=["x", "y"],
eager=False,
lazy_step=2,
)
swarm_hto = ohp.VanillaSwarm(
search_space=ss2d_htoof,
n_particles=200,
objective_function=htoof,
cc=0.3,
sc=0.3,
r1=0.3,
r2=0.3,
use_gradient=use_grad,
gw=0.1,
n_perturb=2,
r3=0.1,
trackers=[sot_hto],
)
print(">>> Use Gradient: ", use_grad)
print(">> Initial Values")
print(f"swarm_hto | G: {swarm_hto.G}, Gs: {swarm_hto.Gs}")
swarm_hto.run(500)
print(">> Final Values")
print(f"swarm_hto | G: {swarm_hto.G}, Gs: {swarm_hto.Gs}")
print(f"htoof | G, Gs: {htoof.global_minima_2d}")
sot_hto.draw_lazy(
particle_indices=[p for p in range(0, swarm_hto.n_particles, 20)],
)
True loss Contour
>>> Use Gradient: False
>> Initial Values
swarm_hto | G: [[ 7.2977996 -9.677767 ]], Gs: -14.331993283198267
>> Final Values
swarm_hto | G: [[-7.909739 -9.306982]], Gs: -17.80278473486288
htoof | G, Gs: ((8.05502, 9.66459, -19.2085), (-8.05502, 9.66459, -19.2085), (8.05502, -9.66459, -19.2085), (-8.05502, -9.66459, -19.2085))
>>> Use Gradient: True
>> Initial Values
swarm_hto | G: [[ 8.547695 -9.451452]], Gs: -16.3324676311381
>> Final Values
swarm_hto | G: [[-8.055022 -9.66459 ]], Gs: -19.20850256787143
htoof | G, Gs: ((8.05502, 9.66459, -19.2085), (-8.05502, 9.66459, -19.2085), (8.05502, -9.66459, -19.2085), (-8.05502, -9.66459, -19.2085))
For more examples, please refer to the Documentation
See the open issues for a full list of proposed features (and known issues).
Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement". Don't forget to give the project a star! Thanks again!
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push main feature/AmazingFeature
) - Open a Pull Request
Distributed under the MIT License. See LICENSE
for more information.
Aditya Prakash
Twitter - @adityaprakash_t
Email - [email protected]
Project Link: https://github.com/adityaprakash-work/OptiHive