Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
net_sup_dem_test.cc
Go to the documentation of this file.
1
2/*
3 Aleph_w
4
5 Data structures & Algorithms
6 version 2.0.0b
7 https://github.com/lrleon/Aleph-w
8
9 This file is part of Aleph-w library
10
11 Copyright (c) 2002-2026 Leandro Rabindranath Leon
12
13 Permission is hereby granted, free of charge, to any person obtaining a copy
14 of this software and associated documentation files (the "Software"), to deal
15 in the Software without restriction, including without limitation the rights
16 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17 copies of the Software, and to permit persons to whom the Software is
18 furnished to do so, subject to the following conditions:
19
20 The above copyright notice and this permission notice shall be included in all
21 copies or substantial portions of the Software.
22
23 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29 SOFTWARE.
30*/
31
32
38#include <gtest/gtest.h>
39#include <tpl_net_sup_dem.H>
40#include <tpl_maxflow.H>
41#include <tpl_dynDlist.H>
42#include <limits>
43#include <random>
44#include <string>
45
46using namespace Aleph;
47
48// =============================================================================
49// Type Aliases for Testing
50// =============================================================================
51
52// Simple network with int flow (use Empty_Class as node info to avoid ambiguity)
56
57// Network with string info and double flow
61
62// =============================================================================
63// Net_Sup_Dem_Node Tests
64// =============================================================================
65
66class NetSupDemNodeTest : public ::testing::Test
67{
68protected:
70};
71
72TEST_F(NetSupDemNodeTest, DefaultConstructor)
73{
74 Node n;
75 EXPECT_EQ(n.supply_flow, 0);
76 EXPECT_EQ(n.get_supply_flow(), 0);
77}
78
79TEST_F(NetSupDemNodeTest, InfoConstructor)
80{
81 Node n("TestNode");
82 EXPECT_EQ(n.supply_flow, 0);
83}
84
85TEST_F(NetSupDemNodeTest, CopyFromPointer)
86{
87 Node original("Original");
88 original.supply_flow = 42;
89
90 Node copy(&original);
91 EXPECT_EQ(copy.supply_flow, 42);
92}
93
94TEST_F(NetSupDemNodeTest, GetSupplyFlowModifiable)
95{
96 Node n;
97 n.get_supply_flow() = 100;
98 EXPECT_EQ(n.supply_flow, 100);
99}
100
101// =============================================================================
102// Net_Sup_Dem_Graph Basic Tests
103// =============================================================================
104
105class NetSupDemGraphTest : public ::testing::Test
106{
107protected:
109};
110
111TEST_F(NetSupDemGraphTest, DefaultConstructor)
112{
113 EXPECT_FALSE(net.exist_aux_net());
114 EXPECT_EQ(net.get_super_source(), nullptr);
115 EXPECT_EQ(net.get_super_sink(), nullptr);
116}
117
118TEST_F(NetSupDemGraphTest, InsertNodeWithSupply)
119{
120 auto* p = net.insert_node(100); // supply node
121 EXPECT_EQ(p->supply_flow, 100);
122 EXPECT_EQ(net.get_supply_flow(p), 100);
123}
124
125TEST_F(NetSupDemGraphTest, InsertNodeWithDemand)
126{
127 auto* p = net.insert_node(-50); // demand node
128 EXPECT_EQ(p->supply_flow, -50);
129 EXPECT_EQ(net.get_supply_flow(p), -50);
130}
131
132TEST_F(NetSupDemGraphTest, InsertTransitNode)
133{
134 auto* p = net.insert_node(0); // transit node
135 EXPECT_EQ(p->supply_flow, 0);
136}
137
138// ==============================================================================
139// Supply/Demand Query Tests
140// =============================================================================
141
142TEST_F(NetSupDemGraphTest, CountSupplyNodes)
143{
144 net.insert_node(100); // supply
145 net.insert_node(50); // supply
146 net.insert_node(0); // transit
147 net.insert_node(-30); // demand
148
149 EXPECT_EQ(net.count_supply_nodes(), 2u);
150}
151
152TEST_F(NetSupDemGraphTest, CountDemandNodes)
153{
154 net.insert_node(100); // supply
155 net.insert_node(-50); // demand
156 net.insert_node(-30); // demand
157 net.insert_node(-20); // demand
158
159 EXPECT_EQ(net.count_demand_nodes(), 3u);
160}
161
163{
164 net.insert_node(100);
165 net.insert_node(50);
166 net.insert_node(-30); // demand, not counted
167
168 EXPECT_EQ(net.total_supply(), 150);
169}
170
172{
173 net.insert_node(100); // supply, not counted
174 net.insert_node(-50);
175 net.insert_node(-30);
176
177 EXPECT_EQ(net.total_demand(), 80);
178}
179
181{
182 net.insert_node(100);
183 net.insert_node(-50);
184 net.insert_node(-50);
185
186 EXPECT_TRUE(net.is_balanced());
187}
188
190{
191 net.insert_node(100);
192 net.insert_node(-30);
193
194 EXPECT_FALSE(net.is_balanced());
195}
196
197// =============================================================================
198// Set Supply Flow Tests
199// =============================================================================
200
202{
203 auto* p = net.insert_node(0);
204 p->out_cap = 200; // Set output capacity
205
206 net.set_supply_flow(p, 150);
207 EXPECT_EQ(p->supply_flow, 150);
208}
209
211{
212 auto* p = net.insert_node(0);
213 p->in_cap = 100; // Set input capacity
214
215 net.set_supply_flow(p, -80);
216 EXPECT_EQ(p->supply_flow, -80);
217}
218
219TEST_F(NetSupDemGraphTest, SetSupplyFlowExceedsCapacityThrows)
220{
221 auto* p = net.insert_node(0);
222 p->out_cap = 50;
223
224 EXPECT_THROW(net.set_supply_flow(p, 100), std::range_error);
225}
226
227TEST_F(NetSupDemGraphTest, SetDemandFlowExceedsCapacityThrows)
228{
229 auto* p = net.insert_node(0);
230 p->in_cap = 30;
231
232 EXPECT_THROW(net.set_supply_flow(p, -50), std::range_error);
233}
234
235// =============================================================================
236// Auxiliary Network Tests
237// =============================================================================
238
239class AuxNetTest : public ::testing::Test
240{
241protected:
243
244 void SetUp() override
245 {
246 // Create a simple supply-demand network
247 // Supply: 100 units
248 // Demand: 80 units (feasible)
249 auto* s1 = net.insert_node(50); // supply 50
250 auto* s2 = net.insert_node(50); // supply 50
251 auto* t1 = net.insert_node(0); // transit
252 auto* d1 = net.insert_node(-40); // demand 40
253 auto* d2 = net.insert_node(-40); // demand 40
254
255 // Set capacities for supply nodes
256 s1->out_cap = 100;
257 s2->out_cap = 100;
258
259 // Set capacities for demand nodes
260 d1->in_cap = 100;
261 d2->in_cap = 100;
262
263 // Create arcs with sufficient capacity
264 net.insert_arc(s1, t1, 100);
265 net.insert_arc(s2, t1, 100);
266 net.insert_arc(t1, d1, 100);
267 net.insert_arc(t1, d2, 100);
268 }
269};
270
271TEST_F(AuxNetTest, ComputeAuxNet)
272{
273 EXPECT_FALSE(net.exist_aux_net());
274
275 auto* result = net.compute_aux_net();
276
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);
281}
282
283TEST_F(AuxNetTest, ComputeAuxNetTwiceThrows)
284{
285 net.compute_aux_net();
286
287 EXPECT_THROW(net.compute_aux_net(), std::domain_error);
288}
289
290TEST_F(AuxNetTest, GetAuxNetBeforeCompute)
291{
292 EXPECT_EQ(net.get_aux_net(), nullptr);
293}
294
295TEST_F(AuxNetTest, GetAuxNetAfterCompute)
296{
297 net.compute_aux_net();
298 EXPECT_EQ(net.get_aux_net(), &net);
299}
300
301TEST_F(AuxNetTest, FreeAuxNet)
302{
303 net.compute_aux_net();
304 EXPECT_TRUE(net.exist_aux_net());
305
306 net.free_aux_net();
307
308 EXPECT_FALSE(net.exist_aux_net());
309 EXPECT_EQ(net.get_super_source(), nullptr);
310 EXPECT_EQ(net.get_super_sink(), nullptr);
311}
312
313TEST_F(AuxNetTest, FreeAuxNetWithoutComputeThrows)
314{
315 EXPECT_THROW(net.free_aux_net(), std::domain_error);
316}
317
318TEST_F(AuxNetTest, IsFeasibleWithoutAuxNetThrows)
319{
320 EXPECT_THROW((void)net.is_feasible(), std::domain_error);
321}
322
323// =============================================================================
324// Feasibility Tests
325// =============================================================================
326
327class FeasibilityTest : public ::testing::Test
328{
329protected:
331};
332
333TEST_F(FeasibilityTest, EmptyNetworkIsFeasible)
334{
335 // Network with no supply or demand (all transit nodes)
336 // Note: When there are no supply/demand nodes, compute_aux_net
337 // removes both super nodes, so exist_aux_net() returns false.
338 // This is correct behavior - there's nothing to check for feasibility.
339 auto* n1 = net.insert_node(0);
340 auto* n2 = net.insert_node(0);
341 net.insert_arc(n1, n2, 100);
342
343 // A network with no supply/demand is trivially balanced and feasible
344 EXPECT_TRUE(net.is_balanced());
345 EXPECT_EQ(net.total_supply(), 0);
346 EXPECT_EQ(net.total_demand(), 0);
347}
348
349// is_feasible() judges the flow of the auxiliary network, which a max-flow
350// algorithm computes; it judged the fields out_flow and in_flow of the
351// nodes, which the network does not write and these tests set by hand
352
353// A supply has nowhere to go without a demand
354TEST_F(FeasibilityTest, SupplyOnlyNetworkIsNotFeasible)
355{
356 auto* s = net.insert_node(100);
357 s->out_cap = 200;
358
359 net.compute_aux_net();
360 EXPECT_FALSE(net.is_feasible());
361}
362
363TEST_F(FeasibilityTest, SupplyNotMeetingFlowRequirement)
364{
365 auto* s = net.insert_node(100);
366 auto* d = net.insert_node(-100);
367 s->out_cap = 200;
368 net.insert_arc(s, d, 50); // only 50 can go
369
370 net.compute_aux_net();
371 (void) edmonds_karp_maximum_flow(net);
372 EXPECT_FALSE(net.is_feasible());
373}
374
375// A demand has nothing to receive without a supply
376TEST_F(FeasibilityTest, DemandOnlyNetworkIsNotFeasible)
377{
378 auto* d = net.insert_node(-80);
379 d->in_cap = 100;
380
381 net.compute_aux_net();
382 EXPECT_FALSE(net.is_feasible());
383}
384
386{
387 auto* s = net.insert_node(80);
388 auto* d = net.insert_node(-80);
389 d->in_cap = 100;
390 net.insert_arc(s, d, 50); // only 50 can come
391
392 net.compute_aux_net();
393 (void) edmonds_karp_maximum_flow(net);
394 EXPECT_FALSE(net.is_feasible());
395}
396
397TEST_F(FeasibilityTest, MixedSupplyDemandFeasible)
398{
399 auto* s = net.insert_node(100);
400 auto* d = net.insert_node(-100);
401
402 s->out_cap = 150;
403 d->in_cap = 150;
404
405 net.insert_arc(s, d, 100);
406
407 net.compute_aux_net();
408 EXPECT_FALSE(net.is_feasible()); // no flow yet
409 (void) edmonds_karp_maximum_flow(net);
410 EXPECT_TRUE(net.is_feasible());
411}
412
413TEST_F(FeasibilityTest, MixedSupplyDemandNotFeasible)
414{
415 auto* s = net.insert_node(100);
416 auto* d = net.insert_node(-100);
417
418 s->out_cap = 150;
419 d->in_cap = 150;
420
421 net.insert_arc(s, d, 80); // only 80 of the 100 can go
422
423 net.compute_aux_net();
424 (void) edmonds_karp_maximum_flow(net);
425 EXPECT_FALSE(net.is_feasible());
426}
427
428// =============================================================================
429// Non-Feasible Nodes Tests
430// =============================================================================
431
432TEST_F(FeasibilityTest, NonFeasibleSupplyNodes)
433{
434 auto* s1 = net.insert_node(100);
435 auto* s2 = net.insert_node(50);
436 auto* d = net.insert_node(-110);
437
438 s1->out_cap = 200;
439 s2->out_cap = 100;
440 net.insert_arc(s1, d, 60); // s1 ships 60 of its 100
441 net.insert_arc(s2, d, 50); // s2 ships its 50, and d receives its 110
442
443 net.compute_aux_net();
444 (void) edmonds_karp_maximum_flow(net);
445
446 DynDlist<SimpleNet::Node*> supply_list, demand_list;
447net.non_feasible_nodes(supply_list, demand_list);
448
449 EXPECT_EQ(supply_list.size(), 1u);
450 EXPECT_EQ(demand_list.size(), 0u);
451 EXPECT_EQ(supply_list.get_first(), s1);
452}
453
454TEST_F(FeasibilityTest, NonFeasibleDemandNodes)
455{
456 auto* d1 = net.insert_node(-100);
457 auto* d2 = net.insert_node(-50);
458 auto* s = net.insert_node(130);
459
460 d1->in_cap = 200;
461 d2->in_cap = 100;
462 net.insert_arc(s, d1, 80); // d1 receives 80 of its 100
463 net.insert_arc(s, d2, 50); // d2 receives its 50, and s ships its 130
464
465 net.compute_aux_net();
466 (void) edmonds_karp_maximum_flow(net);
467
468 DynDlist<SimpleNet::Node*> supply_list, demand_list;
469net.non_feasible_nodes(supply_list, demand_list);
470
471 EXPECT_EQ(supply_list.size(), 0u);
472 EXPECT_EQ(demand_list.size(), 1u);
473 EXPECT_EQ(demand_list.get_first(), d1);
474}
475
476TEST_F(FeasibilityTest, MultipleMixedNonFeasibleNodes)
477{
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);
482
483 s1->out_cap = 200;
484 s2->out_cap = 200;
485 d1->in_cap = 200;
486 d2->in_cap = 200;
487 net.insert_arc(s1, d1, 30); // s1 ships 30 of its 100; d1 receives 30 of 60
488 net.insert_arc(s2, d2, 80); // s2 ships its 70, and d2 receives its 70
489
490 net.compute_aux_net();
491 (void) edmonds_karp_maximum_flow(net);
492
493 DynDlist<SimpleNet::Node*> supply_list, demand_list;
494 net.non_feasible_nodes(supply_list, demand_list);
495
496 EXPECT_EQ(supply_list.size(), 1u);
497 EXPECT_EQ(demand_list.size(), 1u);
498}
499
500// =============================================================================
501// Capacity Validation Tests
502// =============================================================================
503
504TEST_F(NetSupDemGraphTest, SupplyExceedsOutCapacityThrows)
505{
506 auto* p = net.insert_node(0);
507 p->out_cap = 50;
508 p->supply_flow = 100; // Will exceed capacity
509
510 EXPECT_THROW(net.compute_aux_net(), std::range_error);
511}
512
513TEST_F(NetSupDemGraphTest, DemandExceedsInCapacityThrows)
514{
515 auto* p = net.insert_node(0);
516 p->in_cap = 30;
517 p->supply_flow = -50; // Demand exceeds in_cap
518
519 EXPECT_THROW(net.compute_aux_net(), std::range_error);
520}
521
522// =============================================================================
523// Edge Cases
524// =============================================================================
525
526TEST_F(NetSupDemGraphTest, OnlySupplyNodes)
527{
528 auto* s1 = net.insert_node(100);
529 auto* s2 = net.insert_node(50);
530
531 s1->out_cap = 200;
532 s2->out_cap = 100;
533
534 net.compute_aux_net();
535
536 // Should have super-source but no super-sink
537 EXPECT_NE(net.get_super_source(), nullptr);
538 EXPECT_EQ(net.get_super_sink(), nullptr);
539}
540
541TEST_F(NetSupDemGraphTest, OnlyDemandNodes)
542{
543 auto* d1 = net.insert_node(-100);
544 auto* d2 = net.insert_node(-50);
545
546 d1->in_cap = 200;
547 d2->in_cap = 100;
548
549 net.compute_aux_net();
550
551 // Should have super-sink but no super-source
552 EXPECT_EQ(net.get_super_source(), nullptr);
553 EXPECT_NE(net.get_super_sink(), nullptr);
554}
555
556TEST_F(NetSupDemGraphTest, OnlyTransitNodes)
557{
558 net.insert_node(0);
559 net.insert_node(0);
560 net.insert_node(0);
561
562 net.compute_aux_net();
563
564 // Neither super-source nor super-sink needed
565 EXPECT_EQ(net.get_super_source(), nullptr);
566 EXPECT_EQ(net.get_super_sink(), nullptr);
567 // But auxiliary net is still "computed" (flag set)
568 EXPECT_FALSE(net.exist_aux_net()); // Both null means not "exist"
569}
570
571TEST_F(NetSupDemGraphTest, ZeroSupplyValue)
572{
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);
577}
578
579// =============================================================================
580// String Network Tests (Different Types)
581// =============================================================================
582
583class StringNetTest : public ::testing::Test
584{
585protected:
587};
588
589TEST_F(StringNetTest, InsertNodeWithStringInfo)
590{
591 auto* p = net.insert_node("Factory", 100.5);
592 EXPECT_EQ(p->get_info(), "Factory");
593 EXPECT_DOUBLE_EQ(p->supply_flow, 100.5);
594}
595
596TEST_F(StringNetTest, DoubleFlowValues)
597{
598 auto* s = net.insert_node("Source", 100.5);
599 auto* d = net.insert_node("Sink", -50.25);
600
601 s->out_cap = 200.0;
602 d->in_cap = 100.0;
603
604 EXPECT_DOUBLE_EQ(net.total_supply(), 100.5);
605 EXPECT_DOUBLE_EQ(net.total_demand(), 50.25);
606}
607
608// =============================================================================
609// Destructor Tests
610// =============================================================================
611
612TEST(NetSupDemDestructorTest, DestructorFreesAuxNet)
613{
614 auto* net = new SimpleNet();
615 auto* s = net->insert_node(100);
616 auto* d = net->insert_node(-100);
617
618 s->out_cap = 200;
619 d->in_cap = 200;
620
621 net->compute_aux_net();
622 EXPECT_TRUE(net->exist_aux_net());
623
624 // Should not crash - destructor should handle cleanup
625 delete net;
626 SUCCEED();
627}
628
629// =============================================================================
630// Integration Test
631// =============================================================================
632
633TEST(IntegrationTest, CompleteWorkflow)
634{
635 SimpleNet net;
636
637 // Create a balanced network
638 auto* factory1 = net.insert_node(100);
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);
643
644 // Set capacities
645 factory1->out_cap = 150;
646 factory2->out_cap = 100;
647 store1->in_cap = 100;
648 store2->in_cap = 100;
649
650 // Connect with arcs
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);
655
656 // Check balance
657 EXPECT_EQ(net.total_supply(), 150);
658 EXPECT_EQ(net.total_demand(), 150);
659 EXPECT_TRUE(net.is_balanced());
660
661 // Compute auxiliary network
662 net.compute_aux_net();
663 EXPECT_TRUE(net.exist_aux_net());
664
665 // The flow, and its feasibility
666 EXPECT_EQ(edmonds_karp_maximum_flow(net), 150);
667 EXPECT_TRUE(net.is_feasible());
668
669 // Get non-feasible nodes (should be empty)
670 DynDlist<SimpleNet::Node*> supply_list, demand_list;
671 net.non_feasible_nodes(supply_list, demand_list);
672 EXPECT_TRUE(supply_list.is_empty());
673 EXPECT_TRUE(demand_list.is_empty());
674
675 // Cleanup
676 net.free_aux_net();
677 EXPECT_FALSE(net.exist_aux_net());
678}
679
680// A ships 0.7 to B, which wants 0.1, and through C to D, which wants 0.6:
681// it receives 0.7 and sends 0.1 + 0.6, whose exact sum is 2.8e-17 more
682// than 0.7 in binary, well within the tolerance of check_node(). The
683// return of the residues of rounding at the end of the augmenting paths
684// moved it toward the sink, and the arc of D, or of B, to the super-sink
685// lost an ulp of its demand: the network was not feasible
686TEST(FloatingFeasibilityTest, AFlowOfTenthsIsFeasible)
687{
689 auto feasible = [] (auto maximum_flow)
690 {
691 Net net;
692 auto * a = net.insert_node(0.7);
693 auto * b = net.insert_node(-0.1);
694 auto * c = net.insert_node(0.0);
695 auto * d = net.insert_node(-0.6);
696 net.insert_arc(a, b, 3.2);
697 net.insert_arc(a, c, 3.9);
698 net.insert_arc(c, d, 0.7);
699 net.compute_aux_net();
700 maximum_flow(net);
701 return net.is_feasible();
702 };
703 EXPECT_TRUE(feasible([] (Net & net) { edmonds_karp_maximum_flow(net); }));
704 EXPECT_TRUE(feasible([] (Net & net) { ford_fulkerson_maximum_flow(net); }));
705}
706
707namespace
708{
709 using Floating_Net =
711
712 // The maximum flows of the library that run on a network of supplies
713 // and demands, with their names
714 struct Named_Maximum_Flow
715 {
716 const char * name;
717 void (*run)(Floating_Net &);
718 };
719
720 const Named_Maximum_Flow maximum_flows[] = {
721 {"ford_fulkerson", [] (Floating_Net & net) { ford_fulkerson_maximum_flow(net); }},
722 {"edmonds_karp", [] (Floating_Net & net) { edmonds_karp_maximum_flow(net); }},
723 {"dinic", [] (Floating_Net & net) { dinic_maximum_flow(net); }},
724 {"fifo_preflow", [] (Floating_Net & net) { fifo_preflow_maximum_flow(net); }},
725 {"heap_preflow", [] (Floating_Net & net) { heap_preflow_maximum_flow(net); }}};
726} // anonymous namespace
727
728// A supply of 2.4 meets the demands of 1.1 and 1.3, but 2.4 - 1.1 is
729// 1.2999999999999998 in binary: after the path of 1.1, the arc from the
730// super-source bounds the second path, whose flow comes out an ulp short
731// of the demand of 1.3, or of 1.1 if the paths go the other way. An exact
732// comparison took that demand as not met, with the five maximum flows
733TEST(FloatingFeasibilityTest, TheDemandsOfASupplyInTenthsAreMet)
734{
735 for (const auto & [name, maximum_flow] : maximum_flows)
736 {
737 Floating_Net net;
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();
744 maximum_flow(net);
745 EXPECT_TRUE(net.is_feasible()) << name;
746 DynDlist<Floating_Net::Node *> supply_list, demand_list;
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;
750 }
751}
752
753// The tolerance is that of each arc, which keeps a real deficit: a demand
754// of 1.3 that an arc of 1.2 bounds; and, beside a supply and a demand of
755// 1000, a supply and a demand of 1e-7 that no arc joins, which a tolerance
756// of the whole network, 1e-9 of 1000, would take as met
757TEST(FloatingFeasibilityTest, ARealDeficitIsNotMet)
758{
759 for (const auto & [name, maximum_flow] : maximum_flows)
760 {
761 Floating_Net net;
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();
768 maximum_flow(net);
769 EXPECT_FALSE(net.is_feasible()) << name;
770 DynDlist<Floating_Net::Node *> supply_list, demand_list;
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;
776 }
777
778 for (const auto & [name, maximum_flow] : maximum_flows)
779 {
780 Floating_Net net;
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();
787 maximum_flow(net);
788 EXPECT_FALSE(net.is_feasible()) << name;
789 DynDlist<Floating_Net::Node *> supply_list, demand_list;
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;
795 }
796}
797
798// On 300 random networks of 5 to 9 nodes in tenths, the supplies and the
799// demands are what an exact maximum flow in integer tenths ships from
800// and to the nodes that want to supply or demand up to 3.0: the
801// instances are feasible by construction, and each maximum flow finds
802// them so, where an exact comparison took some 490 of 1500 for not
803// feasible
804TEST(FloatingFeasibilityTest, FeasibleInstancesInTenthsAreFeasible)
805{
807 struct Spec_Arc
808 {
809 int u, v;
810 long cap; // in tenths
811 };
812 std::mt19937 gen(31337);
813 size_t instances = 0;
814 for (int k = 0; k < 300; ++k)
815 {
816 // a path through every node from 0 and to n - 1, and some arcs more
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)
821 {
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()});
826 }
827 for (int extra = int(4 + gen() % 11); extra > 0; --extra)
828 {
829 const int u = int(gen() % unsigned(n - 1));
830 const int v = 1 + int(gen() % unsigned(n - 1));
831 if (u != v)
832 arcs.append(Spec_Arc{u, v, cap()});
833 }
834
835 // what each node wants to supply (positive) or demand (negative)
836 Array<long> want(size_t(n), 0);
837 for (int i = 0; i < n; ++i)
838 {
839 const unsigned r = gen() % 4;
840 if (r == 0)
841 want[size_t(i)] = 1 + long(gen() % 30);
842 else if (r == 1)
843 want[size_t(i)] = -(1 + long(gen() % 30));
844 }
845 want[0] = 1 + long(gen() % 30);
846 want[size_t(n - 1)] = -(1 + long(gen() % 30));
847
848 // what an exact maximum flow ships from and to them
849 Integer_Net inet;
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);
857 Array<Integer_Net::Arc *> ends(size_t(n), nullptr);
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)]);
864 Array<long> supply(size_t(n), 0);
865 bool supplies = false, demands = false;
866 for (int i = 0; i < n; ++i)
867 if (ends[size_t(i)] != nullptr)
868 {
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;
872 }
873 if (not supplies or not demands)
874 continue; // nothing to ship
875 ++instances;
876
877 for (const auto & [name, maximum_flow] : maximum_flows)
878 {
879 Floating_Net net;
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)
884 net.insert_arc(nodes[size_t(arcs[j].u)], nodes[size_t(arcs[j].v)],
885 double(arcs[j].cap) / 10.0);
886 net.compute_aux_net();
887 maximum_flow(net);
888 EXPECT_TRUE(net.is_feasible()) << name << " on the instance " << k;
889 }
890 }
891 EXPECT_GT(instances, 250u);
892}
893
894// A supply of infinity and a demand of infinity, which nodes of infinite
895// capacities admit, are met only by an infinite flow. The tolerance of
896// an arc of infinite capacity, infinite too, took a maximum flow of 2.5
897// between them as meeting both, and an infinite flow, inf - inf being
898// NaN, as meeting neither
899TEST(FloatingFeasibilityTest, AnInfiniteSupplyIsMetOnlyByAnInfiniteFlow)
900{
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})
904 {
905 Floating_Net net;
906 auto * a = net.insert_node(inf);
907 auto * b = net.insert_node(-inf);
908 a->out_cap = inf;
909 b->in_cap = inf;
910 net.insert_arc(a, b, cap);
911 net.compute_aux_net();
912 maximum_flow(net);
913 const bool met = cap == inf;
914 EXPECT_EQ(net.is_feasible(), met) << name << " with a -> b of " << cap;
915 DynDlist<Floating_Net::Node *> supply_list, demand_list;
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)
920 {
921 EXPECT_EQ(supply_list.get_first(), a) << name;
922 EXPECT_EQ(demand_list.get_first(), b) << name;
923 }
924 }
925}
926
927// =============================================================================
928// The super nodes when the network changes
929// =============================================================================
930//
931// The network keeps the pointers of its super nodes: whatever changes it,
932// through the network or through a reference to Net_Graph, must leave it
933// remembering the super nodes that it has, and no other. The tests below
934// take every way of changing it, by what the way does to the super nodes:
935// - it takes all the nodes away through the base, by the assignments,
936// the swap() and the move of Net_Graph;
937// - it removes nodes: remove_node() of a super node or of another,
938// clear_graph() and a copy_graph() into the network;
939// - it takes the super nodes along: the swap(), the moves and the copies
940// of the network itself;
941// - it leaves them: an insertion, a change of the arcs, another order of
942// the nodes, and what is done to the network with itself;
943// each with every auxiliary network that a network may have: the two
944// super nodes, the super source alone and the super sink alone. Through a
945// reference to Array_Graph none of this holds: see Net_Graph.
946
947namespace
948{
949 using SimpleBase = SimpleNet::Net_Class;
950
951 // The auxiliary networks that a network may have: with the two super
952 // nodes, with the super source alone, when nothing demands, and with the
953 // super sink alone, when nothing supplies
954 enum class Aux { both, source, sink };
955 constexpr Aux every_aux[] = {Aux::both, Aux::source, Aux::sink};
956
957 const char * name_of(const Aux aux)
958 {
959 return aux == Aux::both ? "with the two super nodes"
960 : aux == Aux::source ? "with the super source alone" : "with the super sink alone";
961 }
962
963 // whether p is a node of net
964 bool is_node_of(const SimpleNet & net, const SimpleNode * p)
965 {
966 for (Node_Iterator<SimpleNet> it(net); it.has_curr(); it.next_ne())
967 if (it.get_curr() == p)
968 return true;
969 return false;
970 }
971
972 // Two nodes joined by an arc, with the auxiliary network asked for; it
973 // keeps the super nodes as compute_aux_net() made them
974 struct Shipment
975 {
976 SimpleNet net;
977 SimpleNode * s = nullptr;
978 SimpleNode * t = nullptr;
979 SimpleArc * st = nullptr;
980 SimpleNode * source = nullptr;
981 SimpleNode * sink = nullptr;
982
983 explicit Shipment(const Aux aux)
984 {
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();
991 }
992
993 // how many super nodes it was made with
994 size_t supers() const { return size_t(source != nullptr) + size_t(sink != nullptr); }
995 };
996
997 // puts in net three nodes and two arcs
998 void fill_plain(SimpleBase & net)
999 {
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);
1005 }
1006
1007 // net says that it has no auxiliary network, and has the nodes given
1008 void expect_no_aux_net(SimpleNet & net, const size_t nodes)
1009 {
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);
1017 }
1018
1019 // net remembers the super nodes given, which are nodes of it, besides
1020 // `own` nodes; and free_aux_net() takes them away, and only them
1021 void expect_aux_net(SimpleNet & net, const SimpleNode * source, const SimpleNode * sink,
1022 const size_t own)
1023 {
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);
1027 if (supers == 0)
1028 {
1029 expect_no_aux_net(net, own);
1030 return;
1031 }
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);
1037 if (supers == 1)
1038 {
1039 EXPECT_FALSE(net.is_feasible()); // without one of them, nothing is shipped
1040 }
1041 net.free_aux_net();
1042 EXPECT_FALSE(net.exist_aux_net());
1043 EXPECT_EQ(net.get_num_nodes(), own);
1044 }
1045}
1046
1047TEST(NetSupDemChangedNetwork, AnExchangeThroughTheBaseTakesTheSuperNodesAway)
1048{
1049 for (const Aux aux : every_aux)
1050 {
1051 SCOPED_TRACE(name_of(aux));
1052 {
1053 // the copy assignment of Net_Graph frees the nodes of the network,
1054 // its super nodes among them
1055 Shipment sh(aux);
1056 SimpleBase other;
1057 fill_plain(other);
1058 SimpleBase & base = sh.net;
1059 base = other;
1060 expect_no_aux_net(sh.net, 3);
1061
1062 // and the network that came takes an auxiliary network of its own
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);
1067 }
1068 {
1069 // the move assignment swaps: the other network has them then, as
1070 // nodes like the others
1071 Shipment sh(aux);
1072 SimpleBase other;
1073 fill_plain(other);
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());
1078 }
1079 {
1080 // so does swap()
1081 Shipment sh(aux);
1082 SimpleBase other;
1083 fill_plain(other);
1084 SimpleBase & base = sh.net;
1085 base.swap(other);
1086 expect_no_aux_net(sh.net, 3);
1087 EXPECT_EQ(other.get_num_nodes(), 2 + sh.supers());
1088 }
1089 {
1090 // and the move construction of a Net_Graph, which empties it
1091 Shipment sh(aux);
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);
1096 }
1097 for (const Aux aux2 : every_aux)
1098 {
1099 // between two networks with their auxiliary networks, each one
1100 // has then the super nodes of the other, and remembers none
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;
1106 base.swap(base2);
1107 expect_no_aux_net(sh.net, 3 + sh2.supers());
1108 expect_no_aux_net(sh2.net, 2 + sh.supers());
1109 }
1110 }
1111}
1112
1113TEST(NetSupDemChangedNetwork, ARemovedSuperNodeIsForgotten)
1114{
1115 for (const Aux aux : every_aux)
1116 for (const bool the_source : {true, false})
1117 for (const bool through_the_base : {false, true})
1118 {
1119 Shipment sh(aux);
1120 SimpleNode * const gone = the_source ? sh.source : sh.sink;
1121 if (gone == nullptr)
1122 continue; // it has no such super node
1123 SCOPED_TRACE(std::string(name_of(aux)) + (the_source ? ", its super source" : ", its super sink")
1124 + (through_the_base ? ", through the base" : ""));
1125 // remove_node() is virtual: through a reference to the base too
1126 if (through_the_base)
1127 static_cast<SimpleBase &>(sh.net).remove_node(gone);
1128 else
1129 sh.net.remove_node(gone);
1130 expect_aux_net(sh.net, the_source ? nullptr : sh.source, the_source ? sh.sink : nullptr, 2);
1131 }
1132}
1133
1134TEST(NetSupDemChangedNetwork, AnotherNodeRemovedIsNoneOfThem)
1135{
1136 for (const Aux aux : every_aux)
1137 {
1138 SCOPED_TRACE(name_of(aux));
1139 {
1140 // a node without arcs
1141 Shipment sh(aux);
1142 sh.net.remove_node(sh.net.insert_node(0));
1143 expect_aux_net(sh.net, sh.source, sh.sink, 2);
1144 }
1145 {
1146 // a node with an arc of a super node, which goes with it
1147 Shipment sh(aux);
1148 sh.net.remove_node(aux == Aux::sink ? sh.t : sh.s);
1149 expect_aux_net(sh.net, sh.source, sh.sink, 1);
1150 }
1151 }
1152}
1153
1154TEST(NetSupDemChangedNetwork, ClearGraphAndCopyGraphTakeTheSuperNodesAway)
1155{
1156 for (const Aux aux : every_aux)
1157 {
1158 SCOPED_TRACE(name_of(aux));
1159 {
1160 Shipment sh(aux);
1161 clear_graph(sh.net);
1162 expect_no_aux_net(sh.net, 0);
1163 }
1164 for (const Aux aux2 : every_aux)
1165 {
1166 // copy_graph() empties the network first; the super nodes of the
1167 // network that it copies come as nodes like the others
1168 SCOPED_TRACE(name_of(aux2));
1169 Shipment sh(aux), from(aux2);
1170 copy_graph(sh.net, from.net);
1171 expect_no_aux_net(sh.net, 2 + from.supers());
1172 expect_aux_net(from.net, from.source, from.sink, 2);
1173 }
1174 }
1175}
1176
1177TEST(NetSupDemChangedNetwork, AChangeThatLeavesTheSuperNodesKeepsThem)
1178{
1179 // each way returns how many nodes the network has then, besides its
1180 // super nodes
1181 struct Way
1182 {
1183 const char * name;
1184 size_t (*change)(Shipment &);
1185 };
1186 const Way ways[] = {
1187 {"nothing", [] (Shipment &) -> size_t { return 2; }},
1188 {"a node inserted", [] (Shipment & sh) -> size_t
1189 {
1190 sh.net.insert_node(0);
1191 return 3;
1192 }},
1193 {"an arc inserted", [] (Shipment & sh) -> size_t
1194 {
1195 sh.net.insert_arc(sh.t, sh.s, 1);
1196 return 2;
1197 }},
1198 {"an arc removed", [] (Shipment & sh) -> size_t
1199 {
1200 sh.net.remove_arc(sh.st);
1201 return 2;
1202 }},
1203 {"the arcs of the super nodes removed", [] (Shipment & sh) -> size_t
1204 {
1206 for (SimpleNode * p : {sh.source, sh.sink})
1207 if (p != nullptr)
1208 for (SimpleBase::Node_Arc_Iterator it(p); it.has_curr(); it.next_ne())
1209 arcs.append(it.get_curr());
1210 for (auto it = arcs.get_it(); it.has_curr(); it.next_ne())
1211 sh.net.remove_arc(it.get_curr());
1212 return 2;
1213 }},
1214 {"an arc disconnected and connected again", [] (Shipment & sh) -> size_t
1215 {
1216 sh.net.disconnect_arc(sh.st);
1217 sh.net.connect_arc(sh.st);
1218 return 2;
1219 }},
1220 {"another order of the nodes", [] (Shipment & sh) -> size_t
1221 {
1222 sh.net.sort_nodes([] (SimpleNode * p, SimpleNode * q) { return p->supply_flow < q->supply_flow; });
1223 return 2;
1224 }},
1225 {"swapped with itself through the base", [] (Shipment & sh) -> size_t
1226 {
1227 SimpleBase & base = sh.net;
1228 base.swap(base);
1229 return 2;
1230 }},
1231 {"assigned to itself through the base", [] (Shipment & sh) -> size_t
1232 {
1233 SimpleBase & base = sh.net;
1234 const SimpleBase & same = sh.net;
1235 base = same;
1236 return 2;
1237 }},
1238 {"moved to itself through the base", [] (Shipment & sh) -> size_t
1239 {
1240 SimpleBase & base = sh.net;
1241 SimpleBase & same = sh.net;
1242 base = std::move(same);
1243 return 2;
1244 }},
1245 {"swapped with itself", [] (Shipment & sh) -> size_t
1246 {
1247 sh.net.swap(sh.net);
1248 return 2;
1249 }},
1250 {"assigned to itself", [] (Shipment & sh) -> size_t
1251 {
1252 const SimpleNet & same = sh.net;
1253 sh.net = same;
1254 return 2;
1255 }},
1256 {"moved to itself", [] (Shipment & sh) -> size_t
1257 {
1258 SimpleNet & same = sh.net;
1259 sh.net = std::move(same);
1260 return 2;
1261 }}
1262 };
1263 for (const Aux aux : every_aux)
1264 for (const Way & way : ways)
1265 {
1266 SCOPED_TRACE(std::string(name_of(aux)) + ", " + way.name);
1267 Shipment sh(aux);
1268 const size_t own = way.change(sh);
1269 expect_aux_net(sh.net, sh.source, sh.sink, own);
1270 }
1271}
1272
1273TEST(NetSupDemChangedNetwork, ItsOwnSwapMovesAndCopiesTakeTheSuperNodesAlong)
1274{
1275 for (const Aux aux : every_aux)
1276 {
1277 SCOPED_TRACE(name_of(aux));
1278 {
1279 Shipment sh(aux);
1280 SimpleNet moved(std::move(sh.net));
1281 expect_no_aux_net(sh.net, 0);
1282 expect_aux_net(moved, sh.source, sh.sink, 2);
1283
1284 // the network that was moved from takes an auxiliary network again
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);
1289 }
1290 {
1291 // a copy has super nodes of its own, at the places of those of the
1292 // network, which keeps its own
1293 Shipment sh(aux);
1294 SimpleNet copy(sh.net);
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)
1298 {
1299 EXPECT_NE(copy.get_super_source(), sh.source);
1300 }
1301 if (sh.sink != nullptr)
1302 {
1303 EXPECT_NE(copy.get_super_sink(), sh.sink);
1304 }
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);
1307 }
1308 for (const Aux aux2 : every_aux)
1309 {
1310 SCOPED_TRACE(name_of(aux2));
1311 {
1312 // the move assignment exchanges them, as swap()
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);
1318 }
1319 {
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);
1325 }
1326 {
1327 // the copy assignment frees those that the target had
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);
1335 }
1336 }
1337 }
1338}
1339
1340namespace
1341{
1342 // is_feasible() says `feasible`, and non_feasible_nodes() gives the
1343 // nodes given, if any: two empty lists if and only if it is feasible
1344 void expect_unmet(SimpleNet & net, const bool feasible, const SimpleNode * supply,
1345 const SimpleNode * demand)
1346 {
1347 EXPECT_EQ(net.is_feasible(), feasible);
1348 DynDlist<SimpleNode *> supplies, demands;
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)
1353 {
1354 EXPECT_EQ(supplies.get_first(), supply);
1355 }
1356 if (demand != nullptr)
1357 {
1358 EXPECT_EQ(demands.get_first(), demand);
1359 }
1360 EXPECT_EQ(supplies.is_empty() and demands.is_empty(), feasible);
1361 }
1362}
1363
1364// Without one of the super nodes nothing is shipped, whatever the flow:
1365// when compute_aux_net() made one alone, and when the other was removed.
1366// A flow fills then the arcs of the super node that there is, towards a
1367// sink of the network or from a source of it
1368TEST(NetSupDemChangedNetwork, WithoutASuperNodeNoSupplyNorDemandIsMet)
1369{
1370 for (const Aux aux : every_aux)
1371 for (const bool with_flow : {false, true})
1372 for (const int removed : {0, 1, 2}) // nothing, the super source, the super sink
1373 {
1374 Shipment sh(aux);
1375 if ((removed == 1 and sh.source == nullptr) or (removed == 2 and sh.sink == nullptr))
1376 continue; // it has no such super node
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" : ""));
1379 if (with_flow)
1380 (void) edmonds_karp_maximum_flow(sh.net);
1381 if (removed == 1)
1382 sh.net.remove_node(sh.source);
1383 else if (removed == 2)
1384 sh.net.remove_node(sh.sink);
1385
1386 if (not sh.net.exist_aux_net())
1387 {
1388 // its only super node went
1389 DynDlist<SimpleNode *> supplies, demands;
1390 EXPECT_THROW(sh.net.non_feasible_nodes(supplies, demands), std::domain_error);
1391 continue;
1392 }
1393 if (aux == Aux::both and removed == 0 and with_flow)
1394 expect_unmet(sh.net, true, nullptr, nullptr); // all of it is shipped
1395 else
1396 expect_unmet(sh.net, false, sh.s->supply_flow > 0 ? sh.s : nullptr,
1397 sh.t->supply_flow < 0 ? sh.t : nullptr);
1398 }
1399}
1400
1401int main(int argc, char** argv)
1402{
1403 ::testing::InitGoogleTest(&argc, argv);
1404 return RUN_ALL_TESTS();
1405}
WeightedDigraph::Node Node
int main()
Simple dynamic array with automatic resizing and functional operations.
Definition tpl_array.H:141
T & append(const T &data)
Append a copy of data
Definition tpl_array.H:331
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.
Definition tpl_graph.H:1287
void SetUp() override
#define TEST(name)
DynArray< Graph::Node * > nodes
Definition graphpic.C:406
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
void clear_graph(GT &g)
Clean a graph: all its nodes and arcs are removed and freed.
Definition tpl_graph.H:4776
void copy_graph(GT &gtgt, const GT &gsrc, bool cookie_map=false)
Explicit copy of graph.
Definition tpl_graph.H:4794
static mpfr_t y
Definition mpfr_mul_d.c:3
Main namespace for Aleph-w library functions.
Net::Flow_Type edmonds_karp_maximum_flow(Net &net)
Maximize flow using the Edmonds-Karp algorithm.
Definition tpl_net.H:2336
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.
Definition tpl_net.H:2706
Itor2 copy(Itor1 sourceBeg, const Itor1 &sourceEnd, Itor2 destBeg)
Copy elements from one range to another.
Definition ahAlgo.H:584
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.
Definition tpl_net.H:2668
Net::Flow_Type ford_fulkerson_maximum_flow(Net &net)
Maximize flow using the Ford-Fulkerson algorithm.
Definition tpl_net.H:2306
TEST_F(NetSupDemNodeTest, DefaultConstructor)
Net_Sup_Dem_Graph< SimpleNode, SimpleArc > SimpleNet
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:160
Flow network implemented with adjacency lists.
Definition tpl_net.H:311
Node * insert_node(const Node_Type &node_info)
Insert a new node by copying node_info.
Definition tpl_net.H:750
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.
Definition tpl_net.H:799
static int * k
Gsl_Compatible_Rng r
Dynamic doubly linked list implementation.
Advanced maximum flow algorithms.
Supply-demand network flow algorithms.