Compare commits
No commits in common. "278e3713c833eeb1773b9db86bf2ba4d10d3b69c" and "3c965eda7b9783be960c9446e5fff7d5e06aa755" have entirely different histories.
278e3713c8
...
3c965eda7b
@ -115,86 +115,15 @@
|
|||||||
]
|
]
|
||||||
costs: [30, 10, 50, 10, 80]
|
costs: [30, 10, 50, 10, 80]
|
||||||
values: [17, 14, 17, 5, 25]
|
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
|
# 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
|
- name: EUKLID
|
||||||
numbers:
|
numbers:
|
||||||
- 2017
|
- 2017
|
||||||
- 58
|
- 58
|
||||||
- name: POLLARD-RHO
|
- name: POLLARD-RHO
|
||||||
growth: LINEAR
|
|
||||||
# growth: EXPONENTIAL
|
|
||||||
number: 534767
|
number: 534767
|
||||||
x-init: 5
|
x-init: 5
|
||||||
|
@ -235,7 +235,6 @@ components:
|
|||||||
type: object
|
type: object
|
||||||
required:
|
required:
|
||||||
- name
|
- name
|
||||||
- growth
|
|
||||||
- number
|
- number
|
||||||
properties:
|
properties:
|
||||||
name:
|
name:
|
||||||
@ -243,8 +242,6 @@ components:
|
|||||||
number:
|
number:
|
||||||
type: integer
|
type: integer
|
||||||
exclusiveMinimum: 0
|
exclusiveMinimum: 0
|
||||||
growth:
|
|
||||||
$ref: '#/components/schemas/EnumPollardGrowthRate'
|
|
||||||
x-init:
|
x-init:
|
||||||
type: integer
|
type: integer
|
||||||
default: 2
|
default: 2
|
||||||
@ -330,21 +327,6 @@ components:
|
|||||||
- GREEDY
|
- GREEDY
|
||||||
- BRANCH-AND-BOUND
|
- 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
|
# Enum Type of walk mode for fitness walk algorithm
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
EnumWalkMode:
|
EnumWalkMode:
|
||||||
|
@ -12,6 +12,5 @@ from src.algorithms.pollard_rho.algorithms import *;
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'pollard_rho_algorithm_linear',
|
'pollard_rho_algorithm',
|
||||||
'pollard_rho_algorithm_exponential',
|
|
||||||
];
|
];
|
||||||
|
@ -18,71 +18,14 @@ from src.algorithms.pollard_rho.display import *;
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'pollard_rho_algorithm_linear',
|
'pollard_rho_algorithm',
|
||||||
'pollard_rho_algorithm_exponential',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# METHOD pollard's rho algorithm - with linear grwoth
|
# METHOD pollard's rho algorithm
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def pollard_rho_algorithm_linear(
|
def pollard_rho_algorithm(
|
||||||
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,
|
n: int,
|
||||||
x_init: int = 2,
|
x_init: int = 2,
|
||||||
verbose: bool = False,
|
verbose: bool = False,
|
||||||
@ -121,7 +64,7 @@ def pollard_rho_algorithm_exponential(
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
repr = display_table_exponential(steps=steps);
|
repr = display_table(steps=steps);
|
||||||
print('');
|
print('');
|
||||||
print('\x1b[1mEuklidescher Algorithmus\x1b[0m');
|
print('\x1b[1mEuklidescher Algorithmus\x1b[0m');
|
||||||
print('');
|
print('');
|
||||||
|
@ -16,37 +16,14 @@ from src.models.pollard_rho import *;
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'display_table_linear',
|
'display_table',
|
||||||
'display_table_exponential',
|
|
||||||
];
|
];
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# METHOD display table - linear
|
# METHOD display table
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def display_table_linear(steps: List[Step]) -> str:
|
def display_table(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({
|
table = pd.DataFrame({
|
||||||
'i': [i for i in range(len(steps))],
|
'i': [i for i in range(len(steps))],
|
||||||
'x': [step.x for step in steps],
|
'x': [step.x for step in steps],
|
||||||
|
@ -26,21 +26,9 @@ __all__ = [
|
|||||||
|
|
||||||
@run_safely()
|
@run_safely()
|
||||||
def endpoint_pollard_rho(command: CommandPollard) -> Result[CallResult, CallError]:
|
def endpoint_pollard_rho(command: CommandPollard) -> Result[CallResult, CallError]:
|
||||||
match command.growth:
|
result = pollard_rho_algorithm(
|
||||||
case EnumPollardGrowthRate.linear:
|
|
||||||
result = pollard_rho_algorithm_linear(
|
|
||||||
n = command.number,
|
n = command.number,
|
||||||
x_init = command.x_init,
|
x_init = command.x_init,
|
||||||
verbose = config.OPTIONS.pollard_rho.verbose,
|
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));
|
return Ok(CallResult(action_taken=True, message=result));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user