-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
138 lines (108 loc) · 3.48 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/usr/bin/python3
import random, datetime
'''
function dp(price, n):
input: array of price and n length of rod
output: max profit and cutting point
1. DP <- array[n+1] of integer
2. for i <- 1 to n+1 do
3. for j <- 0 to i do
4. maxPrice <- getMaxPrice(i, j) // get max price from cutting point
5. endfor
6. DP[i] <- maxPrice
7. endfor
8. return last element of DP array
function bruteforce(price, n):
kamus:
bestPrice : integer
temporaryPrice : integer
i, j : integer
algoritma:
1. bestPrice <- 0
2.
3. for i <- 0 to 2^(n-1) do
4. for j <- 0 to n-1 do
5. temporaryPrice <- doCalculateCuttingPoint(i, j)
6. endfor
7. if temporaryPrice > bestPrice then
8. bestPrice <- temporaryPrice
9. endif
10. endfor
11. return bestPrice
'''
def dynamic_programming(price, n, verbose = False):
dp_vals = [0] * (n + 1)
dp_rods = [[] for _ in range(n + 1)]
if verbose: print()
for i in range(1, n+1):
max_val = -1
best_cut = None
# print(f'dp[{i}] = max {{')
for j in range(i):
# print(f'\tprice[{j}] + dp[{i-j-1}] = {price[j]} + {dp_vals[i-j-1]} = {price[j] + dp_vals[i-j-1]}')
if price[j] + dp_vals[i-j-1] > max_val:
max_val = price[j] + dp_vals[i-j-1]
best_cut = j
dp_vals[i] = max_val
dp_rods[i] = dp_rods[i - best_cut - 1] + [best_cut + 1]
# print('}')
# print(f'dp[{i}] = {max_val}')
# print(f'dp = {dp_vals}')
if verbose:
print(f'{i:>3}', end = ' | ')
for val in dp_vals:
print(f'{val:>2} ', end = '')
print()
if verbose: print()
return dp_vals[n], dp_rods[n]
def bruteforce(price, n, verbose = False):
best_value = 0
best_cuts = []
if verbose: print()
for i in range(2 ** (n - 1)):
tmp_value = 0
tmp_cut = 0
tmp_rod = []
for j in range(n - 1):
if (i >> j) & 1:
tmp_value += price[tmp_cut]
tmp_rod.append(tmp_cut + 1)
tmp_cut = 0
else:
tmp_cut += 1
tmp_value += price[tmp_cut]
tmp_rod.append(tmp_cut + 1)
if tmp_value > best_value:
best_value = tmp_value
best_cuts = tmp_rod
if verbose:
print(f'{i:>3}', end = ' | ')
for i in range(len(tmp_rod)):
print(f'{tmp_rod[i]:>2} ', end = '')
print()
if verbose: print()
return best_value, best_cuts
price = [1, 5, 8, 9, 10, 17, 17, 20, 21, 24, 30, 35, 40, 43, 50, 55, 60, 66, 70, 74]
rod_length = 8
verbose = True
print("- Data -")
print(f"Price: {price}")
print(f"N: {rod_length}" )
print()
print("- Dynamic Programming -")
start = datetime.datetime.now()
max_value, cuts = dynamic_programming(price, rod_length, verbose=verbose)
end = datetime.datetime.now()
time_execution = (end - start).microseconds
print(f"Max profit: {max_value}")
print(f"Solution: {cuts}")
print(f"Time execution: {time_execution} ms")
print()
print("- Bruteforce -")
start = datetime.datetime.now()
max_value, cuts = bruteforce(price, rod_length, verbose=verbose)
end = datetime.datetime.now()
time_execution = (end - start).microseconds
print(f"Max profit: {max_value}")
print(f"Solution: {cuts}")
print(f"Time execution: {time_execution} ms")