2022-06-14 01:35:10 +02:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# IMPORTS
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2022-06-14 09:03:29 +02:00
|
|
|
from src.thirdparty.code import *;
|
2022-06-14 01:35:10 +02:00
|
|
|
from src.thirdparty.maths import *;
|
2022-06-14 09:03:29 +02:00
|
|
|
from src.thirdparty.types import *;
|
2022-06-14 01:35:10 +02:00
|
|
|
|
|
|
|
from src.models.stacks import *;
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# EXPORTS
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
__all__ = [
|
2022-06-14 09:03:29 +02:00
|
|
|
'display_order',
|
2022-06-14 14:40:02 +02:00
|
|
|
'display_rucksack',
|
2022-06-14 01:35:10 +02:00
|
|
|
'display_branch_and_bound',
|
2022-06-14 14:40:02 +02:00
|
|
|
'display_sum',
|
2022-06-14 01:35:10 +02:00
|
|
|
];
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2022-06-14 09:03:29 +02:00
|
|
|
# METHOD display order
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
def display_order(
|
|
|
|
order: List[int],
|
2022-06-14 14:40:02 +02:00
|
|
|
costs: np.ndarray,
|
2022-06-14 09:03:29 +02:00
|
|
|
values: np.ndarray,
|
|
|
|
items: np.ndarray,
|
|
|
|
one_based: bool = False,
|
|
|
|
) -> str:
|
|
|
|
table = pd.DataFrame({
|
2022-06-14 14:40:02 +02:00
|
|
|
'items': items,
|
|
|
|
'order': order,
|
|
|
|
'values': values,
|
|
|
|
'costs': costs,
|
|
|
|
'u': (values/costs),
|
2022-06-14 10:09:21 +02:00
|
|
|
}) \
|
|
|
|
.reset_index(drop=True);
|
2022-06-14 09:03:29 +02:00
|
|
|
if one_based:
|
2022-06-14 10:09:21 +02:00
|
|
|
table['order'] += 1;
|
2022-06-14 09:03:29 +02:00
|
|
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
|
|
|
repr = tabulate(
|
2022-06-14 14:40:02 +02:00
|
|
|
table,
|
|
|
|
headers=['item', 'greedy order', 'value', 'cost', 'value/cost'],
|
2022-06-14 09:03:29 +02:00
|
|
|
showindex=False,
|
|
|
|
colalign=('left', 'center', 'center', 'center', 'right'),
|
|
|
|
tablefmt='rst'
|
|
|
|
);
|
|
|
|
return repr;
|
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
2022-06-14 14:40:02 +02:00
|
|
|
# METHOD display rucksack
|
2022-06-14 01:35:10 +02:00
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2022-06-14 14:40:02 +02:00
|
|
|
def display_rucksack(
|
|
|
|
items: np.ndarray,
|
|
|
|
costs: np.ndarray,
|
2022-06-14 01:35:10 +02:00
|
|
|
values: np.ndarray,
|
|
|
|
) -> str:
|
2022-06-14 14:40:02 +02:00
|
|
|
table = pd.DataFrame({
|
|
|
|
'items': items.tolist() + ['----', '∑'],
|
|
|
|
'costs': costs.tolist() + ['', f'\x1b[92;1m{sum(costs)}\x1b[0m'],
|
|
|
|
'values': values.tolist() + ['', f'\x1b[92;1m{sum(values)}\x1b[0m'],
|
|
|
|
});
|
|
|
|
repr = tabulate(
|
|
|
|
table,
|
|
|
|
headers=['item', 'cost', 'value'],
|
|
|
|
showindex=False,
|
|
|
|
colalign=('left', 'center', 'center'),
|
|
|
|
tablefmt='rst'
|
|
|
|
);
|
|
|
|
return repr;
|
2022-06-14 01:35:10 +02:00
|
|
|
|
2022-06-14 09:03:29 +02:00
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# METHOD display result of branch and bound
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
2022-06-14 01:35:10 +02:00
|
|
|
def display_branch_and_bound(
|
|
|
|
values: np.ndarray,
|
2022-06-14 14:40:02 +02:00
|
|
|
steps: List[Tuple[float, float, Stack, List[float], bool, bool]],
|
|
|
|
order: Optional[List[int]] = None,
|
2022-06-14 01:35:10 +02:00
|
|
|
) -> str:
|
|
|
|
# füge Summen-Ausdrücke für Greedy-Alg hinzu:
|
|
|
|
rows = [];
|
|
|
|
used_vectors = [];
|
2022-06-14 12:19:35 +02:00
|
|
|
for lb_estimate, lb, S, u, can_add_all, can_add_none in steps:
|
|
|
|
pad = '1' if can_add_all else ('0' if can_add_none else '');
|
2022-06-14 01:35:10 +02:00
|
|
|
if u in used_vectors:
|
2022-06-14 12:19:35 +02:00
|
|
|
expr = f'{lb:g}';
|
2022-06-14 01:35:10 +02:00
|
|
|
else:
|
|
|
|
used_vectors.append(u)
|
2022-06-14 14:40:02 +02:00
|
|
|
expr = display_sum(vector=u, values=values, as_maximum=False, order=order);
|
2022-06-14 12:19:35 +02:00
|
|
|
rows.append((f'{lb_estimate:g}', expr, pad, S));
|
2022-06-14 01:35:10 +02:00
|
|
|
|
|
|
|
table = pd.DataFrame(rows) \
|
2022-06-14 12:19:35 +02:00
|
|
|
.rename(columns={0: 'b', 1: 'g(TOP(S))', 2: 'pad?', 3: 'S'}) \
|
2022-06-14 01:35:10 +02:00
|
|
|
.reset_index(drop=True);
|
|
|
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
|
|
|
repr = tabulate(
|
2022-06-14 14:40:02 +02:00
|
|
|
table,
|
2022-06-14 12:19:35 +02:00
|
|
|
headers=['b', 'g(TOP(S))', 'pad?', 'S'],
|
2022-06-14 01:35:10 +02:00
|
|
|
showindex=False,
|
2022-06-14 12:19:35 +02:00
|
|
|
colalign=('left', 'left', 'center', 'right'),
|
2022-06-14 01:35:10 +02:00
|
|
|
tablefmt='rst'
|
|
|
|
);
|
|
|
|
return repr;
|
2022-06-14 14:40:02 +02:00
|
|
|
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
# METHOD display sum
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
|
|
|
|
def display_sum(
|
|
|
|
vector: List[float],
|
|
|
|
values: np.ndarray,
|
|
|
|
order: Optional[List[int]] = None,
|
|
|
|
as_maximum: bool = True,
|
|
|
|
) -> str:
|
|
|
|
parts = [ (u, x) for u, x in zip(vector, values)];
|
|
|
|
if not (order is None):
|
|
|
|
parts = [ parts[j] for j in order ];
|
|
|
|
value = sum([ u*x for u, x in parts]);
|
|
|
|
expr = '+'.join([
|
|
|
|
f'{x:g}' if u == 1 else f'{Fraction(str(u))}·{x:g}'
|
|
|
|
for u, x in parts if u > 0
|
|
|
|
]);
|
|
|
|
if as_maximum:
|
|
|
|
return f'{value:g} = {expr}';
|
|
|
|
else:
|
|
|
|
return f'-{value:g} = -({expr})';
|