Compare commits

..

No commits in common. "2bd07544f35466d42627efcb75537b862181a9f9" and "a7c7179edb09dff21a45bb2322893cfcec5af397" have entirely different histories.

6 changed files with 15 additions and 130 deletions

View File

@ -5,9 +5,8 @@
# IMPORTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from src.thirdparty.maths import *;
from src.thirdparty.plots import *;
from src.thirdparty.types import *;
from src.thirdparty.maths import *;
from models.generated.config import *;
from models.generated.commands import *;
@ -26,12 +25,6 @@ __all__ = [
'metropolis_walk_algorithm',
];
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# CONSTANTS
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
MAX_ITERATIONS = 1000; # um endlose Schleifen zu verhindern
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# METHOD adaptive walk
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -57,15 +50,13 @@ def adaptive_walk_algorithm(
label = lambda x: landscape.label(*x);
# initialisiere
steps = [];
x = coords_init;
fx = f(x);
fy = fx;
N = nbhd(x);
# führe walk aus:
k = 0;
while k < MAX_ITERATIONS:
while True:
# Wähle zufälligen Punkt und berechne fitness-Wert:
y = uniform_random_choice(N);
fy = f(y);
@ -76,20 +67,14 @@ def adaptive_walk_algorithm(
x = y;
fx = fy;
N = nbhd(x);
step = Step(coords=x, label=label(x), improved=True, changed=True);
else:
# Nichts (außer logging) machen!
step = Step(coords=x, label=label(x));
# Nichts machen!
pass;
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
if fx <= min([f(y) for y in N], default=fx):
step.stopped = True;
steps.append(step);
break;
steps.append(step);
k += 1;
return x;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -117,7 +102,6 @@ def gradient_walk_algorithm(
label = lambda x: landscape.label(*x);
# initialisiere
steps = [];
x = coords_init;
fx = landscape.fitness(*x);
fy = fx;
@ -127,8 +111,7 @@ def gradient_walk_algorithm(
Z = [y for y, fy in zip(N, f_values) if fy == fmin];
# führe walk aus:
k = 0;
while k < MAX_ITERATIONS:
while True:
# Wähle zufälligen Punkt mit steilstem Abstieg und berechne fitness-Wert:
y = uniform_random_choice(Z);
fy = fmin;
@ -142,20 +125,14 @@ def gradient_walk_algorithm(
f_values = [f(y) for y in N];
fmin = min(f_values);
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:
# Nichts (außer logging) machen!
step = Step(coords=x, label=label(x));
# Nichts machen!
pass;
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
if fx <= min([f(y) for y in N], default=fx):
step.stopped = True;
steps.append(step);
break;
steps.append(step);
k += 1;
return x;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -184,64 +161,46 @@ def metropolis_walk_algorithm(
nbhd = lambda x: landscape.neighbourhood(*x, r=r, strict=True);
label = lambda x: landscape.label(*x);
# definiere anzahl der hinreichenden Schritt für Stabilität:
n_stable = 2*(3**(landscape.dim) - 1);
# initialisiere
x = coords_init;
fx = f(x);
fy = fx;
nbhd_x = nbhd(x);
steps = [];
step = Step(coords=x, label=label(x));
# führe walk aus:
k = 0;
n_unchanged = 0;
while k < MAX_ITERATIONS:
while True:
# Wähle zufälligen Punkt und berechne fitness-Wert:
y = uniform_random_choice(nbhd_x);
r = uniform(0,1);
fy = f(y);
p = math.exp(-abs(fy-fx)/T);
u = random_binary(p);
# Aktualisieren, wenn sich f-Wert verbessert
# oder mit einer Wahrscheinlichkeit von p:
if fy < fx or u:
# Nur dann aktualisieren, wenn sich f-Wert verbessert:
if fy < fx or r < math.exp(-(fy-fx)/T):
# Punkt + Umgebung + f-Wert aktualisieren
x = y;
fx = fy;
nbhd_x = nbhd(x);
n_unchanged = 0;
step = Step(coords=x, label=label(x), improved=(fy < fx), chance=u, probability=p, changed=True);
else:
# Nichts (außer logging) machen!
n_unchanged += 1;
step = Step(coords=x, label=label(x));
# Nichts machen!
pass;
# »Temperatur« ggf. abkühlen:
if annealing:
T = cool_temperature(T, k);
# Nur dann (erfolgreich) abbrechen, wenn f-Wert lokal Min:
if n_unchanged >= n_stable:
step.stopped = True;
steps.append(step);
if fx <= min([f(y) for y in nbhd_x], default=fx):
break;
steps.append(step);
k += 1;
if verbose:
for step in steps:
print(step);
return x;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# 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);
return T/(1 + T/harm);

View File

@ -6,7 +6,6 @@
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
from src.models.random_walk.landscape import *;
from src.models.random_walk.logging import *;
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# EXPORTS
@ -14,5 +13,4 @@ from src.models.random_walk.logging import *;
__all__ = [
'Landscape',
'Step',
];

View File

@ -58,23 +58,9 @@ class Landscape():
def coords_middle(self) -> tuple:
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:
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:
if self._one_based:
x = tuple(xx + 1 for xx in x);

View File

@ -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);

View File

@ -8,8 +8,6 @@
from fractions import Fraction;
import math;
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 random;
from random import uniform;
@ -24,8 +22,6 @@ __all__ = [
'Fraction',
'math',
'np',
'random_binomial',
'random_binary',
'pd',
'random',
'uniform',

View File

@ -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',
];