def neh_plus(N, m): k = 1 W = [[sum(x), x] for x in N] pi = [] pi_star = [] W.sort(key=lambda x: x[0]) while len(W) != 0: j = W[len(W) - 1][1] pi_star.append(j) for l in range(k): pi.insert(l, j) if FSPService.loss_function(pi, m) < FSPService.loss_function(pi_star, m): pi_star = cp.copy(pi) del pi[l] pi = cp.copy(pi_star) del W[len(W) - 1] if k > 1: x = NEH.select_x(pi, m, j) pi.remove(x) for l in range(k): pi.insert(l, x) if FSPService.loss_function(pi, m) < FSPService.loss_function(pi_star, m): pi_star = cp.copy(pi) del pi[l] pi = cp.copy(pi_star) k += 1 return FSPService.loss_function(pi_star, m), pi_star
def bound_1(Pi, N, m, data) -> int: max_val = -1 E = [sum(col) for col in zip(*N)] for i in range(1, m + 1): Ci = service.loss_function(Pi, i) if Ci + E[i - 1] > max_val: max_val = Ci + E[i - 1] return max_val
def neh(N, m): k = 1 W = [[sum(x), x] for x in N] pi = [] pi_star = [] W.sort(key=lambda x: x[0]) while len(W) != 0: j = W[len(W) - 1][1] pi_star.append(j) for l in range(0, k): pi.insert(l, j) if FSPService.loss_function(pi, m) < FSPService.loss_function(pi_star, m): pi_star = cp.copy(pi) del pi[l] pi = cp.copy(pi_star) k += 1 del W[len(W) - 1] return FSPService.loss_function(pi_star, m), pi
def recursive(S, C_max, leaf, best, m): if len(S) > 0: for i in S: s = copy.copy(S) s.remove(i) l = copy.copy(leaf) l.append(i) recursive(s, C_max, l, best, m) else: C_tmp = FSPService.loss_function(leaf, m) if C_max[0] > C_tmp: C_max[0] = C_tmp best[0] = leaf
def select_x(pi: [], m, j): x = None C_max = math.inf for i in range(len(pi)): if pi[i] == j: continue tmp_task = pi[i] del pi[i] tmp_C_max = FSPService.loss_function(pi, m) if tmp_C_max < C_max: C_max = tmp_C_max x = tmp_task pi.insert(i, tmp_task) return x
def procedure(J, n, m, L, T, reduce_temp_fcn, arg_reduce_temp, init_solution_fcn, move_fcn): T_end = 0.05 pi_new = None pi_star = init_solution_fcn(J, m) pi = init_solution_fcn(J, m) while T > T_end: for k in range(L): i = random.randint(0, n - 1) j = random.randint(0, n - 1) pi_new = move_fcn(pi_star, i, j) delta_C_max = FSPService.loss_function( pi, m) - FSPService.loss_function(pi_new, m) if delta_C_max > 0: r = random.uniform(0, 1) if r >= np.e**(delta_C_max / T): pi_new = cp.copy(pi) pi = pi_new if FSPService.loss_function(pi, m) < FSPService.loss_function( pi_star, m): pi_star = cp.copy(pi) T = reduce_temp_fcn(T, arg_reduce_temp) print(FSPService.loss_function(pi, m))
def BnB(j, N_, Pi_): Pi = copy.deepcopy(Pi_) N = copy.deepcopy(N_) Pi.append(j) N.remove(j) if len(N) > 0: LB = lb(Pi, N, m, data) pass if LB <= UB[0]: for i in N: BnB(i, N, Pi) else: Cmax = service.loss_function(Pi, m) if Cmax < UB[0]: UB[0] = Cmax Pi_star[0] = Pi
def bound_3(Pi, N, m, data): max_val = -1 E = [sum(col) for col in zip(*N)] for i in range(1, m + 1): Ci = service.loss_function(Pi, i) sum_k = 0 for k in range(i, m): min_val = math.inf for j in range(len(N)): if N[j][k] < min_val: min_val = N[j][k] sum_k += min_val if Ci + E[i - 1] + sum_k > max_val: max_val = Ci + E[i - 1] + sum_k return max_val
def bound_4(Pi, N, m, data): max_val = -1 E = [sum(col) for col in zip(*N)] for i in range(m): Ci = service.loss_function(Pi, i + 1) min_val = math.inf # sum_k = None for j in range(len(N)): sum_k = 0 for k in range(i + 1, m): sum_k += N[j][k] min_val = min(min_val, sum_k) if Ci + E[i] + min_val > max_val: max_val = Ci + E[i] + min_val return max_val
def procedure(self): population = self.generate_start_population(self.n) for j in range(self.epoch): new_population = [] + cp.copy(population) parents = Genetic.selection(population) # selection for pair in parents: child = Genetic.crossover(pair[1][0], pair[0][0]) new_population.append([child, self.calc_value(child)]) # crossover random_mutation_quantity = random.randint(1, 5) for i in range(random_mutation_quantity): mutant = Genetic.mutation(random.choice(population)[0]) new_population.append([mutant, self.calc_value(mutant)]) new_population.sort(key=lambda x: -x[1]) population = new_population[:len(population)] return FSPService.loss_function(population[0][0], self.m), population[0][0]
def ub_2(data, m): return service.loss_function(data, m)
BnB(i, N, Pi) else: Cmax = service.loss_function(Pi, m) if Cmax < UB[0]: UB[0] = Cmax Pi_star[0] = Pi Pi = [] for j in N: BnB(j, N, Pi) return UB, Pi_star if __name__ == '__main__': _, m, data = service.read_data( 'D:\Programming\python\SPD\\fsp\data\\data002.txt') Lb_functions = [ BranchAndBounds.bound_1, BranchAndBounds.bound_2, BranchAndBounds.bound_3, BranchAndBounds.bound_4 ] Ub_functions = [BranchAndBounds.ub_1, BranchAndBounds.ub_2] for lb in Lb_functions: for Ub_functions in Ub_functions: """ Kombinacja funkcji [Cmax], [perm] = BranchAndBounds.find_c_max(data, m, ub, lb) print(Cmax) """ pass
def calc_value(self, J): return 1 / (FSPService.loss_function(J, self.m) / self.normalizer)
def __init__(self, path_to_data, epoch): self.n, self.m, self.data = NEHService.read_data_by_rows(path_to_data) self.epoch = epoch self.normalizer = FSPService.loss_function(self.data, self.m) / 2
return T - x @staticmethod def reduce_temperature_geometric(T, alpha): return T * alpha @staticmethod def reduce_temperature_log(T, it): b = it + 1 return T / np.log(b) if __name__ == '__main__': filepath = 'D:\Programming\python\SPD\\neh\dane\\ta003.txt' n, m, data = NEHService.read_data_by_rows(filepath) natural_c_max = FSPService.loss_function(data, m) print(f'natural Cmax = {natural_c_max}') L = [10**2, 10**3, 10**4] T_0 = [math.sqrt(n), n, n**2] temp_reduce_fcn_arr = [ SimulatedAnnealing.reduce_temperature_linear, SimulatedAnnealing.reduce_temperature_geometric, SimulatedAnnealing.reduce_temperature_log ] x = [T_0[0] / (10**3), T_0[0] / (10**4), T_0[0] / (10**5)] alpha = [.97, .95, .9] init_solution_fcn_arr = [ SimulatedAnnealing.init_solution_1, SimulatedAnnealing.init_solution_2, SimulatedAnnealing.init_solution_3 ] move_fcn_arr = [
class BruteForce: @staticmethod def find_c_max(data, m): def recursive(S, C_max, leaf, best, m): if len(S) > 0: for i in S: s = copy.copy(S) s.remove(i) l = copy.copy(leaf) l.append(i) recursive(s, C_max, l, best, m) else: C_tmp = FSPService.loss_function(leaf, m) if C_max[0] > C_tmp: C_max[0] = C_tmp best[0] = leaf tmp = [math.inf] leafs = [] perm = [[]] recursive(data, tmp, leafs, perm, m) return tmp[0], perm[0] _, m, data = FSPService.read_data( 'D:\Programming\python\SPD\\fsp\data\data003.txt') Cmax, perm = BruteForce.find_c_max(data, m) print(Cmax) print(perm)