Skip to content

A python package for biomimetic optimization algorithms.

License

Notifications You must be signed in to change notification settings

adityaprakash-work/OptiHive

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Contributors Forks Stargazers Issues MIT License LinkedIn


Logo

OptiHive

A python package for biomimetic optimization algorithms.
Explore the docs »

View Demo · Report Bug · Request Feature

Table of Contents
  1. About The Project
  2. Getting Started
  3. Usage
  4. Roadmap
  5. Contributing
  6. License
  7. Contact
  8. Acknowledgments

About The Project

OptiHive Screen Shot

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.

(back to top)

Built With

  • Matplotlib
  • NumPy
  • scikit-learn

(back to top)

Getting Started

Installation

  1. Clone the repo
    git clone https://github.com/adityaprakash-work/OptiHive.git
  2. pip
    pip install git+https://github.com/adityaprakash-work/OptiHive.git

(back to top)

Usage

Common Imports

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

Objective Functions

# 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

Using VanillaSwarm with a BenchmarkObjective

Defining the search space

ss2d_htoof = {
    "x": ("con", bucof.bounds_2d[: 2]),
    "y": ("con", bucof.bounds_2d[2:]),	
}

Initializing BenchmarkObjective, VanillaSwarm and SwarmObjectiveTracker

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

hot_obj

>>> 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))

hot_obj_ugf

>>> 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))

hot_obj_ugt

For more examples, please refer to the Documentation

(back to top)

Roadmap

See the open issues for a full list of proposed features (and known issues).

(back to top)

Contributing

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!

  1. Fork the Project
  2. Create your Feature Branch (git checkout -b feature/AmazingFeature)
  3. Commit your Changes (git commit -m 'Add some AmazingFeature')
  4. Push to the Branch (git push main feature/AmazingFeature)
  5. Open a Pull Request

(back to top)

License

Distributed under the MIT License. See LICENSE for more information.

(back to top)

Contact

Aditya Prakash
Twitter - @adityaprakash_t
Email - [email protected]

Project Link: https://github.com/adityaprakash-work/OptiHive

(back to top)

Acknowledgments

(back to top)

About

A python package for biomimetic optimization algorithms.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published