def main(): seed = int(input("Wprowadź Z: ")) generator = RandomNumberGenerator(seed) taskNumber = int(input("Wprowadź rozmiar problemu: ")) tasks = range(1, taskNumber + 1) nr = [] pj = [] #czas wykonania wj = [] #waga/współczynnik kary dj = [] #żądany termin zakończenia for task in tasks: nr.append(task) pj.append(generator.nextInt(1, 29)) A = 0 for number in pj: A += number for task in tasks: wj.append(generator.nextInt(1, 9)) X = 29 #X=A for task in tasks: dj.append(generator.nextInt(1, X)) print("\nnr:", nr) print("pj: ", pj) print("wj ", wj) print("dj", dj) witi_start = target_fun(pj, wj, dj, taskNumber) print(f'\nWiTi dla początkowego = {witi_start[0]}') print(f'C: {witi_start[2]}') print(f'T: {witi_start[1]}') witi_greedy = greedy(pj, wj, dj, taskNumber) print(f'\nWiTi dla Greedy algorithm = {witi_greedy[0]}') print(f'C: {witi_greedy[2]}') print(f'T: {witi_greedy[1]}') witi_brute_force = brute_force(pj, wj, dj, taskNumber) print(f'\nWiTi dla Brute Force = {witi_brute_force[0]}') print(f'C: {witi_brute_force[2]}') print(f'T: {witi_brute_force[1]}')
def __generate_instance(self, seed): RNG = RandomNumberGenerator(seed) task_queue = [] for J in range(0, self.size): task_queue.append({'j': J + 1}) for M in range(1, self.machines + 1): task_queue[J].update({f'p{M}': RNG.nextInt(1, MAX_VALUE)}) task_queue[J].update({f'S{M}': 0}) task_queue[J].update({f'C{M}': 0}) return task_queue
def instanceGenerator(Z, size): generator = RandomNumberGenerator(Z) d = [[0 for i in range(size)] for j in range(size)] for i in range(size): for j in range(size): if (i > j): d[i][j] = generator.nextInt(1, 50) d[j][i] = generator.nextInt(1, 50) return d
def __init__(self, size, seed): self.size = size self.last_permutation = [] RNG = RandomNumberGenerator(seed) p_list = [] w_list = [] d_list = [] p_sum = 0 self.queue = [] for J in range(0, self.size): p_list.append(RNG.nextInt(1, MAX_VALUE)) p_sum += p_list[-1] for J in range(0, self.size): w_list.append(RNG.nextInt(1, 9)) for J in range(0, self.size): d_list.append(RNG.nextInt(1, MAX_D)) for J in range(0, self.size): self.queue.append(task(J + 1, p_list[J], w_list[J], d_list[J])) self.last_permutation.append(J + 1)
def main(): seed = int(input("Wprowadź Z: ")) generator = RandomNumberGenerator(seed) taskNumber = int(input("Wprowadź liczbę zadań: ")) tasks = range(1, taskNumber + 1) machineNumber = int(input("Wprowadź liczbę maszyn: ")) machines = range(1, machineNumber + 1) #lista wszystkich zadan na konkretnych maszynach p_ij = [] # permutacja zadan pi = [] # wyswietlenie rozwiazania po wykonaniu algorytmu optymalizacji solution = [] #lista zadan przydzielonych do jednej maszyny pj = [] for task in tasks: for machine in machines: pj.append(generator.nextInt(1, 29)) p_ij.append(pj.copy()) pj.clear() pi.append(task) print(p_ij) print("Permutacj naturalna: ") print("pi: ", pi) Cj, Cmax = calculate(p_ij, taskNumber, machineNumber) print("C:", Cj) print("Cmax:", Cmax) for task in tasks: solution.append([pi[task - 1], p_ij[task - 1]]) pi = Johnson(tasks, p_ij.copy()) print("Johnson: ") print("pi: ", pi) sort = {x: i for i, x in enumerate(pi)} solution.sort(key=lambda x: sort[x[0]]) Cj, Cmax = calculate([row[1] for row in solution], taskNumber, machineNumber) print("C", Cj) print("Cmax:", Cmax)
def simulatedAnnealing(tasks): RNG = RandomNumberGenerator(123) t = copy.deepcopy(tasks) T = 1550.0 #temperatura zarzenia na bialo rozgrzanej stali :) Tend = 1E-10 # cokolwiek większe od zera it = 0 L = 10 pi = [] pi_new = [] pi_best = [] #natural perm for perm in range(1, t.size + 1): pi.append(perm) pi_best.append(perm) while T > Tend: for k in range(L): i = RNG.nextInt(0, t.size) - 1 j = RNG.nextInt(0, t.size) - 1 pi_new = copy.deepcopy(pi) swap_value = pi_new[i] pi_new[i] = pi_new[j] pi_new[j] = swap_value cmax_new = t.calculate_Cmax(pi_new) cmax_old = t.calculate_Cmax(pi) if cmax_new > cmax_old: r = RNG.nextFloat(0, 1) dCmax = cmax_old - cmax_new if r >= math.exp(dCmax / T): pi_new = copy.deepcopy(pi) pi = copy.deepcopy(pi_new) if t.calculate_Cmax(pi_best) > t.calculate_Cmax(pi): pi_best = copy.deepcopy(pi) it += 1 T = T / math.log(it + 1) #ln(it+1) return pi_best
def main(): seed = int(input("Wprowadź Z: ")) generator = RandomNumberGenerator(seed) taskNumber = int(input("Wprowadź liczbę zadań: ")) tasks = range(1, taskNumber + 1) machineNumber = int(input("Wprowadź liczbę maszyn: ")) machines = range(1, machineNumber + 1) Pi = [] #Permutacja po uszeregowaniu P = [] # Macierz czas wykonania Nr = [] # Lista zadań do wykonania P_Copy = [] #Macierz czasów wykonania Nr_Copy = [] # Lista zadań do wykonania C = [] # Macierz czasów zakończenia zadań S = [] # Macierz czasów trwania zadań for i in range(1, taskNumber + 1): Pi.append(i) for i in range(0, taskNumber): z = [None] * machineNumber C.append(z) for i in range(0, taskNumber): x = [None] * machineNumber S.append(x) for i in range(0, taskNumber): p = [] for j in range(0, machineNumber): p.append(generator.nextInt(1, 29)) P.append(p) P_Copy.append(p) for i in range(1, taskNumber + 1): Nr.append(i) Nr_Copy.append(i) print("\n") print("Tabu Calculate:") tabu_calculate(taskNumber, machineNumber, C, P, Pi, S) print(f"Pi: {Pi}") print(f"P: {P}") print(f"C: {C}") print(f"Cmax: {calculate_cmax(machineNumber,Pi,P)}") tabu_time_start = datetime.datetime.now() newPi = tabuSearch(taskNumber, machineNumber, P, Pi) tabu_time_end = datetime.datetime.now() - tabu_time_start newP = [] for i in range(0, taskNumber): newP.append(P[newPi[i] - 1]) tabu_calculate(taskNumber, machineNumber, C, newP, newPi, S) C, Cmax = calculate_neh(newP, taskNumber, machineNumber) print(f"Tabu search czas: {tabu_time_end}") print(f"Tabu search po sortowaniu: ") print(f"Pi: {newPi}") print(f"C: {C}") print(f"Cmax: {Cmax}") #lista wszystkich zadan na konkretnych maszynach p_ij_neh = P # permutacja zadan pi_neh = Nr p_kj = [] print("\n") print("NEH:") print("p", p_ij_neh) print("Permutacj naturalna: ") print("pi: ", pi_neh) Cj, Cmax = calculate_neh(p_ij_neh, taskNumber, machineNumber) print("C:", Cj) print("Cmax:", Cmax) neh_time_start = datetime.datetime.now() p_kj = NEH(p_ij_neh.copy(), taskNumber, machineNumber) neh_time_end = datetime.datetime.now() - neh_time_start print(f"NEH czas: {neh_time_end}") print("NEH po sortowaniu: ") print("pkj: ", p_kj) Cj, Cmax = calculate_neh(p_kj, taskNumber, machineNumber) print("C:", Cj) print("Cmax:", Cmax) #lista wszystkich zadan na konkretnych maszynach p_ij_johnson = P # permutacja zadan pi_johnson = Nr # wyswietlenie rozwiazania po wykonaniu algorytmu optymalizacji solution = [] print("\n") print("Johnson:") print(p_ij_johnson) print("Permutacj naturalna: ") print("pi: ", pi_johnson) Cj, Cmax = calculate_johnson(p_ij_johnson, taskNumber, machineNumber) print("C:", Cj) print("Cmax:", Cmax) for task in tasks: solution.append([pi_johnson[task - 1], p_ij_johnson[task - 1]]) johson_time_start = datetime.datetime.now() pi = Johnson(tasks, p_ij_johnson.copy()) johson_time_end = datetime.datetime.now() - johson_time_start print(f"Johson czas: {johson_time_end}") print("Johnson po sortowaniu: ") print("pi: ", pi) sort = {x: i for i, x in enumerate(pi)} solution.sort(key=lambda x: sort[x[0]]) Cj, Cmax = calculate_johnson([row[1] for row in solution], taskNumber, machineNumber) print("C", Cj) print("Cmax:", Cmax)
def main(): seed = int(input("Wprowadź Z:")) generator = RandomNumberGenerator(seed) taskNumber = int(input("Wprtowadź liczbe zadań:")) tasks = range(1, taskNumber + 1) nr = [] #wektor kolejnych zadan r = [] #czas przygotowania kolejnych zadań p = [] #czas wykonania kolejnych zadan q = [] #czas dostarczenia/stygnięcia kolejnych zadań #generowanie danych do zadania for task in tasks: nr.append(task) p.append(generator.nextInt(1, 29)) pi = nr.copy() #inicjalizacja permutacji zadań A = 0 # X = 29 for number in p: A += number for task in tasks: r.append(generator.nextInt(1, A)) for task in tasks: q.append(generator.nextInt(1, 29)) print("\nnr:", nr) print("r: ", r) print("p: ", p) print("q: ", q) print("\n") S, C, Cq, Cmax = calculate(r, p, q, taskNumber) print(f'pi: {pi}') print("S: ", S) print("C: ", C) print(f"Cq: {Cq}") print("Cmax", Cmax) print("\n") #inicjalizacja permutacji zadań # pi = nr.copy() #strutktura skladajaca sie z solution = [] for task in tasks: solution.append([pi[task - 1], r[task - 1], p[task - 1], q[task - 1]]) #Schrage print("Po sortowaniu:") pi = schragePmtn(r, p, q, tasks) sort = {x: i for i, x in enumerate(pi)} solution.sort(key=lambda x: sort[x[0]]) rjSchrage = [] pjSchrage = [] qjSchrage = [] for task in tasks: rjSchrage.append(solution[task - 1][1]) pjSchrage.append(solution[task - 1][2]) qjSchrage.append(solution[task - 1][3]) SSchrage, CSchrage, CqSchrage, CmaxSchrager = calculate( rjSchrage, pjSchrage, qjSchrage, taskNumber) print("pi:", pi) print("S: ", SSchrage) print("C: ", CSchrage) print(f"Cq: {CqSchrage}") print("Cmax", CmaxSchrager)