CustomExpression

class flopt.expression.CustomExpression(func, args, name=None)[source]

Objective function from using user defined function.

Parameters:
  • func (function) – objective function

  • arg (list of variables) –

Examples

We have the objective funcion \(simulater(a, b)\) where simulater is a black box function, a and b are continuous variable. In this case, we can input objective function into Problem by using CustomExpression as follows.

a = Variable("a", cat="Continuous")
b = Variable("b", cat="Continuous")
def user_simulater(a, b):
    return simulater(a, b)
obj = CustomExpression(func=user_simulater, args=[a, b])
prob = Problem("simulater")
prob += obj

Note

The order of variables in arg parameter must be the same as the func argument. (However even the name does not have to be the same.)

In addition, we can use some operations (“+”, “-”, “*”, “/”) between CustomExpression and Variable, Expression and CustomExpression.

>>> def user_func(x):
>>>     return x
>>> a = Variable('a', ini_value=3)
>>> obj = CustomExpression(user_func, [a])
>>> obj.value()
>>> 3

For example,

>>> b = Variable('b', ini_value=1)
>>> obj_b = obj + b  # 3+1
>>> obj_b.value()
>>> 4
>>> obj_b.getVariables()
>>> [VarElement("a", -10000000000.0, 10000000000.0, 3),
     VarElement("b", -10000000000.0, 10000000000.0, 1)]