master > master: code py - verbose/display mode als enum
This commit is contained in:
parent
b79cc24bc4
commit
67aa70edfa
@ -41,8 +41,8 @@ def enter():
|
|||||||
# verbose=True,
|
# verbose=True,
|
||||||
# );
|
# );
|
||||||
## Beispiel für Seminarwoche 10 (Blatt 9):
|
## Beispiel für Seminarwoche 10 (Blatt 9):
|
||||||
# hirschberg_algorithm_once(
|
hirschberg_algorithm_once(
|
||||||
hirschberg_algorithm(
|
# hirschberg_algorithm(
|
||||||
# Y = 'ANSPANNEN',
|
# Y = 'ANSPANNEN',
|
||||||
# X = 'ANSTRENGEN',
|
# X = 'ANSTRENGEN',
|
||||||
# Y = 'AGAT',
|
# Y = 'AGAT',
|
||||||
@ -51,8 +51,9 @@ def enter():
|
|||||||
X = 'happily ever, lol',
|
X = 'happily ever, lol',
|
||||||
# Y = 'apple',
|
# Y = 'apple',
|
||||||
# X = 'happily',
|
# X = 'happily',
|
||||||
verbose = True,
|
mode = DisplayMode.COSTS,
|
||||||
just_moves = False,
|
# mode = DisplayMode.MOVES,
|
||||||
|
# mode = DisplayMode.COSTS_AND_MOVES,
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -18,12 +18,19 @@ from src.local.maths import *;
|
|||||||
__all__ = [
|
__all__ = [
|
||||||
'hirschberg_algorithm',
|
'hirschberg_algorithm',
|
||||||
'hirschberg_algorithm_once',
|
'hirschberg_algorithm_once',
|
||||||
|
'DisplayMode'
|
||||||
];
|
];
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# CONSTANTS / SETUP
|
# CONSTANTS / SETUP
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
|
class DisplayMode(Enum):
|
||||||
|
NONE = -1;
|
||||||
|
COSTS = 0;
|
||||||
|
MOVES = 1;
|
||||||
|
COSTS_AND_MOVES = 2;
|
||||||
|
|
||||||
class Directions(Enum):
|
class Directions(Enum):
|
||||||
UNSET = -1;
|
UNSET = -1;
|
||||||
# Prioritäten hier setzen
|
# Prioritäten hier setzen
|
||||||
@ -44,14 +51,13 @@ def missmatch_penalty(x: str, y: str):
|
|||||||
def hirschberg_algorithm_once(
|
def hirschberg_algorithm_once(
|
||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
verbose: bool = False,
|
mode: DisplayMode = DisplayMode.NONE,
|
||||||
just_moves: bool = False,
|
|
||||||
) -> Tuple[str, str]:
|
) -> Tuple[str, str]:
|
||||||
Costs, Moves = compute_cost_matrix(X = '-' + X, Y = '-' + Y);
|
Costs, Moves = compute_cost_matrix(X = '-' + X, Y = '-' + Y);
|
||||||
path = reconstruct_optimal_path(Moves=Moves);
|
path = reconstruct_optimal_path(Moves=Moves);
|
||||||
word_x, word_y = reconstruct_words(X = '-' + X, Y = '-' + Y, moves=[Moves[coord] for coord in path], path=path);
|
word_x, word_y = reconstruct_words(X = '-' + X, Y = '-' + Y, moves=[Moves[coord] for coord in path], path=path);
|
||||||
if verbose:
|
if mode != DisplayMode.NONE:
|
||||||
repr = display_cost_matrix(Costs=Costs, path=path, X = '-' + X, Y = '-' + Y, just_moves=just_moves);
|
repr = display_cost_matrix(Costs=Costs, path=path, X = '-' + X, Y = '-' + Y, mode=mode);
|
||||||
print(f'\n{repr}');
|
print(f'\n{repr}');
|
||||||
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
||||||
print('');
|
print('');
|
||||||
@ -64,13 +70,12 @@ def hirschberg_algorithm_once(
|
|||||||
def hirschberg_algorithm(
|
def hirschberg_algorithm(
|
||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
verbose: bool = False,
|
mode: DisplayMode = DisplayMode.NONE,
|
||||||
just_moves: bool = False,
|
|
||||||
) -> Tuple[str, str]:
|
) -> Tuple[str, str]:
|
||||||
alignments_x, alignments_y = hirschberg_algorithm_step(X=X, Y=Y, depth=1, verbose=verbose, just_moves=just_moves);
|
alignments_x, alignments_y = hirschberg_algorithm_step(X=X, Y=Y, depth=1, mode=mode);
|
||||||
word_x = ''.join(alignments_x);
|
word_x = ''.join(alignments_x);
|
||||||
word_y = ''.join(alignments_y);
|
word_y = ''.join(alignments_y);
|
||||||
if verbose:
|
if mode != DisplayMode.NONE:
|
||||||
display_x = f'[{"][".join(alignments_x)}]';
|
display_x = f'[{"][".join(alignments_x)}]';
|
||||||
display_y = f'[{"][".join(alignments_y)}]';
|
display_y = f'[{"][".join(alignments_y)}]';
|
||||||
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
||||||
@ -85,8 +90,7 @@ def hirschberg_algorithm_step(
|
|||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
depth: int = 0,
|
depth: int = 0,
|
||||||
verbose: bool = False,
|
mode: DisplayMode = DisplayMode.NONE,
|
||||||
just_moves: bool = False,
|
|
||||||
) -> Tuple[List[str], List[str]]:
|
) -> Tuple[List[str], List[str]]:
|
||||||
n = len(Y);
|
n = len(Y);
|
||||||
if n == 1:
|
if n == 1:
|
||||||
@ -112,7 +116,7 @@ def hirschberg_algorithm_step(
|
|||||||
Costs1, Moves1 = compute_cost_matrix(X = '-' + X1, Y = '-' + Y1);
|
Costs1, Moves1 = compute_cost_matrix(X = '-' + X1, Y = '-' + Y1);
|
||||||
Costs2, Moves2 = compute_cost_matrix(X = '-' + X2, Y = '-' + Y2);
|
Costs2, Moves2 = compute_cost_matrix(X = '-' + X2, Y = '-' + Y2);
|
||||||
|
|
||||||
if verbose:
|
if mode != DisplayMode.NONE:
|
||||||
path1, path2 = reconstruct_optimal_path_halves(Costs1=Costs1, Costs2=Costs2, Moves1=Moves1, Moves2=Moves2);
|
path1, path2 = reconstruct_optimal_path_halves(Costs1=Costs1, Costs2=Costs2, Moves1=Moves1, Moves2=Moves2);
|
||||||
repr = display_cost_matrix_halves(
|
repr = display_cost_matrix_halves(
|
||||||
Costs1 = Costs1,
|
Costs1 = Costs1,
|
||||||
@ -123,7 +127,7 @@ def hirschberg_algorithm_step(
|
|||||||
X2 = '-' + X2,
|
X2 = '-' + X2,
|
||||||
Y1 = '-' + Y1,
|
Y1 = '-' + Y1,
|
||||||
Y2 = '-' + Y2,
|
Y2 = '-' + Y2,
|
||||||
just_moves = just_moves,
|
mode = mode,
|
||||||
);
|
);
|
||||||
print(f'\n\x1b[1mRekursionstiefe: {depth}\x1b[0m\n\n{repr}')
|
print(f'\n\x1b[1mRekursionstiefe: {depth}\x1b[0m\n\n{repr}')
|
||||||
|
|
||||||
@ -131,8 +135,8 @@ def hirschberg_algorithm_step(
|
|||||||
coord1, coord2 = get_optimal_transition(Costs1=Costs1, Costs2=Costs2);
|
coord1, coord2 = get_optimal_transition(Costs1=Costs1, Costs2=Costs2);
|
||||||
p = coord1[0];
|
p = coord1[0];
|
||||||
# Divide and Conquer ausführen:
|
# Divide and Conquer ausführen:
|
||||||
alignments_x_1, alignments_y_1 = hirschberg_algorithm_step(X=X[:p], Y=Y[:n], depth=depth+1, verbose=verbose, just_moves=just_moves);
|
alignments_x_1, alignments_y_1 = hirschberg_algorithm_step(X=X[:p], Y=Y[:n], depth=depth+1, verbose=verbose, mode=mode);
|
||||||
alignments_x_2, alignments_y_2 = hirschberg_algorithm_step(X=X[p:], Y=Y[n:], depth=depth+1, verbose=verbose, just_moves=just_moves);
|
alignments_x_2, alignments_y_2 = hirschberg_algorithm_step(X=X[p:], Y=Y[n:], depth=depth+1, verbose=verbose, mode=mode);
|
||||||
|
|
||||||
# Resultate zusammensetzen:
|
# Resultate zusammensetzen:
|
||||||
alignments_x = alignments_x_1 + alignments_x_2;
|
alignments_x = alignments_x_1 + alignments_x_2;
|
||||||
@ -355,8 +359,9 @@ def represent_cost_matrix(
|
|||||||
path: List[Tuple[int, int]],
|
path: List[Tuple[int, int]],
|
||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
|
mode: DisplayMode,
|
||||||
pad: bool = False,
|
pad: bool = False,
|
||||||
) -> Tuple[NDArray[(Any, Any), Any], NDArray[(Any, Any), Any]]:
|
) -> NDArray[(Any, Any), Any]:
|
||||||
m = len(X); # display vertically
|
m = len(X); # display vertically
|
||||||
n = len(Y); # display horizontally
|
n = len(Y); # display horizontally
|
||||||
|
|
||||||
@ -379,22 +384,25 @@ def represent_cost_matrix(
|
|||||||
table[-3, 3:(3+n)] = '--';
|
table[-3, 3:(3+n)] = '--';
|
||||||
table[3:(3+m), -1] = '|';
|
table[3:(3+m), -1] = '|';
|
||||||
|
|
||||||
table_costs = table.copy();
|
match mode:
|
||||||
table_moves = table.copy();
|
case DisplayMode.MOVES:
|
||||||
table_costs[3:(3+m), 3:(3+n)] = Costs.copy();
|
table[3:(3+m), 3:(3+n)] = '.';
|
||||||
table_moves[3:(3+m), 3:(3+n)] = '·';
|
for (i, j) in path:
|
||||||
for (i, j) in path:
|
table[3 + i, 3 + j] = '*';
|
||||||
table_costs[3 + i, 3 + j] = f'{{{table_costs[3 + i, 3 + j]}}}';
|
case DisplayMode.COSTS | DisplayMode.COSTS_AND_MOVES:
|
||||||
table_moves[3 + i, 3 + j] = '*';
|
table[3:(3+m), 3:(3+n)] = Costs.copy();
|
||||||
|
if mode == DisplayMode.COSTS_AND_MOVES:
|
||||||
|
for (i, j) in path:
|
||||||
|
table[3 + i, 3 + j] = f'{{{table[3 + i, 3 + j]}}}';
|
||||||
|
|
||||||
return table_costs, table_moves;
|
return table;
|
||||||
|
|
||||||
def display_cost_matrix(
|
def display_cost_matrix(
|
||||||
Costs: NDArray[(Any, Any), int],
|
Costs: NDArray[(Any, Any), int],
|
||||||
path: List[Tuple[int, int]],
|
path: List[Tuple[int, int]],
|
||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
just_moves: bool = False,
|
mode: DisplayMode,
|
||||||
) -> str:
|
) -> str:
|
||||||
'''
|
'''
|
||||||
Zeigt Kostenmatrix + optimalen Pfad.
|
Zeigt Kostenmatrix + optimalen Pfad.
|
||||||
@ -407,13 +415,7 @@ def display_cost_matrix(
|
|||||||
@returns
|
@returns
|
||||||
- eine 'printable' Darstellung der Matrix mit den Strings X, Y + Indexes.
|
- eine 'printable' Darstellung der Matrix mit den Strings X, Y + Indexes.
|
||||||
'''
|
'''
|
||||||
table_costs, table_moves = represent_cost_matrix(Costs=Costs, path=path, X=X, Y=Y);
|
table = represent_cost_matrix(Costs=Costs, path=path, X=X, Y=Y, mode=mode);
|
||||||
# benutze pandas-Dataframe, um schöner darzustellen:
|
|
||||||
if just_moves:
|
|
||||||
table = table_moves;
|
|
||||||
else:
|
|
||||||
table = table_costs;
|
|
||||||
|
|
||||||
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
||||||
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
||||||
return repr;
|
return repr;
|
||||||
@ -427,7 +429,7 @@ def display_cost_matrix_halves(
|
|||||||
X2: str,
|
X2: str,
|
||||||
Y1: str,
|
Y1: str,
|
||||||
Y2: str,
|
Y2: str,
|
||||||
just_moves: bool = False,
|
mode: DisplayMode,
|
||||||
) -> str:
|
) -> str:
|
||||||
'''
|
'''
|
||||||
Zeigt Kostenmatrix + optimalen Pfad für Schritt im D & C Hirschberg-Algorithmus
|
Zeigt Kostenmatrix + optimalen Pfad für Schritt im D & C Hirschberg-Algorithmus
|
||||||
@ -440,16 +442,11 @@ def display_cost_matrix_halves(
|
|||||||
@returns
|
@returns
|
||||||
- eine 'printable' Darstellung der Matrix mit den Strings X, Y + Indexes.
|
- eine 'printable' Darstellung der Matrix mit den Strings X, Y + Indexes.
|
||||||
'''
|
'''
|
||||||
table_costs1, table_moves1 = represent_cost_matrix(Costs=Costs1, path=path1, X=X1, Y=Y1, pad=True);
|
table1 = represent_cost_matrix(Costs=Costs1, path=path1, X=X1, Y=Y1, mode=mode, pad=True);
|
||||||
table_costs2, table_moves2 = represent_cost_matrix(Costs=Costs2, path=path2, X=X2, Y=Y2, pad=True);
|
table2 = represent_cost_matrix(Costs=Costs2, path=path2, X=X2, Y=Y2, mode=mode, pad=True);
|
||||||
|
|
||||||
# merge Taellen:
|
# merge Taellen:
|
||||||
table_costs = np.concatenate([table_costs1[:, :-1], table_costs2[::-1, ::-1]], axis=1);
|
table = np.concatenate([table1[:, :-1], table2[::-1, ::-1]], axis=1);
|
||||||
table_moves = np.concatenate([table_moves1[:, :-1], table_moves2[::-1, ::-1]], axis=1);
|
|
||||||
if just_moves:
|
|
||||||
table = table_moves;
|
|
||||||
else:
|
|
||||||
table = table_costs;
|
|
||||||
|
|
||||||
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
||||||
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
||||||
|
Loading…
x
Reference in New Issue
Block a user