Compare commits

...

2 Commits

6 changed files with 195 additions and 13 deletions

View File

@ -115,15 +115,86 @@
]
costs: [30, 10, 50, 10, 80]
values: [17, 14, 17, 5, 25]
- name: RUCKSACK
algorithm: BRANCH-AND-BOUND
max-cost: 900
items: [
'Sellerie',
'Sonnenblumenkerne',
'Rote Beete',
'Hirse',
'Buchweizen',
]
costs: [600, 100, 800, 100, 200]
values: [10, 15, 20, 5, 15]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Beispiele für Seminarwoche 12
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- name: RANDOM-WALK
algorithm: GRADIENT
one-based: true
coords-init: [3, 3]
landscape: &ref_landscape1
neighbourhoods:
radius: 1
# metric: MANHATTAN
metric: MAXIMUM
labels:
- x
- y
values:
- [5, 2, 1, 3, 4, 7]
- [8, 4, 3, 5, 5, 6]
- [9, 1, 2, 6, 8, 4]
- [7, 4, 4, 3, 7, 3]
- [6, 4, 2, 1, 0, 7]
- [4, 3, 5, 2, 1, 8]
optimise: MAX
- name: RANDOM-WALK
algorithm: ADAPTIVE
one-based: true
coords-init: [3, 3]
landscape: *ref_landscape1
optimise: MAX
- name: RANDOM-WALK
algorithm: METROPOLIS
annealing: false
temperature-init: 3.
one-based: true
coords-init: [5, 3]
landscape: *ref_landscape1
optimise: MAX
- name: RANDOM-WALK
algorithm: METROPOLIS
annealing: false
temperature-init: 3.
one-based: false
coords-init: [0]
landscape:
neighbourhoods:
radius: 1
metric: MANHATTAN
labels:
- x
values: [4, 6.5, 2]
optimise: MAX
- name: GENETIC
population:
- [3, 5, 4, 1, 6, 7, 2, 8, 9]
- [4, 5, 3, 2, 1, 6, 7, 8, 9]
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Beispiele für Seminarwoche 13
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- name: EUKLID
numbers:
- 2017
- 58
- name: POLLARD-RHO
growth: LINEAR
# growth: EXPONENTIAL
number: 534767
x-init: 5

View File

@ -235,6 +235,7 @@ components:
type: object
required:
- name
- growth
- number
properties:
name:
@ -242,6 +243,8 @@ components:
number:
type: integer
exclusiveMinimum: 0
growth:
$ref: '#/components/schemas/EnumPollardGrowthRate'
x-init:
type: integer
default: 2
@ -327,6 +330,21 @@ components:
- GREEDY
- BRANCH-AND-BOUND
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Enum Type for choice of growth rate in Pollard Algorithm
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EnumPollardGrowthRate:
description: |-
Via the 'tail-chasing' period finding method in Pollard's rho algorithm,
the difference between the indexes of the pseudo-random sequence
can be chosen to growth according to different rates, e.g.
- `LINEAR` - choose `x[k]` and `x[2k]`
- `EXPONENTIAL` - choose `x[k]` and `x[2^{k}]`
type: string
enum:
- LINEAR
- EXPONENTIAL
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Enum Type of walk mode for fitness walk algorithm
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
EnumWalkMode:

View File

@ -12,5 +12,6 @@ from src.algorithms.pollard_rho.algorithms import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__all__ = [
'pollard_rho_algorithm',
'pollard_rho_algorithm_linear',
'pollard_rho_algorithm_exponential',
];

View File

@ -18,14 +18,71 @@ from src.algorithms.pollard_rho.display import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__all__ = [
'pollard_rho_algorithm',
'pollard_rho_algorithm_linear',
'pollard_rho_algorithm_exponential',
];
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHOD pollard's rho algorithm
# METHOD pollard's rho algorithm - with linear grwoth
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def pollard_rho_algorithm(
def pollard_rho_algorithm_linear(
n: int,
x_init: int = 2,
verbose: bool = False,
):
steps = [];
success = False;
f = lambda _: fct(_, n=n);
d = 1;
x = y = x_init;
steps.append(Step(x=x));
k = 0;
k_next = 1;
while True:
# aktualisiere x: x = f(x_prev):
x = f(x);
# aktualisiere y: y = f(f(y_prev)):
y = f(f(y));
# ggT berechnen:
d = math.gcd(abs(x-y), n);
steps.append(Step(x=x, y=y, d=d));
# Abbruchkriterien prüfen:
if d == 1: # weitermachen, solange d == 1
k += 1;
continue;
elif d == n: # versagt
success = False;
break;
else:
success = True;
break;
if verbose:
repr = display_table_linear(steps=steps);
print('');
print('\x1b[1mEuklidescher Algorithmus\x1b[0m');
print('');
print(repr);
print('');
if success:
print('\x1b[1mBerechneter Faktor:\x1b[0m');
print('');
print(f'd = \x1b[1m{d}\x1b[0m.');
else:
print('\x1b[91mKein (Prim)faktor erkannt!\x1b[0m');
print('');
return d;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHOD pollard's rho algorithm - with exponential grwoth
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def pollard_rho_algorithm_exponential(
n: int,
x_init: int = 2,
verbose: bool = False,
@ -64,7 +121,7 @@ def pollard_rho_algorithm(
break;
if verbose:
repr = display_table(steps=steps);
repr = display_table_exponential(steps=steps);
print('');
print('\x1b[1mEuklidescher Algorithmus\x1b[0m');
print('');

View File

@ -16,14 +16,37 @@ from src.models.pollard_rho import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
__all__ = [
'display_table',
'display_table_linear',
'display_table_exponential',
];
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHOD display table
# METHOD display table - linear
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def display_table(steps: List[Step]) -> str:
def display_table_linear(steps: List[Step]) -> str:
table = pd.DataFrame({
'i': [i for i in range(len(steps))],
'x': [step.x for step in steps],
'y': [step.y or '-' for step in steps],
'd': [step.d or '-' for step in steps],
}) \
.reset_index(drop=True);
# benutze pandas-Dataframe + tabulate, um schöner darzustellen:
repr = tabulate(
table,
headers=['i', 'x(i)', 'y(i) = x(2i)', 'gcd(|x - y|,n)'],
showindex=False,
colalign=('right', 'right', 'right', 'center'),
tablefmt='simple',
);
return repr;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHOD display table - exponential
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
def display_table_exponential(steps: List[Step]) -> str:
table = pd.DataFrame({
'i': [i for i in range(len(steps))],
'x': [step.x for step in steps],

View File

@ -26,9 +26,21 @@ __all__ = [
@run_safely()
def endpoint_pollard_rho(command: CommandPollard) -> Result[CallResult, CallError]:
result = pollard_rho_algorithm(
n = command.number,
x_init = command.x_init,
verbose = config.OPTIONS.pollard_rho.verbose,
);
match command.growth:
case EnumPollardGrowthRate.linear:
result = pollard_rho_algorithm_linear(
n = command.number,
x_init = command.x_init,
verbose = config.OPTIONS.pollard_rho.verbose,
);
pass;
case EnumPollardGrowthRate.exponential:
result = pollard_rho_algorithm_exponential(
n = command.number,
x_init = command.x_init,
verbose = config.OPTIONS.pollard_rho.verbose,
);
pass;
case _ as growth:
raise Exception(f'No algorithm implemented for \x1b[1m{growth.value}\x1b[0m as growth rate.');
return Ok(CallResult(action_taken=True, message=result));