Nature-inspired optimization is a very popular subject in engineering, terms heuristic algorithms or metaheuristic algorithms are also common to refer to nature-inspired algorithms. The most famous algorithms are probably Genetic Algorithm, Particle Swarm Optimization, Ant Colony Optimization, and Artificial Bee Colony.
What is NIA?
As a part of a project on Bio-Inspired Computational Methods, we produced a python package called NIA, which stands for Nature-Inspired Algorithms. NIA is tried to be a very simple and standard package, we have planned to include about 30 different algorithms in this package. It was our goal to make NIA very simple to use, to start you can install NIA by simply running pip install nia
that’s it! the example below shows you that how simple is solving the famous benchmark function, Ackley:
from nia.algorithms import GeneticAlgorithm
def ackley(X):
x = X[0]
y = X[1]
return -20 * np.exp(-0.2 * np.sqrt(0.5 * (x**2 + y**2))) - np.exp(0.5 *
(np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) + np.e + 20
nia = GeneticAlgorithm(cost_function=ackley,
lower_bond=[-5,-5],
upper_bond=[5,5],
)
nia.run()
print(nia.message);
The above example is the easiest way to use NIA, obviously, it uses the least parameters, if you want to customize it, there is a full example with all available options:
from nia.algorithms import GeneticAlgorithm
from nia.selections import Tournament
from nia.crossovers import RandomSBX
from nia.mutations import Uniform
import numpy as np
def ackley(X):
x = X[0]
y = X[1]
return -20 * np.exp(-0.2 * np.sqrt(0.5 * (x**2 + y**2))) - np.exp(0.5 *
(np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) + np.e + 20
def log(ga):
print(ga.best)
lower = np.array([-5,-5])
upper = np.array([5,5])
nia = GeneticAlgorithm(cost_function=ackley,
iteration_function=log,
lower_bond=lower,
upper_bond=upper,
quit_criteria = 0.0001,
num_variable = 2,
num_population = 20,
max_iteration = 100,
crossover = RandomSBX(2),
mutation = Uniform(0.05),
selection = Tournament(20)
)
nia.run()
print(nia.message);
According to the above example, you can see that NIA supports different mutation, crossover, and selection functions. Which can be easily imported from separate classes. Instead of the cost function, you can pass an iteration function, which would call in each iteration with the main algorithm object. So you have full control over the algorithm, for example, you may change the population, crossover, bonds, and even cost function. In the above example, we just log the best answer in each iteration. You may want to create a plot using matplotlib to show the progress in each iteration:

List of Algorithms
Here you can find the list of selected algorithms that would be included in NIA:
- Genetic algorithm (GeneticAlgorithm)
- Differential Evolution
- Evolutionary Programming
- Artificial Immune System
- Clonal Selection Algorithm
- Biogeography-based
- Symbiotic Organisms Search
- Ant Colony Optimization
- Artificial Bee Colony (ArtificialBeeColony)
- Moth Flame Optimization Algorithm
- Cuckoo Search
- Green Herons Optimization Algorithm
- Bat Algorithm
- Whale Optimization Algorithm
- Krill Herd
- Fish-swarm Algorithm
- Grey Wolf Optimizer
- Shuffle frog-leaping Algorithm
- Cat Swarm Optimization
- Flower Pollination Algorithm
- Invasive Weed Optimization
- Water Cycle Algorithm
- Teaching–Learning-Based Optimization
- Particle Swarm Optimization (ParticleSwarmOptimization)
- Simulated Annealing Algorithm
- Gravitational Search Algorithm
- Big Bang – Big Crunch
Open-Source
for more information, source codes, and versions you can take a look at the git repository of the NIA and the PyPI page. We welcome any contributions, bug reports, and suggestions.