Zellij Variables

Zellij Variables#

DRAGON search space is based on the class Variable, retrieved from the zellij package.

Variable#

Variable functionnalities can be extended with Addons.

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]

Base variables#

Basic Variable 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 an upper 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.zellij_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.zellij_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.zellij_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.zellij_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

Composed variables#

Composed Variable are Variable made of other Variable.

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.zellij_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. 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 (all elements are constants), False otherwise.

Return type:

boolean

index(value)[source]

Return the index inside the :code:ArrayVar of a given value.

Parameters:

value (Variable) – Targeted Variable in the ArrayVar

Returns:

Index of value.

Return type:

int

append(v)[source]

Append a Variables to the :code:ArrayVar.

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.zellij_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.zellij_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