-
Notifications
You must be signed in to change notification settings - Fork 0
/
exo1.py
89 lines (67 loc) · 2.58 KB
/
exo1.py
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
from random import randint
from random import random
class Equation:
def __init__(self, coeff , sol):
self.c = coeff
self.sol = sol
vect =[]
for i in range(6):
l = []
for _ in range(len(self.c)):
l.append(randint(0,30))
vect.append(l)
self.unk = vect
# repr method
def __repr__(self):
return str(list(zip(self.c, ('a','b','c','d')))) + " = " + str(self.sol)
def getBestResults(self):
x = []
for v in self.unk:
somme = 0
for i in range( len(self.c)):
somme+= self.c[i]* v[i]
x.append((v, abs(somme - 30)))
x.sort(key=lambda tup: tup[1])
return x[0:3]
def bestChromo(self):
return self.getBestResults()[min(range(len(self.getBestResults())), key = lambda i: abs(self.getBestResults()[i][1]-0))]
def croissement(self, bestRes):
newPop = []
for item in bestRes:
newPop.append(item[0])
# flip 0 and 1
newPop.append((bestRes[0][0][0:2] + bestRes[1][0][2:4]))
newPop.append((bestRes[1][0][0:2] + bestRes[0][0][2:4]))
# # flip 0 and 2
newPop.append(( bestRes[0][0][0:2] + bestRes[2][0][2:4]))
# newPop.append(( bestRes[2][0][0:2] + bestRes[0][0][2:4]))
# # flip 1 and 2
# newPop.append(( bestRes[1][0][0:2] + bestRes[2][0][2:4]))
# newPop.append(( bestRes[2][0][0:2] + bestRes[1][0][2:4]))
self.unk = newPop
def mutate(self):
# threshold = (1/population size /chromosome length)
threshold = ( 1 / 6 /4)
prob = random()
# print('prob = ' + str(prob))
if(prob <= threshold):
indexOfChromosome = randint(0,5)
indexOfGene = randint(0,3)
self.unk[indexOfChromosome][indexOfGene] = randint(0,30)
def solve(self):
print("Initial coefficions are equal to : ")
print(self.unk)
compt = 0
while(self.bestChromo()[1] != 0 ):
compt +=1
self.croissement(self.getBestResults())
self.mutate()
self.getBestResults()
# error => distance from 30
# example : error = 3 => equation result = 33
# error = -4 => result = 26
print('solution found in ' + str(compt)+ " generations")
print("solutions is: [Coefficients , equation solution ]")
return self.bestChromo()
eq = Equation((1,2,3,4),30)
print(eq.solve())