Compare commits
No commits in common. "2bd07544f35466d42627efcb75537b862181a9f9" and "a7c7179edb09dff21a45bb2322893cfcec5af397" have entirely different histories.
2bd07544f3
...
a7c7179edb
@ -5,9 +5,8 @@
|
|||||||
# IMPORTS
|
# IMPORTS
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
from src.thirdparty.maths import *;
|
|
||||||
from src.thirdparty.plots import *;
|
|
||||||
from src.thirdparty.types import *;
|
from src.thirdparty.types import *;
|
||||||
|
from src.thirdparty.maths import *;
|
||||||
|
|
||||||
from models.generated.config import *;
|
from models.generated.config import *;
|
||||||
from models.generated.commands import *;
|
from models.generated.commands import *;
|
||||||
@ -26,12 +25,6 @@ __all__ = [
|
|||||||
'metropolis_walk_algorithm',
|
'metropolis_walk_algorithm',
|
||||||
];
|
];
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# CONSTANTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
MAX_ITERATIONS = 1000; # um endlose Schleifen zu verhindern
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# METHOD adaptive walk
|
# METHOD adaptive walk
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -57,15 +50,13 @@ def adaptive_walk_algorithm(
|
|||||||
label = lambda x: landscape.label(*x);
|
label = lambda x: landscape.label(*x);
|
||||||
|
|
||||||
# initialisiere
|
# initialisiere
|
||||||
steps = [];
|
|
||||||
x = coords_init;
|
x = coords_init;
|
||||||
fx = f(x);
|
fx = f(x);
|
||||||
fy = fx;
|
fy = fx;
|
||||||
N = nbhd(x);
|
N = nbhd(x);
|
||||||
|
|
||||||
# führe walk aus:
|
# führe walk aus:
|
||||||
k = 0;
|
while True:
|
||||||
while k < MAX_ITERATIONS:
|
|
||||||
# Wähle zufälligen Punkt und berechne fitness-Wert:
|
# Wähle zufälligen Punkt und berechne fitness-Wert:
|
||||||
y = uniform_random_choice(N);
|
y = uniform_random_choice(N);
|
||||||
fy = f(y);
|
fy = f(y);
|
||||||
@ -76,20 +67,14 @@ def adaptive_walk_algorithm(
|
|||||||
x = y;
|
x = y;
|
||||||
fx = fy;
|
fx = fy;
|
||||||
N = nbhd(x);
|
N = nbhd(x);
|
||||||
step = Step(coords=x, label=label(x), improved=True, changed=True);
|
|
||||||
else:
|
else:
|
||||||
# Nichts (außer logging) machen!
|
# Nichts machen!
|
||||||
step = Step(coords=x, label=label(x));
|
pass;
|
||||||
|
|
||||||
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
|
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
|
||||||
if fx <= min([f(y) for y in N], default=fx):
|
if fx <= min([f(y) for y in N], default=fx):
|
||||||
step.stopped = True;
|
|
||||||
steps.append(step);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
steps.append(step);
|
|
||||||
k += 1;
|
|
||||||
|
|
||||||
return x;
|
return x;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -117,7 +102,6 @@ def gradient_walk_algorithm(
|
|||||||
label = lambda x: landscape.label(*x);
|
label = lambda x: landscape.label(*x);
|
||||||
|
|
||||||
# initialisiere
|
# initialisiere
|
||||||
steps = [];
|
|
||||||
x = coords_init;
|
x = coords_init;
|
||||||
fx = landscape.fitness(*x);
|
fx = landscape.fitness(*x);
|
||||||
fy = fx;
|
fy = fx;
|
||||||
@ -127,8 +111,7 @@ def gradient_walk_algorithm(
|
|||||||
Z = [y for y, fy in zip(N, f_values) if fy == fmin];
|
Z = [y for y, fy in zip(N, f_values) if fy == fmin];
|
||||||
|
|
||||||
# führe walk aus:
|
# führe walk aus:
|
||||||
k = 0;
|
while True:
|
||||||
while k < MAX_ITERATIONS:
|
|
||||||
# Wähle zufälligen Punkt mit steilstem Abstieg und berechne fitness-Wert:
|
# Wähle zufälligen Punkt mit steilstem Abstieg und berechne fitness-Wert:
|
||||||
y = uniform_random_choice(Z);
|
y = uniform_random_choice(Z);
|
||||||
fy = fmin;
|
fy = fmin;
|
||||||
@ -142,20 +125,14 @@ def gradient_walk_algorithm(
|
|||||||
f_values = [f(y) for y in N];
|
f_values = [f(y) for y in N];
|
||||||
fmin = min(f_values);
|
fmin = min(f_values);
|
||||||
Z = [y for y, fy in zip(N, f_values) if fy == fmin];
|
Z = [y for y, fy in zip(N, f_values) if fy == fmin];
|
||||||
step = Step(coords=x, label=label(x), improved=True, changed=True);
|
|
||||||
else:
|
else:
|
||||||
# Nichts (außer logging) machen!
|
# Nichts machen!
|
||||||
step = Step(coords=x, label=label(x));
|
pass;
|
||||||
|
|
||||||
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
|
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
|
||||||
if fx <= min([f(y) for y in N], default=fx):
|
if fx <= min([f(y) for y in N], default=fx):
|
||||||
step.stopped = True;
|
|
||||||
steps.append(step);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
steps.append(step);
|
|
||||||
k += 1;
|
|
||||||
|
|
||||||
return x;
|
return x;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
@ -184,64 +161,46 @@ def metropolis_walk_algorithm(
|
|||||||
nbhd = lambda x: landscape.neighbourhood(*x, r=r, strict=True);
|
nbhd = lambda x: landscape.neighbourhood(*x, r=r, strict=True);
|
||||||
label = lambda x: landscape.label(*x);
|
label = lambda x: landscape.label(*x);
|
||||||
|
|
||||||
# definiere anzahl der hinreichenden Schritt für Stabilität:
|
|
||||||
n_stable = 2*(3**(landscape.dim) - 1);
|
|
||||||
|
|
||||||
# initialisiere
|
# initialisiere
|
||||||
x = coords_init;
|
x = coords_init;
|
||||||
fx = f(x);
|
fx = f(x);
|
||||||
fy = fx;
|
fy = fx;
|
||||||
nbhd_x = nbhd(x);
|
nbhd_x = nbhd(x);
|
||||||
steps = [];
|
|
||||||
step = Step(coords=x, label=label(x));
|
|
||||||
|
|
||||||
# führe walk aus:
|
# führe walk aus:
|
||||||
k = 0;
|
k = 0;
|
||||||
n_unchanged = 0;
|
while True:
|
||||||
while k < MAX_ITERATIONS:
|
|
||||||
# Wähle zufälligen Punkt und berechne fitness-Wert:
|
# Wähle zufälligen Punkt und berechne fitness-Wert:
|
||||||
y = uniform_random_choice(nbhd_x);
|
y = uniform_random_choice(nbhd_x);
|
||||||
|
r = uniform(0,1);
|
||||||
fy = f(y);
|
fy = f(y);
|
||||||
p = math.exp(-abs(fy-fx)/T);
|
|
||||||
u = random_binary(p);
|
|
||||||
|
|
||||||
# Aktualisieren, wenn sich f-Wert verbessert
|
# Nur dann aktualisieren, wenn sich f-Wert verbessert:
|
||||||
# oder mit einer Wahrscheinlichkeit von p:
|
if fy < fx or r < math.exp(-(fy-fx)/T):
|
||||||
if fy < fx or u:
|
|
||||||
# Punkt + Umgebung + f-Wert aktualisieren
|
# Punkt + Umgebung + f-Wert aktualisieren
|
||||||
x = y;
|
x = y;
|
||||||
fx = fy;
|
fx = fy;
|
||||||
nbhd_x = nbhd(x);
|
nbhd_x = nbhd(x);
|
||||||
n_unchanged = 0;
|
|
||||||
step = Step(coords=x, label=label(x), improved=(fy < fx), chance=u, probability=p, changed=True);
|
|
||||||
else:
|
else:
|
||||||
# Nichts (außer logging) machen!
|
# Nichts machen!
|
||||||
n_unchanged += 1;
|
pass;
|
||||||
step = Step(coords=x, label=label(x));
|
|
||||||
|
|
||||||
# »Temperatur« ggf. abkühlen:
|
# »Temperatur« ggf. abkühlen:
|
||||||
if annealing:
|
if annealing:
|
||||||
T = cool_temperature(T, k);
|
T = cool_temperature(T, k);
|
||||||
|
|
||||||
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
|
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
|
||||||
if n_unchanged >= n_stable:
|
if fx <= min([f(y) for y in nbhd_x], default=fx):
|
||||||
step.stopped = True;
|
|
||||||
steps.append(step);
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
steps.append(step);
|
|
||||||
k += 1;
|
k += 1;
|
||||||
|
|
||||||
if verbose:
|
|
||||||
for step in steps:
|
|
||||||
print(step);
|
|
||||||
|
|
||||||
return x;
|
return x;
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# AUXILIARY METHODS
|
# AUXILIARY METHODS
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
def cool_temperature(T: float, k: int, const: float = 2.) -> float:
|
def cool_temperature(T: float, k: int, const: float = 1.) -> float:
|
||||||
harm = const*(k + 1);
|
harm = const*(k + 1);
|
||||||
return T/(1 + T/harm);
|
return T/(1 + T/harm);
|
||||||
|
@ -6,7 +6,6 @@
|
|||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
|
||||||
from src.models.random_walk.landscape import *;
|
from src.models.random_walk.landscape import *;
|
||||||
from src.models.random_walk.logging import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
# EXPORTS
|
# EXPORTS
|
||||||
@ -14,5 +13,4 @@ from src.models.random_walk.logging import *;
|
|||||||
|
|
||||||
__all__ = [
|
__all__ = [
|
||||||
'Landscape',
|
'Landscape',
|
||||||
'Step',
|
|
||||||
];
|
];
|
||||||
|
@ -58,23 +58,9 @@ class Landscape():
|
|||||||
def coords_middle(self) -> tuple:
|
def coords_middle(self) -> tuple:
|
||||||
return tuple(math.floor(s/2) for s in self.shape);
|
return tuple(math.floor(s/2) for s in self.shape);
|
||||||
|
|
||||||
@property
|
|
||||||
def values(self) -> np.ndarray:
|
|
||||||
return self._fct;
|
|
||||||
|
|
||||||
def fitness(self, *x: int) -> float:
|
def fitness(self, *x: int) -> float:
|
||||||
return self._fct[x];
|
return self._fct[x];
|
||||||
|
|
||||||
def axis_label(self, i: int, x: int) -> str:
|
|
||||||
if self._one_based:
|
|
||||||
x = x + 1;
|
|
||||||
name = self._labels[i];
|
|
||||||
return f'{name}{x}';
|
|
||||||
|
|
||||||
def axis_labels(self, i: int) -> str:
|
|
||||||
s = self.shape[i];
|
|
||||||
return [ self.axis_label(i, x) for x in range(s) ];
|
|
||||||
|
|
||||||
def label(self, *x: int) -> str:
|
def label(self, *x: int) -> str:
|
||||||
if self._one_based:
|
if self._one_based:
|
||||||
x = tuple(xx + 1 for xx in x);
|
x = tuple(xx + 1 for xx in x);
|
||||||
|
@ -1,32 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# IMPORTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
from src.thirdparty.types import *;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# EXPORTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'Step',
|
|
||||||
];
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# CLASS Step
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
@dataclass
|
|
||||||
class Step():
|
|
||||||
coords: tuple = field();
|
|
||||||
label: str = field();
|
|
||||||
|
|
||||||
improved: bool = field(default=False);
|
|
||||||
chance: bool = field(default=False);
|
|
||||||
probability: float = field(default=0.);
|
|
||||||
|
|
||||||
changed: bool = field(default=False);
|
|
||||||
stopped: bool = field(default=False);
|
|
4
code/python/src/thirdparty/maths.py
vendored
4
code/python/src/thirdparty/maths.py
vendored
@ -8,8 +8,6 @@
|
|||||||
from fractions import Fraction;
|
from fractions import Fraction;
|
||||||
import math;
|
import math;
|
||||||
import numpy as np;
|
import numpy as np;
|
||||||
from numpy.random import binomial as random_binomial;
|
|
||||||
random_binary = lambda p: (random_binomial(1, p) == 1);
|
|
||||||
import pandas as pd;
|
import pandas as pd;
|
||||||
import random;
|
import random;
|
||||||
from random import uniform;
|
from random import uniform;
|
||||||
@ -24,8 +22,6 @@ __all__ = [
|
|||||||
'Fraction',
|
'Fraction',
|
||||||
'math',
|
'math',
|
||||||
'np',
|
'np',
|
||||||
'random_binomial',
|
|
||||||
'random_binary',
|
|
||||||
'pd',
|
'pd',
|
||||||
'random',
|
'random',
|
||||||
'uniform',
|
'uniform',
|
||||||
|
22
code/python/src/thirdparty/plots.py
vendored
22
code/python/src/thirdparty/plots.py
vendored
@ -1,22 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# IMPORTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
from matplotlib import pyplot as mplt;
|
|
||||||
from matplotlib import animation as mplt_animation;
|
|
||||||
from matplotlib import colors as mplt_colours;
|
|
||||||
from matplotlib import patches as mplt_patches;
|
|
||||||
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
# EXPORTS
|
|
||||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
|
||||||
|
|
||||||
__all__ = [
|
|
||||||
'mplt',
|
|
||||||
'mplt_colours',
|
|
||||||
'mplt_patches',
|
|
||||||
'mplt_animation',
|
|
||||||
];
|
|
Loading…
x
Reference in New Issue
Block a user