Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
tpl_net_sup_dem_example.cc
Go to the documentation of this file.
1
33#include <iostream>
34#include <tpl_net_sup_dem.H>
35#include <tpl_net.H>
36
37using namespace std;
38using namespace Aleph;
39
40int main()
41{
42 cout << "=== Supply-Demand Networks: Educational Examples ===\n\n";
43
44 // =========================================================================
45 // EXAMPLE 1: Basic Distribution Network
46 // =========================================================================
47 {
48 cout << "--- Example 1: Simple Distribution Problem ---\n\n";
49
50 // STEP 1: Define network type
54
55 cout << "SCENARIO: Warehouse distribution to stores\n";
56 cout << "=========================================\n\n";
57
58 // STEP 2: Create supply nodes (warehouses)
59 cout << "SUPPLY NODES (Warehouses):\n";
60 auto warehouse1 = network.insert_node("Warehouse1", 100); // supplies 100 units
61 auto warehouse2 = network.insert_node("Warehouse2", 150); // supplies 150 units
62 cout << " Warehouse1: supplies 100 units\n";
63 cout << " Warehouse2: supplies 150 units\n";
64 cout << " Total supply: 250 units\n\n";
65
66 // STEP 3: Create demand nodes (stores)
67 cout << "DEMAND NODES (Stores):\n";
68 auto store1 = network.insert_node("Store1", -80); // demands 80 units
69 auto store2 = network.insert_node("Store2", -100); // demands 100 units
70 auto store3 = network.insert_node("Store3", -70); // demands 70 units
71 cout << " Store1: demands 80 units\n";
72 cout << " Store2: demands 100 units\n";
73 cout << " Store3: demands 70 units\n";
74 cout << " Total demand: 250 units\n\n";
75
76 // STEP 4: Create transit nodes (distribution centers)
77 cout << "TRANSIT NODES (Distribution Centers):\n";
78 auto hub = network.insert_node("Hub", 0); // zero supply (just passes flow)
79 cout << " Hub: transit point (no supply/demand)\n\n";
80
81 // STEP 5: Connect with capacity constraints
82 cout << "NETWORK CONNECTIONS (with capacities):\n";
83 network.insert_arc(warehouse1, hub, 100); // capacity 100
84 network.insert_arc(warehouse2, hub, 150); // capacity 150
85 network.insert_arc(hub, store1, 80); // capacity 80
86 network.insert_arc(hub, store2, 100); // capacity 100
87 network.insert_arc(hub, store3, 70); // capacity 70
88
89 cout << " Warehouse1 -> Hub: capacity 100\n";
90 cout << " Warehouse2 -> Hub: capacity 150\n";
91 cout << " Hub -> Store1: capacity 80\n";
92 cout << " Hub -> Store2: capacity 100\n";
93 cout << " Hub -> Store3: capacity 70\n\n";
94
95 // STEP 6: Check balance
96 cout << "FEASIBILITY CHECK:\n";
97 cout << " Total supply: 250 units\n";
98 cout << " Total demand: 250 units\n";
99 cout << " Network is BALANCED ✓\n\n";
100
101 cout << "KEY INSIGHT: Balanced network (supply = demand) has feasible solution\n";
102 cout << " Flow can satisfy all demands from available supply\n\n";
103 }
104
105 // =========================================================================
106 // EXAMPLE 2: Multi-Source Multi-Sink
107 // =========================================================================
108 {
109 cout << "--- Example 2: Complex Distribution Network ---\n\n";
110
114
115 cout << "SCENARIO: Multiple factories → Multiple customers\n";
116 cout << "================================================\n\n";
117
118 // Multiple suppliers
119 auto factory_a = network.insert_node("Factory_A", 200);
120 auto factory_b = network.insert_node("Factory_B", 180);
121 auto factory_c = network.insert_node("Factory_C", 220);
122
123 cout << "Factories (suppliers):\n";
124 cout << " Factory A: 200 units\n";
125 cout << " Factory B: 180 units\n";
126 cout << " Factory C: 220 units\n";
127 cout << " Total: 600 units\n\n";
128
129 // Multiple consumers
130 auto customer1 = network.insert_node("Customer1", -150);
131 auto customer2 = network.insert_node("Customer2", -200);
132 auto customer3 = network.insert_node("Customer3", -120);
133 auto customer4 = network.insert_node("Customer4", -130);
134
135 cout << "Customers (demanders):\n";
136 cout << " Customer 1: 150 units\n";
137 cout << " Customer 2: 200 units\n";
138 cout << " Customer 3: 120 units\n";
139 cout << " Customer 4: 130 units\n";
140 cout << " Total: 600 units\n\n";
141
142 // Distribution hubs
143 auto hub_north = network.insert_node("Hub_North", 0);
144 auto hub_south = network.insert_node("Hub_South", 0);
145
146 cout << "Distribution hubs (transit):\n";
147 cout << " Hub North: 0 (transit only)\n";
148 cout << " Hub South: 0 (transit only)\n\n";
149
150 // Create connections
151 cout << "Network topology:\n";
152 cout << " Factories → Hubs → Customers\n";
153 cout << " Multiple paths for redundancy\n\n";
154
155 // Factory to hubs
156 network.insert_arc(factory_a, hub_north, 150);
157 network.insert_arc(factory_a, hub_south, 100);
158 network.insert_arc(factory_b, hub_north, 100);
159 network.insert_arc(factory_b, hub_south, 120);
160 network.insert_arc(factory_c, hub_north, 120);
161 network.insert_arc(factory_c, hub_south, 150);
162
163 // Hubs to customers
164 network.insert_arc(hub_north, customer1, 150);
165 network.insert_arc(hub_north, customer2, 120);
166 network.insert_arc(hub_south, customer2, 100);
167 network.insert_arc(hub_south, customer3, 120);
168 network.insert_arc(hub_south, customer4, 130);
169
170 cout << "ADVANTAGES OF MULTI-HUB DESIGN:\n";
171 cout << " ✓ Redundancy (multiple paths)\n";
172 cout << " ✓ Load balancing across hubs\n";
173 cout << " ✓ Geographic optimization\n";
174 cout << " ✓ Fault tolerance\n\n";
175 }
176
177 // =========================================================================
178 // EXAMPLE 3: Unbalanced Network (Excess Supply)
179 // =========================================================================
180 {
181 cout << "--- Example 3: Unbalanced Network ---\n\n";
182
186
187 cout << "SCENARIO: Supply > Demand\n";
188 cout << "=========================\n\n";
189
190 // More supply than demand
191 auto supplier = network.insert_node("Supplier", 500); // supply 500
192 auto customer = network.insert_node("Customer", -300); // demand 300
193
194 cout << "Supply: 500 units\n";
195 cout << "Demand: 300 units\n";
196 cout << "Excess: 200 units\n\n";
197
198 network.insert_arc(supplier, customer, 500);
199
200 cout << "HANDLING EXCESS SUPPLY:\n";
201 cout << " Option 1: Add dummy demand node (sink excess)\n";
202 cout << " Option 2: Model as upper bound on supply\n";
203 cout << " Option 3: Storage/inventory node\n\n";
204
205 // Add dummy sink for excess
206 auto storage = network.insert_node("Storage", -200); // absorb excess
207 network.insert_arc(supplier, storage, 200);
208
209 cout << "Solution: Added storage node\n";
210 cout << " Supplier -> Customer: 300 units\n";
211 cout << " Supplier -> Storage: 200 units\n";
212 cout << " Network now balanced ✓\n\n";
213 }
214
215 // =========================================================================
216 // EXAMPLE 4: Capacity Constraints Matter
217 // =========================================================================
218 {
219 cout << "--- Example 4: Capacity Bottlenecks ---\n\n";
220
224
225 cout << "SCENARIO: Balanced but infeasible due to capacity\n";
226 cout << "================================================\n\n";
227
228 auto source = network.insert_node("Source", 100);
229 auto sink = network.insert_node("Sink", -100);
230 auto bottleneck = network.insert_node("Bottleneck", 0);
231
232 cout << "Network: Source -> Bottleneck -> Sink\n";
233 cout << " Source supply: 100\n";
234 cout << " Sink demand: 100\n";
235 cout << " Balanced! ✓\n\n";
236
237 // BUT: capacity is too small!
238 network.insert_arc(source, bottleneck, 100);
239 network.insert_arc(bottleneck, sink, 50); // BOTTLENECK!
240
241 cout << "Arc capacities:\n";
242 cout << " Source -> Bottleneck: 100 (OK)\n";
243 cout << " Bottleneck -> Sink: 50 (TOO SMALL!)\n\n";
244
245 cout << "PROBLEM: Capacity bottleneck!\n";
246 cout << " Can only send 50 units through bottleneck\n";
247 cout << " Cannot satisfy demand of 100\n";
248 cout << " Network is INFEASIBLE ✗\n\n";
249
250 cout << "LESSON: Balanced supply/demand ≠ feasible solution\n";
251 cout << " Must also check capacity constraints!\n\n";
252 }
253
254 // =========================================================================
255 // EXAMPLE 5: Real-World Power Grid
256 // =========================================================================
257 {
258 cout << "--- Example 5: Power Grid Distribution ---\n\n";
259
260 cout << "REAL-WORLD APPLICATION: Electric Power Grid\n";
261 cout << "==========================================\n\n";
262
263 cout << "NODES:\n";
264 cout << " * Power plants (supply nodes)\n";
265 cout << " - Coal plant: 500 MW\n";
266 cout << " - Solar farm: 200 MW\n";
267 cout << " - Wind farm: 150 MW\n";
268 cout << "\n";
269 cout << " * Cities (demand nodes)\n";
270 cout << " - City A: -300 MW\n";
271 cout << " - City B: -250 MW\n";
272 cout << " - City C: -200 MW\n";
273 cout << "\n";
274 cout << " * Substations (transit nodes)\n";
275 cout << " - Balance load\n";
276 cout << " - Transform voltage\n";
277 cout << " - No generation or consumption\n\n";
278
279 cout << "ARCS (Transmission Lines):\n";
280 cout << " * Capacity = power line rating (MW)\n";
281 cout << " * Cost = transmission loss/distance\n";
282 cout << " * Redundancy for reliability\n\n";
283
284 cout << "OPTIMIZATION GOALS:\n";
285 cout << " 1. Meet all demand (feasibility)\n";
286 cout << " 2. Minimize transmission losses (cost)\n";
287 cout << " 3. Respect line capacities (constraints)\n";
288 cout << " 4. Load balancing across plants\n\n";
289
290 cout << "ALGORITHMS:\n";
291 cout << " * Check feasibility: Max-flow algorithm\n";
292 cout << " * Minimize cost: Min-cost flow\n";
293 cout << " * Handle failures: Network simplex\n\n";
294 }
295
296 cout << "=== SUMMARY: Supply-Demand Networks ===\n";
297 cout << "\n1. NODE TYPES:\n";
298 cout << " Supply (source): positive value (produces)\n";
299 cout << " Demand (sink): negative value (consumes)\n";
300 cout << " Transit: zero value (passes flow)\n";
301 cout << "\n2. FEASIBILITY CONDITIONS:\n";
302 cout << " ✓ Total supply = Total demand (balance)\n";
303 cout << " ✓ Capacities allow required flows\n";
304 cout << " ✓ Network is connected\n";
305 cout << "\n3. COMMON PATTERNS:\n";
306 cout << " * Hub-and-spoke: Central distribution\n";
307 cout << " * Multi-tier: Factory→Hub→Store\n";
308 cout << " * Redundant paths: Fault tolerance\n";
309 cout << "\n4. REAL-WORLD APPLICATIONS:\n";
310 cout << " * Supply chain logistics\n";
311 cout << " * Power grid distribution\n";
312 cout << " * Water/gas networks\n";
313 cout << " * Transportation planning\n";
314 cout << " * Telecommunications\n";
315 cout << "\n5. KEY ALGORITHMS:\n";
316 cout << " Feasibility: Max-flow (source to sink)\n";
317 cout << " Optimal: Min-cost flow\n";
318 cout << " Complexity: Polynomial time\n";
319 cout << "\n6. DESIGN PRINCIPLES:\n";
320 cout << " * Balance supply and demand\n";
321 cout << " * Size capacities appropriately\n";
322 cout << " * Add redundancy for reliability\n";
323 cout << " * Use transit nodes for routing flexibility\n";
324
325 return 0;
326}
Network graph with supply and demand nodes.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
DynList< T > maps(const C &c, Op op)
Classic map operation.
STL namespace.
Arc of a flow network implemented with adjacency lists.
Definition tpl_net.H:115
Network flow graph structures.
Supply-demand network flow algorithms.