def run(self): machine_count, job_count = self.data.shape if self.method=="B&B": #do somethign print('BB') print(self.data) elif self.method=="CDS": start = time() result = CDS(self.data) end = time() self.label.setText(bend(50,'{}\nOrdre : {}\nMakespan : {}\nTemps d\'execution : {:.6}s'.format(self.label.text(),result[0],result[1],end - start))) plotGantt(self.data,result[0],"CDS",job_count) elif self.method=="NEH": start = time() result = neh(self.data) end = time() self.label.setText(bend(50,'{}\nOrdre : {}\nMakespan : {}\nTemps d\'execution : {:.6}s'.format(self.label.text(),result[0],result[1],end - start))) plotGantt(self.data,result[0],"NEH",job_count) elif self.method=="GENETIC": population_size = 50 # Generer une les individus de la population aleatoirement initPop = [sample(list(range(0, job_count)), job_count) for _ in range(0, population_size)] # same repeated individual start = time() result = genetic(self.data, initPop, population_size, 0.1, 200) end = time() self.label.setText(bend(50,'{}\nOrdre : {}\nMakespan : {}\nTemps d\'execution : {:.6}s'.format(self.label.text(),result[0],result[1],end - start))) plotGantt(self.data,result[0],"Genetic",job_count) elif self.method=="RECUIT": start = time() result = simulated_annealing(self.data, Ti = 790,Tf = 3 ,alpha = 0.93) end = time() self.label.setText(bend(50,'{}\nOrdre : {}\nMakespan : {}\nTemps d\'execution : {:.6}s'.format(self.label.text(),result[0],result[1],end - start))) plotGantt(self.data,result[0],"Genetic",job_count) elif self.method=="HYBRID": population_size = 50 # Generer une les individus de la population aleatoirement initPop = [sample(list(range(0, job_count)), job_count) for _ in range(0, population_size)] # same repeated individual start = time() result = genetic_rt(self.data, initPop, population_size, 0.1, 200) end = time() self.label.setText(bend(50,'{}\nOrdre : {}\n Makespan : {}\nTemps d\'execution : {:.6}s'.format(self.label.text(),result[0],result[1],end - start))) plotGantt(self.data,result[0],"Hybrid",job_count)
def simulated_annealing(matrice, Ti = 790,Tf = 3 ,alpha = 0.93): #Number of jobs given nb_machines, job_count = matrice.shape n = job_count; default_timer = None if sys.platform == "win32": default_timer = time.time() else: default_timer = time.time() s = default_timer #Initialize the primary seq old_seq = neh(matrice) old_seq = old_seq[0] old_makeSpan = makespan(old_seq,matrice) #print("old sequence: ",old_seq) #print("old makespan: ",old_makeSpan) new_seq = [] delta_mk1 = 0 #Initialize the temperature T = Ti Tf = Tf alpha = alpha # of iterations temp_cycle = 0 while T >= Tf : new_seq = old_seq.copy() job = new_seq.pop(randint(0,n-1)) new_seq.insert(randint(0,n-1),job) new_make_span = makespan(new_seq,matrice) delta_mk1 = new_make_span - old_makeSpan if delta_mk1 <= 0: old_seq = new_seq old_makeSpan = new_make_span else : Aprob = np.exp(-(delta_mk1/T)) if Aprob > np.random.uniform(0.5,0.9): old_seq = new_seq old_makeSpan = new_make_span else : #The solution is discarded pass T = T * alpha temp_cycle += 1 e = default_timer #Result Sequence seq = old_seq schedules = np.zeros((nb_machines, job_count), dtype=dict) # schedule first job alone first task = {"name": "job_{}".format( seq[0] + 1), "start_time": 0, "end_time": matrice[0][seq[0]]} schedules[0][0] = task for m_id in range(1, nb_machines): start_t = schedules[m_id - 1][0]["end_time"] end_t = start_t + matrice[m_id][0] task = {"name": "job_{}".format( seq[0] + 1), "start_time": start_t, "end_time": end_t} schedules[m_id][0] = task for index, job_id in enumerate(seq[1::]): start_t = schedules[0][index]["end_time"] end_t = start_t + matrice[0][job_id] task = {"name": "job_{}".format( job_id + 1), "start_time": start_t, "end_time": end_t} schedules[0][index + 1] = task for m_id in range(1, nb_machines): start_t = max(schedules[m_id][index]["end_time"], schedules[m_id - 1][index + 1]["end_time"]) end_t = start_t +matrice[m_id][job_id] task = {"name": "job_{}".format( job_id + 1), "start_time": start_t, "end_time": end_t} schedules[m_id][index + 1] = task t_t = e - s return seq, old_makeSpan
machine_count, job_count = matrice.shape print('{} machines et {} jobs'.format(machine_count, job_count)) print('Algorithme CDS :') start = time() result = CDS(matrice) end = time() print(' Ordre : {}'.format(result[0])) print(' Makespan : {}'.format(result[1])) print(' Temps d\'execution : {:.6}s\n'.format(end - start)) tempsCDS.append(end - start) makespanCDS.append(result[1]) print('Algorithme NEH :') start = time() result = neh(matrice) end = time() print(' Ordre : {}'.format(result[0])) print(' Makespan : {}'.format(result[1])) print(' Temps d\'execution : {:.6}s\n'.format(end - start)) tempsNEH.append(end - start) makespanNEH.append(result[1]) print('Algorithme de Recuit Simulé :') start = time() result = simulated_annealing(matrice, Ti=790, Tf=3, alpha=0.93) end = time() print(' Ordre : {}'.format(result[0])) print(' Makespan : {}'.format(result[1])) print(' Temps d\'execution : {:.6}s\n'.format(end - start)) tempsSA.append(end - start)
def cockroach(iterations, steps, cockroach_count, job_count, machine_count, jobTimes, isNehEnabled, showDynamicallyGraph, controller): jobs = {} states = [] optimal_state = [] optimal_cost = 999999999999999999 optimals = [] visual = controller.visual print("VISUAL: ", visual) # print('Times:') if jobTimes is None: for i in xrange(job_count): times = map(lambda x: int(x), raw_input().split(' ')[:machine_count]) jobs[i] = times #jobs[i] = [jobTimes[i]] else: for i in xrange(job_count): #jobs[i] = times jobs[i] = jobTimes[i] print(jobs) if isNehEnabled == False: for i in xrange(cockroach_count): state = range(job_count) shuffle(state) states.append(state) cost = calculate_cost(jobs, state) # print(cost, state) if cost < optimal_cost: optimal_cost, optimal_state = cost, state[:] else: neh_result_state = neh.neh(jobs) states.append(neh_result_state) cost = calculate_cost(jobs, neh_result_state) print "Neh makespan and state: (", cost, "), ", neh_result_state if cost < optimal_cost: optimal_cost, optimal_state = cost, neh_result_state[:] for i in xrange(1, cockroach_count): state = range(job_count) shuffle(state) states.append(state) cost = calculate_cost(jobs, state) # print(cost, state) if cost < optimal_cost: optimal_cost, optimal_state = cost, state[:] print(optimal_cost, optimal_state) counter = 0 makespan_table = [] xData = [] yData = [] plt.ion() if showDynamicallyGraph: #na szybko ten if fig = plt.figure() ax = fig.add_subplot(111) line1, = ax.plot([], [], '-k', label='black') plt.plot([0, controller.iterations], [controller.upperbound, controller.upperbound], 'r') plt.plot([0, controller.iterations], [controller.lowerbound, controller.lowerbound], 'g') plt.xlim([0, controller.iterations]) threshold = iterations / randint(4, 10) for i in xrange(iterations): yData.append(optimal_cost) xData.append(i) if showDynamicallyGraph: #dynamicznie rysowany wykres 1 line1.set_ydata(yData) line1.set_xdata(range(len(yData))) ax.relim() ax.autoscale_view() plt.draw() print optimal_cost makespan_table.append(optimal_cost) optimal_cost, change_s = swarm(jobs, states, steps, optimal_cost, optimal_state, visual) optimal_cost, change_d = disperse(states, jobs, optimal_cost, optimal_state) if not change_s and not change_d: counter += 1 if counter > threshold: rand = range(len(jobs)) shuffle(rand) new_cost = calculate_cost(jobs, rand) if acceptance(new_cost, optimal_cost, i) > uniform(0, 1): optimals.append((optimal_cost, optimal_state[:])) for s in states: if s == optimal_state: for j in xrange(randint(1, 2)): step(s, rand) new_cost = calculate_cost(jobs, s) optimal_state, optimal_cost = s[:], new_cost counter = 0 break else: counter = 0 ruthless(states, optimal_state) for i in optimals: if i[0] < optimal_cost: optimal_cost, optimal_state = i return (optimal_cost, optimal_state, makespan_table)
def run_cockroaches(iterations, steps, cockroach_count, job_count, machine_count, job_times): job_times = neh.neh(job_times) r = cockroach(iterations, steps, cockroach_count, job_count, machine_count, job_times, True, False) print(r[0], r[1])
from datareader import get_data from makespan import makespan, to_natural_order, get_order from simulated_annealing import simulated_annealing from improved_simulated_annealing import improved_simulated_annealing from neh import neh tasks, numb_of_machines = get_data("data.001") # INITIAL ORDER init_order = get_order(tasks) init_makespan = makespan(init_order, tasks, numb_of_machines) print("[INIT] makespan: {}, time: {}" .format(init_makespan, 0)) # NEH ORDER neh_order, neh_time = neh(copy.deepcopy(tasks), numb_of_machines) neh_makespan = makespan(neh_order, tasks, numb_of_machines) print("[NEH ] makespan: {}, time: {}" .format(neh_makespan, neh_time)) # SIMULATED ANNEALING ORDER init_temp = 5000 final_temp = 0.1 u = 0.98 cooling_fcn_type = 0 move_type = 0 insert = 0 simulated_annealing_order, iterations_sa, sa_time = simulated_annealing(copy.deepcopy(tasks), numb_of_machines, init_temp, final_temp, u, cooling_fcn_type, move_type, insert) simulated_annealing_makespan = makespan(simulated_annealing_order, tasks, numb_of_machines)
table = PrettyTable() table.field_names = ["Algorithm", "Cmax", "Real time"] #MODIF? MODIF = 3 start = 0 stop = 0 tasks_val, machines_val, tasks = read_data.read_data("ta001.txt") if MODIF == 0: print("Clasic neh") start = time.perf_counter() seq, cmax = neh.neh(tasks, machines_val, tasks_val) stop = time.perf_counter() print("Best sequence: ", seq) print("Best Cmax: ", cmax) policzony = round((stop - start), 3) print("Time: ", policzony) if MODIF == 1: #mod print("MOD neh") start = time.perf_counter() seq, cmax = neh.neh_wm(tasks, machines_val, tasks_val) stop = time.perf_counter() print("Best sequence: ", seq) print("Best Cmax: ", cmax) policzony = round((stop - start), 3)
def cockroach(iterations, steps, cockroach_count, job_count, machine_count, jobTimes, isNehEnabled, showDynamicallyGraph, controller): jobs = {} states = [] optimal_state = [] optimal_cost = 999999999999999999 optimals = [] visual = controller.visual print("VISUAL: ", visual) # print('Times:') if jobTimes is None: for i in xrange(job_count): times = map(lambda x: int(x), raw_input().split(' ')[:machine_count]) jobs[i] = times #jobs[i] = [jobTimes[i]] else: for i in xrange(job_count): #jobs[i] = times jobs[i] = jobTimes[i] print(jobs) if isNehEnabled == False: for i in xrange(cockroach_count): state = range(job_count) shuffle(state) states.append(state) cost = calculate_cost(jobs, state) # print(cost, state) if cost < optimal_cost: optimal_cost, optimal_state = cost, state[:] else: neh_result_state = neh.neh(jobs) states.append(neh_result_state) cost = calculate_cost(jobs, neh_result_state) print "Neh makespan and state: (", cost, "), ", neh_result_state if cost < optimal_cost: optimal_cost, optimal_state = cost, neh_result_state[:] for i in xrange(1, cockroach_count): state = range(job_count) shuffle(state) states.append(state) cost = calculate_cost(jobs, state) # print(cost, state) if cost < optimal_cost: optimal_cost, optimal_state = cost, state[:] print(optimal_cost, optimal_state) counter = 0 makespan_table = [] xData = [] yData = [] plt.ion() if showDynamicallyGraph: #na szybko ten if fig = plt.figure() ax = fig.add_subplot(111) line1, = ax.plot([], [],'-k',label='black') plt.plot([0, controller.iterations], [controller.upperbound, controller.upperbound], 'r') plt.plot([0, controller.iterations], [controller.lowerbound, controller.lowerbound], 'g') plt.xlim([0, controller.iterations]) threshold = iterations / randint(4, 10) for i in xrange(iterations): yData.append(optimal_cost) xData.append(i) if showDynamicallyGraph: #dynamicznie rysowany wykres 1 line1.set_ydata(yData) line1.set_xdata(range(len(yData))) ax.relim() ax.autoscale_view() plt.draw() print optimal_cost makespan_table.append(optimal_cost) optimal_cost, change_s = swarm(jobs, states, steps, optimal_cost, optimal_state, visual) optimal_cost, change_d = disperse(states, jobs, optimal_cost, optimal_state) if not change_s and not change_d: counter += 1 if counter > threshold: rand = range(len(jobs)) shuffle(rand) new_cost = calculate_cost(jobs, rand) if acceptance(new_cost, optimal_cost, i) > uniform(0, 1): optimals.append((optimal_cost, optimal_state[:])) for s in states: if s == optimal_state: for j in xrange(randint(1, 2)): step(s, rand) new_cost = calculate_cost(jobs, s) optimal_state, optimal_cost = s[:], new_cost counter = 0 break else: counter = 0 ruthless(states, optimal_state) for i in optimals: if i[0] < optimal_cost: optimal_cost, optimal_state = i return (optimal_cost, optimal_state, makespan_table)
def run_cockroaches(iterations, steps, cockroach_count, job_count, machine_count, job_times): job_times = neh.neh(job_times) r = cockroach(iterations, steps, cockroach_count, job_count, machine_count, job_times, True, False) print(r[0], r[1])