Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to minimize a function using Deap?

I need to minimize a function using genetic algorithm and PSO.

Different posts suggest to use DEAP (I am using python) but I do not even understand how to start.

We can consider for example f on the interval i

i=arange(-10,10,0.1)
def f(x):
    return x*sin(x)

How can I minimize this function using DEAP?

like image 786
Donbeo Avatar asked Mar 14 '14 11:03

Donbeo


2 Answers

I realise I'm late in my reply but hopefully I can help someone out there new to the DEAP library.

In order to minimise you must create a FitnessMin class as shown below:

creator.create("FitnessMin", base.Fitness, weights=(-1.0,))

Note how I have a weight of -1.0, if we were to have this as 1.0 we would be maximising.

Then once you do this, apply the fitness class to your Individual, which is essentially the Chromosome you wish to train:

creator.create("Individual", list, fitness=creator.FitnessMin)

Take a look at eaSimple in the DEAP library as it is the best one to start with. Here is a list of algorithms provided by DEAP out of the box: http://deap.readthedocs.io/en/master/api/algo.html

like image 133
Jason McCullough Avatar answered Oct 25 '22 10:10

Jason McCullough


Actually there is an example for that : http://deap.rtfd.org/en/master/examples/pso_basic.html

Note that I'm a DEAP developer.

like image 28
mitch Avatar answered Oct 25 '22 11:10

mitch