From c2cb11a141a9d2501473e1b72d227e95d8d53493 Mon Sep 17 00:00:00 2001 From: raj_mathe Date: Tue, 21 Jun 2022 19:01:59 +0200 Subject: [PATCH] =?UTF-8?q?woche12=20>=20master:=20code=20py=20-=20vorbere?= =?UTF-8?q?chnungen=20gem=C3=A4=C3=9F=20modell?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/endpoints/ep_algorithm_random_walk.py | 16 +++++++++++++ .../src/models/random_walk/landscape.py | 24 +++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/code/python/src/endpoints/ep_algorithm_random_walk.py b/code/python/src/endpoints/ep_algorithm_random_walk.py index 37b5953..0324f03 100644 --- a/code/python/src/endpoints/ep_algorithm_random_walk.py +++ b/code/python/src/endpoints/ep_algorithm_random_walk.py @@ -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 diff --git a/code/python/src/models/random_walk/landscape.py b/code/python/src/models/random_walk/landscape.py index 131a941..dad9239 100644 --- a/code/python/src/models/random_walk/landscape.py +++ b/code/python/src/models/random_walk/landscape.py @@ -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: