**Motivation**

Optimization problem studies maximizing or minimizing some function

**y**=f(**x**) with some range of choices available for **x**. Biologically inspired (bio-inspired) algorithms for optimization problems are now widely used. A few examples of such optimization are:

particle swarm optimization (PSO) that is based on the swarming behavior of fish and birds,

firefly algorithm (FA) that is based on the flashing behavior of swarming fireflies,

ant colony optimization (ACO) that is based on the interaction of social insects (e.g., ants)

bee algorithms are all based on the foraging behavior of honey bees. [1]

Recently on different forums there were questions how this optimization technique can be applied to text mining tasks such as classification or clustering. Those problems usually use algorithms that come from data mining or machine learning fields such as k-means clustering, SVM (support vector machine), naive Bayes.

How can we apply bio-inspired algorithms for clustering?

Let’s take a python package inspyred which has many bio-inspired algorithms including evolutionary computation, swarm intelligence, and immunocomputing [2]. This package has examples for specific predetermined functions that are used as benchmarks.

However the code also can be customized to run for user defined functions.

Below is the example of customized code for finding minimum of function f(x)=(x-1)**2, in this code function evaluate was added to specify how we calculate fitness. In this example we use PSO algorithm.

We run this code and we get what we expected to get. Below you can find also the final output of the program.

So now we understand what we need to change to fit optimization to different problems. Specifically we need to modify fitness function. Additionally we need to modify range and observer.

In the next post Bio-Inspired Optimization for Text Mining-2 Numerical One Dimensional Example we will continue to modify source code for solving clustering problem.

```
# f(x)=(x-1)**2 minimize on 0...2
## http://pythonhosted.org/inspyred/examples.html#ant-colony-optimization
## https://aarongarrett.github.io/inspyred/reference.html#benchmarks-benchmark-optimization-functions
from time import time
from random import Random
import inspyred
def my_observer(population, num_generations, num_evaluations, args):
best = max(population)
print('{0:6} -- {1} : {2}'.format(num_generations,
best.fitness,
str(best.candidate)))
def generate(random, args):
return [random.uniform(0, 2)]
def evaluate(candidates, args):
fitness = []
for cand in candidates:
fit = [(c-1)**2 for c in cand]
fitness.append(fit)
return fitness
def main(prng=None, display=False):
if prng is None:
prng = Random()
prng.seed(time())
ea = inspyred.swarm.PSO(prng)
ea.observer = my_observer
ea.terminator = inspyred.ec.terminators.evaluation_termination
ea.topology = inspyred.swarm.topologies.ring_topology
final_pop = ea.evolve(generator=generate,
evaluator=evaluate,
pop_size=100,
bounder=inspyred.ec.Bounder(0, 2),
maximize=False,
max_evaluations=30000,
neighborhood_size=2)
if display:
best = max(final_pop)
print('Best Solution: \n{0}'.format(str(best)))
return ea
if __name__ == '__main__':
main(display=True)
```

The output of program:

```
Best Solution:
[1.0] : [0.0]
```

**References**

1. A Brief Review of Nature-Inspired Algorithms for Optimization

2. inspyred: Bio-inspired Algorithms in Python

3. Bio-Inspired Optimization for Text Mining-2 Numerical One Dimensional Example