38#include <gtest/gtest.h>
61 return std::fabs(a - b) < eps;
68 auto s = net.insert_node();
69 auto t = net.insert_node();
78 auto s = net.insert_node();
79 auto a = net.insert_node();
80 auto b = net.insert_node();
81 auto t = net.insert_node();
95 auto s = net.insert_node();
96 auto a = net.insert_node();
97 auto b = net.insert_node();
98 auto t = net.insert_node();
116 EXPECT_EQ(net.
vsize(), 0);
117 EXPECT_EQ(net.
esize(), 0);
118 EXPECT_EQ(net.num_commodities(), 0);
128 EXPECT_EQ(net.
vsize(), 2);
129 EXPECT_EQ(net.
esize(), 1);
130 EXPECT_EQ(arc->capacity, 10.0);
131 EXPECT_EQ(arc->base_cost, 5.0);
136 auto net = build_single_arc_network(10.0, 1.0);
138 auto nodes = net.nodes();
139 auto it =
nodes.get_it();
140 auto s = it.get_curr(); it.next();
141 auto t = it.get_curr();
143 size_t k = net.add_commodity(s, t, 5.0,
"Commodity 1");
146 EXPECT_EQ(net.num_commodities(), 1);
148 const auto& comm = net.get_commodity(0);
149 EXPECT_EQ(comm.source, s);
150 EXPECT_EQ(comm.sink, t);
151 EXPECT_EQ(comm.demand, 5.0);
152 EXPECT_EQ(comm.name,
"Commodity 1");
162 net.add_commodity(s, t, 5.0);
163 net.add_commodity(s, t, 8.0);
166 arc->set_flow(1, 8.0);
168 EXPECT_EQ(arc->flow(0), 5.0);
169 EXPECT_EQ(arc->flow(1), 8.0);
170 EXPECT_EQ(arc->total_flow(), 13.0);
171 EXPECT_EQ(arc->residual(), 7.0);
176 auto net = build_diamond_network();
178 auto nodes = net.nodes();
179 auto it =
nodes.get_it();
180 auto s = it.get_curr(); it.next();
181 it.next(); it.next();
182 auto t = it.get_curr();
184 net.add_commodity(s, t, 5.0,
"K1");
185 net.add_commodity(s, t, 3.0,
"K2");
187 EXPECT_EQ(net.num_commodities(), 2);
188 EXPECT_EQ(net.get_commodity(0).demand, 5.0);
189 EXPECT_EQ(net.get_commodity(1).demand, 3.0);
198 auto net = build_single_arc_network(10.0, 2.0);
200 auto nodes = net.nodes();
201 auto it =
nodes.get_it();
202 auto s = it.get_curr(); it.next();
203 auto t = it.get_curr();
205 net.add_commodity(s, t, 5.0);
209 EXPECT_TRUE(result.is_optimal());
210 EXPECT_NEAR(result.total_cost, 10.0, 0.1);
215 auto net = build_diamond_network();
217 auto nodes = net.nodes();
218 auto it =
nodes.get_it();
219 auto s = it.get_curr(); it.next();
220 it.next(); it.next();
221 auto t = it.get_curr();
223 net.add_commodity(s, t, 5.0);
227 EXPECT_TRUE(result.is_optimal());
228 EXPECT_NEAR(result.total_cost, 10.0, 1.0);
233 auto net = build_parallel_paths();
235 auto nodes = net.nodes();
236 auto it =
nodes.get_it();
237 auto s = it.get_curr(); it.next();
238 it.next(); it.next();
239 auto t = it.get_curr();
241 net.add_commodity(s, t, 8.0);
245 EXPECT_TRUE(result.is_optimal());
246 EXPECT_GT(result.total_cost, 0.0);
267 net.add_commodity(s1, t1, 3.0,
"K1");
268 net.add_commodity(s2, t2, 3.0,
"K2");
272 EXPECT_TRUE(result.is_optimal());
273 EXPECT_GT(result.total_cost, 0.0);
284 net.add_commodity(s, t, 4.0,
"K1");
285 net.add_commodity(s, t, 4.0,
"K2");
289 EXPECT_TRUE(result.is_optimal());
290 EXPECT_NEAR(result.total_cost, 8.0, 0.1);
292 EXPECT_NEAR(arc->flow(0), 4.0, 0.1);
293 EXPECT_NEAR(arc->flow(1), 4.0, 0.1);
294 EXPECT_NEAR(arc->total_flow(), 8.0, 0.1);
299 auto net = build_diamond_network();
301 auto nodes = net.nodes();
302 auto it =
nodes.get_it();
303 auto s = it.get_curr(); it.next();
304 it.next(); it.next();
305 auto t = it.get_curr();
307 net.add_commodity(s, t, 2.0,
"K1");
308 net.add_commodity(s, t, 3.0,
"K2");
309 net.add_commodity(s, t, 4.0,
"K3");
313 EXPECT_TRUE(result.is_optimal());
314 EXPECT_GT(result.total_cost, 0.0);
323 auto net = build_single_arc_network(10.0, 1.0);
325 auto nodes = net.nodes();
326 auto it =
nodes.get_it();
327 auto s = it.get_curr(); it.next();
328 auto t = it.get_curr();
330 net.add_commodity(s, t, 0.0);
334 EXPECT_TRUE(result.is_optimal());
335 EXPECT_NEAR(result.total_cost, 0.0, 0.1);
340 auto net = build_diamond_network();
344 EXPECT_TRUE(result.is_optimal());
345 EXPECT_NEAR(result.total_cost, 0.0, 0.1);
359 net.add_commodity(s, t, 4.0);
360 net.add_commodity(s, t, 5.0);
364 EXPECT_TRUE(result.is_optimal());
365 EXPECT_TRUE(net.capacities_respected());
376 net.add_commodity(s, t, 3.0,
"Cheap");
377 net.add_commodity(s, t, 2.0,
"Expensive");
380 arc->set_cost(0, 1.0);
381 arc->set_cost(1, 5.0);
386 EXPECT_TRUE(result.is_optimal());
387 EXPECT_NEAR(result.total_cost, 13.0, 0.1);
396 const int grid_size = 4;
399 std::vector<TestNode*> grid_nodes;
400 grid_nodes.reserve(grid_size * grid_size);
402 for (
int i = 0; i < grid_size * grid_size; ++i)
405 auto node_at = [&](
int i,
int j) {
return grid_nodes[i * grid_size + j]; };
408 for (
int i = 0; i < grid_size; ++i)
410 for (
int j = 0; j < grid_size; ++j)
412 if (j + 1 < grid_size)
414 mcf_net.
insert_arc(node_at(i, j), node_at(i, j + 1), 5.0, 1.0);
415 mcf_net.
insert_arc(node_at(i, j + 1), node_at(i, j), 5.0, 1.0);
417 if (i + 1 < grid_size)
419 mcf_net.
insert_arc(node_at(i, j), node_at(i + 1, j), 5.0, 1.0);
420 mcf_net.
insert_arc(node_at(i + 1, j), node_at(i, j), 5.0, 1.0);
425 mcf_net.add_commodity(node_at(0, 0), node_at(grid_size-1, grid_size-1),
426 2.0,
"TopLeft-BottomRight");
427 mcf_net.add_commodity(node_at(0, grid_size-1), node_at(grid_size-1, 0),
428 2.0,
"TopRight-BottomLeft");
432 std::cout <<
"Grid " << grid_size <<
"x" << grid_size <<
": "
433 <<
"cost=" << result.total_cost
434 <<
", time=" << result.solve_time_ms <<
" ms"
435 <<
", iterations=" << result.iterations <<
"\n";
437 EXPECT_TRUE(result.is_optimal());
441 EXPECT_NEAR(result.total_cost, 2 * 6.0 + 2 * 6.0, 1e-9);
448 const int grid_size = 4;
451 std::vector<TestNode*> grid_nodes;
452 grid_nodes.reserve(grid_size * grid_size);
454 for (
int i = 0; i < grid_size * grid_size; ++i)
457 auto node_at = [&](
int i,
int j) {
return grid_nodes[i * grid_size + j]; };
459 for (
int i = 0; i < grid_size; ++i)
460 for (
int j = 0; j < grid_size; ++j)
462 if (j + 1 < grid_size)
463 mcf_net.
insert_arc(node_at(i, j), node_at(i, j + 1), 5.0, 1.0);
464 if (i + 1 < grid_size)
465 mcf_net.
insert_arc(node_at(i, j), node_at(i + 1, j), 5.0, 1.0);
468 mcf_net.add_commodity(node_at(0, 0), node_at(grid_size-1, grid_size-1),
469 2.0,
"TopLeft-BottomRight");
470 mcf_net.add_commodity(node_at(0, grid_size-1), node_at(grid_size-1, 0),
471 2.0,
"TopRight-BottomLeft");
475 EXPECT_FALSE(result.is_optimal());
476 EXPECT_EQ(result.status,
decltype(result)::Infeasible);
496 net.add_commodity(s, t, 5.0,
"K1");
497 net.add_commodity(s, t, 3.0,
"K2");
501 EXPECT_TRUE(result.is_optimal());
502 EXPECT_EQ(result.commodity_costs.size(), 2u);
503 EXPECT_GE(result.solve_time_ms, 0.0);
504 EXPECT_GT(result.iterations, 0u);
507 for (
auto c : result.commodity_costs)
509 EXPECT_NEAR(
sum, result.total_cost, 0.1);
518static_assert(DigraphType<TestNet>);
530 net.add_commodity(s, t, 2.0);
534 as->set_flow(0, 1.0);
535 at->set_flow(0, 2.0);
536 EXPECT_TRUE(net.demands_satisfied());
539 as->set_flow(0, 0.0);
540 EXPECT_FALSE(net.demands_satisfied());
543 sa->set_flow(0, 2.0);
544 as->set_flow(0, 2.0);
545 EXPECT_FALSE(net.demands_satisfied());
549 sa->set_flow(0, 3.0);
550 as->set_flow(0, 1.0);
551 ss->set_flow(0, 4.0);
552 EXPECT_TRUE(net.demands_satisfied());
562 auto s1s2 = net.
insert_arc(s1, s2, 10.0, 1.0);
563 auto s2s1 = net.
insert_arc(s2, s1, 10.0, 1.0);
565 net.add_commodity(s1, t, 1.0,
"from s1");
566 net.add_commodity(s2, t, 2.0,
"from s2");
571 s2s1->set_flow(0, 2.0);
572 s2t->set_flow(0, 1.0);
573 s2s1->set_flow(1, 1.0);
574 s1s2->set_flow(1, 1.0);
575 s2t->set_flow(1, 2.0);
576 EXPECT_TRUE(net.demands_satisfied());
579 s2s1->set_flow(0, 0.0);
580 EXPECT_FALSE(net.demands_satisfied());
585 auto net = build_diamond_network();
586 auto nodes = net.nodes();
587 auto it =
nodes.get_it();
588 auto s = it.get_curr(); it.next();
589 it.next(); it.next();
590 auto t = it.get_curr();
591 net.add_commodity(s, t, 5.0,
"K1");
592 net.add_commodity(s, t, 3.0,
"K2");
594 EXPECT_FALSE(net.demands_satisfied());
596 ASSERT_TRUE(result.is_optimal());
597 EXPECT_TRUE(net.demands_satisfied());
598 EXPECT_TRUE(net.capacities_respected());
603 auto net = build_single_arc_network(10.0, 1.0);
604 EXPECT_TRUE(net.demands_satisfied());
609 ::testing::InitGoogleTest(&argc, argv);
610 return RUN_ALL_TESTS();
Multi-commodity flow network (directed graph).
Arc * insert_arc(Node *src, Node *tgt, Flow_Type capacity, Flow_Type cost=0)
Insert arc with capacity and cost.
constexpr size_t vsize() const noexcept
See get_num_nodes().
size_t esize() const noexcept
Return the total of arcs of graph.
TestNet build_single_arc_network(double capacity, double cost)
TestNet build_diamond_network()
static constexpr double EPSILON
TestNet build_parallel_paths()
bool nearly_equal(double a, double b, double eps=EPSILON)
DynArray< Graph::Node * > nodes
Main namespace for Aleph-w library functions.
MCF_Result< typename Net::Flow_Type > solve_multicommodity_flow(Net &net)
Solve multi-commodity flow problem.
T sum(const Container &container, const T &init=T{})
Compute sum of all elements.
Arc information for multi-commodity flow.
Flow network implemented with adjacency lists.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
void set_flow(Arc *arc, const Flow_Type &flow)
Set the flow of an arc. Throws if flow exceeds capacity.
Arc * insert_arc(Node *src_node, Node *tgt_node, const Flow_Type &cap, const Flow_Type &flow, const typename Arc::Arc_Type &arc_info=Arc_Type())
Insert a capacitated arc with an initial flow.
FooMap m(5, fst_unit_pair_hash, snd_unit_pair_hash)
Multi-commodity flow algorithms.
Graph_Anode< Empty_Class > TestNode
TEST_F(MultiCommodityTest, EmptyNetwork)