2022-06-14 01:35:10 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# IMPORTS
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
from src.thirdparty.types import *;
|
|
|
|
from src.thirdparty.maths import *;
|
|
|
|
|
|
|
|
from src.models.stacks import *;
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# EXPORTS
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
__all__ = [
|
2022-06-14 01:53:48 +02:00
|
|
|
'display_sum',
|
2022-06-14 01:35:10 +02:00
|
|
|
'display_branch_and_bound',
|
|
|
|
];
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# METHODS display
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2022-06-14 01:53:48 +02:00
|
|
|
def display_sum(
|
2022-06-14 01:35:10 +02:00
|
|
|
vector: Union[List[int], List[float]],
|
|
|
|
values: np.ndarray,
|
|
|
|
) -> str:
|
|
|
|
value = sum([ u*x for u, x in zip(vector,values)]);
|
|
|
|
expr = '+'.join([
|
|
|
|
f'{x:g}' if u == 1 else f'{Fraction(str(u))}·{x:g}'
|
|
|
|
for u, x in zip(vector,values) if u > 0
|
|
|
|
]);
|
|
|
|
return f'{value:g} (={expr})';
|
|
|
|
|
|
|
|
def display_branch_and_bound(
|
|
|
|
values: np.ndarray,
|
|
|
|
steps: List[Tuple[float, float, Stack]]
|
|
|
|
) -> str:
|
|
|
|
# füge Summen-Ausdrücke für Greedy-Alg hinzu:
|
|
|
|
rows = [];
|
|
|
|
used_vectors = [];
|
|
|
|
for lb_estimate, lb, u, S in steps:
|
|
|
|
if u in used_vectors:
|
|
|
|
rows.append((f'{lb_estimate:g}', f'{lb:g}', S));
|
|
|
|
else:
|
|
|
|
used_vectors.append(u)
|
2022-06-14 01:53:48 +02:00
|
|
|
rows.append((f'{lb_estimate:g}', display_sum(vector=u, values=values), S));
|
2022-06-14 01:35:10 +02:00
|
|
|
|
|
|
|
table = pd.DataFrame(rows) \
|
|
|
|
.rename(columns={0: 'b', 1: 'g(TOP(S))', 2: 'S'}) \
|
|
|
|
.reset_index(drop=True);
|
|
|
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
|
|
|
repr = tabulate(
|
|
|
|
pd.DataFrame(table),
|
|
|
|
headers=['b', 'g(TOP(S))', 'S'],
|
|
|
|
showindex=False,
|
|
|
|
colalign=('left', 'left', 'right'),
|
|
|
|
tablefmt='rst'
|
|
|
|
);
|
|
|
|
return repr;
|