Don't explode low/high, but use explicit parameters

This commit is contained in:
Matthias 2021-04-10 07:12:31 +02:00
parent 34e47db18d
commit ea4b5d675d

View File

@ -25,9 +25,8 @@ class BaseParameter(ABC):
category: Optional[str] category: Optional[str]
default: Any default: Any
value: Any value: Any
opt_range: Sequence[Any]
def __init__(self, *, opt_range: Sequence[Any], default: Any, space: Optional[str] = None, def __init__(self, *, default: Any, space: Optional[str] = None,
optimize: bool = True, load: bool = True, **kwargs): optimize: bool = True, load: bool = True, **kwargs):
""" """
Initialize hyperopt-optimizable parameter. Initialize hyperopt-optimizable parameter.
@ -44,7 +43,6 @@ class BaseParameter(ABC):
self.category = space self.category = space
self._space_params = kwargs self._space_params = kwargs
self.value = default self.value = default
self.opt_range = opt_range
self.optimize = optimize self.optimize = optimize
self.load = load self.load = load
@ -69,7 +67,6 @@ class BaseParameter(ABC):
class IntParameter(BaseParameter): class IntParameter(BaseParameter):
default: int default: int
value: int value: int
opt_range: Sequence[int]
def __init__(self, low: Union[int, Sequence[int]], high: Optional[int] = None, *, default: int, def __init__(self, low: Union[int, Sequence[int]], high: Optional[int] = None, *, default: int,
space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs): space: Optional[str] = None, optimize: bool = True, load: bool = True, **kwargs):
@ -90,10 +87,12 @@ class IntParameter(BaseParameter):
if high is None or isinstance(low, Sequence): if high is None or isinstance(low, Sequence):
if not isinstance(low, Sequence) or len(low) != 2: if not isinstance(low, Sequence) or len(low) != 2:
raise OperationalException('IntParameter space must be [low, high]') raise OperationalException('IntParameter space must be [low, high]')
opt_range = low self.low, self.high = low
else: else:
opt_range = [low, high] self.low = low
super().__init__(opt_range=opt_range, default=default, space=space, optimize=optimize, self.high = high
super().__init__(default=default, space=space, optimize=optimize,
load=load, **kwargs) load=load, **kwargs)
def get_space(self, name: str) -> 'Integer': def get_space(self, name: str) -> 'Integer':
@ -101,13 +100,12 @@ class IntParameter(BaseParameter):
Create skopt optimization space. Create skopt optimization space.
:param name: A name of parameter field. :param name: A name of parameter field.
""" """
return Integer(*self.opt_range, name=name, **self._space_params) return Integer(low=self.low, high=self.high, name=name, **self._space_params)
class RealParameter(BaseParameter): class RealParameter(BaseParameter):
default: float default: float
value: float value: float
opt_range: Sequence[float]
def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *, def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *,
default: float, space: Optional[str] = None, optimize: bool = True, default: float, space: Optional[str] = None, optimize: bool = True,
@ -129,10 +127,11 @@ class RealParameter(BaseParameter):
if high is None or isinstance(low, Sequence): if high is None or isinstance(low, Sequence):
if not isinstance(low, Sequence) or len(low) != 2: if not isinstance(low, Sequence) or len(low) != 2:
raise OperationalException(f'{self.__class__.__name__} space must be [low, high]') raise OperationalException(f'{self.__class__.__name__} space must be [low, high]')
opt_range = low self.low, self.high = low
else: else:
opt_range = [low, high] self.low = low
super().__init__(opt_range=opt_range, default=default, space=space, optimize=optimize, self.high = high
super().__init__(default=default, space=space, optimize=optimize,
load=load, **kwargs) load=load, **kwargs)
def get_space(self, name: str) -> 'Real': def get_space(self, name: str) -> 'Real':
@ -140,13 +139,12 @@ class RealParameter(BaseParameter):
Create skopt optimization space. Create skopt optimization space.
:param name: A name of parameter field. :param name: A name of parameter field.
""" """
return Real(*self.opt_range, name=name, **self._space_params) return Real(low=self.low, high=self.high, name=name, **self._space_params)
class DecimalParameter(RealParameter): class DecimalParameter(BaseParameter):
default: float default: float
value: float value: float
opt_range: Sequence[float]
def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *, def __init__(self, low: Union[float, Sequence[float]], high: Optional[float] = None, *,
default: float, decimals: int = 3, space: Optional[str] = None, default: float, decimals: int = 3, space: Optional[str] = None,
@ -162,11 +160,22 @@ class DecimalParameter(RealParameter):
parameter fieldname is prefixed with 'buy_' or 'sell_'. parameter fieldname is prefixed with 'buy_' or 'sell_'.
:param optimize: Include parameter in hyperopt optimizations. :param optimize: Include parameter in hyperopt optimizations.
:param load: Load parameter value from {space}_params. :param load: Load parameter value from {space}_params.
:param kwargs: Extra parameters to skopt.space.Real. :param kwargs: Extra parameters to skopt.space.Integer.
""" """
self._decimals = decimals self._decimals = decimals
default = round(default, self._decimals) default = round(default, self._decimals)
super().__init__(low=low, high=high, default=default, space=space, optimize=optimize,
if high is not None and isinstance(low, Sequence):
raise OperationalException(f'{self.__class__.__name__} space invalid.')
if high is None or isinstance(low, Sequence):
if not isinstance(low, Sequence) or len(low) != 2:
raise OperationalException(f'{self.__class__.__name__} space must be [low, high]')
self.low, self.high = low
else:
self.low = low
self.high = high
super().__init__(default=default, space=space, optimize=optimize,
load=load, **kwargs) load=load, **kwargs)
def get_space(self, name: str) -> 'SKDecimal': def get_space(self, name: str) -> 'SKDecimal':
@ -174,7 +183,7 @@ class DecimalParameter(RealParameter):
Create skopt optimization space. Create skopt optimization space.
:param name: A name of parameter field. :param name: A name of parameter field.
""" """
return SKDecimal(*self.opt_range, decimals=self._decimals, name=name, return SKDecimal(low=self.low, high=self.high, decimals=self._decimals, name=name,
**self._space_params) **self._space_params)
@ -200,7 +209,8 @@ class CategoricalParameter(BaseParameter):
if len(categories) < 2: if len(categories) < 2:
raise OperationalException( raise OperationalException(
'CategoricalParameter space must be [a, b, ...] (at least two parameters)') 'CategoricalParameter space must be [a, b, ...] (at least two parameters)')
super().__init__(opt_range=categories, default=default, space=space, optimize=optimize, self.opt_range = categories
super().__init__(default=default, space=space, optimize=optimize,
load=load, **kwargs) load=load, **kwargs)
def get_space(self, name: str) -> 'Categorical': def get_space(self, name: str) -> 'Categorical':