import random import numpy as np # Initialization n = 7 # number of items C = 10 # bin capacity tmax = 100 # number of iterations psize = 10 # population size items=[2, 5, 4, 7, 1, 3, 8] #items weights def afdo(n, C, tmax, psize, items): # Generate initial population using modified FF heuristic pop = [] for i in range(psize): random.shuffle(items) bins = [[]] for item in items: for j in range(len(bins)): if sum(bins[j]) + item <= C: bins[j].append(item) break elif j == len(bins)-1: bins.append([item]) break pop.append(bins) # Fitness function def fitness(solution): f = 0 z = 2 for j in range(len(solution)): fill = sum(solution[j]) f += (fill/C)**z return f/len(bins) # Improve the initial solution using AFDO t = 0 fit = fitness(pop[0]); xstar = pop[0] while t <= tmax: # Artificial scout bee evaluation for i in range(1,psize): newfit = fitness(pop[i]) if newfit < fit: newfit = fit xstar = pop[i] bstar=len(xstar) # Artificial scout bee movement wf = 1 for i in range(psize): xi = pop[i] fw = abs(fitness(xstar)/fitness(xi))*wf r = round(random.random(),1) #nombre aleatoire entre 0 et 1, arrondi a 1 chiffre apres la virgule if fw == 1 or fw == 0 or fitness(xi) == 0: def generate_raw_vectors(pop[i], len(pop[i]), n, r): raw_vectors = [] for i in range(num_items): old_bin = pop[i][1] new_bin = random.randint(1, len(pop[i])) raw_vectors.append((pop[i][0], old_bin, new_bin)) return raw_vectors else: t += 1 # Output the results print("Final assignment of items to bins:", xstar) print("Minimum number of used bins:", bstar) return 1 afdo(n,C,tmax,psize,items)