I need to use a metaheuristic algorithm to solve an optimization problem on a Python codebase.

Metaheuristics usually need to be written in C++ or Java as they involve a lot of iterations, while Python is weak from this point of view.


  1. do any Python metaheuristic packages which wrap faster languages as C++/Java exist?
  2. do any Python metaheuristic packages based on maybe cython on numba exist?
  3. other solutions?
  • $\begingroup$ In the vast majority of optimization problems, the real bottleneck is the evaluation of the objective function, not the internals of the algorithm. What are 100 ms spent inside an optimization algorithm iteration compared to real life problems which require minutes (or hours) to evaluate the objective function? Your question makes sense only if you're playing with toy objective functions or you're lucky enough that your problem can be super-quickly solved. There are many implementations of genetic/metaheuristic algorithms in Python. $\endgroup$ – Infinity77 Sep 5 at 20:05

As a long shot, if you have a Python version (or implement it yourself), running it with PyPy instead of Python might make things much faster, as it is well suited to code that uses Python built-ins and also many loops. It optimises these cases very well, through tricks such as garbage collection. The latest version also supports Python3.5 and 3.6 as well as NumPy.

From their website the main benefits:

Speed: thanks to its Just-in-Time compiler, Python programs often run faster on PyPy. (What is a JIT compiler?)

“If you want your code to run faster, you should probably just use PyPy.” — Guido van Rossum (creator of Python)

Memory usage: memory-hungry Python programs (several hundreds of MBs or more) might end up taking less space than they do in CPython.

Compatibility: PyPy is highly compatible with existing python code. It supports cffi and can run popular python libraries like twisted and django.

Stackless: PyPy comes by default with support for stackless mode, providing micro-threads for massive concurrency.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.