Skip to content
Snippets Groups Projects
Commit a6ad45ba authored by tobiglaser's avatar tobiglaser
Browse files

Blatt 2 BruteForce fertig

parent f552fba3
No related branches found
No related tags found
No related merge requests found
File added
import numpy as np
#config:
solve_brute_force = True
solve_dynamic = False
# item:
# name weight value
weight = 1
value = 2
def dynamic(items: list, capacity: int) -> list:
num_items = len(items)
array = np.zeros(shape=(num_items+1, capacity), dtype=int)
for row, item in enumerate(items, start=1):
help_row = np.array[num_items]
for j, column in range(item[weight], capacity+1):
if item[1] >= j:
prev_weight = array[row-1]
if prev_weight + item[weight] <= column:
help_row[column] = prev_weight + item[weight]
pass #!
def traverse(items: list, max_weight: int, bits: list, level: int, depth: int, best_value, best_bits: list) -> list:
# Abbruchbedingung
weight = 0
value = 0
for i, item in enumerate(items):
if bits[i]:
weight += item[1]
value += item[2]
# teste ob wert besser
if value >= best_value[0] and weight < max_weight:
best_value[0] = value
best_bits[0] = bits.copy()
# gehe nicht tiefer
if level == depth:
# Ausgabe des Bitmusters
# print(bits)
return
# traversiere links
bits[level] = 1
traverse(items=items,
max_weight=max_weight,
bits=bits,
level=level+1,
depth=depth,
best_value=best_value,
best_bits=best_bits)
# Aufräumen
bits[level] = 0
# traversiere rechts
#bits[level] = 0
traverse(items=items,
max_weight=max_weight,
bits=bits,
level=level+1,
depth=depth,
best_value=best_value,
best_bits=best_bits)
def brute(items: list, capacity: int) -> list:
mybits = []
best_bits = []
for i in range(len(items)):
mybits.append(0)
best_bits.append([])
best_bits[0] = mybits.copy()
best_value = [0]
traverse(items=items,
max_weight=capacity,
bits=mybits,
level=0,
depth=len(items),
best_value=best_value,
best_bits=best_bits)
print(best_bits)
gewicht = 0
for i, item in enumerate(items):
if best_bits[0][i]:
gewicht += item[1]
print(item[0])
print('Gewicht: ', gewicht)
print('Wert: ', best_value[0])
def parse_data_file(path: str) -> list:
items = []
with open(file=path, mode='r') as file:
for line in file:
entries = line.split('\t')
if entries[0] != 'i':
items.append((str(entries[0]), int(entries[1]), int(entries[2])))
return items
def get_items(file_path: str = None) -> list:
if file_path == None:
return [
('map', 9, 150), ('compass', 13, 35), ('water', 153, 200), ('sandwich', 50, 160),
('glucose', 15, 60), ('tin', 68, 45), ('banana', 27, 60), ('apple', 39, 40),
('cheese', 23, 30), ('beer', 52, 10), ('suntan cream', 11, 70), ('camera', 32, 30),
('t-shirt', 24, 15), ('trousers', 48, 10), ('umbrella', 73, 40),
('waterproof trousers', 42, 70), ('waterproof overclothes', 43, 75),
('note-case', 22, 80), ('sunglasses', 7, 20), ('towel', 18, 12), ('socks', 4, 50),
('book', 30, 10) # courtesy https://rosettacode.org/wiki/Knapsack_problem/0-1
]
else:
return parse_data_file(file_path)
if __name__ == "__main__":
if solve_brute_force:
items = get_items()
brute(items, 500)
if solve_dynamic:
items = get_items("Blatt2/knapsack01.data")
dynamic(items, 10000)
\ No newline at end of file
File added
i Gewicht Wert
1 116 146
2 46 759
3 78 755
4 106 926
5 145 758
6 136 921
7 31 193
8 96 705
9 137 309
10 109 345
11 105 121
12 187 628
13 193 979
14 198 97
15 149 770
16 193 91
17 22 201
18 95 157
19 58 157
20 199 394
21 171 500
22 102 182
23 186 411
24 124 532
25 15 427
26 131 746
27 147 621
28 64 315
29 200 462
30 32 802
31 25 139
32 6 262
33 29 815
34 191 535
35 116 209
36 50 698
37 22 42
38 5 163
39 171 288
40 200 879
41 197 211
42 113 173
43 150 576
44 164 916
45 24 700
46 197 412
47 194 245
48 151 924
49 131 839
50 5 186
51 2 105
52 46 333
53 179 32
54 152 511
55 151 516
56 36 98
57 52 924
58 123 363
59 58 607
60 184 196
61 147 944
62 159 438
63 167 611
64 114 817
65 140 526
66 42 10
67 111 294
68 127 313
69 134 837
70 73 279
71 111 792
72 188 891
73 27 922
74 196 358
75 107 295
76 175 731
77 85 200
78 120 982
79 46 804
80 19 602
81 39 694
82 90 833
83 16 88
84 173 133
85 198 586
86 103 653
87 113 228
88 187 193
89 13 959
90 175 79
91 50 137
92 108 139
93 6 629
94 7 861
95 28 355
96 134 156
97 71 776
98 182 64
99 70 425
100 20 201
101 21 633
102 26 815
103 34 959
104 82 283
105 173 834
106 50 512
107 139 908
108 36 5
109 110 98
110 198 984
111 109 263
112 32 801
113 133 118
114 32 152
115 19 25
116 67 168
117 18 199
118 166 945
119 101 751
120 54 48
121 169 157
122 167 433
123 141 809
124 121 960
125 32 283
126 81 486
127 200 383
128 33 154
129 16 401
130 99 85
131 153 647
132 124 36
133 167 679
134 41 929
135 174 527
136 57 544
137 130 338
138 21 95
139 194 376
140 147 86
141 174 999
142 109 141
143 53 957
144 186 215
145 155 954
146 148 973
147 167 701
148 136 769
149 134 15
150 73 35
151 124 480
152 160 754
153 54 194
154 87 91
155 73 867
156 175 219
157 66 807
158 54 357
159 154 520
160 75 55
161 90 512
162 45 718
163 96 679
164 34 103
165 131 421
166 158 160
167 161 93
168 100 593
169 49 766
170 104 928
171 194 191
172 16 417
173 35 527
174 187 519
175 89 884
176 44 917
177 193 210
178 125 150
179 50 140
180 112 416
181 45 724
182 61 232
183 4 865
184 55 996
185 48 751
186 124 449
187 96 749
188 178 731
189 54 87
190 194 192
191 162 285
192 12 700
193 116 200
194 173 914
195 129 893
196 192 326
197 129 71
198 17 714
199 132 946
200 9 610
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment