Composed variables implementation#
Composed Variables are Variables made of other Variables.
- class ArrayVar(Variable)[source]
Bases:
Variable
ArrayVar
defines Variable describing lists of Variable. This class is iterable.- Parameters:
label (str) – Name of the variable.
*args (list[Variable]) – Elements of the
ArrayVar
. All elements must be of type Variable
Examples
>>> from dragon.search_space.base_variables import ArrayVar, IntVar, FloatVar, CatVar >>> a = ArrayVar(IntVar("int_1", 0,8), ... IntVar("int_2", 4,45), ... FloatVar("float_1", 2,12), ... CatVar("cat_1", ["Hello", 87, 2.56])) >>> print(a) ArrayVar(, [IntVar(int_1, [0;8]), IntVar(int_2, [4;45]), FloatVar(float_1, [2;12]), CatVar(cat_1, ['Hello', 87, 2.56])]) >>> a.random() [5, 15, 8.483221226216427, 'Hello']
- random(size=1)[source]
- Parameters:
size (int, default=None) – Number of draws.
- Returns:
out – Return a list composed of the values returned by each Variable of
ArrayVar
. Ifsize
>1, return a list of list- Return type:
float or list[float]
- isconstant()[source]
- Returns:
out – Return True, if this Variable is a constant (all elements are constants), False otherwise.
- Return type:
boolean
- index(value)[source]
Return the index inside the
ArrayVar
of a givenvalue
.- Parameters:
value (Variable) – Targeted Variable in the ArrayVar
- Returns:
Index of
value
.- Return type:
int
- append(v)[source]
Append a
Variable
to theArrayVar
.- Parameters:
v (Variable) – Variable to be added to the
ArrayVar
- class Block(Variable)[source]
Bases:
Variable
Block defines Variable which will repeat multiple times a Variable.
- Parameters:
label (str) – Name of the variable.
value (Variable) – Variable that will be repeated
repeat (int) – Number of repeats.
Examples
>>> from dragon.search_space.base_variables import Block, ArrayVar, FloatVar, IntVar >>> content = ArrayVar("test", ... IntVar("int_1", 0,8), ... IntVar("int_2", 4,45), ... FloatVar("float_1", 2,12)) >>> a = Block("size 3 Block", content, 3) >>> print(a) Block(size 3 Block, [IntVar(int_1, [0;8]), IntVar(int_2, [4;45]), FloatVar(float_1, [2;12]),]) >>> a.random(3) [[[7, 22, 6.843164591359903], [5, 18, 10.608957810018786], [4, 21, 10.999649079045858]], [[5, 9, 9.773288692746476], [1, 12, 6.1909724243671445], [4, 12, 9.404313234593669]], [[4, 10, 2.72648188721585], [1, 44, 5.319257221471118], [4, 24, 9.153357213126071]]]
- random(size=1)[source]
- Parameters:
size (int, default=None) – Number of draws.
- Returns:
out – Return a list composed of the results from the Variable random() method, repeated repeat times. If size > 1, return a list of list.
- Return type:
float or list[float]
- isconstant()[source]
- Returns:
out – Return True, if this Variable is a constant (the repeated Variable is constant), False otherwise.
- Return type:
boolean
- class DynamicBlock(Block)[source]
Bases:
Block
A DynamicBlock is a Block with a random number of repeats.
- Parameters:
label (str) – Name of the variable.
value (Variable) – Variable that will be repeated
repeat (int) – Maximum number of repeats.
Examples
>>> from dragon.search_space.base_variables import DynamicBlock, ArrayVar, FloatVar, IntVar >>> content = ArrayVar(IntVar("int_1", 0,8), ... IntVar("int_2", 4,45), ... FloatVar("float_1", 2,12)) >>> a = DynamicBlock("max size 10 Block", content, 10) >>> print(a) DynamicBlock(max size 10 Block, [IntVar(int_1, [0;8]), IntVar(int_2, [4;45]), FloatVar(float_1, [2;12]),]) >>> a.random() [[[3, 12, 10.662362255103403], [7, 9, 5.496860842510198], [3, 37, 7.25449459082227], [4, 28, 4.912883181322568]], [[3, 23, 5.150228671772998]], [[6, 30, 6.1181372194738515]]]
- random(size=1)[source]
- Parameters:
size (int, default=None) – Number of draws.
n_repeat (max size of randomly generated block)
- Returns:
out – Return a list composed of the results from the Variable random() method, repeated repeat times. If size > 1, return a list of list.
- Return type:
float or list[float]
- isconstant()[source]
- Returns:
out – Return False, a dynamic block cannot be constant. (It is a binary)
- Return type:
False