Problem

class flopt.problem.Problem(name=None, sense=OptimizationType.Minimize)[source]

Interface between User and Solver

Parameters:
  • name (str) – name of problem

  • sense (str, optional) – minimize, maximize (future satisfiability is added)

name

name of problem

Type:

str

sense

minimize, maximize (future satisfiability is added)

Type:

str, optional

obj
Type:

Expression family

solver
Type:

Solver or None

time

solving time

Type:

float

Examples

>>> prob = Problem(name='test')

When we want to solve the maximize problem, then

>>> prob = Problem(name='test', sense='maximize')

We solve

>>> prob.solve(solver=solver_name or solver object, timelimit=10)

After solving, we can obtain the objective value.

>>> prob.getObjectiveValue()
addConstraint(const, name=None)[source]

add constraint into problem. __iadd__(), “+=” operations call this function.

Parameters:
  • const (Constraint) – constraint

  • name (str or None) – constraint name

Examples

import flopt
prob = flopt.Problem(algo=...)
x = flopt.Variable("x")
y = flopt.Variable("y")
prob.addConstraint(x + y >= 2)
boundsToIneq()[source]

Create a problem object has bounds constraints of variables as inequal constraints

Returns:

prob

Return type:

Problem

clone(variable_clone=False)[source]

create clone object :param variable_clone: if it is true, variables are cloned in expression :type variable_clone: bool

Returns:

prob

Return type:

Problem

getConstraints()[source]
Returns:

list of constraints in this problem

Return type:

list of Constraint

getObjectiveValue()[source]
Returns:

the objective value

Return type:

float or int

getSolution(k=1)[source]

get the k-top solution

Parameters:

k (int) – return k-top solution

getVariables()[source]
Returns:

set of VarElement used in this problem

Return type:

set

removeDuplicatedConstraints()[source]

Remove duplicated constraints in problem

Examples

import flopt
a = flopt.Variable("a")
b = flopt.Variable("b")
c = flopt.Variable("c")

prob = flopt.Problem(name="Test")
prob += a + b >= 0
prob += a + b >= 0
prob += a >= -b
prob += 0 >= -a - b
prob += Sum([a, b]) >= 0

len(prob.constraints)
>>> 5

prob.removeDuplicatedConstraints()
len(prob.constraints)
>>> 1
replace(correspondence_dict)[source]

Replace variable to another variables or expression

Parameters:

correspondence_dict (dict) – key is Variable and value is Variable or ExpressionElement

Examples

import flopt

# create problem
x = flopt.Variable("x", lowBound=4, upBound=5)
prob = flopt.Problem()
prob += x
prob.show()
>>> Name: None
>>> Type         : Problem
>>> sense        : Minimize
>>> objective    : x+0
>>> #constraints : 0
>>> #variables   : 1 (Continuous 1)
>>>
>>>
>>> V 0, name x, Continuous 4 <= x <= 5

# convert bounds of variables to constraints
prob = prob.clone().boundsToIneq()
prob.show()
>>> Name: None
>>>   Type         : Problem
>>>   sense        : Minimize
>>>   objective    : x+0
>>>   #constraints : 2
>>>   #variables   : 1 (Continuous 1)
>>>
>>>   C 0, name None, 4-x <= 0
>>>   C 1, name None, x-5 <= 0
>>>
>>>   V 0, name x, Continuous None <= x <= None

# replace x with x_plus + x_minus
x_plus = flopt.Variable("x_plus", lowBound=0)
x_minus = flopt.Variable("x_minus", lowBound=0)
prob.replace(correspondence_dict={x: x_plus + x_minus})
prob.show()
>>> Name: None
>>>   Type         : Problem
>>>   sense        : Minimize
>>>   objective    : x_plus+x_minus
>>>   #constraints : 2
>>>   #variables   : 2 (Continuous 2)
>>>
>>>   C 0, name None, 4-(x_plus+x_minus) <= 0
>>>   C 1, name None, x_plus+x_minus-5 <= 0
>>>
>>>   V 0, name x_minus, Continuous 0 <= x_minus <= None
>>>   V 1, name x_plus, Continuous 0 <= x_plus <= None
setBestBound(best_bound)[source]
Parameters:

best_bound (float) – best objective value of this problem

setObjective(obj, name=None)[source]

set objective function. __iadd__(), “+=” operations call this function.

Parameters:

obj (int, float, Variable family or Expression family) – objective function

setSolution(k=1)[source]

set the k-top solution to variables

Parameters:

k (int) – set k-top solution data to variables

solve(solver=None, timelimit=None, lowerbound=None, optimized_variables=None, msg=False, **kwargs)[source]

solve this problem

Parameters:
  • solver (Solver or None) –

  • timelimit (float or None) –

  • lowerbound (float or None) – solver terminates when it obtains the solution whose objective value is lower than this value

  • optimized_variables (None or list, tuple, np.ndarray or any container of Variable) – if it is specified, solver will optimize only the variables in optimized_variables

  • msg (bool) – if true, display the message from solver

Returns:

  • Status – return the status of solving

  • Log – return log object

Examples

import flopt
a = flopt.Variable("a")
b = flopt.Variable("b")
c = flopt.Variable("c")

prob = flopt.Problem(name="Test")
prob += a + b
prob += a + b >= 0

solver = flopt.Solver("auto")
status, logs = prob.solve(solver=solver)

When user want to optimize a part of variables under otherwise variables are fixed, user specify optmized_variables in problem.solve().

# optimize only a
status, log = prob.solve(optimized_variables=[a], timelimit=1)
toEq()[source]

Create a problem object with only equal constraints

Returns:

prob

Return type:

Problem

toIneq()[source]

Create a problem object with only inequal constraints

Returns:

prob

Return type:

Problem

toProblemType()[source]
Returns:

problem_type – key is “Variable”, “Objective”, “Constraint”

Return type:

dict

toRelax()[source]

Create relaxed problem

Returns:

prob

Return type:

Problem