Expression¶
- class flopt.expression.Expression(elmA, elmB, operator, name=None)[source]¶
Expression Base Class
This represents the operation of two items elmA (operator) elmB
- Parameters
elmA (Variable family or Expression family) – first element
elmB (Variable family or Expression family) – later element
operator (str) – operator between elmA and elmB
- elmA¶
first element
- Type
Variable family or Expression family
- elmB¶
later element
- Type
Variable family or Expression family
- operator¶
operator between elmA and elmB
- Type
str
Examples
import flopt from flopt.expression import Expression a = flopt.Variable(name="a", ini_value=1, cat="Integer") b = flopt.Variable(name="b", ini_value=2, cat="Continuous") c = Expression(a, b, "+") # same as a + b c >>> a+b c.value() >>> 3 c.getVariables() >>> {VarElement("b", 1, 2, 2), VarElement("a", 0, 1, 1)}
operator “+”, “-”, “*”, “/”, “^” and “%” are supported for Integer, Binary and Continuous Variables.
import flopt from flopt.expression import Expression a = flopt.Variable(name="a", ini_value=1, cat="Integer") # a.value() is 1 b = flopt.Variable(name="b", ini_value=2, cat="Continuous") # b.value() is 2 Expression(a, b, "+").value() # a+b addition >>> 3 Expression(a, b, "-").value() # a-b substraction >>> -1 Expression(a, b, "*").value() # a*b multiplication >>> 2 Expression(a, b, "/").value() # a/b division >>> 0.5 Expression(a, b, "^").value() # a/b division >>> 1 Expression(a, b, "%").value() # a%b modulo >>> 1
operator “&”, “|” are supported for Binary Variable.
import flopt from flopt.expression import Expression a = flopt.Variable(name="a", ini_value=1, cat="Binary") b = flopt.Variable(name="b", ini_value=0, cat="Binary") Expression(a, b, "&").value().value() # a&b bitwise and >>> 0 Expression(a, b, "|").value().value() # a&b bitwise or >>> 1
- diff(x)¶
- Parameters
x (VarElement family) –
- Returns
the expression differentiated by x
- Return type
- expand()¶
- Return type
- hess(x)[source]¶
hessian :param x: :type x: list or numpy.array of VarElement family
- Returns
hess – hess[i, j] = hessian of self for x[i] and x[j]
- Return type
numpy array of Expression
Examples
import flopt x = flopt.Variable("x") y = flopt.Variable("y") f = x * x * y # hessian matrix for [x, y] print(f.hess([x, y])) >>> [[Expression(y, 0, +) Expression(x, 0, +)] >>> [Expression(2*x, 0, +) Const(0)]] # hessian matrix for [x, y] print(f.hess([y, x])) >>> [[Const(0) Expression(2*x, 0, +)] >>> [Expression(x, 0, +) Expression(y, 0, +)]]
- isIsing()¶
- Returns
return true if this expression is ising else false
- Return type
bool
- isLinear()¶
- Returns
return true if this expression is linear else false
- Return type
bool
Examples
>>> from flopt import Variable >>> a = Variable('a', ini_value=3) >>> b = Variable('b', ini_value=3) >>> (a+b).isLinear() >>> True >>> (a*b).isLinear() >>> False
- isQuadratic()¶
- Returns
return true if this expression is quadratic else false
- Return type
bool
- jac(x)[source]¶
jacobian :param x: :type x: list or numpy.array of VarElement family
- Returns
jac – jac[i] = jacobian of self for x[i]
- Return type
numpy array of Expression
Examples
import flopt x = flopt.Variable("x") y = flopt.Variable("y") f = x * y # jacobian vector for [x, y] f.jac([x, y]) >>> [Expression(y, 0, +) Expression(x, 0, +)] # jacobian vector for [y, x] f.jac([y, x]) >>> [Expression(x, 0, +) Expression(y, 0, +)]
- max(*args, **kwargs)¶
Calculate max value of expression when expression is linear or quadratic
- Returns
maximum value of this expression can take
- Return type
float
- maximize(solver='auto', *args, **kwargs)¶
Optimize the maximize problem has this expression as objective
- min(*args, **kwargs)¶
Calculate min value of expression when expression is linear or quadratic
- Returns
minimum value of this expression can take
- Return type
float
- minimize(solver='auto', *args, **kwargs)¶
Optimize the minimize problem has this expression as objective
- simplify()¶
- Return type
- toBinary()¶
create expression replased binary to spin
- Return type
- toIsing(x=None)¶
- Parameters
x (list or numpy.array or VarElement family) –
- Returns
IsingStructure(‘IsingStructure’, ‘J h x’), converted from sum(a_ij x_i x_j; i >= j) + sum(b_i x_i) + c = sum(a_ij x_i x_j; i >= j) + sum(b_i x_i) + sum(c/n x_i x_i), as J_ij = a_ij (i != j), a_ii + c/n (i == j), h_i = b_i
- Return type
collections.namedtuple
- toLinear(x=None)¶
- Parameters
x (list or numpy.array of VarElement family) –
- Returns
LinearStructure = collections.namedtuple(‘LinearStructure’, ‘c C x’), where c.T.dot(x) + C
- Return type
collections.namedtuple
- toQuadratic(x=None)¶
- Parameters
x (list or numpy.array or VarElement family) –
- Returns
QuadraticStructure(‘QuadraticStructure’, ‘Q c C x’), such that 1/2 x^T Q x + c^T x + C, Q^T = Q
- Return type
collections.namedtuple
- toSpin()¶
create expression replased binary to spin
- Return type
- traverseAncestors()¶
traverse ancestors of self
- Yields
Expression or VarElement