-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBnB.py
105 lines (86 loc) · 3.2 KB
/
BnB.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import time
#membuat fungsi utama
def assignment_problem(cost_matrix):
#menyimpan ukuran matrix
n = len(cost_matrix)
#menyimpan solusi / cost terbaik
min_cost = float('inf')
#fungsi menghitung batas (bound) pada simpul-simpul yang belum dipilih
def calculate_bound(cost_matrix, path, level):
bound = 0
used = [False] * n
for i in range(level):
bound += cost_matrix[path[i]][i]
used[path[i]] = True
for i in range(level, n):
min_cost = float('inf')
for j in range(n):
if not used[j] and cost_matrix[j][i] < min_cost:
min_cost = cost_matrix[j][i]
bound += min_cost
return bound
def branch_and_bound(cost_matrix):
min_cost = float('inf')
path = list(range(n))
best_path = None
#fungsi untuk eksplorasi pencarian
def backtrack(cost_matrix, path, level):
nonlocal min_cost, best_path
#Periksa level saat ini pada var n (ukuran matriks)
if level == n:
cost = sum(cost_matrix[i][path[i]] for i in range(n))
if cost < min_cost:
min_cost = cost
best_path = path.copy()
return
#batasan eksplorasi jika sudah melebihi nilai minimum / optimal
bound = calculate_bound(cost_matrix, path, level)
if bound >= min_cost:
return
for i in range(level, n):
path[level], path[i] = path[i], path[level]
backtrack(cost_matrix, path, level + 1)
path[level], path[i] = path[i], path[level]
backtrack(cost_matrix, path, 0)
return min_cost, best_path
#menyelesaikan masalah
min_cost, best_path = branch_and_bound(cost_matrix)
#menampilkan hasil
print("Cost (Biaya) :", min_cost)
print("Solusi :", [x+1 for x in best_path]) # Menampilkan indeks dimulai dari 1
def read_cost_matrix_from_file(file_path):
with open(file_path, 'r') as file:
lines = file.readlines()
cost_matrix = [[int(num) for num in line.strip().split()] for line in lines]
return cost_matrix
def read_cost_matrix_from_keyboard():
print("Masukkan ukuran matriks :")
n = int(input())
print("Masukkan matriks :")
cost_matrix = []
for _ in range(n):
row = list(map(int, input().split()))
cost_matrix.append(row)
return cost_matrix
#main program
print("\nBonjour, Selamat datang!")
print("\nPilih opsi input:")
print("1. Masukkan matriks dari keyboard")
print("2. Baca matriks dari file teks")
print(" ")
option = input("Masukkan pilihan (1 atau 2): ")
if option == "1":
cost_matrix = read_cost_matrix_from_keyboard()
start = time.time()
assignment_problem(cost_matrix)
end = time.time()
print("Waktu Eksekusi :", end - start)
elif option == "2":
file_path = input("Masukkan file path teks: ")
cost_matrix = read_cost_matrix_from_file(file_path)
start = time.time()
assignment_problem(cost_matrix)
end = time.time()
print("Waktu Eksekusi :", end - start)
else:
print("Pilihan tidak valid. Program berakhir.\n")