Variables
Contents
Variables
Abstract variables
Variables functionnalities can be extended with Addons.
- class Variable(label, **kwargs)[source]
Bases:
abc.ABCVariables is an Abstract class defining what a variable is in a Search space.
- Parameters
label (str) – Name of the variable.
kwargs (dict) – Kwargs will be the different addons you want to add to a Variables. Known addons are: * to_discrete : VarConverter * to_continuous : VarConverter * neighbor : VarNeighborhood
- label
- abstract random(size=None)[source]
- abstract isconstant()[source]
- abstract subset()[source]
Base variables
Basic Variables are the low level bricks to define a variable in Zellij.
- class IntVar(label, lower, upper, sampler=<built-in method randint of numpy.random.mtrand.RandomState object>, **kwargs)[source]
Bases:
zellij.core.variables.VariableIntVar is a Variables discribing an Integer variable. T he
lowerandupperbounds are included.- 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.
- up_bound
Lower bound of the variable
- Type
int
- low_bound
Upper bound of the variable
- Type
int
Examples
>>> from zellij.core.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 Variables is a constant (
lower`==:code:`upper), False otherwise.- Return type
boolean
- subset(lower, upper)[source]
- class FloatVar(label, lower, upper, sampler=<built-in method uniform of numpy.random.mtrand.RandomState object>, tolerance=1e-14, **kwargs)[source]
Bases:
zellij.core.variables.VariableFloatVar is a Variables discribing a Float variable.
- 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 zellij.core.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 Variables is a constant (
lower`==:code:`upper), False otherwise.- Return type
boolean
- subset(lower, upper)[source]
- class CatVar(Variable)[source]
Bases:
zellij.core.variables.VariableCatVar is a Variables discribing what a categorical variable is.
- 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 zellij.core.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]
- isconstant()[source]
- Returns
out – Return True, if this Variables is a constant (
len(feature)==1), False otherwise.- Return type
boolean
- subset(lower, upper)[source]
Constant
The constant Variables should not be used. In particular conditions, it can sometimes replace a regular Variables, particularly for decomposition Metaheuristic. User should implement directly the constant inside the loss function.
- class Constant(label, value, **kwargs)[source]
Bases:
zellij.core.variables.VariableConstantis a Variables 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 zellij.core.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
- subset(l, u)[source]
Composed variables
Composed Variables are Variables made of other Variables.
- class ArrayVar(Variable)[source]
Bases:
zellij.core.variables.VariableArrayVaris a Variables describing a list of Variables. This class is iterable.- Parameters
label (str) – Name of the variable.
*args (list[Variable]) – Elements of the
ArrayVar. All elements must be of type Variables
Examples
>>> from zellij.core.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 Variables of
ArrayVar. Ifsize>1, return a list of list- Return type
float or list[float]
- isconstant()[source]
- Returns
out – Return True, if this Variables is a constant (all elements are constants), False otherwise.
- Return type
boolean
- subset(lower, upper)[source]
- index(value)[source]
Return the index inside the :code:
ArrayVarof a givenvalue.- 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
Future variables
Future Variables, are variables that are implemented in Zellij. However they are not yet supported by the whole package. (They cannot be save for example)
- class Block(Variable)[source]
Bases:
zellij.core.variables.VariableA Block is a Variables which will repeat multiple times a Variables.
- Parameters
label (str) – Name of the variable.
value (Variable) – Variables that will be repeated
repeat (int) – Number of repeats.
Examples
>>> from zellij.core.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 Variables random() method, repeated repeat times. If size > 1, return a list of list.
- Return type
float or list[float]
- isconstant()[source]
- subset(lower, upper)[source]
- class DynamicBlock(Block)[source]
Bases:
zellij.core.variables.BlockA DynamicBlock is a Block with a random number of repeats.
- Parameters
label (str) – Name of the variable.
value (Variable) – Variables that will be repeated
repeat (int) – Maximum number of repeats.
Examples
>>> from zellij.core.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.
- Returns
out – Return a list composed of the results from the Variables 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