Compare commits
3 Commits
aaa0b7a124
...
4001551c9c
Author | SHA1 | Date |
---|---|---|
|
4001551c9c | 2 months ago |
|
17711327ef | 2 months ago |
|
3d05f7ae1d | 2 months ago |
21 changed files with 557 additions and 4 deletions
@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.algorithms.genetic.algorithms import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'genetic_algorithm', |
||||
]; |
@ -0,0 +1,38 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.thirdparty.types import *; |
||||
from src.thirdparty.maths import *; |
||||
|
||||
from models.generated.config import *; |
||||
from src.core.log import *; |
||||
from src.core.utils import *; |
||||
from src.models.genetic import *; |
||||
from src.algorithms.genetic.display import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'genetic_algorithm', |
||||
]; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# METHOD genetic algorithm |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
def genetic_algorithm( |
||||
individual1: List[str], |
||||
individual2: List[str], |
||||
verbose: bool, |
||||
): |
||||
''' |
||||
Führt den genetischen Algorithmus auf 2 Individuen aus. |
||||
''' |
||||
log_warn('Noch nicht implementiert!'); |
||||
return; |
@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.thirdparty.code import *; |
||||
from src.thirdparty.maths import *; |
||||
from src.thirdparty.types import *; |
||||
|
||||
from src.core.log import *; |
||||
from src.models.genetic import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'display_table', |
||||
]; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# METHOD display table |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
def display_table( |
||||
) -> str: |
||||
log_warn('Noch nicht implementiert!'); |
||||
return ''; |
@ -0,0 +1,18 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.algorithms.random_walk.algorithms import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'adaptive_walk_algorithm', |
||||
'gradient_walk_algorithm', |
||||
'metropolis_walk_algorithm', |
||||
]; |
@ -0,0 +1,75 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.thirdparty.types import *; |
||||
from src.thirdparty.maths import *; |
||||
|
||||
from models.generated.config import *; |
||||
from models.generated.commands import *; |
||||
from src.core.log import *; |
||||
from src.core.utils import *; |
||||
from src.models.random_walk import *; |
||||
from src.algorithms.random_walk.display import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'adaptive_walk_algorithm', |
||||
'gradient_walk_algorithm', |
||||
'metropolis_walk_algorithm', |
||||
]; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# METHOD adaptive walk |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
def adaptive_walk_algorithm( |
||||
landscape: Landscape, |
||||
r: float, |
||||
optimise: EnumOptimiseMode, |
||||
verbose: bool, |
||||
): |
||||
''' |
||||
Führt den Adapative-Walk Algorithmus aus, um ein lokales Minimum zu bestimmen. |
||||
''' |
||||
log_warn('Noch nicht implementiert!'); |
||||
return; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# METHOD gradient walk |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
def gradient_walk_algorithm( |
||||
landscape: Landscape, |
||||
r: float, |
||||
optimise: EnumOptimiseMode, |
||||
verbose: bool, |
||||
): |
||||
''' |
||||
Führt den Gradient-Descent (bzw. Ascent) Algorithmus aus, um ein lokales Minimum zu bestimmen. |
||||
''' |
||||
log_warn('Noch nicht implementiert!'); |
||||
return; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# METHOD metropolis walk |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
def metropolis_walk_algorithm( |
||||
landscape: Landscape, |
||||
r: float, |
||||
annealing: bool, |
||||
optimise: EnumOptimiseMode, |
||||
verbose: bool, |
||||
): |
||||
''' |
||||
Führt den Metropolis-Walk Algorithmus aus, um ein lokales Minimum zu bestimmen. |
||||
''' |
||||
log_warn('Noch nicht implementiert!'); |
||||
return; |
@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.thirdparty.code import *; |
||||
from src.thirdparty.maths import *; |
||||
from src.thirdparty.types import *; |
||||
|
||||
from src.core.log import *; |
||||
from src.models.random_walk import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'display_table', |
||||
]; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# METHOD display table |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
def display_table( |
||||
) -> str: |
||||
log_warn('Noch nicht implementiert!'); |
||||
return ''; |
@ -0,0 +1,34 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.thirdparty.code import *; |
||||
|
||||
from models.generated.commands import *; |
||||
from src.core.calls import *; |
||||
from src.setup import config; |
||||
from src.algorithms.genetic import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'endpoint_genetic', |
||||
]; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# ENDPOINT |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
@run_safely() |
||||
def endpoint_genetic(command: CommandGenetic) -> Result[CallResult, CallError]: |
||||
result = genetic_algorithm( |
||||
individual1 = command.population[0], |
||||
individual2 = command.population[1], |
||||
verbose = config.OPTIONS.genetic.verbose, |
||||
); |
||||
return Ok(CallResult(action_taken=True, message=result)); |
@ -0,0 +1,59 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.thirdparty.code import *; |
||||
|
||||
from models.generated.commands import *; |
||||
from src.core.calls import *; |
||||
from src.setup import config; |
||||
from src.models.random_walk import *; |
||||
from src.algorithms.random_walk import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'endpoint_random_walk', |
||||
]; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# ENDPOINT |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
@run_safely() |
||||
def endpoint_random_walk(command: CommandRandomWalk) -> Result[CallResult, CallError]: |
||||
landscape = Landscape( |
||||
values = command.landscape.values, |
||||
metric = command.landscape.neighbourhoods.metric, |
||||
); |
||||
match command.algorithm: |
||||
case EnumWalkMode.adaptive: |
||||
result = adaptive_walk_algorithm( |
||||
landscape = landscape, |
||||
r = command.landscape.neighbourhoods.radius, |
||||
optimise = command.optimise, |
||||
verbose = config.OPTIONS.random_walk.verbose |
||||
); |
||||
case EnumWalkMode.gradient: |
||||
result = gradient_walk_algorithm( |
||||
landscape = landscape, |
||||
r = command.landscape.neighbourhoods.radius, |
||||
optimise = command.optimise, |
||||
verbose = config.OPTIONS.random_walk.verbose |
||||
); |
||||
case EnumWalkMode.metropolis: |
||||
result = metropolis_walk_algorithm( |
||||
landscape = landscape, |
||||
r = command.landscape.neighbourhoods.radius, |
||||
annealing = command.annealing, |
||||
optimise = command.optimise, |
||||
verbose = config.OPTIONS.random_walk.verbose |
||||
); |
||||
case _ as alg: |
||||
raise Exception(f'No algorithm implemented for {alg.value}.'); |
||||
return Ok(CallResult(action_taken=True, message=result)); |
@ -0,0 +1,16 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from src.models.random_walk.landscape import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'Landscape', |
||||
]; |
@ -0,0 +1,90 @@
|
||||
#!/usr/bin/env python3 |
||||
# -*- coding: utf-8 -*- |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# IMPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
from __future__ import annotations; |
||||
|
||||
from src.thirdparty.maths import *; |
||||
from src.thirdparty.misc import *; |
||||
from src.thirdparty.types import *; |
||||
|
||||
from models.generated.commands import *; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# EXPORTS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
__all__ = [ |
||||
'Landscape', |
||||
]; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# METHOD fitness function -> Array |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
class Landscape(): |
||||
_fct: np.ndarray; |
||||
_metric: EnumLandscapeMetric; |
||||
_radius: float; |
||||
|
||||
def __init__( |
||||
self, |
||||
values: DataTypeLandscapeValues, |
||||
metric: EnumLandscapeMetric = EnumLandscapeMetric.maximum, |
||||
): |
||||
self._fct = convert_to_nparray(values); |
||||
self._metric = metric; |
||||
return; |
||||
|
||||
@property |
||||
def shape(self) -> tuple: |
||||
return self._fct.shape; |
||||
|
||||
@property |
||||
def dim(self) -> int: |
||||
return len(self._fct.shape); |
||||
|
||||
def fitness(self, *x: int) -> float: |
||||
return self._fct[x]; |
||||
|
||||
def neighbourhood(self, *x: int, r: float, strict: bool = False) -> List[tuple]: |
||||
sides = [ |
||||
[ 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) ] |
||||
for xx, s in zip(x, self.shape) |
||||
]; |
||||
match self._metric: |
||||
case EnumLandscapeMetric.maximum: |
||||
umg = list(itertools_product(*sides)); |
||||
case EnumLandscapeMetric.manhattan: |
||||
umg = [ |
||||
(*x[:i], xx, *x[(i+1):]) |
||||
for i, side in enumerate(sides) |
||||
for xx in side |
||||
]; |
||||
case _: |
||||
umg = [ x ]; |
||||
if strict: |
||||
umg = [ p for p in umg if p != x ]; |
||||
return umg; |
||||
|
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
# AUXILIARY METHODS |
||||
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
||||
|
||||
def convert_to_array(values: DataTypeLandscapeValues) -> list: |
||||
return [ |
||||
x if isinstance(x, float) else convert_to_array(x) |
||||
for x in values.__root__ |
||||
]; |
||||
|
||||
def convert_to_nparray(values: DataTypeLandscapeValues) -> np.ndarray: |
||||
try: |
||||
list_of_lists = convert_to_array(values); |
||||
return np.asarray(list_of_lists, dtype=float); |
||||
except: |
||||
raise ValueError('Could not convert to a d-dimensional array! Ensure that the dimensions are consistent.'); |
Loading…
Reference in new issue