master > master: code py - darstellung

- greedy permutation in Tabelle musste invertiert werden
- Aktualisierung der bound musste beim Loggin erscheinen werden
- value/cost zwecks leichter Vergleichbarkeit als Dezimalzahlen darstellen
This commit is contained in:
RD 2022-06-16 12:50:04 +02:00
parent ba394993e0
commit efacd73e51
2 changed files with 13 additions and 11 deletions

View File

@ -131,9 +131,18 @@ def rucksack_branch_and_bound_algorithm(
# top-Element auslesen und Bound berechnen:
A: Mask = S.top();
bound_subtree, choice, order_, pad = estimate_lower_bound(mask=A, max_cost=max_cost, costs=costs, values=values, items=items);
# für logging:
# für logging (irrelevant für Algorithmus):
if verbose:
step = Step(bound=bound, bound_subtree=bound_subtree, stack_str=str(S), choice=choice, order=order_, indexes=A.indexes_unset, pad=pad);
if bound_subtree < bound:
if not A.splittable() or pad != MaskValue.UNSET:
step.move = EnumBranchAndBoundMove.BOUND;
step.bound = bound_subtree;
else:
step.move = EnumBranchAndBoundMove.BRANCH;
logged_steps.append(step);
S.pop();
# Update nur nötig, wenn die (eingeschätzte) untere Schranke von A das bisherige Minimum verbessert:
if bound_subtree < bound:
@ -144,9 +153,6 @@ def rucksack_branch_and_bound_algorithm(
if pad != MaskValue.UNSET:
A = A.pad(pad);
mask = A;
# für logging:
if verbose:
step.move = EnumBranchAndBoundMove.BOUND;
# Branch sonst
else:
B, C = A.split();
@ -154,11 +160,6 @@ def rucksack_branch_and_bound_algorithm(
# Nur dann C auf Stack legen, wenn mind. eine Möglichkeit in C die Kapazitätsschranke erfüllt:
if sum(costs[C.indexes_one]) <= max_cost:
S.push(C);
# für logging:
if verbose:
step.move = EnumBranchAndBoundMove.BRANCH;
if verbose:
logged_steps.append(step);
# Aspekte der Lösung speichern
rucksack = mask.indexes_one; # Indexes von Items im Rucksack

View File

@ -9,6 +9,7 @@ from src.thirdparty.code import *;
from src.thirdparty.maths import *;
from src.thirdparty.types import *;
from src.core.utils import *;
from src.setup import config;
from models.generated.config import *;
from src.models.stacks import *;
@ -38,10 +39,10 @@ def display_order(
) -> str:
table = pd.DataFrame({
'items': items,
'order': order,
'order': iperm(order),
'values': values,
'costs': costs,
'margin': [str(Fraction(Fraction(value), Fraction(cost))) for cost, value in zip(costs, values)],
'margin': [f'{value/cost:.6f}' for cost, value in zip(costs, values)],
}) \
.reset_index(drop=True);
if one_based: