-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmaximumflow.jl
160 lines (124 loc) · 3.38 KB
/
maximumflow.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
module MaximumFlow
using ..Network
using JuMP, HiGHS
import ..OperationsResearchModels: solve
export MaximumFlowProblem
export MaximumFlowResult
struct MaximumFlowResult
path::Array{Connection,1}
flow::Float64
end
struct MaximumFlowProblem
connections::Array{Connection,1}
end
"""
solve(problem)
# Arguments
`problem::MaximumFlowProblem`: The problem in type of MaximumFlowProblem
# Output
`MaximumFlowResult`: The custom data type that holds path and flow.
# Example
```julia
julia> conns = [
Connection(1, 2, 3),
Connection(1, 3, 2),
Connection(1, 4, 4),
Connection(2, 5, 3),
Connection(3, 5, 1),
Connection(3, 6, 1),
Connection(4, 6, 2),
Connection(5, 7, 6),
Connection(6, 7, 5),
];
julia> problem = MaximumFlowProblem(conns)
julia> result = solve(problem);
julia> result.path
9-element Vector{Connection}:
Connection(1, 2, 3.0, "x12")
Connection(1, 3, 2.0, "x13")
Connection(1, 4, 2.0, "x14")
Connection(2, 5, 3.0, "x25")
Connection(3, 5, 1.0, "x35")
Connection(3, 6, 1.0, "x36")
Connection(4, 6, 2.0, "x46")
Connection(5, 7, 4.0, "x57")
Connection(6, 7, 3.0, "x67")
julia> result.flow
7.0
```
"""
function solve(problem::MaximumFlowProblem)
cns = problem.connections
function leftexpressions(node::Int64, nodes::Array{Connection,1}, model)
lst = []
for conn in nodes
if conn.to == node
push!(lst, conn)
end
end
if length(lst) == 0
return :f
end
expr = @expression(model, 0)
for i = eachindex(lst)
expr += x[lst[i].from, lst[i].to]
end
return expr
end
function rightexpressions(node::Int64, nodes::Array{Connection,1}, model)
lst = []
for conn in nodes
if conn.from == node
push!(lst, conn)
end
end
if length(lst) == 0
return :f
end
expr = @expression(model, 0)
for i = eachindex(lst)
expr += x[lst[i].from, lst[i].to]
end
return expr
end
model = Model(HiGHS.Optimizer)
MOI.set(model, MOI.Silent(), true)
mynodes = nodes(cns)
n = length(mynodes)
startnode = start(cns)
finishnode = finish(cns)
# Variables
@variable(model, f)
@variable(model, x[1:n, 1:n] .>= 0)
# Objective Function
@objective(model, Max, f)
# Constraints
for nextnode in mynodes
leftexpr = leftexpressions(nextnode, cns, model)
rightexpr = rightexpressions(nextnode, cns, model)
if leftexpr == :f
@constraint(model, rightexpr == f)
elseif rightexpr == :f
@constraint(model, leftexpr == f)
else
@constraint(model, leftexpr - rightexpr == 0)
end
end
for nd in cns
@constraint(model, x[nd.from, nd.to] <= nd.value)
end
@constraint(model, f >= 0)
optimize!(model)
xs = value.(x)
cost = JuMP.objective_value(model)
solutionnodes = []
for i = 1:n
for j = 1:n
if xs[i, j] > 0
push!(solutionnodes, Connection(i, j, xs[i, j]))
end
end
end
return MaximumFlowResult(solutionnodes, cost)
end
end # end of module