-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEssaim.py
More file actions
71 lines (53 loc) · 1.58 KB
/
Copy pathEssaim.py
File metadata and controls
71 lines (53 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import random
import matplotlib.pyplot as plt
#fonction a maximiser
def f(x):
return -x**2
class Boid:
def __init__(self,xmin, xmax):
self.x=random.uniform(xmin, xmax)
self.v=random.gauss(0,1)
self.p=self.x
def maj(self, w, phip, phig,g, rp, rg):
self.v=self.v*w + phip*rp*(self.p-self.x) + phig*rg*(g-self.x)
self.x+=self.v
self.calcul_p()
def calcul_p(self):
if (f(self.x)>f(self.p)):
self.p=self.x
class Essaim:
def __init__(self, N, xmin, xmax, w, phip, phig):
self.boids=[]
self.w=w
self.phip=phip
self.phig=phig
for i in xrange(0,N):
self.boids.append(Boid(xmin, xmax))
self.g = self.calcul_g()
def calcul_g(self):
liste_p = [ self.boids[i].p for i in xrange(0, len(self.boids)) ]
liste_ep = map(f,liste_p)
indice_max = liste_ep.index(max(liste_ep))
return liste_p[indice_max]
def maximisation(self, t):
liste_g = []
liste_eg = []
for i in xrange(0, t):
liste_g.append(self.g)
liste_eg.append(f(self.g))
print "g : ",self.g, " eg : ", f(self.g)
rp=random.uniform(0,1)
rg=random.uniform(0,1)
for boid in self.boids:
boid.maj(self.w, self.phip, self.phig, self.g, rp, rg)
self.g = self.calcul_g()
plt.plot(range(0,t), liste_eg, color='b', label = 'qualite')
plt.plot(range(0,t), liste_g, color='r', label = 'position')
axes = plt.gca()
axes.set_xlabel('iterations')
axes.set_ylabel('qualite ou position')
plt.legend(loc='upper right', prop={'size':10})
plt.show()
essaim = Essaim(10,-100,100,0.1,0.3,0.5)
essaim.maximisation(20)
#on retrouve bien le max de -x**2 qui vaut 0 en 0