38#include <gtest/gtest.h>
75 EXPECT_EQ(n.supply_flow, 0);
76 EXPECT_EQ(n.get_supply_flow(), 0);
82 EXPECT_EQ(n.supply_flow, 0);
87 Node original(
"Original");
88 original.supply_flow = 42;
91 EXPECT_EQ(
copy.supply_flow, 42);
97 n.get_supply_flow() = 100;
98 EXPECT_EQ(n.supply_flow, 100);
113 EXPECT_FALSE(net.exist_aux_net());
114 EXPECT_EQ(net.get_super_source(),
nullptr);
115 EXPECT_EQ(net.get_super_sink(),
nullptr);
120 auto* p = net.insert_node(100);
121 EXPECT_EQ(p->supply_flow, 100);
122 EXPECT_EQ(net.get_supply_flow(p), 100);
127 auto* p = net.insert_node(-50);
128 EXPECT_EQ(p->supply_flow, -50);
129 EXPECT_EQ(net.get_supply_flow(p), -50);
134 auto* p = net.insert_node(0);
135 EXPECT_EQ(p->supply_flow, 0);
144 net.insert_node(100);
147 net.insert_node(-30);
149 EXPECT_EQ(net.count_supply_nodes(), 2u);
154 net.insert_node(100);
155 net.insert_node(-50);
156 net.insert_node(-30);
157 net.insert_node(-20);
159 EXPECT_EQ(net.count_demand_nodes(), 3u);
164 net.insert_node(100);
166 net.insert_node(-30);
168 EXPECT_EQ(net.total_supply(), 150);
173 net.insert_node(100);
174 net.insert_node(-50);
175 net.insert_node(-30);
177 EXPECT_EQ(net.total_demand(), 80);
182 net.insert_node(100);
183 net.insert_node(-50);
184 net.insert_node(-50);
186 EXPECT_TRUE(net.is_balanced());
191 net.insert_node(100);
192 net.insert_node(-30);
194 EXPECT_FALSE(net.is_balanced());
203 auto* p = net.insert_node(0);
206 net.set_supply_flow(p, 150);
207 EXPECT_EQ(p->supply_flow, 150);
212 auto* p = net.insert_node(0);
215 net.set_supply_flow(p, -80);
216 EXPECT_EQ(p->supply_flow, -80);
221 auto* p = net.insert_node(0);
224 EXPECT_THROW(net.set_supply_flow(p, 100), std::range_error);
229 auto* p = net.insert_node(0);
232 EXPECT_THROW(net.set_supply_flow(p, -50), std::range_error);
273 EXPECT_FALSE(net.exist_aux_net());
275 auto* result = net.compute_aux_net();
277 EXPECT_EQ(result, &net);
278 EXPECT_TRUE(net.exist_aux_net());
279 EXPECT_NE(net.get_super_source(),
nullptr);
280 EXPECT_NE(net.get_super_sink(),
nullptr);
285 net.compute_aux_net();
287 EXPECT_THROW(net.compute_aux_net(), std::domain_error);
292 EXPECT_EQ(net.get_aux_net(),
nullptr);
297 net.compute_aux_net();
298 EXPECT_EQ(net.get_aux_net(), &net);
303 net.compute_aux_net();
304 EXPECT_TRUE(net.exist_aux_net());
308 EXPECT_FALSE(net.exist_aux_net());
309 EXPECT_EQ(net.get_super_source(),
nullptr);
310 EXPECT_EQ(net.get_super_sink(),
nullptr);
315 EXPECT_THROW(net.free_aux_net(), std::domain_error);
320 EXPECT_THROW((
void)net.is_feasible(), std::domain_error);
340 auto* n2 = net.insert_node(0);
341 net.insert_arc(n1, n2, 100);
344 EXPECT_TRUE(net.is_balanced());
345 EXPECT_EQ(net.total_supply(), 0);
346 EXPECT_EQ(net.total_demand(), 0);
356 auto* s = net.insert_node(100);
359 net.compute_aux_net();
360 EXPECT_FALSE(net.is_feasible());
365 auto* s = net.insert_node(100);
366 auto* d = net.insert_node(-100);
368 net.insert_arc(s, d, 50);
370 net.compute_aux_net();
372 EXPECT_FALSE(net.is_feasible());
378 auto* d = net.insert_node(-80);
381 net.compute_aux_net();
382 EXPECT_FALSE(net.is_feasible());
387 auto* s = net.insert_node(80);
388 auto* d = net.insert_node(-80);
390 net.insert_arc(s, d, 50);
392 net.compute_aux_net();
394 EXPECT_FALSE(net.is_feasible());
399 auto* s = net.insert_node(100);
400 auto* d = net.insert_node(-100);
405 net.insert_arc(s, d, 100);
407 net.compute_aux_net();
408 EXPECT_FALSE(net.is_feasible());
410 EXPECT_TRUE(net.is_feasible());
415 auto* s = net.insert_node(100);
416 auto* d = net.insert_node(-100);
421 net.insert_arc(s, d, 80);
423 net.compute_aux_net();
425 EXPECT_FALSE(net.is_feasible());
434 auto* s1 = net.insert_node(100);
435 auto* s2 = net.insert_node(50);
436 auto* d = net.insert_node(-110);
440 net.insert_arc(s1, d, 60);
441 net.insert_arc(s2, d, 50);
443 net.compute_aux_net();
447net.non_feasible_nodes(supply_list, demand_list);
449 EXPECT_EQ(supply_list.
size(), 1u);
450 EXPECT_EQ(demand_list.
size(), 0u);
456 auto* d1 = net.insert_node(-100);
457 auto* d2 = net.insert_node(-50);
458 auto* s = net.insert_node(130);
462 net.insert_arc(s, d1, 80);
463 net.insert_arc(s, d2, 50);
465 net.compute_aux_net();
469net.non_feasible_nodes(supply_list, demand_list);
471 EXPECT_EQ(supply_list.
size(), 0u);
472 EXPECT_EQ(demand_list.
size(), 1u);
478 auto* s1 = net.insert_node(100);
479 auto* s2 = net.insert_node(70);
480 auto* d1 = net.insert_node(-60);
481 auto* d2 = net.insert_node(-70);
487 net.insert_arc(s1, d1, 30);
488 net.insert_arc(s2, d2, 80);
490 net.compute_aux_net();
494 net.non_feasible_nodes(supply_list, demand_list);
496 EXPECT_EQ(supply_list.
size(), 1u);
497 EXPECT_EQ(demand_list.
size(), 1u);
506 auto* p = net.insert_node(0);
508 p->supply_flow = 100;
510 EXPECT_THROW(net.compute_aux_net(), std::range_error);
515 auto* p = net.insert_node(0);
517 p->supply_flow = -50;
519 EXPECT_THROW(net.compute_aux_net(), std::range_error);
528 auto* s1 = net.insert_node(100);
529 auto* s2 = net.insert_node(50);
534 net.compute_aux_net();
537 EXPECT_NE(net.get_super_source(),
nullptr);
538 EXPECT_EQ(net.get_super_sink(),
nullptr);
543 auto* d1 = net.insert_node(-100);
544 auto* d2 = net.insert_node(-50);
549 net.compute_aux_net();
552 EXPECT_EQ(net.get_super_source(),
nullptr);
553 EXPECT_NE(net.get_super_sink(),
nullptr);
562 net.compute_aux_net();
565 EXPECT_EQ(net.get_super_source(),
nullptr);
566 EXPECT_EQ(net.get_super_sink(),
nullptr);
568 EXPECT_FALSE(net.exist_aux_net());
573 auto* p = net.insert_node(0);
574 EXPECT_EQ(p->supply_flow, 0);
575 EXPECT_EQ(net.count_supply_nodes(), 0u);
576 EXPECT_EQ(net.count_demand_nodes(), 0u);
592 EXPECT_EQ(p->get_info(),
"Factory");
593 EXPECT_DOUBLE_EQ(p->supply_flow, 100.5);
598 auto* s = net.insert_node(
"Source", 100.5);
599 auto* d = net.insert_node(
"Sink", -50.25);
604 EXPECT_DOUBLE_EQ(net.total_supply(), 100.5);
605 EXPECT_DOUBLE_EQ(net.total_demand(), 50.25);
612TEST(NetSupDemDestructorTest, DestructorFreesAuxNet)
615 auto* s = net->insert_node(100);
616 auto* d = net->insert_node(-100);
621 net->compute_aux_net();
622 EXPECT_TRUE(net->exist_aux_net());
633TEST(IntegrationTest, CompleteWorkflow)
639 auto* factory2 = net.insert_node(50);
640 auto* warehouse = net.insert_node(0);
641 auto* store1 = net.insert_node(-80);
642 auto* store2 = net.insert_node(-70);
645 factory1->out_cap = 150;
646 factory2->out_cap = 100;
647 store1->in_cap = 100;
648 store2->in_cap = 100;
651 net.insert_arc(factory1, warehouse, 100);
652 net.insert_arc(factory2, warehouse, 50);
653 net.insert_arc(warehouse, store1, 80);
654 net.insert_arc(warehouse, store2, 70);
657 EXPECT_EQ(net.total_supply(), 150);
658 EXPECT_EQ(net.total_demand(), 150);
659 EXPECT_TRUE(net.is_balanced());
662 net.compute_aux_net();
663 EXPECT_TRUE(net.exist_aux_net());
667 EXPECT_TRUE(net.is_feasible());
671 net.non_feasible_nodes(supply_list, demand_list);
672 EXPECT_TRUE(supply_list.
is_empty());
673 EXPECT_TRUE(demand_list.
is_empty());
677 EXPECT_FALSE(net.exist_aux_net());
686TEST(FloatingFeasibilityTest, AFlowOfTenthsIsFeasible)
689 auto feasible = [] (
auto maximum_flow)
699 net.compute_aux_net();
701 return net.is_feasible();
714 struct Named_Maximum_Flow
717 void (*run)(Floating_Net &);
720 const Named_Maximum_Flow maximum_flows[] = {
733TEST(FloatingFeasibilityTest, TheDemandsOfASupplyInTenthsAreMet)
735 for (
const auto & [name, maximum_flow] : maximum_flows)
738 auto * a = net.insert_node(2.4);
739 auto * b = net.insert_node(-1.1);
740 auto * c = net.insert_node(-1.3);
741 net.insert_arc(a, b, 5.0);
742 net.insert_arc(a, c, 5.0);
743 net.compute_aux_net();
745 EXPECT_TRUE(net.is_feasible()) << name;
747 net.non_feasible_nodes(supply_list, demand_list);
748 EXPECT_TRUE(supply_list.
is_empty()) << name;
749 EXPECT_TRUE(demand_list.
is_empty()) << name;
757TEST(FloatingFeasibilityTest, ARealDeficitIsNotMet)
759 for (
const auto & [name, maximum_flow] : maximum_flows)
762 auto * a = net.insert_node(2.4);
763 auto * b = net.insert_node(-1.1);
764 auto * c = net.insert_node(-1.3);
765 net.insert_arc(a, b, 5.0);
766 net.insert_arc(a, c, 1.2);
767 net.compute_aux_net();
769 EXPECT_FALSE(net.is_feasible()) << name;
771 net.non_feasible_nodes(supply_list, demand_list);
772 ASSERT_EQ(supply_list.
size(), 1u) << name;
773 EXPECT_EQ(supply_list.
get_first(), a) << name;
774 ASSERT_EQ(demand_list.
size(), 1u) << name;
775 EXPECT_EQ(demand_list.
get_first(), c) << name;
778 for (
const auto & [name, maximum_flow] : maximum_flows)
781 auto * a = net.insert_node(1000.0);
782 auto * b = net.insert_node(-1000.0);
783 auto * c = net.insert_node(1e-7);
784 auto * d = net.insert_node(-1e-7);
785 net.insert_arc(a, b, 1000.0);
786 net.compute_aux_net();
788 EXPECT_FALSE(net.is_feasible()) << name;
790 net.non_feasible_nodes(supply_list, demand_list);
791 ASSERT_EQ(supply_list.
size(), 1u) << name;
792 EXPECT_EQ(supply_list.
get_first(), c) << name;
793 ASSERT_EQ(demand_list.
size(), 1u) << name;
794 EXPECT_EQ(demand_list.
get_first(), d) << name;
804TEST(FloatingFeasibilityTest, FeasibleInstancesInTenthsAreFeasible)
812 std::mt19937 gen(31337);
813 size_t instances = 0;
814 for (
int k = 0;
k < 300; ++
k)
817 const int n = 5 + int(gen() % 5);
819 auto cap = [&gen] {
return long(1 + gen() % 40); };
820 for (
int i = 1; i < n - 1; ++i)
822 const int u = int(gen() %
unsigned(i));
823 arcs.append(Spec_Arc{u, i, cap()});
824 const int v = i + 1 + int(gen() %
unsigned(n - 1 - i));
825 arcs.append(Spec_Arc{i, v, cap()});
827 for (
int extra =
int(4 + gen() % 11); extra > 0; --extra)
829 const int u = int(gen() %
unsigned(n - 1));
830 const int v = 1 + int(gen() %
unsigned(n - 1));
832 arcs.append(Spec_Arc{u, v, cap()});
837 for (
int i = 0; i < n; ++i)
839 const unsigned r = gen() % 4;
841 want[size_t(i)] = 1 + long(gen() % 30);
843 want[size_t(i)] = -(1 + long(gen() % 30));
845 want[0] = 1 + long(gen() % 30);
846 want[size_t(n - 1)] = -(1 + long(gen() % 30));
851 for (
int i = 0; i < n; ++i)
852 inodes.
append(inet.insert_node());
853 auto * s = inet.insert_node();
854 auto * t = inet.insert_node();
855 for (
size_t j = 0; j <
arcs.size(); ++j)
856 inet.insert_arc(inodes[
size_t(
arcs[j].u)], inodes[size_t(
arcs[j].v)],
arcs[j].cap);
858 for (
int i = 0; i < n; ++i)
859 if (want[
size_t(i)] > 0)
860 ends[size_t(i)] = inet.insert_arc(s, inodes[
size_t(i)], want[
size_t(i)]);
861 else if (want[
size_t(i)] < 0)
862 ends[size_t(i)] = inet.insert_arc(inodes[
size_t(i)], t, -want[
size_t(i)]);
865 bool supplies =
false, demands =
false;
866 for (
int i = 0; i < n; ++i)
867 if (ends[
size_t(i)] !=
nullptr)
869 supply[size_t(i)] = want[size_t(i)] > 0 ? ends[size_t(i)]->flow : -ends[size_t(i)]->flow;
870 supplies = supplies or supply[size_t(i)] > 0;
871 demands = demands or supply[size_t(i)] < 0;
873 if (not supplies or not demands)
877 for (
const auto & [name, maximum_flow] : maximum_flows)
881 for (
int i = 0; i < n; ++i)
882 nodes.
append(net.insert_node(
double(supply[
size_t(i)]) / 10.0));
883 for (
size_t j = 0; j <
arcs.size(); ++j)
885 double(
arcs[j].cap) / 10.0);
886 net.compute_aux_net();
888 EXPECT_TRUE(net.is_feasible()) << name <<
" on the instance " <<
k;
891 EXPECT_GT(instances, 250u);
899TEST(FloatingFeasibilityTest, AnInfiniteSupplyIsMetOnlyByAnInfiniteFlow)
901 const double inf = std::numeric_limits<double>::infinity();
902 for (
const auto & [name, maximum_flow] : maximum_flows)
903 for (
const double cap : {2.5, inf})
906 auto * a = net.insert_node(inf);
907 auto * b = net.insert_node(-inf);
910 net.insert_arc(a, b, cap);
911 net.compute_aux_net();
913 const bool met = cap == inf;
914 EXPECT_EQ(net.is_feasible(), met) << name <<
" with a -> b of " << cap;
916 net.non_feasible_nodes(supply_list, demand_list);
917 EXPECT_EQ(supply_list.
size(), met ? 0u : 1u) << name <<
" with a -> b of " << cap;
918 EXPECT_EQ(demand_list.
size(), met ? 0u : 1u) << name <<
" with a -> b of " << cap;
919 if (not met
and supply_list.
size() == 1u
and demand_list.
size() == 1u)
921 EXPECT_EQ(supply_list.
get_first(), a) << name;
922 EXPECT_EQ(demand_list.
get_first(), b) << name;
954 enum class Aux { both, source, sink };
955 constexpr Aux every_aux[] = {Aux::both, Aux::source, Aux::sink};
957 const char * name_of(
const Aux aux)
959 return aux == Aux::both ?
"with the two super nodes"
960 : aux == Aux::source ?
"with the super source alone" :
"with the super sink alone";
967 if (it.get_curr() == p)
983 explicit Shipment(
const Aux aux)
985 s = net.insert_node(aux == Aux::sink ? 0 : 10);
986 t = net.insert_node(aux == Aux::source ? 0 : -10);
987 st = net.insert_arc(s, t, 20);
988 net.compute_aux_net();
989 source = net.get_super_source();
990 sink = net.get_super_sink();
994 size_t supers()
const {
return size_t(source !=
nullptr) + size_t(sink !=
nullptr); }
998 void fill_plain(SimpleBase & net)
1000 auto x = net.insert_node();
1001 auto y = net.insert_node();
1002 auto z = net.insert_node();
1003 net.insert_arc(x,
y, 1);
1004 net.insert_arc(
y, z, 1);
1010 ASSERT_FALSE(net.exist_aux_net());
1011 EXPECT_EQ(net.get_super_source(),
nullptr);
1012 EXPECT_EQ(net.get_super_sink(),
nullptr);
1013 EXPECT_EQ(net.get_aux_net(),
nullptr);
1014 EXPECT_THROW(net.free_aux_net(), std::domain_error);
1015 EXPECT_THROW((
void) net.is_feasible(), std::domain_error);
1016 EXPECT_EQ(net.get_num_nodes(),
nodes);
1024 ASSERT_EQ(net.get_super_source(), source);
1025 ASSERT_EQ(net.get_super_sink(), sink);
1026 const size_t supers = size_t(source !=
nullptr) + size_t(sink !=
nullptr);
1029 expect_no_aux_net(net, own);
1032 ASSERT_TRUE(source ==
nullptr or is_node_of(net, source));
1033 ASSERT_TRUE(sink ==
nullptr or is_node_of(net, sink));
1034 EXPECT_TRUE(net.exist_aux_net());
1035 EXPECT_EQ(net.get_aux_net(), &net);
1036 EXPECT_EQ(net.get_num_nodes(), own + supers);
1039 EXPECT_FALSE(net.is_feasible());
1042 EXPECT_FALSE(net.exist_aux_net());
1043 EXPECT_EQ(net.get_num_nodes(), own);
1047TEST(NetSupDemChangedNetwork, AnExchangeThroughTheBaseTakesTheSuperNodesAway)
1049 for (
const Aux aux : every_aux)
1051 SCOPED_TRACE(name_of(aux));
1058 SimpleBase & base = sh.net;
1060 expect_no_aux_net(sh.net, 3);
1063 sh.net.set_supply_flow(sh.net.get_first_node(), 5);
1064 sh.net.compute_aux_net();
1065 ASSERT_NE(sh.net.get_super_source(),
nullptr);
1066 expect_aux_net(sh.net, sh.net.get_super_source(),
nullptr, 3);
1074 SimpleBase & base = sh.net;
1075 base = std::move(other);
1076 expect_no_aux_net(sh.net, 3);
1077 EXPECT_EQ(other.get_num_nodes(), 2 + sh.supers());
1084 SimpleBase & base = sh.net;
1086 expect_no_aux_net(sh.net, 3);
1087 EXPECT_EQ(other.get_num_nodes(), 2 + sh.supers());
1092 SimpleBase & base = sh.net;
1093 SimpleBase taken(std::move(base));
1094 EXPECT_EQ(taken.get_num_nodes(), 2 + sh.supers());
1095 expect_no_aux_net(sh.net, 0);
1097 for (
const Aux aux2 : every_aux)
1101 SCOPED_TRACE(name_of(aux2));
1102 Shipment sh(aux), sh2(aux2);
1103 sh2.net.insert_node(0);
1104 SimpleBase & base = sh.net;
1105 SimpleBase & base2 = sh2.net;
1107 expect_no_aux_net(sh.net, 3 + sh2.supers());
1108 expect_no_aux_net(sh2.net, 2 + sh.supers());
1113TEST(NetSupDemChangedNetwork, ARemovedSuperNodeIsForgotten)
1115 for (
const Aux aux : every_aux)
1116 for (
const bool the_source : {
true,
false})
1117 for (
const bool through_the_base : {
false,
true})
1120 SimpleNode *
const gone = the_source ? sh.source : sh.sink;
1121 if (gone ==
nullptr)
1123 SCOPED_TRACE(std::string(name_of(aux)) + (the_source ?
", its super source" :
", its super sink")
1124 + (through_the_base ?
", through the base" :
""));
1126 if (through_the_base)
1127 static_cast<SimpleBase &
>(sh.net).remove_node(gone);
1129 sh.net.remove_node(gone);
1130 expect_aux_net(sh.net, the_source ?
nullptr : sh.source, the_source ? sh.sink :
nullptr, 2);
1134TEST(NetSupDemChangedNetwork, AnotherNodeRemovedIsNoneOfThem)
1136 for (
const Aux aux : every_aux)
1138 SCOPED_TRACE(name_of(aux));
1142 sh.net.remove_node(sh.net.insert_node(0));
1143 expect_aux_net(sh.net, sh.source, sh.sink, 2);
1148 sh.net.remove_node(aux == Aux::sink ? sh.t : sh.s);
1149 expect_aux_net(sh.net, sh.source, sh.sink, 1);
1154TEST(NetSupDemChangedNetwork, ClearGraphAndCopyGraphTakeTheSuperNodesAway)
1156 for (
const Aux aux : every_aux)
1158 SCOPED_TRACE(name_of(aux));
1162 expect_no_aux_net(sh.net, 0);
1164 for (
const Aux aux2 : every_aux)
1168 SCOPED_TRACE(name_of(aux2));
1169 Shipment sh(aux), from(aux2);
1171 expect_no_aux_net(sh.net, 2 + from.supers());
1172 expect_aux_net(from.net, from.source, from.sink, 2);
1177TEST(NetSupDemChangedNetwork, AChangeThatLeavesTheSuperNodesKeepsThem)
1184 size_t (*change)(Shipment &);
1186 const Way ways[] = {
1187 {
"nothing", [] (Shipment &) ->
size_t {
return 2; }},
1188 {
"a node inserted", [] (Shipment & sh) ->
size_t
1190 sh.net.insert_node(0);
1193 {
"an arc inserted", [] (Shipment & sh) ->
size_t
1195 sh.net.insert_arc(sh.t, sh.s, 1);
1198 {
"an arc removed", [] (Shipment & sh) ->
size_t
1200 sh.net.remove_arc(sh.st);
1203 {
"the arcs of the super nodes removed", [] (Shipment & sh) ->
size_t
1208 for (SimpleBase::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
1210 for (
auto it =
arcs.get_it(); it.has_curr(); it.next_ne())
1211 sh.net.remove_arc(it.get_curr());
1214 {
"an arc disconnected and connected again", [] (Shipment & sh) ->
size_t
1216 sh.net.disconnect_arc(sh.st);
1217 sh.net.connect_arc(sh.st);
1220 {
"another order of the nodes", [] (Shipment & sh) ->
size_t
1222 sh.net.sort_nodes([] (
SimpleNode * p,
SimpleNode * q) {
return p->supply_flow < q->supply_flow; });
1225 {
"swapped with itself through the base", [] (Shipment & sh) ->
size_t
1227 SimpleBase & base = sh.net;
1231 {
"assigned to itself through the base", [] (Shipment & sh) ->
size_t
1233 SimpleBase & base = sh.net;
1234 const SimpleBase & same = sh.net;
1238 {
"moved to itself through the base", [] (Shipment & sh) ->
size_t
1240 SimpleBase & base = sh.net;
1241 SimpleBase & same = sh.net;
1242 base = std::move(same);
1245 {
"swapped with itself", [] (Shipment & sh) ->
size_t
1247 sh.net.swap(sh.net);
1250 {
"assigned to itself", [] (Shipment & sh) ->
size_t
1256 {
"moved to itself", [] (Shipment & sh) ->
size_t
1259 sh.net = std::move(same);
1263 for (
const Aux aux : every_aux)
1264 for (
const Way & way : ways)
1266 SCOPED_TRACE(std::string(name_of(aux)) +
", " + way.name);
1268 const size_t own = way.change(sh);
1269 expect_aux_net(sh.net, sh.source, sh.sink, own);
1273TEST(NetSupDemChangedNetwork, ItsOwnSwapMovesAndCopiesTakeTheSuperNodesAlong)
1275 for (
const Aux aux : every_aux)
1277 SCOPED_TRACE(name_of(aux));
1281 expect_no_aux_net(sh.net, 0);
1282 expect_aux_net(moved, sh.source, sh.sink, 2);
1285 sh.net.insert_node(7);
1286 sh.net.compute_aux_net();
1287 ASSERT_NE(sh.net.get_super_source(),
nullptr);
1288 expect_aux_net(sh.net, sh.net.get_super_source(),
nullptr, 1);
1295 EXPECT_EQ(
copy.get_super_source() !=
nullptr, sh.source !=
nullptr);
1296 EXPECT_EQ(
copy.get_super_sink() !=
nullptr, sh.sink !=
nullptr);
1297 if (sh.source !=
nullptr)
1299 EXPECT_NE(
copy.get_super_source(), sh.source);
1301 if (sh.sink !=
nullptr)
1303 EXPECT_NE(
copy.get_super_sink(), sh.sink);
1305 expect_aux_net(
copy,
copy.get_super_source(),
copy.get_super_sink(), 2);
1306 expect_aux_net(sh.net, sh.source, sh.sink, 2);
1308 for (
const Aux aux2 : every_aux)
1310 SCOPED_TRACE(name_of(aux2));
1313 Shipment sh(aux), target(aux2);
1314 target.net.insert_node(0);
1315 target.net = std::move(sh.net);
1316 expect_aux_net(target.net, sh.source, sh.sink, 2);
1317 expect_aux_net(sh.net, target.source, target.sink, 3);
1320 Shipment sh(aux), sh2(aux2);
1321 sh2.net.insert_node(0);
1322 sh.net.swap(sh2.net);
1323 expect_aux_net(sh.net, sh2.source, sh2.sink, 3);
1324 expect_aux_net(sh2.net, sh.source, sh.sink, 2);
1328 Shipment sh(aux), target(aux2);
1329 target.net.insert_node(0);
1330 target.net = sh.net;
1331 EXPECT_EQ(target.net.get_super_source() !=
nullptr, sh.source !=
nullptr);
1332 EXPECT_EQ(target.net.get_super_sink() !=
nullptr, sh.sink !=
nullptr);
1333 expect_aux_net(target.net, target.net.get_super_source(), target.net.get_super_sink(), 2);
1334 expect_aux_net(sh.net, sh.source, sh.sink, 2);
1347 EXPECT_EQ(net.is_feasible(), feasible);
1349 net.non_feasible_nodes(supplies, demands);
1350 ASSERT_EQ(supplies.
size(), supply ==
nullptr ? 0u : 1u);
1351 ASSERT_EQ(demands.
size(), demand ==
nullptr ? 0u : 1u);
1352 if (supply !=
nullptr)
1354 EXPECT_EQ(supplies.
get_first(), supply);
1356 if (demand !=
nullptr)
1368TEST(NetSupDemChangedNetwork, WithoutASuperNodeNoSupplyNorDemandIsMet)
1370 for (
const Aux aux : every_aux)
1371 for (
const bool with_flow : {
false,
true})
1372 for (
const int removed : {0, 1, 2})
1375 if ((removed == 1
and sh.source ==
nullptr) or (removed == 2
and sh.sink ==
nullptr))
1377 SCOPED_TRACE(std::string(name_of(aux)) + (with_flow ?
", after a maximum flow" :
", without a flow")
1378 + (removed == 1 ?
", its super source removed" : removed == 2 ?
", its super sink removed" :
""));
1382 sh.net.remove_node(sh.source);
1383 else if (removed == 2)
1384 sh.net.remove_node(sh.sink);
1386 if (not sh.net.exist_aux_net())
1390 EXPECT_THROW(sh.net.non_feasible_nodes(supplies, demands), std::domain_error);
1393 if (aux == Aux::both
and removed == 0
and with_flow)
1394 expect_unmet(sh.net,
true,
nullptr,
nullptr);
1396 expect_unmet(sh.net,
false, sh.s->supply_flow > 0 ? sh.s :
nullptr,
1397 sh.t->supply_flow < 0 ? sh.t :
nullptr);
1403 ::testing::InitGoogleTest(&argc, argv);
1404 return RUN_ALL_TESTS();
WeightedDigraph::Node Node
Simple dynamic array with automatic resizing and functional operations.
T & append(const T &data)
Append a copy of data
constexpr bool is_empty() const noexcept
Return true if this (as header node) is empty.
Dynamic doubly linked list with O(1) size and bidirectional access.
const size_t & size() const noexcept
Return the number of elements (constant time)
T & get_first()
Return a modifiable reference to first item in the list.
T & append(const T &item)
Append a copied item at the end of the list.
void next_ne() noexcept(shows_ne)
Advances the iterator to the next filtered element, as next() does.
Network graph with supply and demand nodes.
Net_Graph< SimpleNode, SimpleArc > Net_Class
The base network class type.
Node * insert_node(const Node_Type &node_info, const Flow_Type &supply)
Insert a node with info and supply/demand value.
Node with supply/demand flow value.
Filtered iterator on the nodes of a graph.
DynArray< Graph::Node * > nodes
DynArray< Graph::Arc * > arcs
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
void copy_graph(GT >gt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Main namespace for Aleph-w library functions.
Net::Flow_Type edmonds_karp_maximum_flow(Net &net)
Maximize flow using the Edmonds-Karp algorithm.
Net::Flow_Type dinic_maximum_flow(Net &net)
Compute maximum flow using Dinic's algorithm.
Net::Flow_Type heap_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a height-ordered heap.
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
and
Check uniqueness with explicit hash + equality functors.
Net::Flow_Type fifo_preflow_maximum_flow(Net &net)
Preflow-push maximum flow using a FIFO queue of active nodes.
Net::Flow_Type ford_fulkerson_maximum_flow(Net &net)
Maximize flow using the Ford-Fulkerson algorithm.
TEST_F(NetSupDemNodeTest, DefaultConstructor)
Net_Sup_Dem_Graph< SimpleNode, SimpleArc > SimpleNet
Arc of a flow network implemented with adjacency lists.
Flow network implemented with adjacency lists.
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
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.
Dynamic doubly linked list implementation.
Advanced maximum flow algorithms.
Supply-demand network flow algorithms.