def tabu(N, NL, S, L, items, completion, tardiness, line_values, item_values, G, lambda1, lambda2): TL = [None] * NL from_same = [] from_diff = [] pairs = generate.pairsets(L) completion_temp = completion[:] # backup all the var tardiness_temp = tardiness[:] line_values_temp = line_values[:] item_values_temp = item_values[:] delta_star = 0 Delta = 0 L_star = L[:] m = len(S) S_star = [None] * m for l in xrange(m): S_star[l] = S[l][:] for k in xrange(N): delta = [] issame = 0 isdiff = 0 out = 0 for s in pairs: job = list(s) a_L_index = max(L.index(job[0]), L.index(job[1])) b_L_index = min(L.index(job[0]), L.index(job[1])) a = L[a_L_index] b = L[b_L_index] l_a = generate.find_job(a, S) l_b = generate.find_job(b, S) a_idx = S[l_a].index(a) b_idx = S[l_b].index(b) if s not in TL: if l_a == l_b: S_k, c_k, t_k, v_k, delta_h = same_delta( a, b, S[l_a], items, line_values_temp[l_a], lambda1, lambda2 ) else: # delta_I S_I_1, S_I_2, c_I_1, c_I_2, t_I_1, t_I_2, v_I_1, v_I_2, c_I_2, delta_I = delta1( S, l_a, l_b, a, b_idx, item_values, items, line_values_temp, lambda1, lambda2 ) # delta_II S_II_1, S_II_2, c_II_1, c_II_2, t_II_1, t_II_2, v_II_1, v_II_2, delta_II = delta2( S, l_a, l_b, b, a_idx, item_values, items, line_values_temp, lambda1, lambda2 ) # delta_III S_III_1, S_III_2, c_III_1, c_III_2, t_III_1, t_III_2, v_III_1, v_III_2, delta_III = delta3( S, l_a, l_b, a, b, a_idx, b_idx, item_values, items, line_values_temp, lambda1, lambda2 ) delta_h = min(delta_I, delta_II, delta_III) elif s in from_same: continue else: # delta_i S_i_1, S_i_2, c_i_1, c_i_2, t_i_1, t_i_2, v_i_1, v_i_2, c_i_2, delta_i = delta1( S, l_a, l_b, a, b_idx, item_values, items, line_values_temp, lambda1, lambda2 ) # delta_ii S_ii_1, S_ii_2, c_ii_1, c_ii_2, t_ii_1, t_ii_2, v_ii_1, v_ii_2, delta_ii = delta2( S, l_a, l_b, b, a_idx, item_values, items, line_values_temp, lambda1, lambda2 ) delta_h = min(delta_i, delta_ii) if delta == [] or delta_h < delta: delta = delta_h a_temp, b_temp = a, b l_a_temp, l_b_temp = l_a, l_b if l_a == l_b: S_temp = S_k[:] c_temp, t_temp, v_temp = c_k, t_k, v_k issame, isdiff = 1, 0 out = 0 else: if s in TL: out = 1 issame, isdiff = 0, 0 if delta == delta_i: c1_temp, t1_temp, v1_temp = c_i_1, t_i_1, v_i_1 c2_temp, t2_temp, v2_temp = c_i_2, t_i_2, v_i_2 S1_temp, S2_temp = S_i_1, S_i_2 elif delta == delta_ii: c1_temp, t1_temp, v1_temp = c_ii_1, t_ii_1, v_ii_1 c2_temp, t2_temp, v2_temp = c_ii_2, t_ii_2, v_ii_2 S1_temp, S2_temp = S_ii_1, S_ii_2 else: out = 0 if delta == delta_I: c1_temp, t1_temp, v1_temp = c_I_1, t_I_1, v_I_1 c2_temp, t2_temp, v2_temp = c_I_2, t_I_2, v_I_2 S1_temp, S2_temp = S_I_1, S_I_2 issame, isdiff = 0, 0 elif delta == delta_II: c1_temp, t1_temp, v1_temp = c_II_1, t_II_1, v_II_1 c2_temp, t2_temp, v2_temp = c_II_2, t_II_2, v_II_2 S1_temp, S2_temp = S_II_1, S_II_2 issame, isdiff = 0, 0 elif delta == delta_III: c1_temp, t1_temp, v1_temp = c_III_1, t_III_1, v_III_1 c2_temp, t2_temp, v2_temp = c_III_2, t_III_2, v_III_2 S1_temp, S2_temp = S_III_1, S_III_2 issame, isdiff = 0, 1 if delta == []: continue L = generate.innerswap(L, L.index(a_temp), L.index(b_temp)) pairs = generate.pairsets(L) change_set = set([a_temp, b_temp]) if out: TL[TL.index(change_set)] = None from_diff.remove(change_set) delete_set = TL.pop(0) if delete_set in from_same: from_same.remove(delete_set) elif delete_set in from_diff: from_diff.remove(delete_set) if isdiff or issame: TL.append(change_set) if isdiff: from_diff.append(change_set) if issame: from_same.append(change_set) else: TL.append(None) if l_a_temp != l_b_temp: c_temp = c1_temp + c2_temp t_temp = t1_temp + t2_temp v_temp = v1_temp + v2_temp i = 0 for j in S1_temp + S2_temp: completion_temp[j] = c_temp[i] tardiness_temp[j] = t_temp[i] item_values_temp[j] = v_temp[i] i += 1 S[l_a_temp] = S1_temp[:] S[l_b_temp] = S2_temp[:] line_values_temp[l_a_temp] = sum(v1_temp) line_values_temp[l_b_temp] = sum(v2_temp) else: S[l_a_temp] = S_temp[:] line_values_temp[l_a_temp] = sum(v_temp) TL, from_same, from_diff = generate.TL_update(TL, from_same, from_diff, S) Delta += delta if Delta < delta_star: delta_star = Delta for l in xrange(m): S_star[l] = S[l][:] L_star = L[:] line_values = line_values_temp[:] item_values = item_values_temp[:] completion = completion_temp[:] tardiness = tardiness_temp[:] return delta_star, S_star, L_star, line_values, item_values, completion, tardiness
def tabu(N, NL, S, L, items, G, lambda1, lambda2): TL = [None] * NL from_same = [] from_diff = [] pairs = generate.pairsets(L) G_star = G L_star = L[:] S_c = [] S_ori = [] for s in S: S_ori.append(s[:]) S_c.append(s[:]) G_test = 0 for k in xrange(N): issame = 0 isdiff = 0 out = 0 s = random.choice(pairs) move = 0 job = list(s) a_L_index = max(L.index(job[0]), L.index(job[1])) b_L_index = min(L.index(job[0]), L.index(job[1])) a = L[a_L_index] b = L[b_L_index] l_a = generate.find_job(a, S) l_b = generate.find_job(b, S) a_idx = S[l_a].index(a) b_idx = S[l_b].index(b) if s not in TL: if l_a == l_b: # a <--> b S_temp = same_S(S, l_a, a_idx, b_idx) G_temp = test_G(items, S_temp, lambda1, lambda2) else: # a -> b's S_temp_1 = diff1_S(S, l_a, l_b, a, b_idx) G_temp_1 = test_G(items, S_temp_1, lambda1, lambda2) # b -> a's S_temp_2 = diff2_S(S, l_a, l_b, b, a_idx) G_temp_2 = test_G(items, S_temp_2, lambda1, lambda2) # a <--> b S_temp_3 = diff3_S(S, l_a, l_b, a, b, a_idx, b_idx) G_temp_3 = test_G(items, S_temp_3, lambda1, lambda2) G_temp = min(G_temp_1, G_temp_2, G_temp_3) elif s in from_same: continue elif s in from_diff: # a -> b's S_temp_1 = diff1_S(S, l_a, l_b, a, b_idx) G_temp_1 = test_G(items, S_temp_1, lambda1, lambda2) # b -> a's S_temp_2 = diff2_S(S, l_a, l_b, b, a_idx) G_temp_2 = test_G(items, S_temp_2, lambda1, lambda2) G_temp = min(G_temp_1, G_temp_2) if G_test == 0 or G_temp < G_test: move = 1 G_test = G_temp a_temp, b_temp = a, b l_a_temp, l_b_temp = l_a, l_b if l_a == l_b: S_star = S_temp issame, isdiff = 1, 0 out = 0 else: if s in TL: out = 1 issame, isdiff = 0, 0 if G_temp == G_temp_1: S_star = S_temp_1 elif G_temp == G_temp_2: S_star = S_temp_2 else: out = 0 if G_temp == G_temp_1: S_star = S_temp_1 issame, isdiff = 0, 0 elif G_temp == G_temp_2: S_star = S_temp_2 issame, isdiff = 0, 0 elif G_temp == G_temp_3: S_star = S_temp_3 issame, isdiff = 0, 1 if move == 0: continue L = generate.innerswap(L, L.index(a_temp), L.index(b_temp)) pairs = generate.pairsets(L) change_set = set([a_temp, b_temp]) if out: TL[TL.index(change_set)] = None from_diff.remove(change_set) delete_set = TL.pop(0) if delete_set in from_same: from_same.remove(delete_set) elif delete_set in from_diff: from_diff.remove(delete_set) if isdiff or issame: TL.append(change_set) if isdiff: from_diff.append(change_set) if issame: from_same.append(change_set) else: TL.append(None) S = S_star G = G_test TL, from_same, from_diff = generate.TL_update(TL, from_same, from_diff, S) if G < G_star: G_star = G l = 0 for s in S: S_c[l][:] = s[:] l += 1 L_star = L[:] return G_star, S_c
def tabu(N,NL,S,L,items,G,lambda1,lambda2): TL = [None]*NL from_same = [] from_diff = [] pairs = generate.pairsets(L) G_star = G L_star = L[:] S_c = [] S_ori = [] for s in S: S_ori.append(s[:]) S_c.append(s[:]) G_test = 0 for k in xrange(N): issame = 0 isdiff = 0 out = 0 s = random.choice(pairs) move = 0 job = list(s) a_L_index = max(L.index(job[0]),L.index(job[1])) b_L_index = min(L.index(job[0]),L.index(job[1])) a = L[a_L_index] b = L[b_L_index] l_a = generate.find_job(a,S) l_b = generate.find_job(b,S) a_idx = S[l_a].index(a) b_idx = S[l_b].index(b) if s not in TL: if l_a == l_b: # a <--> b S_temp = same_S(S,l_a,a_idx,b_idx) G_temp = test_G(items,S_temp,lambda1,lambda2) else: # a -> b's S_temp_1 = diff1_S(S,l_a,l_b,a,b_idx) G_temp_1 = test_G(items,S_temp_1,lambda1,lambda2) # b -> a's S_temp_2 = diff2_S(S,l_a,l_b,b,a_idx) G_temp_2 = test_G(items,S_temp_2,lambda1,lambda2) # a <--> b S_temp_3 = diff3_S(S,l_a,l_b,a,b,a_idx,b_idx) G_temp_3 = test_G(items,S_temp_3,lambda1,lambda2) G_temp = min(G_temp_1,G_temp_2,G_temp_3) elif s in from_same: continue elif s in from_diff: # a -> b's S_temp_1 = diff1_S(S,l_a,l_b,a,b_idx) G_temp_1 = test_G(items,S_temp_1,lambda1,lambda2) # b -> a's S_temp_2 = diff2_S(S,l_a,l_b,b,a_idx) G_temp_2 = test_G(items,S_temp_2,lambda1,lambda2) G_temp = min(G_temp_1,G_temp_2) if G_test == 0 or G_temp < G_test: move = 1 G_test = G_temp a_temp,b_temp = a,b l_a_temp,l_b_temp = l_a,l_b if l_a == l_b: S_star = S_temp issame,isdiff = 1,0 out = 0 else: if s in TL: out = 1 issame,isdiff = 0,0 if G_temp == G_temp_1: S_star = S_temp_1 elif G_temp == G_temp_2: S_star = S_temp_2 else: out = 0 if G_temp == G_temp_1: S_star = S_temp_1 issame,isdiff = 0,0 elif G_temp == G_temp_2: S_star = S_temp_2 issame,isdiff = 0,0 elif G_temp == G_temp_3: S_star = S_temp_3 issame,isdiff = 0,1 if move == 0: continue L = generate.innerswap(L,L.index(a_temp),L.index(b_temp)) pairs = generate.pairsets(L) change_set = set([a_temp,b_temp]) if out: TL[TL.index(change_set)] = None from_diff.remove(change_set) delete_set = TL.pop(0) if delete_set in from_same: from_same.remove(delete_set) elif delete_set in from_diff: from_diff.remove(delete_set) if isdiff or issame: TL.append(change_set) if isdiff: from_diff.append(change_set) if issame: from_same.append(change_set) else: TL.append(None) S = S_star G = G_test TL,from_same,from_diff = generate.TL_update(TL,from_same,from_diff,S) if G < G_star: G_star = G l =0 for s in S: S_c[l][:] = s[:] l += 1 L_star = L[:] return G_star,S_c