def ameliorer_Sol(self, n, c, liste_obj): # appliquer BFD pour generer la solution initiale Zs, liste_obj = best_fit_dec(liste_obj, n, c) S = [] for i in range(Zs): S.append(Model.Bin(i, c)) for o in liste_obj: S[o[1]].ranger_obj(Model.Objet(liste_obj.index(o), o[0])) # ordonner les boites selon Ws croissant S.sort(key=lambda x: x.total_weight, reverse=True) # attribuer id des boites selon leur position for i in range(Zs): S[i].set_id(i) # initialisaiton des variables de controle self.iter = 0 for i in range(Zs): self.chd.append(0) self.dm.append([]) for j in range(Zs): self.dm[i].append(-1) improvement = True while improvement: # tant qu'on a trouvé une meilleure solution, on boucle encore improvement = False for j in range(len(S)): # pour chaque boite j if not improvement: # tant qu'on a pas trouver une sol realisable self.iter = self.iter + 1 # obtenir une nouvelle solution S' (etapes 1-2-3) # (1) eliminer la boite j de S # (2) redistribuer ses articles sur les autres boites Sprim = self.redistribute(S, j) # (3) mettre a jour la date de modif des boites concernées for i in range(len(S)): for j in range( len(Sprim) ): # """" les deux boucles pour parcourir les boites dans s et sprim et voir celle qui a changé""" if S[i].id == Sprim[j].id and ( S[i].total_weight != Sprim[j].total_weight): self.chd[S[i].id] = self.iter if self.realisable(Sprim): # ie : apres redistribution on a obtenue une meilleure solution S = deepcopy(Sprim) Zs = len(S) improvement = True else: Snv = self.recherche_locale(Sprim) if self.realisable(Snv): print("RL realisable") S = deepcopy(Snv) Zs = len(S) improvement = True return S
def callWOA(N, C, liste, nb_whales=50, max_iter=50, b=1.5, a=2): liste2 = [] for i in range(len(liste)): liste2.append(Model.Objet(i, liste[i])) woa = WOA(liste2, C) sol, nb = woa.optimize(nb_whales=50, max_iter=50, b=1.5, a=2) Sol = [Model.Bin(0, C)] for i in range(len(sol)): if Sol[-1].capacite_restante() >= liste2[sol[i]].weight: Sol[-1].ranger_obj(liste2[sol[i]]) else: Sol.append(Model.Bin(len(Sol), C)) Sol[-1].ranger_obj(liste2[sol[i]]) return nb, Sol
def RS(self, n, c, list, Tinit=20, T0=0.1, R=200, alpha=0.9): self.n = n self.c = c self.list = list """Solution initiale générée aleatoirement """ """First fit applied to the random list""" random.shuffle(list) liste_obj = first_fit(list, c) Zs = len(liste_obj) S = [] for i in range(Zs): S.append(Model.Bin(i, c)) for j in range(len(liste_obj[i])): S[i].ranger_obj(Model.Objet(i + j, liste_obj[i][j])) """ initialisaiton des parametres""" Best = deepcopy(S) T = Tinit # T0 = self.Temperature_min() Tmoy = ( Tinit + T0 ) / 2 # la barriere entre le regime haute temperature et basse temperature """tant que le seuil de temperature n'est pas atteint, recherche locale avec la temperature T""" while T > T0: cpt = 0 """repeter le processus de recherche locale R fois """ for i in range(R): if cpt == 30: break """generer aléatoirement une solution voisine de S""" if T >= Tmoy: """Mode High temperature => Type1""" Sprim = self.generer_voisin1(S) else: """Mode Low temperature => Type2""" Sprim = self.generer_voisin2(S) # print("type2") if self.F(Sprim) > self.F(S): """accepter Sprim """ S = deepcopy(Sprim) cpt = 0 if len(Sprim) < len(Best): Best = deepcopy(Sprim) # print("amelio 2 ") else: cpt = cpt + 1 """accepter Sprim avec un proba """ u = np.random.uniform(0, 1) proba = math.exp((len(S) - len(Sprim)) / T) if u > proba: S = deepcopy(Sprim) if len(Sprim) < len(Best): Best = deepcopy(Sprim) # print("amelio2") """diminution de la temperature""" T = T * alpha return len(Best), Best
def hrh_ilwoa_rs(n, c, list): """transform the list of int into a list of Objects""" liste = [] for i in range(len(list)): liste.append(Model.Objet(i, list[i])) """execute ilwoa""" ilwoa = ILWOA(liste, c) sol, nb = ilwoa.optimize() """get result and transform it """ Sol = [Model.Bin(0, c)] for i in range(len(sol)): if Sol[-1].capacite_restante() >= liste[sol[i]].weight: Sol[-1].ranger_obj(liste[sol[i]]) else: Sol.append(Model.Bin(len(Sol), c)) Sol[-1].ranger_obj(liste[sol[i]]) print("\tILWOA: {} ".format(nb)) """execute RS""" rs = RS() nb, result = rs.RS(n, c, list, Sol) print("\tILWOA_RS: {}".format(nb))
def ag_mix(n, c, list): """heuristics + RS apres mutation """ """execute ag""" ag = main(5, 25, 10, n, c, list) sol = solution(ag[0]) """get result and transform it to a list of Bins""" bins = [] for i in range(sol[-1]): bins.append(Model.Bin(i, c)) for j in range(len(sol[0][i])): bins[i].ranger_obj( Model.Objet(sol[0][i][j] - 1, list[sol[0][i][j] - 1])) print("\tAG_mix: {}".format(len(bins))) return bins
def ilwoa_rs_rs(n, c, list): """transform the list of int into a list of Objects""" liste = [] for i in range(len(list)): liste.append(Model.Objet(i, list[i])) """execute ilwoa""" ilwoars = ILWOASA(liste, c) sol, nb = ilwoars.optimize( max_iter=50) # change here if you wanna try with less iterations """get result and transform it """ Sol = [Model.Bin(0, c)] for i in range(len(sol)): if Sol[-1].capacite_restante() >= liste[sol[i]].weight: Sol[-1].ranger_obj(liste[sol[i]]) else: Sol.append(Model.Bin(len(Sol), c)) Sol[-1].ranger_obj(liste[sol[i]]) print("\tILWOA_RS1: {} ".format(nb)) """execute RS""" rs = RS.RS() nb, result = rs.RS(n, c, list, Sol) print("\tILWOA_RS2: {}".format(nb))
def hrh_ag_rs(n, c, list): """execute ag""" ag = main(n,c,list) sol= solution(ag[0]) """get result and transform it to a list of Bins""" bins=[] for i in range (sol[-1]): bins.append(Model.Bin(i,c)) for j in range(len(sol[0][i])): bins[i].ranger_obj(Model.Objet(sol[0][i][j]-1,list[sol[0][i][j]-1])) rs= RS() nb ,result= rs.RS(n,c,list,bins) return nb, result
def ag_rs_rs(n, c, list): """rs inside AG, suivie d'un RS""" ag = main(5, 25, 10, n, c, list) sol = solution(ag[0]) """get result and transform it to a list of Bins""" bins = [] for i in range(sol[-1]): bins.append(Model.Bin(i, c)) for j in range(len(sol[0][i])): bins[i].ranger_obj( Model.Objet(sol[0][i][j] - 1, list[sol[0][i][j] - 1])) print("\tAG_RS: {}".format(len(bins))) """execute RS""" rs = RS() nb, result = rs.RS_iteratif(n, c, list, bins) print("\tAG_RS*2: {}".format(nb)) return result
def ag_mix_rs(n, c, list): """heuristics + RS apres mutation +RS HAUT NIVEAU """ """execute ag""" ag = main(5, 25, 10, n, c, list) sol = solution(ag[0]) """get result and transform it to a list of Bins""" bins = [] for i in range(sol[-1]): bins.append(Model.Bin(i, c)) for j in range(len(sol[0][i])): bins[i].ranger_obj( Model.Objet(sol[0][i][j] - 1, list[sol[0][i][j] - 1])) print("\tAG_MIX: {}".format(len(bins))) """execute RS""" rs = RS() nb, result = rs.RS(n, c, list, bins) print("\tAG_MIX_RS: {}".format(nb)) return result
def AG_hyb1(n, c, list): """heuristics + RS (haut niveau) """ """execute ag""" ag = main(500, 25, 10, n, c, list) sol = solution(ag[0]) """get result and transform it to a list of Bins""" bins = [] for i in range(sol[-1]): bins.append(Model.Bin(i, c)) for j in range(len(sol[0][i])): bins[i].ranger_obj( Model.Objet(sol[0][i][j] - 1, list[sol[0][i][j] - 1])) # print("\tAG: {}".format(len(bins))) """execute RS""" rs = RS() nb, result = rs.RS(n, c, list, bins) print("\tAG2_RS: {}".format(nb)) return result
def create_bins(self, sol): bin_list = [] id_bin = 0 i = 0 bin_ = Model.Bin(id_bin, self.capacity) for obj in sol: #print(obj) o = Model.Objet(obj, self.objects[obj].weight) if bin_.capacite_restante() >= o.weight: bin_.ranger_obj(o) if (np.where(sol == obj)[0][0] == len(sol) - 1): bin_list.append(copy.deepcopy(bin_)) else: # current object can't fit in current bin bin_list.append(copy.deepcopy(bin_)) id_bin += 1 bin_.set_id(id_bin) bin_.set_obj([]) bin_.ranger_obj(o) if (np.where(sol == obj)[0][0] == len(sol) - 1): bin_list.append(copy.deepcopy(bin_)) return bin_list
def RS(self, n, c, list, S, Tinit=30, T0=0.1, R=1000, alpha=0.95, init=False): t_exec = time.time() deltaF = [] self.n = n self.c = c self.list = list R = min(R, int(n)) # alpha = (1-1/n) # T0=Tinit/math.log(n,2) # Tinit= n """Solution initiale générée aléatoirement """ if not S: random.shuffle(list) N = len(list) C = c something_opt, liste_obj = first_fit(N, C, list) Zs = len(liste_obj) S = [] for i in range(Zs): S.append(Model.Bin(i, c)) for j in range(len(liste_obj[i])): S[i].ranger_obj(Model.Objet(i + j, liste_obj[i][j])) #print("solu inti") #print(len(S)) """ initialisaiton des parametres""" Best = deepcopy(S) # Tinit=self.Temperature_initiale() T = Tinit # T0 = self.Temperature_min() Tmoy = ( Tinit + T0 ) / 2 # la barriere entre le regime haute temperature et basse temperature """tant que le seuil de temperature n'est pas atteint, recherche locale avec la temperature T""" while T > T0 and time.time() - t_exec < 60: improve = False cpt = 0 proba = -1 # print(T) """repeter le processus de recherche locale R fois """ for i in range(R): """generer aléatoirement une solution voisine de S""" if T > Tmoy: """Mode High temperature => Type1""" Sprim = self.generer_voisin1(S) else: """Mode Low temperature => Type2""" Sprim = self.generer_voisin2(S) #print(len(Sprim)) if init: deltaF.append(self.F(S) - self.F(Sprim)) if self.F(Sprim) > self.F(S): """accepter Sprim """ S = deepcopy(Sprim) if len(Sprim) < len(Best): Best = deepcopy(Sprim) improve = True cpt = cpt + 1 else: """accepter Sprim avec un proba """ u = np.random.uniform(0, 1) # print(((self.F(Sprim)-self.F(S))/T)) # print(math.exp((self.F(Sprim) - self.F(S)) / T)) proba = math.exp(-(self.F(S) - self.F(Sprim)) / T) if u > proba: S = deepcopy(Sprim) if len(Sprim) < len(Best): Best = deepcopy(Sprim) if cpt >= 20: break if not improve and proba < 0.1: break """diminution de la temperature""" T = T * alpha boxes = [] for i in Best: boxes.append(i.get_objects) list_boxes = [] for j in boxes: box = [] for a in j: box.append(a.weight) list_boxes.append(box) if init: return deltaF, len(Best), list_boxes else: return len(Best), list_boxes