def branch_and_bound(entrada): global length_of_x, conjuntos_de_x, coeficientes length_of_x, conjuntos_de_x, coeficientes = load.loadFileEx(entrada) #print(conjuntos_de_x) initial_x_set = create_initial_x_set(length_of_x) array_de_nos = create_initial_array_de_nos() max_value = create_initial_max_value() max_set = [] x = 1 last_x = length_of_x while (x <= last_x) and (len(array_de_nos) > 0): #print ("x atual e " + str(x) + " | max value e " + str(max_value) + " | set size e " + str(len(array_de_nos))) novo_set = [] for set in array_de_nos: set_inverted = copy.deepcopy(set) if (set_inverted[x] == 1): set_inverted[x] = 0 else: set_inverted[x] = 1 novo_set.append(set_inverted) same_set = copy.deepcopy(set) novo_set.append(same_set) array_de_nos = novo_set for set in array_de_nos: f_result = bb.resultado_de_soma(set, conjuntos_de_x, coeficientes, length_of_x) max_value = max(max_value, f_result) #print(str(set) + " " + str(f_result) + " " + str(max_value)) #print(max_set) #print(set) if max_value == f_result: #print("Substitute") max_set = copy.deepcopy(set) #print("new max_set") #print(max_set) #print("---------------------------------") array_de_nos = apply_bound(array_de_nos) x = x + 1 #print(max_set) #print(bb.resultado_de_soma(max_set, conjuntos_de_x, coeficientes,length_of_x)) return max_set, bb.resultado_de_soma(max_set, conjuntos_de_x, coeficientes, length_of_x)
def branch_and_bound(): initial_x_set = create_initial_x_set(x_size) array_de_nos = [] array_de_nos.append(initial_x_set) relax_function_value = relax_function(conjuntos_de_x, coeficientes) max_value = relax_function_value max_set = [] x = 0 last_x = length_of_x-1 while(x <= last_x) and (len(array_de_nos) > 0): novo_set = [] for set in array_de_nos: set_with = set set[x] = 1 novo_set.append(set_with) set_without = set novo_set.append(set_without) array_de_nos = novo_set for set in array_de_nos: f_result = bb.resultado_de_soma(set, conjuntos_de_x, coeficientes, length_of_x) max_value = max(max_value, f_result) #print(max_value) #print(f_result) #print(max_set) #print(set) if max_value == f_result: #print("Substitute") max_set = copy.deepcopy(set) #print("new max_set") #print(max_set) #print("---------------------------------") length_set = len(array_de_nos) i = 0 #da pra melhorar essa parte para nao ter que fazer DOIS calculos seguindos while (i < len(array_de_nos)): if (bb.resultado_de_soma(array_de_nos[i], conjuntos_de_x, coeficientes, length_of_x) < max_value): array_de_nos.remove(array_de_nos[i]) else: i = i+1 x = x+1 print(max_set) print(bb.resultado_de_soma(max_set, conjuntos_de_x, coeficientes,length_of_x))
def branch_and_bound(): print(length_of_x) initial_x_set = create_initial_x_set(length_of_x) array_de_nos = create_initial_array_de_nos() max_value = create_initial_max_value() max_set = [] x = 1 last_x = length_of_x while (x <= last_x) and (len(array_de_nos) > 0): print("x atual e " + str(x)) novo_set = [] print(x) for set in array_de_nos: set_with = copy.deepcopy(set) set_with[x] = 1 novo_set.append(set_with) set_without = copy.deepcopy(set) novo_set.append(set_without) array_de_nos = novo_set for set in array_de_nos: f_result = bb.resultado_de_soma(set, conjuntos_de_x, coeficientes, length_of_x) max_value = max(max_value, f_result) print(max_value) #print(f_result) #print(max_set) #print(set) if max_value == f_result: #print("Substitute") max_set = copy.deepcopy(set) #print("new max_set") #print(max_set) #print("---------------------------------") array_de_nos = apply_bound(array_de_nos) x = x + 1 print(max_set) print( bb.resultado_de_soma(max_set, conjuntos_de_x, coeficientes, length_of_x))
def apply_bound(array_de_nos): quant = len(array_de_nos) i = 0 soma = 0 while (i < quant): soma += bb.resultado_de_soma(array_de_nos[i], conjuntos_de_x, coeficientes, length_of_x) i += 1 media = soma / quant i = 0 while (i < quant): if bb.resultado_de_soma(array_de_nos[i], conjuntos_de_x, coeficientes, length_of_x) < media: del array_de_nos[i] quant = len(array_de_nos) else: i += 1 return array_de_nos
def branch_and_bound(): print(length_of_x) initial_x_set = create_initial_x_set(length_of_x) array_de_nos = [] array_de_nos.append(initial_x_set) relax_function_value = relax_function(conjuntos_de_x, coeficientes) max_value = relax_function_value max_set = [] x = 1 last_x = length_of_x initial_bound = -1000 max_quantidade_de_x = 4 while(x <= last_x) and (len(array_de_nos) > 0): novo_set = [] print(x) for set in array_de_nos: set_with = copy.deepcopy(set) set_with[x] = 1 novo_set.append(set_with) set_without = copy.deepcopy(set) novo_set.append(set_without) array_de_nos = novo_set for set in array_de_nos: f_result = bb.resultado_de_soma(set, conjuntos_de_x, coeficientes, length_of_x) max_value = max(max_value, f_result) #print(max_value) #print(f_result) #print(max_set) #print(set) if max_value == f_result: print("Substitute") max_set = copy.deepcopy(set) print("new max_set") print(max_set) print("---------------------------------") length_set = len(array_de_nos) i = 0 #da pra melhorar essa parte para nao ter que fazer DOIS calculos seguindos while (i < len(array_de_nos)): if (bb.resultado_de_soma(array_de_nos[i], conjuntos_de_x, coeficientes, length_of_x) < initial_bound): print(bb.resultado_de_soma(array_de_nos[i], conjuntos_de_x, coeficientes, length_of_x)) array_de_nos.remove(array_de_nos[i]) initial_bound = initial_bound+1 elif (quant_of_x(array_de_nos[i]) > max_quantidade_de_x): print(array_de_nos[i]) #array_de_nos.remove(array_de_nos[i]) del array_de_nos[i] else: i = i+1 x = x+1 print(max_set) print(bb.resultado_de_soma(max_set, conjuntos_de_x, coeficientes,length_of_x))