Base Variables implementation#
- class Variable(label, **kwargs)[source]
Bases:
ABC
Variable is an Abstract class defining what a variable is in a given search space.
- Parameters:
label (str) – Name of the variable.
kwargs (dict) – Kwargs will be the different addons you want to add to a Variable. Known addons are: * neighbor : VarNeighborhood
- label
- abstract random(size=None)[source]
- abstract isconstant()[source]
Variables functionnalities can be extended with Addons.
Base Variables are the low level bricks to compose a search space in DRAGON.
- class IntVar(label, lower, upper, sampler=<built-in method randint of numpy.random.mtrand.RandomState object>, **kwargs)[source]
Bases:
Variable
IntVar defines Variable discribing Integer variables. The user must specify a
lower
and anupper
bounds for the Integer variable.- Parameters:
label (str) – Name of the variable.
lower (int) – Lower bound of the variable
upper (int) – Upper bound of the variable
sampler (Callable, default=np.random.randint) – Function that takes lower bound, upper bound and a size as parameters and defines how the random values for the variable should be sampled.
- up_bound
Lower bound of the variable
- : int
Upper bound of the variable
- Type:
int
Examples
>>> from dragon.search_space.base_variables import IntVar >>> a = IntVar("test", 0, 5) >>> print(a) IntVar(test, [0;5]) >>> a.random() 1
- random(size=None)[source]
- Parameters:
size (int, default=None) – Number of draws.
- Returns:
out – Return an int if
size`=1, a :code:`list[int]
else.- Return type:
int or list[int]
- isconstant()[source]
- Returns:
out – Return True, if this Variable is a constant (
lower`==:code:`upper
), False otherwise.- Return type:
boolean
- class FloatVar(label, lower, upper, sampler=<built-in method uniform of numpy.random.mtrand.RandomState object>, tolerance=1e-14, **kwargs)[source]
Bases:
Variable
FloatVar defines Variable discribing Float variables.
- Parameters:
label (str) – Name of the variable.
lower ({int,float}) – Lower bound of the variable
upper ({int,float}) – Upper bound of the variable
sampler (Callable, default=np.random.uniform) – Function that takes lower bound, upper bound and a size as parameters.
- up_bound
Lower bound of the variable
- Type:
{int,float}
- low_bound
Upper bound of the variable
- Type:
{int,float}
Examples
>>> from dragon.search_space.base_variables import FloatVar >>> a = FloatVar("test", 0, 5.0) >>> print(a) FloatVar(test, [0;5.0]) >>> a.random() 2.2011985711663056
- random(size=None)[source]
- Parameters:
size (int, default=None) – Number of draws.
- Returns:
out – Return a float if
size`=1, a :code:`list[float]
else.- Return type:
float or list[float]
- isconstant()[source]
- Returns:
out – Return True, if this Variable is a constant (
lower`==:code:`upper
), False otherwise.- Return type:
boolean
- class CatVar(Variable)[source]
Bases:
Variable
CatVar defines Variable discribing categorical variables.
- Parameters:
label (str) – Name of the variable.
features (list) – List of all choices.
weights (list[float]) – Weights associated to each elements of
features
. The sum of all positive elements of this list, must be equal to 1.
- features
- weights
Examples
>>> from dragon.search_space.base_variables import CatVar, IntVar >>> a = CatVar("test", ['a', 1, 2.56, IntVar("int", 100 , 200)]) >>> print(a) CatVar(test, ['a', 1, 2.56, IntVar(int, [100;200])]) >>> a.random(10) ['a', 180, 2.56, 'a', 'a', 2.56, 185, 2.56, 105, 1]
- random(size=1)[source]
- Parameters:
size (int, default=1) – Number of draws.
- Returns:
out – Return a feature if
size`=1, a :code:`list[features]
else. Features can be Variable. When seleted, it will return a random point from this Variable.- Return type:
float or list[float]
- isconstant()[source]
- Returns:
out – Return True, if this Variable is a constant (
len(feature)==1
), False otherwise.- Return type:
boolean
- class Constant(label, value, **kwargs)[source]
Bases:
Variable
Constant
is a Variable discribing a constant of any type.- Parameters:
label (str) – Name of the variable.
value (object) – Constant value
- label
Name of the variable.
- Type:
str
- value
Constant value
- Type:
object
Examples
>>> from dragon.search_space.base_variables import Constant >>> a = Constant("test", 5) >>> print(a) Constant(test, 5) >>> a.random() 5
- random(size=None)[source]
- Parameters:
size (int, default=None) – Number of draws.
- Returns:
out – Return an int if
size`=1, a :code:`list[self.value]
else.- Return type:
int or list[int]
- isconstant()[source]
- Returns:
out – Return True
- Return type:
boolean