I need to minimize a function using genetic algorithm and PSO.
Different posts suggest to use DEAP 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?
Dear Luca,
The first thing to do would be to study the theory behind evolutionary computation. There exist an excellent introductory book by Melanie Mitchell, An introduction to genetic algorithm. Then, you can browse the tutorials of DEAP that will help you understand the concepts behind the library. Finally you can have a look at the examples that will show you his to use DEAP in multiple optimisation scenarios.
Have fun with DEAP,
François-Michel
--
You received this message because you are subscribed to the Google Groups "deap-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
To unsubscribe from this group and stop receiving emails from it, send an email to deap-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
#import some libraries not all are needed
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from numpy import *
from numpy.linalg import *
import sklearn as sk
import scipy as sp
import scipy.io
from matplotlib.pyplot import *
from scipy.interpolate import *
from scipy.optimize import minimize
from scipy.misc import derivative
from sklearn.preprocessing import *
from scipy.optimize import *
t1=linspace(-50,50,100)
#define two set of points sig1 and sig2
sig1=sin(t1/2)+np.random.normal(scale=0.1,size=len(t1))
sig2=sin(t1/2)+np.random.normal(scale=0.1,size=len(t1))
# built 2 function f0 and g
f0=interp1d(t1,sig1,kind="cubic",bounds_error=False,fill_value=-10000)
g=interp1d(t1,sig2,kind="cubic",bounds_error=False,fill_value=10000)
#Define some value
A=2
B=0.8
#this is the function that I would like to estimatedef s(t):
return A+B*t+
def inv_s(t):
return (t-B)/A
# this is a reparametrization of f0 so my signals now not are aligned
def f(t):
return f0(s(t))
#define a new interval where I will study my problem
Ig=arange(t1.min(),t1.max(),1)
If=(Ig-A)/B
I_min=max(If.min(),Ig.min())
I_max=min(If.max(),Ig.max())
J=linspace(min(If.min(),Ig.min()),max(If.max(),Ig.max()))
I=linspace(I_min,I_max,1000)
# here you can visualize the signal
#plot(J,f(J),J,g(J))
#ylim(-1.2,1.2)
#This is the cost function that I would like to minimize the solution is x=[A,B]
def cost(x,T=I):
a=x[0]
b=x[1]
return norm(g(b*T+a)-f(T))/len(T)
# only one line is changed
import operator
import random
import numpy
from deap import base
from deap import benchmarks
from deap import creator
from deap import tools
creator.create("FitnessMax", base.Fitness, weights=(-1.0,))<-----------------------weights are negative because I want to minize
creator.create("Particle", list, fitness=creator.FitnessMax, speed=list,
smin=None, smax=None, best=None)
def generate(size, pmin, pmax, smin, smax):
part = creator.Particle(random.uniform(pmin, pmax) for _ in range(size))
part.speed = [random.uniform(smin, smax) for _ in range(size)]
part.smin = smin
part.smax = smax
return part
def updateParticle(part, best, phi1, phi2):
u1 = (random.uniform(0, phi1) for _ in range(len(part)))
u2 = (random.uniform(0, phi2) for _ in range(len(part)))
v_u1 = map(operator.mul, u1, map(operator.sub, part.best, part))
v_u2 = map(operator.mul, u2, map(operator.sub, best, part))
part.speed = list(map(operator.add, part.speed, map(operator.add, v_u1, v_u2)))
for i, speed in enumerate(part.speed):
if speed < part.smin:
part.speed[i] = part.smin
elif speed > part.smax:
part.speed[i] = part.smax
part[:] = list(map(operator.add, part, part.speed))
toolbox = base.Toolbox()
toolbox.register("particle", generate, size=2, pmin=-6, pmax=6, smin=-3, smax=3)
toolbox.register("population", tools.initRepeat, list, toolbox.particle)
toolbox.register("update", updateParticle, phi1=2.0, phi2=2.0)
toolbox.register("evaluate", cost) #<----------------------------------------------I replaced the default function with cost
def main():
pop = toolbox.population(n=5)
stats = tools.Statistics(lambda ind: ind.fitness.values)
stats.register("avg", numpy.mean)
stats.register("std", numpy.std)
stats.register("min", numpy.min)
stats.register("max", numpy.max)
logbook = tools.Logbook()
logbook.header = ["gen", "evals"] + stats.fields
GEN = 1000
best = None
for g in range(GEN):
for part in pop:
part.fitness.values = toolbox.evaluate(part)#<----------- this line return an error I should probably define the cost function in another way. HOW?
if not part.best or part.best.fitness < part.fitness:
part.best = creator.Particle(part)
part.best.fitness.values = part.fitness.values
if not best or best.fitness < part.fitness:
best = creator.Particle(part)
best.fitness.values = part.fitness.values
for part in pop:
toolbox.update(part, best)
# Gather all the fitnesses in one list and print the stats
logbook.record(gen=g, evals=len(pop), **stats.compile(pop))
print(logbook.stream)
return pop, logbook, best
if __name__ == "__main__":
main()