From 026cd6addfd3ac8223b8bb0add9fd4ed0e67f9df Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Tue, 14 Jun 2022 01:53:48 +0200 Subject: [PATCH] master > master: code py - display verbessert --- .../src/algorithms/rucksack/algorithms.py | 54 ++++++++++++------- .../python/src/algorithms/rucksack/display.py | 6 +-- code/python/src/models/rucksack/solution.py | 4 ++ 3 files changed, 43 insertions(+), 21 deletions(-) diff --git a/code/python/src/algorithms/rucksack/algorithms.py b/code/python/src/algorithms/rucksack/algorithms.py index deb91aa..a72196c 100644 --- a/code/python/src/algorithms/rucksack/algorithms.py +++ b/code/python/src/algorithms/rucksack/algorithms.py @@ -62,23 +62,31 @@ def rucksack_greedy_algorithm( vector[i] = (capacity - weight_total)/weights[i]; break; - # verbose output hier behandeln (irrelevant für Algorithmus): - if verbose: - repr = display_greedy(vector=vector, values=values); - print(''); - print('\x1b[1mRucksack Problem - Greedy\x1b[0m'); - print(''); - print(repr); - print(''); - - # Lösung ausgeben - return Solution( + # Aspekte der Lösung speichern: + soln = Solution( vector = vector, items = items[rucksack].tolist(), weights = weights[rucksack].tolist(), values = values[rucksack].tolist(), ); + # verbose output hier behandeln (irrelevant für Algorithmus): + if verbose: + expr_value = display_sum(vector=soln.vector, values=values); + expr_weight = display_sum(vector=soln.vector, values=weights); + print(''); + print('\x1b[1mRucksack Problem - Greedy\x1b[0m'); + print(''); + print(f'Rucksack: {", ".join(soln.items)}.'); + if fractional: + print(f'Vector: {soln.vector_support}'); + print(f'max. Value ≈ {expr_value}'); + print(f'∑ Weights = {expr_weight}'); + print(''); + + # Lösung ausgeben + return soln; + # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # METHOD branch and bound algorithm # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -122,23 +130,33 @@ def rucksack_branch_and_bound_algorithm( lb_estimate = lb; vector = A; + # Aspekte der Lösung speichern + rucksack = vector.indexes_one; # Indexes von Items im Rucksack + soln = Solution( + vector = vector.decision, + items = items[rucksack].tolist(), + values = values[rucksack].tolist(), + weights = weights[rucksack].tolist(), + ); + # verbose output hier behandeln (irrelevant für Algorithmus): if verbose: + expr_value = display_sum(vector=soln.vector, values=values); + expr_weight = display_sum(vector=soln.vector, values=weights); repr = display_branch_and_bound(values=values, steps=logged_steps); print(''); print('\x1b[1mRucksack Problem - Branch & Bound\x1b[0m'); print(''); print(repr); print(''); + print(f'Rucksack: {", ".join(soln.items)}.'); + print(f'max. Value ≈ {expr_value}'); + print(f'∑ Weights = {expr_weight}'); + print(''); + # Lösung ausgeben - rucksack = vector.indexes_one; # Indexes von Items im Rucksack - return Solution( - vector = vector.decision, - items = items[rucksack].tolist(), - values = values[rucksack].tolist(), - weights = weights[rucksack].tolist(), - ); + return soln; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ # AUXILIARY METHOD resort diff --git a/code/python/src/algorithms/rucksack/display.py b/code/python/src/algorithms/rucksack/display.py index 4291923..2d81c8e 100644 --- a/code/python/src/algorithms/rucksack/display.py +++ b/code/python/src/algorithms/rucksack/display.py @@ -15,7 +15,7 @@ from src.models.stacks import *; # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ __all__ = [ - 'display_greedy', + 'display_sum', 'display_branch_and_bound', ]; @@ -23,7 +23,7 @@ __all__ = [ # METHODS display # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -def display_greedy( +def display_sum( vector: Union[List[int], List[float]], values: np.ndarray, ) -> str: @@ -46,7 +46,7 @@ def display_branch_and_bound( rows.append((f'{lb_estimate:g}', f'{lb:g}', S)); else: used_vectors.append(u) - rows.append((f'{lb_estimate:g}', display_greedy(vector=u, values=values), S)); + rows.append((f'{lb_estimate:g}', display_sum(vector=u, values=values), S)); table = pd.DataFrame(rows) \ .rename(columns={0: 'b', 1: 'g(TOP(S))', 2: 'S'}) \ diff --git a/code/python/src/models/rucksack/solution.py b/code/python/src/models/rucksack/solution.py index 3679f01..269f5e3 100644 --- a/code/python/src/models/rucksack/solution.py +++ b/code/python/src/models/rucksack/solution.py @@ -34,6 +34,10 @@ class Solution(SolutionRaw): def support(self) -> List[float]: return [ i for i, v in enumerate(self.vector) if v > 0 ]; + @property + def vector_support(self) -> List[float]: + return [ v for v in self.vector if v > 0 ]; + @property def total_weight(self) -> float: return sum([ self.vector[i]*x for (i, x) in zip(self.support, self.weights) ]);