Compare commits
No commits in common. "a536d16c1d012255f29d5b9c12fae586aa231732" and "954becea26b3d5df85f2c5ac772917f79758c850" have entirely different histories.
a536d16c1d
...
954becea26
@ -4,4 +4,3 @@ pytest-lazy-fixture>=0.6.3
|
|||||||
typing>=3.7.4.3
|
typing>=3.7.4.3
|
||||||
numpy>=1.22.3
|
numpy>=1.22.3
|
||||||
pandas>=1.4.1
|
pandas>=1.4.1
|
||||||
tabulate>=0.8.9
|
|
||||||
|
@ -9,7 +9,6 @@ import math;
|
|||||||
import numpy as np;
|
import numpy as np;
|
||||||
import pandas as pd;
|
import pandas as pd;
|
||||||
import random;
|
import random;
|
||||||
from tabulate import tabulate;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# EXPORTS
|
# EXPORTS
|
||||||
@ -20,5 +19,4 @@ __all__ = [
|
|||||||
'np',
|
'np',
|
||||||
'pd',
|
'pd',
|
||||||
'random',
|
'random',
|
||||||
'tabulate',
|
|
||||||
];
|
];
|
||||||
|
@ -52,7 +52,6 @@ def enter():
|
|||||||
# Y = 'apple',
|
# Y = 'apple',
|
||||||
# X = 'happily',
|
# X = 'happily',
|
||||||
verbose = True,
|
verbose = True,
|
||||||
just_moves = False,
|
|
||||||
);
|
);
|
||||||
return;
|
return;
|
||||||
|
|
||||||
|
@ -45,16 +45,14 @@ def hirschberg_algorithm_once(
|
|||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
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 verbose:
|
||||||
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);
|
||||||
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(word_y);
|
print(word_y);
|
||||||
print(len(word_x) * '-');
|
print(len(word_x) * '-');
|
||||||
print(word_x);
|
print(word_x);
|
||||||
@ -65,16 +63,14 @@ def hirschberg_algorithm(
|
|||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
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, verbose=verbose);
|
||||||
word_x = ''.join(alignments_x);
|
word_x = ''.join(alignments_x);
|
||||||
word_y = ''.join(alignments_y);
|
word_y = ''.join(alignments_y);
|
||||||
if verbose:
|
if verbose:
|
||||||
display_x = f'[{"][".join(alignments_x)}]';
|
display_x = '|'.join(alignments_x);
|
||||||
display_y = f'[{"][".join(alignments_y)}]';
|
display_y = '|'.join(alignments_y);
|
||||||
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
print(f'\n\x1b[1mOptimales Alignment:\x1b[0m');
|
||||||
print('');
|
|
||||||
print(display_y);
|
print(display_y);
|
||||||
print(len(display_x) * '-');
|
print(len(display_x) * '-');
|
||||||
print(display_x);
|
print(display_x);
|
||||||
@ -86,7 +82,6 @@ def hirschberg_algorithm_step(
|
|||||||
Y: str,
|
Y: str,
|
||||||
depth: int = 0,
|
depth: int = 0,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
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:
|
||||||
@ -123,7 +118,6 @@ def hirschberg_algorithm_step(
|
|||||||
X2 = '-' + X2,
|
X2 = '-' + X2,
|
||||||
Y1 = '-' + Y1,
|
Y1 = '-' + Y1,
|
||||||
Y2 = '-' + Y2,
|
Y2 = '-' + Y2,
|
||||||
just_moves = just_moves,
|
|
||||||
);
|
);
|
||||||
print(f'\n\x1b[1mRekursionstiefe: {depth}\x1b[0m\n\n{repr}')
|
print(f'\n\x1b[1mRekursionstiefe: {depth}\x1b[0m\n\n{repr}')
|
||||||
|
|
||||||
@ -131,8 +125,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);
|
||||||
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);
|
||||||
|
|
||||||
# Resultate zusammensetzen:
|
# Resultate zusammensetzen:
|
||||||
alignments_x = alignments_x_1 + alignments_x_2;
|
alignments_x = alignments_x_1 + alignments_x_2;
|
||||||
@ -381,10 +375,10 @@ def represent_cost_matrix(
|
|||||||
|
|
||||||
table_costs = table.copy();
|
table_costs = table.copy();
|
||||||
table_moves = table.copy();
|
table_moves = table.copy();
|
||||||
table_costs[3:(3+m), 3:(3+n)] = Costs.copy();
|
table_costs[3:(3+m), 3:(3+n)] = Costs;
|
||||||
table_moves[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_costs[3 + i, 3 + j] = f'{{{table_costs[3 + i, 3 + j]}}}';
|
# table_costs[3 + i, 3 + j] = f'\x1b[92;1m{table_costs[3 + i, 3 + j]}\x1b[0m';
|
||||||
table_moves[3 + i, 3 + j] = '*';
|
table_moves[3 + i, 3 + j] = '*';
|
||||||
|
|
||||||
return table_costs, table_moves;
|
return table_costs, table_moves;
|
||||||
@ -394,7 +388,6 @@ def display_cost_matrix(
|
|||||||
path: List[Tuple[int, int]],
|
path: List[Tuple[int, int]],
|
||||||
X: str,
|
X: str,
|
||||||
Y: str,
|
Y: str,
|
||||||
just_moves: bool = False,
|
|
||||||
) -> str:
|
) -> str:
|
||||||
'''
|
'''
|
||||||
Zeigt Kostenmatrix + optimalen Pfad.
|
Zeigt Kostenmatrix + optimalen Pfad.
|
||||||
@ -409,13 +402,12 @@ def display_cost_matrix(
|
|||||||
'''
|
'''
|
||||||
table_costs, table_moves = represent_cost_matrix(Costs=Costs, path=path, X=X, Y=Y);
|
table_costs, table_moves = represent_cost_matrix(Costs=Costs, path=path, X=X, Y=Y);
|
||||||
# benutze pandas-Dataframe, um schöner darzustellen:
|
# benutze pandas-Dataframe, um schöner darzustellen:
|
||||||
if just_moves:
|
h = table_costs.shape[0];
|
||||||
table = table_moves;
|
costs_repr = pd.DataFrame(table_costs).to_string(index=False, header=False);
|
||||||
else:
|
moves_repr = pd.DataFrame(table_moves).to_string(index=False, header=False);
|
||||||
table = table_costs;
|
table = np.concatenate([table_costs, np.full(shape=(h, 1), dtype=object, fill_value=' '), table_moves], axis=1);
|
||||||
|
|
||||||
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
repr = pd.DataFrame(table).to_string(index=False, header=False);
|
||||||
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
|
||||||
return repr;
|
return repr;
|
||||||
|
|
||||||
def display_cost_matrix_halves(
|
def display_cost_matrix_halves(
|
||||||
@ -427,7 +419,6 @@ def display_cost_matrix_halves(
|
|||||||
X2: str,
|
X2: str,
|
||||||
Y1: str,
|
Y1: str,
|
||||||
Y2: str,
|
Y2: str,
|
||||||
just_moves: bool = False,
|
|
||||||
) -> 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
|
||||||
@ -444,13 +435,14 @@ def display_cost_matrix_halves(
|
|||||||
table_costs2, table_moves2 = represent_cost_matrix(Costs=Costs2, path=path2, X=X2, Y=Y2, pad=True);
|
table_costs2, table_moves2 = represent_cost_matrix(Costs=Costs2, path=path2, X=X2, Y=Y2, pad=True);
|
||||||
|
|
||||||
# merge Taellen:
|
# merge Taellen:
|
||||||
|
h = table_costs1.shape[0];
|
||||||
table_costs = np.concatenate([table_costs1, table_costs2[::-1, ::-1]], axis=1);
|
table_costs = np.concatenate([table_costs1, table_costs2[::-1, ::-1]], axis=1);
|
||||||
table_moves = np.concatenate([table_moves1, table_moves2[::-1, ::-1]], axis=1);
|
table_moves = np.concatenate([table_moves1, table_moves2[::-1, ::-1]], axis=1);
|
||||||
if just_moves:
|
table = np.concatenate([table_costs, np.full(shape=(h, 1), dtype=object, fill_value=' '), table_moves], axis=1);
|
||||||
table = table_moves;
|
|
||||||
else:
|
|
||||||
table = table_costs;
|
|
||||||
|
|
||||||
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
|
# benutze pandas-Dataframe, um schöner darzustellen:
|
||||||
repr = tabulate(pd.DataFrame(table), showindex=False, stralign='center', tablefmt='plain');
|
# costs_repr = pd.DataFrame(table_costs).to_string(index=False, header=False);
|
||||||
|
# moves_repr = pd.DataFrame(table_moves).to_string(index=False, header=False);
|
||||||
|
# return costs_repr, moves_repr;
|
||||||
|
repr = pd.DataFrame(table).to_string(index=False, header=False);
|
||||||
return repr;
|
return repr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user