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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::marker::PhantomData;
// IMPORT CXX LIBRARY
cpp! {{
#include "ortools/graph/max_flow.h"
}}
cpp_class!(
#[doc(hidden)]
unsafe struct MaxFlowInner as "operations_research::MaxFlow"
);
/// Default instance MaxFlow that uses StarGraph. Note that we cannot just use a
/// typedef because of dependent code expecting MaxFlow to be a real class.
pub struct MaxFlow<'graph> {
/// Lifetime limiter for graph
_graph: PhantomData<&'graph ()>,
/// Original solver
inner: Box<MaxFlowInner>,
}
impl<'graph> MaxFlow<'graph> {
/// Creates a new `MaxFlow` struct.
pub fn new(
graph: &'graph super::StarGraph,
source: super::NodeIndex,
target: super::NodeIndex,
) -> Self {
Self {
_graph: PhantomData,
inner: unsafe {
cpp!([
graph as "const operations_research::StarGraph*",
source as "operations_research::NodeIndex",
target as "operations_research::NodeIndex"
] -> Box<MaxFlowInner> as "operations_research::MaxFlow*"
{
return new operations_research::MaxFlow(graph, source, target);
}
)
},
}
}
/// Change the capacity of an arc.
pub fn set_arc_capacity(&mut self, arc: super::ArcIndex, capacity: super::FlowQuantity) {
let inner = self.inner.as_mut();
unsafe {
cpp!([
inner as "operations_research::MaxFlow*",
arc as "operations_research::ArcIndex",
capacity as "operations_research::FlowQuantity"
]
{
return inner->SetArcCapacity(arc, capacity);
}
)
}
}
// Sets the flow for arc.
pub fn set_arc_flow(&mut self, arc: super::ArcIndex, flow: super::FlowQuantity) {
let inner = self.inner.as_mut();
unsafe {
cpp!([
inner as "operations_research::MaxFlow*",
arc as "operations_research::ArcIndex",
flow as "operations_research::FlowQuantity"
]
{
return inner->SetArcFlow(arc, flow);
}
)
}
}
/// Returns output if a maximum flow was solved.
pub fn solve(&mut self) -> Option<MaxFlowOutput<'graph, '_>> {
let inner = self.inner.as_mut();
let solved = unsafe {
cpp!([
inner as "operations_research::MaxFlow*"
] -> bool as "bool"
{
return inner->Solve();
}
)
};
if solved {
Some(MaxFlowOutput { solver: self })
} else {
None
}
}
}
/// Successful output of MaxFlow.
pub struct MaxFlowOutput<'graph, 'solver> {
/// Callee solver
solver: &'solver MaxFlow<'graph>,
}
impl<'graph, 'solver> MaxFlowOutput<'graph, 'solver> {
/// Returns the total flow found by the algorithm.
pub fn get_optimal_flow(&self) -> super::FlowQuantity {
let inner = self.solver.inner.as_ref();
unsafe {
cpp!([
inner as "const operations_research::MaxFlow*"
] -> super::FlowQuantity as "operations_research::FlowQuantity"
{
return inner->GetOptimalFlow();
}
)
}
}
/// Returns the flow on arc using the equations given in the comment on
/// residual_arc_capacity_.
pub fn flow(&self, arc: super::ArcIndex) -> super::FlowQuantity {
let inner = self.solver.inner.as_ref();
unsafe {
cpp!([
inner as "const operations_research::MaxFlow*",
arc as "operations_research::ArcIndex"
] -> super::FlowQuantity as "operations_research::FlowQuantity"
{
return inner->Flow(arc);
}
)
}
}
/// Returns the capacity of arc using the equations given in the comment on
/// residual_arc_capacity_.
pub fn capacity(&self, arc: super::ArcIndex) -> super::FlowQuantity {
let inner = self.solver.inner.as_ref();
unsafe {
cpp!([
inner as "const operations_research::MaxFlow*",
arc as "operations_research::ArcIndex"
] -> super::FlowQuantity as "operations_research::FlowQuantity"
{
return inner->Capacity(arc);
}
)
}
}
/// Returns the status. `NotSolved` is returned if
/// the problem has been modified in such a way that
/// the previous solution becomes invalid.
pub fn status(&self) -> MaxFlowStatus {
let inner = self.solver.inner.as_ref();
let status = unsafe {
cpp!([
inner as "const operations_research::MaxFlow*"
] -> u8 as "uint8_t"
{
switch (inner->status()) {
case operations_research::MaxFlowStatusClass::Status::NOT_SOLVED:
return 0;
case operations_research::MaxFlowStatusClass::Status::OPTIMAL:
return 1;
case operations_research::MaxFlowStatusClass::Status::INT_OVERFLOW:
return 2;
case operations_research::MaxFlowStatusClass::Status::BAD_INPUT:
return 3;
case operations_research::MaxFlowStatusClass::Status::BAD_RESULT:
return 4;
default:
return 4;
}
}
)
};
match status {
0 => MaxFlowStatus::NotSolved,
1 => MaxFlowStatus::Optimal,
2 => MaxFlowStatus::IntOverflow,
3 => MaxFlowStatus::BadInput,
4 | 5.. => MaxFlowStatus::BadResult,
}
}
}
/// Solves the problem (finds the maximum flow from the given source to the
/// given sink), and returns the problem status.
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum MaxFlowStatus {
/// The problem was not solved, or its data were edited.
NotSolved,
/// solve() was called and found an optimal solution.
Optimal,
/// There is a feasible flow > max possible flow.
IntOverflow,
/// The input is inconsistent.
BadInput,
/// There was an error.
BadResult,
}