McfModel¶
High-level API for Multi-Commodity Flow problems. Provides a simplified interface for defining networks, commodities, and solving MCF variants.
Constructor¶
import flowty
model = flowty.McfModel() # default (Tree)
model = flowty.McfModel(flowty.McfModel.Type.Path) # path-based
model = flowty.McfModel(flowty.McfModel.Type.Tree) # tree-based
| Type | Description |
|---|---|
Path |
Column generation with shortest-path pricing |
Tree |
Tree-based decomposition |
Network¶
addEdge¶
Add a single directed edge.
edge_id = model.addEdge(source, target, cost, capacity)
| Parameter | Type | Description |
|---|---|---|
source |
int |
Source vertex |
target |
int |
Target vertex |
cost |
float |
Edge cost |
capacity |
float |
Edge capacity |
| Returns | int |
Edge ID |
addEdges¶
Batch add edges. Accepts Python lists or NumPy arrays.
first_edge_id = model.addEdges(sources, targets, costs, capacities)
| Parameter | Type | Description |
|---|---|---|
sources |
list[int] or np.ndarray |
Source vertices |
targets |
list[int] or np.ndarray |
Target vertices |
costs |
list[float] or np.ndarray |
Edge costs |
capacities |
list[float] or np.ndarray |
Edge capacities |
| Returns | int |
First edge ID |
Commodities¶
addCommodity¶
Add a single commodity.
model.addCommodity(origin, destination, demand)
| Parameter | Type | Description |
|---|---|---|
origin |
int |
Origin vertex |
destination |
int |
Destination vertex |
demand |
float |
Commodity demand |
addCommodities¶
Batch add commodities.
model.addCommodities(origins, destinations, demands)
addCommodityWithResources¶
Add a commodity with per-commodity resource constraints. Requires McfModel.Type.Path.
model.addCommodityWithResources(origin, destination, demand, resources, feasibilityRules, updateRules)
| Parameter | Type | Description |
|---|---|---|
origin |
int |
Origin vertex |
destination |
int |
Destination vertex |
demand |
float |
Commodity demand |
resources |
list |
Resource definitions (see Resources & Rules) |
feasibilityRules |
list |
Feasibility rule definitions |
updateRules |
list |
Update rule definitions |
addCommodityGraph¶
Add a commodity with per-commodity edge costs and optional resources. Useful when different commodities have different routing costs. Requires McfModel.Type.Path.
model.addCommodityGraph(origin, destination, demand, edgeCosts, resources, feasibilityRules, updateRules)
| Parameter | Type | Description |
|---|---|---|
edgeCosts |
list[float] |
Per-commodity edge costs (length |E|) |
| Other params | Same as addCommodityWithResources |
Constraints¶
addLinearConstraint¶
Add a linear constraint on edge flows.
model.addLinearConstraint(edgeTerms, rhs, lazy=True)
| Parameter | Type | Description |
|---|---|---|
edgeTerms |
list[tuple[float, int]] |
List of (coefficient, edge_id) tuples |
rhs |
float |
Right-hand side |
lazy |
bool |
Lazy constraint (default True) |
Configuration¶
setPricing¶
model.setPricing(flowty.McfModel.Pricing.ShortestPath) # default
model.setPricing(flowty.McfModel.Pricing.Rcspp) # resource-constrained
Use Rcspp when commodities have resource constraints. Requires McfModel.Type.Path -- not supported with Tree.
setSlackMode¶
model.setSlackMode(flowty.McfModel.SlackMode.Auto) # automatic
model.setSlackMode(flowty.McfModel.SlackMode.NoneType) # no slack
model.setSlackMode(flowty.McfModel.SlackMode.Demands) # slack on demands
model.setSlackMode(flowty.McfModel.SlackMode.Edges) # slack on edges
setPenalty¶
Set penalty cost for uncovered demand.
model.setPenalty(100.0)
setPreferMaster¶
model.setPreferMaster(True)
setResources / setRules¶
Set global resources and rules (applied to all commodities). See Resources & Rules.
resources = [({"E": [[5, 5, 3, 3]], "G": [6]}, "cap")]
model.setResources(resources)
rules = [("Capacity", ["cap"], "cap_rule")]
model.setRules(rules, rules) # (feasibilityRules, updateRules)
setParam¶
Set solver parameters. See Parameters.
model.setParam("LogLevel", 2)
model.setParam("TimeLimit", 300)
clearData¶
Reset all network and commodity data.
model.clearData()
Solving¶
solve¶
status = model.solve()
Returns flowty.Model.Status: Optimal, Infeasible, TimeLimit, or NodeLimit.
getSolution¶
solution = model.getSolution()
Returns an McfSolution or None.
Bounds¶
ub = model.getUpperBound() # primal bound
lb = model.getLowerBound() # dual bound
McfSolution¶
| Property | Type | Description |
|---|---|---|
.cost |
float |
Objective value |
.edgeFlows |
list[float] |
Flow on each edge (indexed by edge ID) |
Example¶
import flowty
model = flowty.McfModel()
# Build a small network
# 0 --1.0--> 1 --1.0--> 3
# 0 --1.0--> 2 --2.0--> 3
model.addEdge(0, 1, 1.0, 10.0)
model.addEdge(1, 3, 1.0, 10.0)
model.addEdge(0, 2, 1.0, 10.0)
model.addEdge(2, 3, 2.0, 10.0)
# Route 1 unit from 0 to 3
model.addCommodity(0, 3, 1.0)
status = model.solve()
solution = model.getSolution()
if solution:
print(f"Cost: {solution.cost}") # 2.0 (path 0->1->3)
print(f"Flows: {solution.edgeFlows}")