124 lines
4.0 KiB
Python
124 lines
4.0 KiB
Python
#!/usr/bin/env python3
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# IMPORTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
from src.thirdparty.types import *;
|
|
from src.thirdparty.maths import *;
|
|
|
|
from src.hirschberg.constants import *;
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# EXPORTS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
__all__ = [
|
|
'represent_cost_matrix',
|
|
'display_cost_matrix',
|
|
'display_cost_matrix_halves',
|
|
];
|
|
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
# METHODS
|
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
|
|
def represent_cost_matrix(
|
|
Costs: np.ndarray, # NDArray[(Any, Any), int],
|
|
path: List[Tuple[int, int]],
|
|
X: str,
|
|
Y: str,
|
|
verb: VerboseMode,
|
|
pad: bool = False,
|
|
) -> np.ndarray: # NDArray[(Any, Any), Any]:
|
|
m = len(X); # display vertically
|
|
n = len(Y); # display horizontally
|
|
|
|
# erstelle string-Array:
|
|
if pad:
|
|
table = np.full(shape=(3 + m + 3, 3 + n + 1), dtype=object, fill_value='');
|
|
else:
|
|
table = np.full(shape=(3 + m, 3 + n), dtype=object, fill_value='');
|
|
|
|
# topmost rows:
|
|
table[0, 3:(3+n)] = [str(j) for j in range(n)];
|
|
table[1, 3:(3+n)] = [y for y in Y];
|
|
table[2, 3:(3+n)] = '--';
|
|
# leftmost columns:
|
|
table[3:(3+m), 0] = [str(i) for i in range(m)];
|
|
table[3:(3+m), 1] = [x for x in X];
|
|
table[3:(3+m), 2] = '|';
|
|
|
|
if pad:
|
|
table[-3, 3:(3+n)] = '--';
|
|
table[3:(3+m), -1] = '|';
|
|
|
|
match verb:
|
|
case VerboseMode.MOVES:
|
|
table[3:(3+m), 3:(3+n)] = '\x1b[2m.\x1b[0m';
|
|
for (i, j) in path:
|
|
table[3 + i, 3 + j] = '\x1b[31;1m*\x1b[0m';
|
|
case VerboseMode.COSTS | VerboseMode.COSTS_AND_MOVES:
|
|
table[3:(3+m), 3:(3+n)] = Costs.copy();
|
|
if verb == VerboseMode.COSTS_AND_MOVES:
|
|
for (i, j) in path:
|
|
table[3 + i, 3 + j] = f'\x1b[31;4;1m{table[3 + i, 3 + j]}\x1b[0m';
|
|
|
|
return table;
|
|
|
|
def display_cost_matrix(
|
|
Costs: np.ndarray, # NDArray[(Any, Any), int],
|
|
path: List[Tuple[int, int]],
|
|
X: str,
|
|
Y: str,
|
|
verb: VerboseMode,
|
|
) -> str:
|
|
'''
|
|
Zeigt Kostenmatrix + optimalen Pfad.
|
|
|
|
@inputs
|
|
- `Costs` - Kostenmatrix
|
|
- `Moves` - Kodiert die optimalen Schritte
|
|
- `X`, `Y` - Strings
|
|
|
|
@returns
|
|
- eine 'printable' Darstellung der Matrix mit den Strings X, Y + Indexes.
|
|
'''
|
|
table = represent_cost_matrix(Costs=Costs, path=path, X=X, Y=Y, verb=verb);
|
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
|
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
|
return repr;
|
|
|
|
def display_cost_matrix_halves(
|
|
Costs1: np.ndarray, # NDArray[(Any, Any), int],
|
|
Costs2: np.ndarray, # NDArray[(Any, Any), int],
|
|
path1: List[Tuple[int, int]],
|
|
path2: List[Tuple[int, int]],
|
|
X1: str,
|
|
X2: str,
|
|
Y1: str,
|
|
Y2: str,
|
|
verb: VerboseMode,
|
|
) -> str:
|
|
'''
|
|
Zeigt Kostenmatrix + optimalen Pfad für Schritt im D & C Hirschberg-Algorithmus
|
|
|
|
@inputs
|
|
- `Costs1`, `Costs2` - Kostenmatrizen
|
|
- `Moves1`, `Moves2` - Kodiert die optimalen Schritte
|
|
- `X1`, `X2`, `Y1`, `Y2` - Strings
|
|
|
|
@returns
|
|
- eine 'printable' Darstellung der Matrix mit den Strings X, Y + Indexes.
|
|
'''
|
|
table1 = represent_cost_matrix(Costs=Costs1, path=path1, X=X1, Y=Y1, verb=verb, pad=True);
|
|
table2 = represent_cost_matrix(Costs=Costs2, path=path2, X=X2, Y=Y2, verb=verb, pad=True);
|
|
|
|
# merge Taellen:
|
|
table = np.concatenate([table1[:, :-1], table2[::-1, ::-1]], axis=1);
|
|
|
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
|
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
|
return repr;
|