def main(): import genetic import sys # To make command line print complete nd arrays. np.set_printoptions(threshold=sys.maxsize) # Run for entire 120 problem range for problem in range(0,120): jobs = jobs_list[problem] machines = machines_list[problem] timeseed = timeseed_list[problem] # Generate processing time matrix a = random_matrix(machines, jobs, timeseed) # Transpose matrix to calulate makespan at = a.transpose() # Start with all possible permutations of first 4 jobs import itertools init_jobs = 4 init_job_list = list(range(init_jobs)) # Generate all 24 permutations sequence_list = np.array(list(itertools.permutations(init_job_list))) # This loop will run till init_jobs == jobs while init_jobs <= jobs: makespan_list = np.array([]) for seq in sequence_list: seq = list(seq) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], seq)) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Roulette wheel .. 3 x 20 output sequence_list, makespan_list = roulette_wheel(sequence_list, makespan_list) # Again, sort and reduce to 20 sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) #print("Best 20\n", sequence_list, makespan_list) # Now ordered crossover each of the 20 sequences with each other and add to lists for i in range(20): for j in range(20): if i != j: child = genetic.ordered_crossover(sequence_list[i], sequence_list[j]) child = np.array(child) sequence_list = np.vstack((sequence_list, child)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(child))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Applying mutation. Inverse mutation. for i in sequence_list: mutated = genetic.inverse_mutation(i) mutated = np.array(mutated) sequence_list = np.vstack((sequence_list, mutated)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(mutated))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Applying another mutation. Pairwise Swap Mutation. for i in sequence_list: mutated = genetic.pairwise_swap_mutation(i) mutated = np.array(mutated) sequence_list = np.vstack((sequence_list, mutated)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(mutated))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Ordered Crossover again for i in range(20): for j in range(20): if i != j: child = genetic.ordered_crossover(sequence_list[i], sequence_list[j]) child = np.array(child) sequence_list = np.vstack((sequence_list, child)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(child))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Set first element of sorted list as best makespan. best_sequence = sequence_list[0] best_makespan = makespan_list[0] # Bring in next job into every position in sequence_list init_jobs += 1 init_job_list = list(range(init_jobs)) new_sequence_list = np.array([]).reshape(0, init_jobs) for s in sequence_list: for pos in range(s.size + 1): new_sequence_list = np.vstack((new_sequence_list, np.insert(s, pos, init_jobs - 1))) sequence_list = new_sequence_list best_sequence = list(map(int, best_sequence)) best_sequence = list(map(str, best_sequence)) best_sequence = ', '.join(best_sequence) if best_makespan < makespans[problem]: taillard.cell(row = problem + 3, column = 6).value = best_makespan taillard.cell(row = problem + 3, column = 7).value = best_sequence taillard_file.save('Makespans.xlsx') print("Jobs: ", jobs, "\nMachines: ", machines, "\nTimeseed: ", timeseed, "\nOptimal sequence: ", best_sequence, "\nOptimal makespan: ", best_makespan, "\n")
def solve_benchmark_problem(problem, jobs, machines, population, nbgeneration, probabilite, show_matrix=True): # read matrix size a = read_matrix(problem, machines, jobs) at = a.transpose() # Start with all possible permutations of first 4 jobs init_jobs = 4 init_job_list = list(range(init_jobs)) # Generate all 24 permutations sequence_list = np.array(list(permutations(init_job_list))) geneation = 0 while init_jobs <= jobs and geneation < nbgeneration: makespan_list = np.array([]) for seq in sequence_list: seq = list(seq) makespan_list = np.append( makespan_list, calculate_makespan(at[init_job_list], seq)) # Sort both arrays in ascending order of makespan # and reduce to population size best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list, population) # Roulette wheel .. 3 x population size output sequence_list, makespan_list = genetic.roulette_wheel( sequence_list, makespan_list) # Again, sort and reduce to population size sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list, population) # Now ordered crossover each of the population size sequences with each other # and add to lists for i in range(nbgeneration): for j in range(nbgeneration): if i != j: child = genetic.ordered_crossover(sequence_list[i], sequence_list[j]) child = np.array(child) sequence_list = np.vstack((sequence_list, child)) makespan_list = np.append( makespan_list, calculate_makespan(at[init_job_list], list(child))) # Sort both arrays in ascending order of makespan # and reduce to population size best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list, population) # Applying mutation. Inverse Mutation. for i in sequence_list: mutated = genetic.inverse_mutation(i, probabilite) mutated = np.array(mutated) sequence_list = np.vstack((sequence_list, mutated)) makespan_list = np.append( makespan_list, calculate_makespan(at[init_job_list], list(mutated))) # Sort both arrays in ascending order of makespan # and reduce to population size best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list, population) # Applying another mutation. Pairwise Swap Mutation. for i in sequence_list: mutated = genetic.pairwise_swap_mutation(i, probabilite) mutated = np.array(mutated) sequence_list = np.vstack((sequence_list, mutated)) makespan_list = np.append( makespan_list, calculate_makespan(at[init_job_list], list(mutated))) # Sort both arrays in ascending order of makespan # and reduce to population best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list, population) #Order cross over again for i in range(population): for j in range(population): if i != j: child = genetic.ordered_crossover(sequence_list[i], sequence_list[j]) child = np.array(child) sequence_list = np.vstack((sequence_list, child)) makespan_list = np.append( makespan_list, calculate_makespan(at[init_job_list], list(child))) # Sort both arrays in ascending order of makespan # and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list, population) # Set first element of sorted list as best makespan. best_sequence = sequence_list[0] best_makespan = makespan_list[0] # Bring in next job into every position in sequence_list init_jobs += 1 init_job_list = list(range(init_jobs)) new_sequence_list = np.array([], dtype=int).reshape(0, init_jobs) for s in sequence_list: for pos in range(s.size + 1): new_sequence_list = np.vstack( (new_sequence_list, np.insert(s, pos, init_jobs - 1))) sequence_list = new_sequence_list return best_sequence, best_makespan
def calculate_optimal_makespan(*args): import genetic import sys # To make command line print complete nd arrays np.set_printoptions(threshold=sys.maxsize) problem = problem_num.get() jobs = jobs_list[problem] machines = machines_list[problem] timeseed = timeseed_list[problem] # Write these to GUI jobs_val.set(jobs) macs_val.set(machines) time_val.set(timeseed) # Generate matrix of times a = random_matrix(machines, jobs, timeseed) print("Problem No.:", problem, "\nMatrix:\n", a) # Transpose matrix to calulate makespan at = a.transpose() # Start with all possible permutations of first 4 jobs import itertools init_jobs = 4 init_job_list = list(range(init_jobs)) # Generate all 24 permutations sequence_list = np.array(list(itertools.permutations(init_job_list))) # This loop will eventually run till init_jobs <= jobs while init_jobs <= jobs: makespan_list = np.array([]) for seq in sequence_list: seq = list(seq) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], seq)) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Roulette wheel .. 3 x 20 output sequence_list, makespan_list = roulette_wheel(sequence_list, makespan_list) # Again, sort and reduce to 20 sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Now ordered crossover each of the 20 sequences with each other and add to lists for i in range(20): for j in range(20): if i != j: child = genetic.ordered_crossover(sequence_list[i], sequence_list[j]) child = np.array(child) sequence_list = np.vstack((sequence_list, child)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(child))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Applying mutation. Inverse Mutation. for i in sequence_list: mutated = genetic.inverse_mutation(i) mutated = np.array(mutated) sequence_list = np.vstack((sequence_list, mutated)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(mutated))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Applying another mutation. Pairwise Swap Mutation. for i in sequence_list: mutated = genetic.pairwise_swap_mutation(i) mutated = np.array(mutated) sequence_list = np.vstack((sequence_list, mutated)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(mutated))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Ordered Crossover again for i in range(20): for j in range(20): if i != j: child = genetic.ordered_crossover(sequence_list[i], sequence_list[j]) child = np.array(child) sequence_list = np.vstack((sequence_list, child)) makespan_list = np.append(makespan_list, calculate_makespan(at[init_job_list], list(child))) # Sort both arrays in ascending order of makespan and reduce to 20 best sequences sequence_list, makespan_list = sort_and_reduce(sequence_list, makespan_list) # Set first element of sorted list as best makespan. best_sequence = sequence_list[0] best_makespan = makespan_list[0] # Bring in next job into every position in sequence_list init_jobs += 1 init_job_list = list(range(init_jobs)) new_sequence_list = np.array([]).reshape(0, init_jobs) for s in sequence_list: for pos in range(s.size + 1): new_sequence_list = np.vstack((new_sequence_list, np.insert(s, pos, init_jobs - 1))) sequence_list = new_sequence_list print("\nBest Sequence:\n", best_sequence, "\n\n") makespan_val.set(best_makespan)