Giter Site home page Giter Site logo

haaleo / swarmlib Goto Github PK

View Code? Open in Web Editor NEW
473.0 10.0 107.0 197.01 MB

This repository implements several swarm optimization algorithms and visualizes them. Implemented algorithms: Particle Swarm Optimization (PSO), Firefly Algorithm (FA), Cuckoo Search (CS), Ant Colony Optimization (ACO), Artificial Bee Colony (ABC), Grey Wolf Optimizer (GWO) and Whale Optimization Algorithm (WOA)

License: BSD 3-Clause "New" or "Revised" License

Python 98.91% Makefile 1.09%
ant-colony-optimization traveling-salesman-problem tsplib95 tsp aco pypi tsplib optimization-algorithms swarm firefly-algorithm

swarmlib's Introduction

Hi there 👋🏼

LinkedIn Mail Stackoverflow Gitter

I'm Leo, I got a computer science degree in Robotics, Cognition, Intelligence at the Technical University of Munich. The topic of my master's thesis is whether and to what extent it is possible to identify vague requirements with machine learning. Here you find the implementation and the thesis is located here.

I am here to contribute to awesome projects and to stay in touch with the open source community.

Favorite Languages and Tools

GitHub Statistics

Leo's github stats

swarmlib's People

Contributors

alxfmpl avatar haaleo avatar nimishverma avatar nkoutsov avatar rhodriguerrier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

swarmlib's Issues

Visualize GWO alpha, beta, delta wolves

Description

To better understand the Grey Wolf Optimizer algorithm it could help to visualize the different wolf types.
To enhance understanding of its convergence visualizing the current best wolf like the cuckoo search does could be benefitial, too.

Add Unit Tests

Description

As swarmlib is growing it is essential to ensure the key functionality works flawless. Therefore at least the base components (of the utility module should be tested.

Proposal

  • Add some first unit tests using pytest. Testing matplotlib related things is documented here.
  • Testing animations is not supported out of the box (ref matplotlib/pytest-mpl#74). Therefore, for the scope of this issue it should be sufficient to initialize the BaseVisualizer and then check with ptest-mpl whether the background is drawn correctly

Strange Error which might be caused by matplotlib

Python Version swarmlib Version OS
3.8.12 0.14.1 win 11

Description

I used the demo code from the cuckoo problem while resulting in a failure of
"TypeError: can't multiply sequence by non-int of type 'complex'"
I have attached the pdf of the notebook file to the report, please help.
Test.pdf

Expected Behavior

Actual Behavior

Steps to Reproduce

Context

Add bird swarm algorithm

Description

Add the Bird Swarm Algorithm . The corresponding paper can be found here.
If you cannot retrieve the paper you can reach out to me on gitter.
Be sure to leave a comment before you you start coding.

To get started checkout the CONTRIBUTING.md.

Move CI/CD Pipeline to GitHub Actions

Description

Since I already have experience on setting up GitHub actions for all three major OS (macOS, windows, linux) and the project's source is on GitHub already it makes sense to also move the CI/CD pipeline to GitHub.
Further GitHub actions allows to easily trigger a publish when a release was created in the UI.

Decouple the algorithms from the CLI and visualization

Description

Recently, I received quite some requests to decouple the algorithms from the CLI and the visualization. Those users want to use the bare algorithms for other tasks not only educational purposes.

To support their use cases I want to split of the algorithm implementations to their own pypi package (maybe called swarmlib-api?). This package then would aim to provide an easy to use API to the algorithms. We should figure out how the API definition should look like to integrate nicely with other packages like for example with sklearn.

Steps

  1. Figure out nice and good to use API definitions:
    1. Is the current ProblemBase and ProblemBase.solve() approach good, meaning it integrates with other packages easily?
  2. Come up with a nice name for the API package ( maybe also the algorithm package should become swarmlib and the CLI package swarmlibCLI)
  3. Implement it in its own repo and publish it on pypi.
  4. swarmlib should then use the new package.

This issue is more an archive for suggestions and input. Feedback is welcome. Followup implementation tasks will be defined in their own issues.

Set the log level at runtime

Description

Currently the log level is hardcoded to be INFO. Allowing it to be set by the users during runtime could be beneficial for future bug reports.

Proposal

  • Either add a top level flag --log-level which can be set to one of python's log level
  • Or add a (boolean) flag --verbose similar how sklearn uses it. (reference)

IndexError: index 0 is out of bounds for axis 1 with size 0

I receive this same error trying launching master with:
swarm fireflies 14 --function michalewicz --alpha 0.5 --beta 0.2 --gamma 1.0
swarm whales 14 -f michalewicz -n 10
swarm wolves 10 -f=michalewicz

(...)
2023-04-16 17:58:14,875 [INFO ] Current best value: -0.9966775719782092, Overall best value: -1.7399912676909937
2023-04-16 17:58:14,877 [INFO ] Current best value: -1.0013626000092621, Overall best value: -1.7399912676909937
Traceback (most recent call last):
File "", line 198, in run_module_as_main
File "", line 88, in run_code
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\Scripts\swarm.exe_main
.py", line 7, in
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\swarmlib_main
.py", line 86, in run_swarm
algorithm(args)
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\swarmlib\gwo\main.py", line 19, in _run_gwo
problem.replay()
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\swarmlib\util\problem_base.py", line 25, in replay
self._visualizer.replay()
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\swarmlib\util\base_visualizer.py", line 85, in replay
_ = animation.FuncAnimation(self._fig, self._animate, frames=frames, interval=self.__frame_interval,
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\matplotlib\animation.py", line 1694, in init
super().init(fig, **kwargs)
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\matplotlib\animation.py", line 1416, in init
super().init(fig, event_source=event_source, *args, **kwargs)
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\matplotlib\animation.py", line 880, in init
self._setup_blit()
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\matplotlib\animation.py", line 1214, in _setup_blit
self._init_draw()
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\matplotlib\animation.py", line 1750, in _init_draw
self._drawn_artists = self._init_func()
^^^^^^^^^^^^^^^^^
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\swarmlib\util\base_visualizer.py", line 94, in _init
self.__particles.set_offsets([[]])
File "C:\Users\Alberto\AppData\Roaming\Python\Python311\site-packages\matplotlib\collections.py", line 551, in set_offsets
(np.asanyarray(self.convert_xunits(offsets[:, 0]), float),
~~~~~~~^^^^^^
IndexError: index 0 is out of bounds for axis 1 with size 0

(...)
AttributeError: 'FuncAnimation' object has no attribute '_resize_id'

Support 3D Visualization

Description

To better visualize the nature of the benchmark functions swarmlib should support 3D visualization additionally.

Proposal

  • Probably the algorithms must pass the whole Coordinate (including its value) to the Visualizer. No since the Visualizer knows the positions and the objective function it can calculate the values itself.
  • The main work of this is probably the extension of the Visualizer.
  • This could be enabled by an --3D flag.

What if the input of the object function is not 2-dimensional?

Is your feature request related to a problem? Please describe.
A clear and concise description of what the problem is. Ex. I'm always frustrated when [...]

Describe the solution you'd like
A clear and concise description of what you want to happen.

Describe alternatives you've considered
A clear and concise description of any alternative solutions or features you've considered.

Additional context
Add any other context or screenshots about the feature request here.

Cannot import matplotlib

When I started to install the swarmlib I found it worked. But later I used 'swarm --version' to check installing. It happened a error said "ImportError: DLL load failed: 找不到指定的模块。";
pls help me I preciated much. Thanks.

Read swarmlib's dependencies only from the requirements.txt file

Description

Currently swarmlib's dependencies are managed in two files:

  • requirements.txt for local development
  • setup.py when bundling and shipping the package

Because of that one must edit both of those files if one changes a dependency. This can be error prone.

Proposal

Instead of hard coding the dependencies in the setup.py as well they can be read from the requirements.txt file. The file can be read line by line and then passed to the setup.py's install_requires option.

If you consider to pick this issue, please take a look at the CONTRIBUTING.md file first.

Index Error

| Python Version | swarmlib Version | OS |
| 3.8.13 | 0.14.1 | Windows 10 |

Description

Try to run the Firefly Algorithm and the test code

problem = FireflyProblem(function=FUNCTIONS['michalewicz'], firefly_number=20)
best_firefly = problem.solve()
problem.replay()

problem.replay() doesn't work:

IndexError Traceback (most recent call last)
--> problem.replay()

---> 25 self._visualizer.replay()

File ...\Miniconda3\envs\optimization\lib\site-packages\swarmlib\util\base_visualizer.py:85, in BaseVisualizer.replay(self, **kwargs)
82 frames = int(self.__intervals*self.__interval_ms/self.__frame_interval)
84 # iteration_number+1 for initialization frame
---> 85 _ = animation.FuncAnimation(self._fig, self._animate, frames=frames, interval=self.__frame_interval,
86 blit=True, init_func=self._init, repeat=self.__continuous, fargs=[frames])
88 plt.show()

File. ..\Miniconda3\envs\optimization\lib\site-packages\matplotlib\animation.py:1634, in FuncAnimation.init(self, fig, func, frames, init_func, fargs, save_count, cache_frame_data, **kwargs)
1631 # Needs to be initialized so the draw functions work without checking
1632 self._save_seq = []
-> 1634 super().init(fig, **kwargs)
...
--> 564 (np.asarray(self.convert_xunits(offsets[:, 0]), 'float'),
565 np.asarray(self.convert_yunits(offsets[:, 1]), 'float')))
566 self.stale = True

IndexError: index 0 is out of bounds for axis 1 with size 0

Support more benchmark functions

Description

The currently used benchmark functions namely ackley and michalewicz are hard coded in the library.
Consuming the functions instead via an external library (pypi package) could enable access to way more functions.

Add Sailfish Optimizer

Description

Add the Sailfish Optimizer . The corresponding paper can be found here.
If you cannot retrieve the paper you can reach out to me on gitter.
Be sure to leave a comment before you you start coding.

To get started checkout the CONTRIBUTING.md.

Adding Grey Wolf Optimization Algorithm

Hi,
I checked out this project and it looks impressive! I have worked on EAs and SIs for some time and would like to contribute to this project by adding Grey Wolf Optimization Algorithm (S. Mirjalili et al 2014) .

Show help message when calling CLI without args.

Python Version swarmlib Version OS
3.8 v0.14.0 ubuntu 20.04

Description

When just calling swarm without any args an error is shown.

Instead of the error message the CLI's help message should be shown.

Expected Behavior

Show the help message when executing swarm without args.

Actual Behavior

An error message is shown when executing swarm without args.

Steps to Reproduce

  1. Install swarmlib
  2. Execute the command swarm without any args.

Context

The error message shown:

$ swarm
Traceback (most recent call last):
  File "/home/hanisch/.local/bin/swarm", line 8, in <module>
    sys.exit(run_swarm())
  File "/home/hanisch/.local/lib/python3.8/site-packages/swarmlib/__main__.py", line 85, in run_swarm
    algorithm = args.pop('func')
KeyError: 'func'

Proposal

In the __main__.py check whether func in args before calling args.pop('func'). Call parser.print_usage() as necessary.

Strange Error which might be caused by matplotlib

Python Version swarmlib Version OS
3.8.12 0.14.1 win 11

Description

I used the demo code from the cuckoo problem while resulting in a failure of
"TypeError: can't multiply sequence by non-int of type 'complex'"
I have attached the pdf of the notebook file to the report, please help.
Test.pdf

Expected Behavior

Actual Behavior

Steps to Reproduce

Context

Use color gradient for intermediate best solutions

Description

Currently in the CS and ABC algorithms the same color (yellow) is used to visualize the current best solution.

Proposal

To indicate the convergence of the algorithms the colors for the current best solution shall be retrieved from a color gradient. For example very light yellow to very dark yellow. With this change the user can easily understand how the algorithm converges.

Resources

Rename cuckoo search's --max-generations option to --iteration-number

Description

The cuckoo search is the only algorithm whose amount of iterations is indicated by the --max-generations flag. All other algorithms use a flag --iteration-number to indicate that. Originally this naming was chosen to stick as close as possible to the paper's wording.

Pros

However, this causes that every algorithm must specify in its main.py its own option --iteration-number. Renaming the cuckoo search's option to --iteration-number enables us to move this option from an algorithm level to the top level of the CLI (swarm -i 15 wolves 12 instead of swarm wolves -i 15 12). In my opinion this is somewhat cleaner from a developer's point of view.

Cons

On the other hand, renaming the cuckoo search's option removes some of the algorithm's explanations and could make it less accessible to users. This is in contrast to swarmlib's overall goal: Making it easier to get into swarm optimization.

Summary

With this issue we want to discuss the trade-off of cleaner code vs. user accessibility.

Input and opinions from the community are heavily appreciated.

Unable to install the library

Hi,

I am trying to install the library using the pip and getting following error on Ubuntu 16

$pip install swarmlib

ERROR: Could not find a version that satisfies the requirement swarmlib (from versions: none)
ERROR: No matching distribution found for swarmlib

can you help?Thanks

Is it possible to use Particle Swarm Optimization with custom functions?

Hello,

This is not a bug report, but a question about usage. Since i haven't found any documentation on the wiki about this topic, i would like to know if it is possible to use PSOProblem with a custom function, instead of using the FUNCTIONS module depicted in this example.

Going a little further inside the code i found that all preset functions are wrapped into a specific type.

Thanks in advance.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.