-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpls.py
82 lines (69 loc) · 2.75 KB
/
pls.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
from tools import *
import time
#fonction voisinage
def voisinage( current, infos):
objet_dehor = set(range(infos["n"])) - set(current) # The other pickable objects
W = sum([infos["weight"][i] for i in current]) # The total weight of the current solution
voisins = []
for obj_index in current:
objet_liber = set(filter(lambda i: infos["weight"][i] <= infos["W"] - W + infos["weight"][obj_index], objet_dehor))
for obj in objet_liber:
copy_objet_liber = objet_liber.copy()
copy_objet_liber.remove(obj)
chosen_index_others = [obj]
new_W = W - infos["weight"][obj_index] + infos["weight"][obj]
copy_objet_liber = set(filter(lambda i: infos["weight"][i] <= infos['W'] - new_W, copy_objet_liber))
voisins.append(([obj_index], chosen_index_others))
while new_W <= infos['W'] and len(copy_objet_liber) > 0:
new_index_other = random.choice(list(copy_objet_liber))
copy_objet_liber.remove(new_index_other)
chosen_index_others.append(new_index_other)
new_W += infos["weight"][new_index_other]
copy_objet_liber = set(filter(lambda i: infos["weight"][i] <= infos['W'] - new_W, copy_objet_liber))
# print('Trouver voisins:',len(voisins))
# print('*',end='')
return voisins
def get_voisins( current_index, voisin):
ens_solu = current_index[:] + voisin[1]
for v in voisin[0]:
ens_solu.remove(v)
return ens_solu
#fonction mise a jour
def MSJ(infos, ens, x):
if sum(infos["weight"][i] for i in x) > infos["W"]:
return False
score_x = get_y(infos, x)
liste_remove = []
for y in ens:
score_y = get_y(infos, y)
if dominer(score_x,score_y):
liste_remove.append(y)
elif dominer(score_y, score_x):
return False
for y in liste_remove:
ens.remove(y)
ens.append(x)
return True
def PLS(file,nb_objectif,nb_objet):
infos = read_file(file.format(0),nb_objectif = nb_objectif,nb_objet=nb_objet )
Xe = init_p(infos)
ens_p = Xe[:]
ens_aux = []
ens_deja = [ens_p]
while len(ens_p) != 0:
for p in ens_p:
current_y = get_y(infos, p)
voisins = voisinage(p, infos)
for voisin in voisins:
new_p = get_voisins(p, voisin)
new_y = get_y(infos, new_p)
if not dominer(current_y,new_y):
if MSJ(infos, Xe, new_p):
MSJ( infos, ens_aux, new_p)
p_solu = set(p)
if p_solu not in ens_deja:
ens_deja.append(p_solu)
ens_p = [p for p in ens_aux if set(p) not in ens_deja]
ens_aux = []
# print('')
return Xe