master > master: code py - display verbessert

This commit is contained in:
RD 2022-06-14 01:53:48 +02:00
parent ea36c82728
commit 026cd6addf
3 changed files with 43 additions and 21 deletions

View File

@ -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

View File

@ -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'}) \

View File

@ -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) ]);