38#include <gtest/gtest.h>
83 EXPECT_EQ(net.vsize(), 0);
84 EXPECT_EQ(net.esize(), 0);
85 EXPECT_FALSE(net.has_aux_net());
86 EXPECT_EQ(net.get_aux_net(),
nullptr);
93 auto n1 = net.insert_node(10.0);
94 auto n2 = net.insert_node(5.0);
95 net.insert_arc(n1, n2, 15.0, 0.0);
97 EXPECT_NO_THROW(net.compute_aux_net());
98 EXPECT_TRUE(net.has_aux_net());
108 auto node = net.insert_node();
110 EXPECT_EQ(net.vsize(), 1);
111 EXPECT_EQ(node->max_cap, numeric_limits<double>::max());
112 EXPECT_EQ(node->in_flow, 0.0);
113 EXPECT_EQ(node->out_flow, 0.0);
118 auto node = net.insert_node(100.0);
120 EXPECT_EQ(node->max_cap, 100.0);
125 auto node = net.insert_node(0.0);
127 EXPECT_EQ(node->max_cap, 0.0);
132 EXPECT_THROW(net.insert_node(-1.0), std::domain_error);
141 auto node = net.insert_node(numeric_limits<double>::max());
142 EXPECT_EQ(node->max_cap, numeric_limits<double>::max());
147 auto node = net.insert_node(numeric_limits<double>::min());
148 EXPECT_EQ(node->max_cap, numeric_limits<double>::min());
153 auto node = net.insert_node(numeric_limits<double>::epsilon());
154 EXPECT_EQ(node->max_cap, numeric_limits<double>::epsilon());
163 auto n1 = net.insert_node(10.0);
164 auto n2 = net.insert_node(10.0);
165 auto arc = net.insert_arc(n1, n2, 0.0, 0.0);
167 EXPECT_EQ(arc->cap, 0.0);
168 EXPECT_EQ(arc->flow, 0.0);
173 auto n1 = net.insert_node(10.0);
174 auto n2 = net.insert_node(10.0);
175 auto arc = net.insert_arc(n1, n2, 5.0, 5.0);
177 EXPECT_EQ(arc->cap, 5.0);
178 EXPECT_EQ(arc->flow, 5.0);
183 auto n1 = net.insert_node(10.0);
184 auto n2 = net.insert_node(10.0);
186 EXPECT_THROW(net.insert_arc(n1, n2, 5.0, 10.0), std::overflow_error);
195 auto n1 = net.insert_node(10.0);
196 auto n2 = net.insert_node(5.0);
197 net.insert_arc(n1, n2, 15.0, 0.0);
199 net.compute_aux_net();
200 EXPECT_THROW(net.compute_aux_net(), std::domain_error);
207 EXPECT_THROW(net.free_aux_net(), std::domain_error);
212 EXPECT_THROW(net.update(), std::domain_error);
217 auto n1 = net.insert_node(10.0);
218 auto n2 = net.insert_node(5.0);
219 net.insert_arc(n1, n2, 15.0, 0.0);
222 for (
int i = 0; i < 3; ++i)
224 EXPECT_NO_THROW(net.compute_aux_net());
225 EXPECT_TRUE(net.has_aux_net());
226 EXPECT_NO_THROW(net.free_aux_net());
227 EXPECT_FALSE(net.has_aux_net());
237 auto n1 = net.insert_node(10.0);
238 auto n2 = net.insert_node(5.0);
239 net.insert_arc(n1, n2, 15.0, 3.0);
243 EXPECT_EQ(
copy.vsize(), 2);
244 EXPECT_EQ(
copy.esize(), 1);
245 EXPECT_FALSE(
copy.has_aux_net());
250 auto n1 = net.insert_node(10.0);
251 auto n2 = net.insert_node(5.0);
252 net.insert_arc(n1, n2, 15.0, 3.0);
253 net.compute_aux_net();
257 EXPECT_EQ(moved.
vsize(), 2);
258 EXPECT_EQ(moved.
esize(), 1);
285 auto b = net.insert_node(5.0);
286 auto c = net.insert_node(15.0);
289 net.insert_arc(b, c, 8.0, 0.0);
291 EXPECT_EQ(net.vsize(), 3);
292 EXPECT_EQ(net.esize(), 2);
294 auto aux = net.compute_aux_net();
295 EXPECT_NE(aux,
nullptr);
299 EXPECT_EQ(aux->vsize(), 6);
300 EXPECT_EQ(aux->esize(), 5);
307 vector<DefaultNet::Node*>
nodes;
308 for (
int i = 0; i < 10; ++i)
309 nodes.push_back(net.insert_node(10.0 + i));
311 for (
int i = 0; i < 9; ++i)
312 net.insert_arc(
nodes[i],
nodes[i+1], 5.0 + i, 0.0);
314 EXPECT_EQ(net.vsize(), 10);
315 EXPECT_EQ(net.esize(), 9);
317 auto aux = net.compute_aux_net();
318 EXPECT_EQ(aux->vsize(), 20);
319 EXPECT_EQ(aux->esize(), 19);
331 auto hub = net.insert_node(50.0);
333 for (
int i = 0; i < 5; ++i)
335 auto spoke = net.insert_node(10.0);
336 net.insert_arc(spoke, hub, 15.0, 0.0);
339 EXPECT_EQ(net.vsize(), 6);
340 EXPECT_EQ(net.esize(), 5);
342 auto aux = net.compute_aux_net();
343 EXPECT_EQ(aux->vsize(), 12);
344 EXPECT_EQ(aux->esize(), 11);
363 auto a = net.insert_node(100.0);
364 auto b = net.insert_node(30.0);
365 auto c = net.insert_node(40.0);
366 auto d = net.insert_node(100.0);
368 net.insert_arc(a, b, 50.0, 0.0);
369 net.insert_arc(a, c, 60.0, 0.0);
370 net.insert_arc(b, d, 50.0, 0.0);
371 net.insert_arc(c, d, 60.0, 0.0);
373 EXPECT_EQ(net.vsize(), 4);
374 EXPECT_EQ(net.esize(), 4);
376 auto aux = net.compute_aux_net();
377 EXPECT_EQ(aux->vsize(), 8);
378 EXPECT_EQ(aux->esize(), 8);
389 vector<DefaultNet::Node*>
nodes;
390 for (
int i = 0; i < 5; ++i)
391 nodes.push_back(net.insert_node(20.0));
393 for (
int i = 0; i < 5; ++i)
394 for (
int j = 0; j < 5; ++j)
396 net.insert_arc(
nodes[i],
nodes[j], 10.0, 0.0);
398 EXPECT_EQ(net.vsize(), 5);
399 EXPECT_EQ(net.esize(), 20);
401 auto aux = net.compute_aux_net();
402 EXPECT_EQ(aux->vsize(), 10);
403 EXPECT_EQ(aux->esize(), 25);
415 vector<DefaultNet::Node*> left, right;
417 for (
int i = 0; i < 3; ++i)
418 left.push_back(net.insert_node(10.0));
420 for (
int i = 0; i < 3; ++i)
421 right.push_back(net.insert_node(15.0));
426 net.insert_arc(
l,
r, 5.0, 0.0);
428 EXPECT_EQ(net.vsize(), 6);
429 EXPECT_EQ(net.esize(), 9);
431 auto aux = net.compute_aux_net();
432 EXPECT_EQ(aux->vsize(), 12);
433 EXPECT_EQ(aux->esize(), 15);
467 vector<DefaultNet::Node*> datacenters;
468 for (
int i = 0; i < 3; ++i)
472 vector<DefaultNet::Node*> edges;
473 for (
int i = 0; i < 10; ++i)
477 for (
int i = 0; i < 10; ++i)
480 int dc2 = (i + 1) % 3;
481 net.
insert_arc(edges[i], datacenters[dc1], 50.0, 0.0);
482 net.
insert_arc(edges[i], datacenters[dc2], 50.0, 0.0);
485 EXPECT_EQ(net.
vsize(), 13);
486 EXPECT_EQ(net.
esize(), 20);
489 EXPECT_NE(aux,
nullptr);
492 EXPECT_EQ(aux->vsize(), 26);
493 EXPECT_EQ(aux->esize(), 33);
513 vector<DefaultNet::Node*> pumps;
514 for (
int i = 0; i < 3; ++i)
517 vector<DefaultNet::Node*> neighborhoods;
518 for (
int i = 0; i < 10; ++i)
522 for (
auto pump : pumps)
526 for (
int i = 0; i < 10; ++i)
528 int pump_idx = i % 3;
529 net.
insert_arc(pumps[pump_idx], neighborhoods[i], 600.0, 0.0);
533 EXPECT_NE(aux,
nullptr);
536 EXPECT_GT(aux->vsize(), 0);
537 EXPECT_GT(aux->esize(), 0);
555 for (
int i = 0; i < 4; ++i)
556 for (
int j = 0; j < 4; ++j)
560 for (
int i = 0; i < 4; ++i)
561 for (
int j = 0; j < 3; ++j)
563 net.
insert_arc(grid[i][j], grid[i][j+1], 50.0, 0.0);
564 net.
insert_arc(grid[i][j+1], grid[i][j], 50.0, 0.0);
568 for (
int i = 0; i < 3; ++i)
569 for (
int j = 0; j < 4; ++j)
571 net.
insert_arc(grid[i][j], grid[i+1][j], 50.0, 0.0);
572 net.
insert_arc(grid[i+1][j], grid[i][j], 50.0, 0.0);
575 EXPECT_EQ(net.
vsize(), 16);
576 EXPECT_EQ(net.
esize(), 48);
579 EXPECT_EQ(aux->vsize(), 32);
580 EXPECT_EQ(aux->esize(), 64);
607 const int NUM_NODES = 500;
608 const int NUM_ARCS_PER_NODE = 5;
610 vector<DefaultNet::Node*>
nodes;
611 uniform_real_distribution<double> cap_dist(10.0, 1000.0);
612 uniform_int_distribution<int> node_dist(0, NUM_NODES - 1);
615 for (
int i = 0; i < NUM_NODES; ++i)
619 set<pair<int, int>> existing_arcs;
620 for (
int i = 0; i < NUM_NODES; ++i)
622 for (
int j = 0; j < NUM_ARCS_PER_NODE; ++j)
624 int target = node_dist(
rng);
625 if (target != i && existing_arcs.find({i, target}) == existing_arcs.end())
627 existing_arcs.insert({i, target});
633 EXPECT_EQ(net.
vsize(), NUM_NODES);
634 EXPECT_GT(net.
esize(), 0);
638 EXPECT_NE(aux,
nullptr);
639 EXPECT_EQ(aux->vsize(), 2 * NUM_NODES);
640 EXPECT_EQ(aux->esize(), NUM_NODES + net.
esize());
643 EXPECT_NO_THROW(net.
update());
655 const int CHAIN_LENGTH = 1000;
657 vector<DefaultNet::Node*>
nodes;
660 for (
int i = 1; i < CHAIN_LENGTH; ++i)
666 EXPECT_EQ(net.
vsize(), CHAIN_LENGTH);
667 EXPECT_EQ(net.
esize(), CHAIN_LENGTH - 1);
670 EXPECT_EQ(aux->vsize(), 2 * CHAIN_LENGTH);
682 const int WIDTH = 200;
688 for (
int i = 0; i <
WIDTH; ++i)
702 EXPECT_NE(aux,
nullptr);
720 auto n2 = net.insert_node(10.0);
730 EXPECT_EQ(n1->in_flow, 0.0);
731 EXPECT_EQ(n1->out_flow, 0.0);
732 EXPECT_EQ(arc->flow, 0.0);
737 auto n1 = net.insert_node(10.0);
738 auto n2 = net.insert_node(5.0);
739 net.insert_arc(n1, n2, 8.0, 0.0);
747 EXPECT_TRUE(net.check_node_capacities());
752 auto n1 = net.insert_node(10.0);
753 auto n2 = net.insert_node(5.0);
754 net.insert_arc(n1, n2, 8.0, 0.0);
762 EXPECT_FALSE(net.check_node_capacities());
767 auto node = net.insert_node(10.0);
769 node->out_flow = 5.0;
773 EXPECT_EQ(node->max_cap, 5.0);
778 auto node = net.insert_node(10.0);
780 node->out_flow = 5.0;
788 auto node = net.insert_node(10.0);
806 auto n2 = net.insert_node(0, 50L);
807 auto arc = net.insert_arc(n1, n2, 75L, 0L);
809 EXPECT_EQ(n1->max_cap, 100L);
810 EXPECT_EQ(n2->max_cap, 50L);
811 EXPECT_EQ(arc->cap, 75L);
816 auto n1 = net.insert_node(0, 100L);
817 auto n2 = net.insert_node(0, 50L);
818 net.insert_arc(n1, n2, 75L, 0L);
820 auto aux = net.compute_aux_net();
821 EXPECT_NE(aux,
nullptr);
839 auto n2 = net.insert_node(
"Sink", 50.0);
840 auto arc = net.insert_arc(n1, n2, 75.0, 0.0,
"Connection");
842 EXPECT_EQ(n1->get_info(),
"Source");
843 EXPECT_EQ(n2->get_info(),
"Sink");
844 EXPECT_EQ(arc->get_info(),
"Connection");
849 auto n1 = net.insert_node(
"Node_A", 100.0);
850 auto n2 = net.insert_node(
"Node_B", 50.0);
851 net.insert_arc(n1, n2, 75.0, 0.0);
853 auto aux = net.compute_aux_net();
854 EXPECT_NE(aux,
nullptr);
872 auto n2 = net.insert_node(20.0);
875 net.compute_aux_net();
881 EXPECT_NE(cookie1,
nullptr);
882 EXPECT_NE(cookie2,
nullptr);
886 auto aux_arc1 =
static_cast<AuxArc*
>(cookie1);
894 auto n1 = net.insert_node(10.0);
895 auto n2 = net.insert_node(20.0);
896 auto arc = net.insert_arc(n1, n2, 15.0, 0.0);
898 net.compute_aux_net();
902 EXPECT_NE(cookie,
nullptr);
906 auto aux_arc =
static_cast<AuxArc*
>(cookie);
931 uniform_real_distribution<double> cap_dist(1.0, 100.0);
932 uniform_int_distribution<int> op_dist(0, 2);
934 vector<DefaultNet::Node*>
nodes;
937 for (
int i = 0; i < 1000; ++i)
939 int op = op_dist(
rng);
941 if (
nodes.size() < 2 || op == 0)
945 nodes.push_back(node);
947 else if (op == 1 &&
nodes.size() >= 2)
950 uniform_int_distribution<size_t> node_dist(0,
nodes.size() - 1);
951 size_t src_idx = node_dist(
rng);
952 size_t tgt_idx = node_dist(
rng);
953 if (src_idx != tgt_idx)
985 EXPECT_GT(net.
vsize(), 0);
993 vector<double> extreme_caps = {
995 numeric_limits<double>::min(),
996 numeric_limits<double>::epsilon(),
1003 numeric_limits<double>::max() / 2,
1006 vector<DefaultNet::Node*>
nodes;
1007 for (
double cap : extreme_caps)
1011 for (
size_t i = 0; i <
nodes.size() - 1; ++i)
1041 it.get_curr()->flow = 1.0;
1056TEST(NetCapGraphChangedNetwork, UpdateTakesAnUnchangedNetwork)
1059 ASSERT_NO_THROW(ch.net.update());
1060 EXPECT_EQ(ch.ab->flow, 1.0);
1061 EXPECT_EQ(ch.bc->flow, 1.0);
1062 EXPECT_EQ(ch.b->in_flow, 1.0);
1065TEST(NetCapGraphChangedNetwork, UpdateTakesTheNetworkInAnotherOrder)
1073 { return net.get_src_node(p)->max_cap < net.get_src_node(q)->max_cap; });
1074 ASSERT_EQ(ch.net.get_first_node(), ch.b);
1075 ASSERT_EQ(ch.net.get_first_arc(), ch.bc);
1076 ASSERT_NO_THROW(ch.net.update());
1077 EXPECT_EQ(ch.ab->flow, 1.0);
1078 EXPECT_EQ(ch.bc->flow, 1.0);
1079 EXPECT_EQ(ch.a->in_flow, 1.0);
1082TEST(NetCapGraphChangedNetwork, UpdateRefusesItAfterACopyOverTheNetwork)
1092 EXPECT_THROW(ch.net.update(), std::domain_error);
1095 ASSERT_TRUE(ch.net.has_aux_net());
1096 ch.net.free_aux_net();
1097 ch.net.compute_aux_net();
1098 EXPECT_NO_THROW(ch.net.update());
1101TEST(NetCapGraphChangedNetwork, UpdateRefusesItAfterARemoval)
1105 ch.net.remove_arc(ch.bc);
1106 EXPECT_THROW(ch.net.update(), std::domain_error);
1107 EXPECT_EQ(ch.ab->flow, 0.0);
1111 ch.net.remove_node(ch.c);
1112 EXPECT_THROW(ch.net.update(), std::domain_error);
1113 EXPECT_EQ(ch.ab->flow, 0.0);
1120 EXPECT_THROW(ch.net.update(), std::domain_error);
1125 ch.net.remove_arc(ch.bc);
1126 ch.net.insert_arc(ch.b, ch.c, 8.0, 0.0);
1127 EXPECT_THROW(ch.net.update(), std::domain_error);
1131TEST(NetCapGraphChangedNetwork, UpdateRefusesItAfterADisconnection)
1137 ch.net.disconnect_arc(ch.bc);
1138 ch.net.insert_arc(ch.a, ch.c, 8.0, 0.0);
1140 EXPECT_THROW(ch.net.update(), std::domain_error);
1141 EXPECT_EQ(ch.ab->flow, 0.0);
1143 ch.net.free_aux_net();
1144 ch.net.compute_aux_net();
1145 EXPECT_NO_THROW(ch.net.update());
1154 EXPECT_THROW(ch.net.update(), std::domain_error);
1155 EXPECT_EQ(ch.ab->flow, 0.0);
1160 ch.net.disconnect_arc(ch.bc);
1161 ch.net.connect_arc(ch.bc);
1162 EXPECT_THROW(ch.net.update(), std::domain_error);
1163 EXPECT_EQ(ch.bc->flow, 0.0);
1167TEST(NetCapGraphChangedNetwork, UpdateRefusesItAfterASwapThroughTheBase)
1175 fill_as_the_chain(other);
1178 EXPECT_THROW(ch.net.update(), std::domain_error);
1180 ch.net.free_aux_net();
1181 ch.net.compute_aux_net();
1182 EXPECT_NO_THROW(ch.net.update());
1188 fill_as_the_chain(other);
1190 base = std::move(other);
1192 EXPECT_THROW(ch.net.update(), std::domain_error);
1200 EXPECT_THROW(one.net.update(), std::domain_error);
1201 EXPECT_THROW(two.net.update(), std::domain_error);
1202 EXPECT_EQ(one.ab->flow, 0.0);
1203 EXPECT_EQ(two.ab->flow, 0.0);
1211 fill_as_the_chain(ch.net);
1212 EXPECT_THROW(ch.net.update(), std::domain_error);
1216TEST(NetCapGraphChangedNetwork, UpdateTakesItAfterASwapWithItself)
1222 ASSERT_NO_THROW(ch.net.update());
1223 EXPECT_EQ(ch.ab->flow, 1.0);
1224 EXPECT_EQ(ch.bc->flow, 1.0);
1227TEST(NetCapGraphChangedNetwork, UpdateRefusesItAfterAnInsertion)
1231 ch.net.insert_node(1.0);
1232 EXPECT_THROW(ch.net.update(), std::domain_error);
1236 ch.net.insert_arc(ch.a, ch.c, 1.0, 0.0);
1237 EXPECT_THROW(ch.net.update(), std::domain_error);
1238 EXPECT_EQ(ch.ab->flow, 0.0);
1242TEST(NetCapGraphChangedNetwork, TheMarkGoesWithTheAuxiliaryNetwork)
1245 stale.net.remove_arc(stale.bc);
1249 EXPECT_THROW(moved.
update(), std::domain_error);
1251 moved.
swap(fresh.net);
1252 EXPECT_NO_THROW(moved.
update());
1253 EXPECT_THROW(fresh.net.update(), std::domain_error);
1256 assigned = std::move(fresh.net);
1257 EXPECT_THROW(assigned.
update(), std::domain_error);
1261 EXPECT_THROW(
copy.update(), std::domain_error);
1262 copy.compute_aux_net();
1263 EXPECT_NO_THROW(
copy.update());
1268 ASSERT_NO_THROW(taken.
update());
1269 EXPECT_EQ(valid.ab->flow, 1.0);
1271 valid.ab->flow = 0.0;
1273 target = std::move(taken);
1274 ASSERT_NO_THROW(target.
update());
1275 EXPECT_EQ(valid.ab->flow, 1.0);
1284 ::testing::InitGoogleTest(&argc, argv);
1285 return RUN_ALL_TESTS();
void * insert_arc(void *arc)
static void set_node_cap(Node *node, const Flow_Type &cap)
Set the capacity of a node.
bool has_aux_net() const noexcept
Check if auxiliary network has been computed.
void free_aux_net()
Free the auxiliary network.
void update()
Update flow values from auxiliary network to this network.
void swap(Net_Cap_Graph &other) noexcept
Swap contents with another network, with their auxiliary networks.
Node * insert_node(Node *p) override
Aux_Net * compute_aux_net()
Compute the equivalent arc-capacitated auxiliary network.
Node with capacity constraint for flow networks.
Flow_Type max_cap
Maximum flow capacity that can pass through this node.
Node with supply/demand flow value.
constexpr size_t vsize() const noexcept
See get_num_nodes().
size_t esize() const noexcept
Return the total of arcs of graph.
DynArray< Graph::Node * > nodes
#define ARC_COOKIE(p)
Return the arc cookie
#define NODE_COOKIE(p)
Return the node cookie
void copy_graph(GT >gt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Main namespace for Aleph-w library functions.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
TEST_F(NetCapGraphEdgeCases, EmptyNetwork_NoNodes)
Iterator over arcs of a graph.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
void remove_arc(Arc *arc) override
Remove arc arc from the network.
virtual void disconnect_arc(Arc *arc)
Disconnect arc arc from the graph without deleting it.
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.
void swap(Net_Graph &other) noexcept
Swap contents with another network.
Network flow graph structures.
Network flow graph with capacities.