Skip to content

Model

Model

The optimization model.

Model regular linear constraints and variables like

m = Model()
m += xsum(2 * m.addVar(lb=0, ub=1, obj=c[i], type="B") for i in range(2)) == 1

Do graph stuff like

m = Model()
g = m.addGraph(
    obj=c,
    edges=e,
    source=0,
    sink=n - 1,
    L=1,
    U=n - 2,
    type="B"
)
m.addResourceDisposable(
    graph=g,
    consumptionType="E",
    weight=t,
    boundsType="V",
    lb=a,
    ub=b,
    name="t",
)

constr: List[flowty.entities.Constr] property readonly

The constraints of the model.

name: str property writable

The model name.

objectiveValue: float property readonly

The objective value.

vars: List[flowty.entities.Var] property readonly

The variables of the model.

__init__(self, name='') special

Initialize the optimization model.

Parameters:

Name Type Description Default
name str

The name of the model.

''

addConstr(self, equa, name='')

Add constraint to the model

Parameters:

Name Type Description Default
equa LinEqua

The constraint as a linear equation.

required
name str

Name of the variable

''

Returns:

Type Description
Constr

The constraint added

addGraph(self, obj=[], edges=[], source=0, sink=-1, L=1, U=1, type='B', names=None)

Add a graph. Implicitly adds edges as variables.

Parameters:

Name Type Description Default
obj List[float]

The edge cost to map into the objective function for each variable.

[]
edges List[Tuple[int, int]]

The list of edges given as tuples of vertex indices.

[]
source int

The source vertex.

0
sink int

The sink vertex.

-1
L float

Lower bound on path flow.

1
U float

Upper bound on path flow.

1
type str

Type as in VarType for the path flow values. The type can be "C", "B" or "I" which is continuous, binary or integer path flow. For <{http://www.w3.org/1998/Math/MathML}math><{http://www.w3.org/1998/Math/MathML}semantics><{http://www.w3.org/1998/Math/MathML}mrow><{http://www.w3.org/1998/Math/MathML}mi>U<{http://www.w3.org/1998/Math/MathML}mo>><{http://www.w3.org/1998/Math/MathML}mn>1<{http://www.w3.org/1998/Math/MathML}annotation encoding="application/x-tex">U > 1 this allows integer flow on paths. Enforcing a binary restriction in this case is a modeling decision.

'B'
names Union[str, List[str]]

A prefix or a list of names for the edges.

None

Returns:

Type Description
Graph

The graph added.

addPackingSet(self, packingSet)

Add a packing set <{http://www.w3.org/1998/Math/MathML}math><{http://www.w3.org/1998/Math/MathML}semantics><{http://www.w3.org/1998/Math/MathML}mrow><{http://www.w3.org/1998/Math/MathML}mi>B<{http://www.w3.org/1998/Math/MathML}annotation encoding="application/x-tex">B where <{http://www.w3.org/1998/Math/MathML}math><{http://www.w3.org/1998/Math/MathML}semantics><{http://www.w3.org/1998/Math/MathML}mrow><{http://www.w3.org/1998/Math/MathML}msub><{http://www.w3.org/1998/Math/MathML}mo>∑<{http://www.w3.org/1998/Math/MathML}mrow><{http://www.w3.org/1998/Math/MathML}mi>i<{http://www.w3.org/1998/Math/MathML}mo>∈<{http://www.w3.org/1998/Math/MathML}mi>B<{http://www.w3.org/1998/Math/MathML}msub><{http://www.w3.org/1998/Math/MathML}mi>x<{http://www.w3.org/1998/Math/MathML}mi>i<{http://www.w3.org/1998/Math/MathML}mo>≤<{http://www.w3.org/1998/Math/MathML}mn>1<{http://www.w3.org/1998/Math/MathML}annotation encoding="application/x-tex">\sum_{i \in B} x_i \leq 1 holds true.

Parameters:

Name Type Description Default
packingSet List[flowty.entities.Var]

The set of variables in <{http://www.w3.org/1998/Math/MathML}math><{http://www.w3.org/1998/Math/MathML}semantics><{http://www.w3.org/1998/Math/MathML}mrow><{http://www.w3.org/1998/Math/MathML}mi>B<{http://www.w3.org/1998/Math/MathML}annotation encoding="application/x-tex">B

required

addResourceCustom(self, graph, name='')

Add a custom resource constraint.

Can be accessed using the name in callbacks.

Parameters:

Name Type Description Default
graph Graph

The graph to add the resource constraint to.

required
name str

The resource name.

''

addResourceDisposable(self, graph, consumptionType='V', weight=1, boundsType='V', lb=0, ub=1, name=None)

Add a disposable resource constraint.

Parameters:

Name Type Description Default
graph Graph

The graph to add the resource constraint to.

required
consumptionType str

Where is the resource being consumed, edge E or vertex V.

'V'
weight Union[float, List[float]]

The amount to consume.

1
boundsType str

Where is the resource being bounded, edge E, vertex V or none N.

'V'
lb Union[float, List[float]]

Lower bound enforced as indicated by boundsType.

0
ub Union[float, List[float]]

Upper bound enforced as indicated by boundsType.

1
name str

The resource name.

None

addResourceNonDisposable(self, graph, consumptionType='V', weight=1, boundsType='V', lb=0, ub=1, name=None)

Add a non-disposable resource constraint.

Parameters:

Name Type Description Default
graph Graph

The graph to add the resource constraint to.

required
consumptionType str

Where is the resource being consumed, edge E or vertex V.

'V'
weight Union[float, List[float]]

The amount to consume.

1
boundsType str

Where is the resource being bounded, edge E, vertex V or none N.

'V'
lb Union[float, List[float]]

Lower bound enforced as indicated by boundsType.

0
ub Union[float, List[float]]

Upper bound enforced as indicated by boundsType.

1
name str

The resource name.

None

addVar(self, lb=-inf, ub=inf, obj=0, type='C', name='')

Add variable to the model

Parameters:

Name Type Description Default
lb float

Lower bound

-inf
ub float

Upper bound

inf
obj float

Objective coefficient

0
type str

Type as in VarType. Values can be "C", "B", or "I" which is continuous, binary, or general integer type respectively

'C'
name str

Name of the variable

''

Returns:

Type Description
Var

The variable added

optimize(self)

Optimize the model.

Returns:

Type Description
OptimizationStatus

The status of the optimization.

read(self, filename)

Read a model from file.

Parameters:

Name Type Description Default
filename str

The path to file.

required

setCallback(self, callback)

Set a callback function with signature

def callback(callbackModel: CallbackModel, where: Where): -> None
    pass

Use the CallbackModel to get/set data. Use the Where to determine where in the algorithm flow the callback is invoked.

setLicenseKey(self, user, key)

Set the user and license key. If no key is set the community license is used.

Note

The community license is a free license to the general community which may have limited features and additional restrictions.

Parameters:

Name Type Description Default
user str

The user name.

required
key str

The license key.

required

setObjective(self, expr)

Set the objective function.

Parameters:

Name Type Description Default
expr Union[flowty.entities.LinExpr, Tuple[str, flowty.entities.LinExpr]]

Defaults to minimization. Otherwise set with tuple of a ObjSense and a linear expression.

required

setParam(self, key, value)

Set a parameter. Valid parameters are

Name Type Description Default
Algorithm str What algorithm to use. Options are PathMIP for the path MIP algorithm, MIP for the branch-and-cut algorithm, and DP for the dynamic programming algorithm. PathMIP
Verbosity str Turn verbosity on/off. On
Threads int The approximate number of threads to use. 4
NodeLimit int The branch-and-bound node limit. If limit is reached optimization stops with status NodeLimit inf
SolutionLimit int The maximum number of solutions. If limit is reached optimization stops with status SolutionLimit inf
TimeLimit float The computation time limit in seconds. If limit is reached optimization stops with status TimeLimit inf
SubproblemTimeLimit float The computation time limit in seconds for solving subproblems. If limit is reached and feasible solutions are found the optimization continues. Bounds are not calculated. If no feasible solutions are found the optimization stops with status TimeLimit inf
Gap float The absolute gap between objective upper and lower bound. 1e-4
CallbackDP str If a callback function is set should it also be used in the dynamic programing algorithm. For performance reasons this should be avoided if not needed. Off
MIPSolver str Specify the underlying MIP solver. Options are Cbc for the COIN Cbc solver or Xpress for FICO Xpress solver. Cbc

Parameters:

Name Type Description Default
key str

The name of the parameter to set.

required
value Union[str, int, float]

The value to assign.

required

write(self, filename)

Write a model to file.

Parameters:

Name Type Description Default
filename str

The path to file.

required