woche12 > master: code py - vorberechnungen gemäß modell

This commit is contained in:
RD 2022-06-21 19:01:59 +02:00
parent 01ef8c5758
commit c2cb11a141
2 changed files with 38 additions and 2 deletions

View File

@ -27,15 +27,28 @@ __all__ = [
@run_safely()
def endpoint_random_walk(command: CommandRandomWalk) -> Result[CallResult, CallError]:
# Compute landscape (fitness fct + topology) + initial co-ordinates:
one_based = command.one_based;
landscape = Landscape(
values = command.landscape.values,
labels = command.landscape.labels,
metric = command.landscape.neighbourhoods.metric,
one_based = one_based,
);
if isinstance(command.coords_init, list):
coords_init = tuple(command.coords_init);
if one_based:
coords_init = tuple(xx - 1 for xx in coords_init);
assert len(coords_init) == landscape.dim, 'Dimension of initial co-ordinations inconsistent with landscape!';
else:
coords_init = landscape.coords_middle;
match command.algorithm:
case EnumWalkMode.adaptive:
result = adaptive_walk_algorithm(
landscape = landscape,
r = command.landscape.neighbourhoods.radius,
coords_init = coords_init,
optimise = command.optimise,
verbose = config.OPTIONS.random_walk.verbose
);
@ -43,6 +56,7 @@ def endpoint_random_walk(command: CommandRandomWalk) -> Result[CallResult, CallE
result = gradient_walk_algorithm(
landscape = landscape,
r = command.landscape.neighbourhoods.radius,
coords_init = coords_init,
optimise = command.optimise,
verbose = config.OPTIONS.random_walk.verbose
);
@ -50,6 +64,8 @@ def endpoint_random_walk(command: CommandRandomWalk) -> Result[CallResult, CallE
result = metropolis_walk_algorithm(
landscape = landscape,
r = command.landscape.neighbourhoods.radius,
coords_init = coords_init,
T = command.temperature_init,
annealing = command.annealing,
optimise = command.optimise,
verbose = config.OPTIONS.random_walk.verbose

View File

@ -27,16 +27,23 @@ __all__ = [
class Landscape():
_fct: np.ndarray;
_labels: list[str];
_metric: EnumLandscapeMetric;
_radius: float;
_one_based: bool;
def __init__(
self,
values: DataTypeLandscapeValues,
labels: List[str],
metric: EnumLandscapeMetric = EnumLandscapeMetric.maximum,
one_based: bool = False,
):
self._fct = convert_to_nparray(values);
assert len(labels) == self.dim, 'A label is required for each axis/dimension!';
self._labels = labels;
self._metric = metric;
self._one_based = one_based;
return;
@property
@ -47,14 +54,27 @@ class Landscape():
def dim(self) -> int:
return len(self._fct.shape);
@property
def coords_middle(self) -> tuple:
return tuple(math.floor(s/2) for s in self.shape);
def fitness(self, *x: int) -> float:
return self._fct[x];
def label(self, *x: int) -> str:
if self._one_based:
x = tuple(xx + 1 for xx in x);
expr = ','.join([ f'{name}{xx}' for name, xx in zip(self._labels, x)]);
if self.dim > 1:
expr = f'({expr})';
return expr;
def neighbourhood(self, *x: int, r: float, strict: bool = False) -> List[tuple]:
r = int(r);
sides = [
[ xx - j for j in range(1,r+1) if xx - j in range(s) ]
[ xx - j for j in range(1, r+1) if xx - j in range(s) ]
+ ([ xx ] if xx in range(s) else [])
+ [ xx + j for j in range(1,r+1) if xx + j in range(s) ]
+ [ xx + j for j in range(1, r+1) if xx + j in range(s) ]
for xx, s in zip(x, self.shape)
];
match self._metric: