# Variable¶

 API of variable generation `VarElement`(name[, lowBound, upBound, ini_value]) Base Variable class `VarContinuous`(name[, lowBound, upBound, ...]) Continuous Variable `VarInteger`(name, lowBound, upBound, ini_value) Integer Variable `VarBinary`(name[, ini_value, spin]) Binary Variable `VarSpin`(name, ini_value[, binary]) Spin Variable, which takes only 1 or -1 `VarPermutation`(name[, lowBound, upBound, ...]) Permutation Variable
class flopt.variable.VariableFactory[source]

API of variable generation

array(name, shape, lowBound=None, upBound=None, cat='Continuous', ini_value=None)[source]
Parameters
• name (str) – name of variable

• shape (int of tuple of int) – shape of array

• lowBound (number class or array of number class) – lowBound

• upBound (number class or array of number class) – upBound

• cat (str or array of cat) – category of variable

• ini_value (number class or array of number class) – set value to variable

Return type

numpy.array

Examples

```>>> Variable.array('x', 2, cat='Binary')
>>> array([Variable(x_0, cat="Binary", ini_value=0),
>>>        Variable(x_1, cat="Binary", ini_value=0)], dtype=object)
>>>
>>> Variable.array('x', (2, 2), cat='Binary')
>>> array([[Variable(x_0_0, cat="Binary", ini_value=0),
>>>         Variable(x_0_1, cat="Binary", ini_value=0)],
>>>        [Variable(x_1_0, cat="Binary", ini_value=0),
>>>         Variable(x_1_1, cat="Binary", ini_value=0)]], dtype=object)
```
classmethod dict(name, keys, lowBound=None, upBound=None, cat='Continuous', ini_value=None)[source]
Parameters
• name (str) – name of variable

• keys (tuple or generator) – keys of variable dictionary

• lowBound (float, optional) – lowBound

• upBound (float, optional) – upBound

• cat (str, optional) – category of variable

• ini_value (float, optional) – set value to variable

Return type

dict

Examples

```>>> Variable.dict('x', [0, 1])
>>> {0: Variable(x_0, cat="Continuous", ini_value=0.0),
>>>  1: Variable(x_1, cat="Continuous", ini_value=0.0)}
>>>
>>> Variable.dict('x', range(2), cat='Binary')
>>> {0: Variable(x_0, cat="Binary", ini_value=0),
>>>  1: Variable(x_1, cat="Binary", ini_value=0)}
>>>
>>> Variable.dict('x', (range(2), range(2)), cat='Binary')
>>> {(0, 0): Variable(x_0_0, cat="Binary", ini_value=0),
(0, 1): Variable(x_0_1, cat="Binary", ini_value=0),
(1, 0): Variable(x_1_0, cat="Binary", ini_value=0),
(1, 1): Variable(x_1_1, cat="Binary", ini_value=0)}
>>>
>>> # not work
>>> # Variable.dict('x', [range(2), range(2)], cat='Binary')
```
classmethod dicts(name, indices=None, lowBound=None, upBound=None, cat=VariableType.Continuous, ini_value=None, index_start=[])[source]
Parameters
• name (str) – name of variable

• indices (tuple or generator) – keys of variable dictionary

• lowBound (float, optional) – lowBound

• upBound (float, optional) – upBound

• cat (str, optional) – category of variable

• ini_value (float, optional) – set value to variable

Return type

dict

classmethod matrix(name, n_row, n_col, lowBound=None, upBound=None, cat='Continuous', ini_value=None)[source]

Overwrap of VariableFactory.array

Parameters
• name (str) – name of variable

• n_row (int) – number of rows

• n_col (int) – number of columns

• lowBound (number class or array of number class) – lowBound

• upBound (number class or array of number class) – upBound

• cat (str or array of cat) – category of variable

• ini_value (number class or array of number class) – set value to variable

Return type

numpy.array

Examples

```>>> Variable.matrix('x', 2, 2, cat='Binary')
>>> array([[Variable(x_0_0, cat="Binary", ini_value=0),
>>>         Variable(x_0_1, cat="Binary", ini_value=0)],
>>>        [Variable(x_1_0, cat="Binary", ini_value=0),
>>>         Variable(x_1_1, cat="Binary", ini_value=0)]], dtype=object)
```
class flopt.variable.VarElement(name, lowBound=None, upBound=None, ini_value=None)[source]

Base Variable class

clip()[source]

map in an feasible area by clipping. ex. value < lowBound -> value = lowBound, value > upBound -> value = upBound

diff(x)[source]
Parameters

x (VarElement family) –

Returns

the expression differentiated by x

Return type

Expression

feasible()[source]
Returns

return true if value of self is in between lowBound and upBound else false

Return type

bool

setRandom(scale=1.0)[source]

set random value to variable

type()[source]
Returns

return variable type

Return type

str

value(*args, **kwargs)[source]
Returns

return value of variable

Return type

float or int

class flopt.variable.VarContinuous(name, lowBound=None, upBound=None, ini_value=None)[source]

Continuous Variable

class flopt.variable.VarInteger(name, lowBound, upBound, ini_value)[source]

Integer Variable

class flopt.variable.VarBinary(name, ini_value=None, spin=None)[source]

Binary Variable

Note

Binary Variable behaves differently in “-” and “~” operation.

“-” is the subtraction as interger variable, and “~” is the inversion as binary (bool) variable.

```>>> a = Variable('a', intValue=1, cat='Binary')
>>> a.value()
>>> 1
>>> (-a).value()
>>> -1
>>> (~a).value()
>>> 0
```
class flopt.variable.VarSpin(name, ini_value, binary=None)[source]

Spin Variable, which takes only 1 or -1

class flopt.variable.VarPermutation(name, lowBound=None, upBound=None, ini_value=None)[source]

Permutation Variable

This has [lowBound, … upBound] range permutation.

Examples

```>>> a = Variable('a', lowBound=0, upBound=3, cat='Permutation')
>>> a.value()
>>> [2, 1, 3, 0]   # randomized
>>> b = Variable('b', lowBound=0, upBound=3, ini_value=[0,1,2,3], cat='Permutation')
>>> b.value()
>>> [0, 1, 2, 3]
```

We can use list operation to Permutation Variable

```>>> b[1]
>>> 1
>>> len(b)
>>> 4
>>> b[1:3]
>>> [1, 2]
```
setRandom(scale=None)[source]

shuffle the list

value(*args, **kwargs)[source]
Return type

list