Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
random_net.H
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
44# ifndef RANDOM_NET_H
45# define RANDOM_NET_H
46
47# include <gsl/gsl_rng.h>
48# include <gsl/gsl_randist.h>
49
50# include <memory>
51# include <stdexcept>
52# include <algorithm>
53# include <tpl_dynArray.H>
54# include <tpl_dynSetTree.H>
55# include <tpl_netcost.H>
56# include <tpl_indexArc.H>
57# include <ah-errors.H>
58
59
60template <class Net>
62{
63 typedef typename Net::Node Node;
64 typedef typename Net::Arc Arc;
65
68 std::unique_ptr<Net> net;
69
70public:
71 Random_Network_Flow(const unsigned int seed = time(nullptr))
73 {
74 gsl_rng_set(r, seed % gsl_rng_max(r));
75 }
76
81
82private:
84 size_t tgt_idx_rank,
85 const double & cap_mean,
86 const double & cap_sigma,
87 const double & density)
88 {
89 IndexArc<Net> arcs(*this->net.get());
92 const size_t & num_nodes_src_rank = src_rank.size();
93 const size_t & num_nodes_tgt_rank = tgt_rank.size();
94 for (int i = 0; i < num_nodes_src_rank; ++i)
95 {
96 auto src = src_rank.access(i);
97 const double lambda = density * num_nodes_tgt_rank;
98 size_t num_arcs = gsl_ran_exponential(r, lambda);
99 num_arcs = std::min(num_arcs, num_nodes_tgt_rank);
100 for (int k = 0; k < num_arcs; ++k)
101 {
102 repeat:
104 auto tgt = tgt_rank.access(tgt_idx);
105 if (arcs.search(src, tgt) != nullptr)
106 goto repeat;
107
108 const double cap = gsl_ran_gaussian(r, cap_sigma) + cap_mean;
109 this->net->insert_arc(src, tgt, cap);
110 }
111 }
112 }
113
115 const double & cap_mean,
116 const double & cap_sigma,
117 const double & forward_density)
118 {
119 assert(curr_rank_idx < rank.size() - 1);
122 }
123
125 const double & cap_mean,
126 const double & cap_sigma,
127 const double & backward_density)
128 {
132 }
133
134 // create num_ranks each one with num_nodes_by_rank according to the
135 // mean of a normal with rank_sigma as deviation
136 void create_ranks(const size_t num_ranks,
137 const size_t num_nodes_by_rank,
138 const double & rank_sigma)
139 {
141 << "Rank sigma " + std::to_string(rank_sigma) + " less than 0";
143 << "Rank sigma " + std::to_string(rank_sigma) + " greater than 1";
144 for (int i = 0; i < num_ranks; ++i)
145 {
147 const size_t num_nodes = num_nodes_by_rank + mu;
148 rank.touch(i).reserve(num_nodes);
149 DynArray<Node *> & curr_rank = rank.access(i);
150 for (int k = 0; k < num_nodes; ++k)
151 curr_rank.access(k) = this->net->insert_node();
152 }
153 }
154
160 void create_arcs(const double & cap_mean,
161 const double & cap_sigma,
162 const double & forward_density,
163 const double & backward_density)
164 {
166 << "forward density less than backward density";
168 << "forward density out of range";
170 << "backward density out of range";
171
172 const size_t & N = rank.size();
173
174 // Need at least 2 ranks to create inter-rank arcs
175 if (N < 2)
176 return;
177
179 for (int i = 1; i < N - 1; ++i)
180 {
183 }
185 }
186
187public:
213 Net * operator ()(const size_t num_ranks,
214 const size_t num_nodes_by_rank,
215 const double & rank_sigma = 0.2,
216 const double & cap_mean = 100,
217 const double & cap_sigma = 0.9,
218 const double & forward_density = 0.3,
219 const double & backward_density = 0.01)
220 {
221 // Create fresh network for this generation
222 this->net.reset(new Net);
223 rank.empty();
224
227
228 return this->net.release();
229 }
230};
231
232
233# endif // RANDOM_NET_H
Exception handling system with formatted messages for Aleph-w.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
Definition ah-errors.H:522
int num_nodes
Definition btreepic.C:410
size_t size() const noexcept
Count the number of elements of the list.
Definition htlist.H:1319
Index for fast arc lookup by its endpoint nodes.
void create_ranks(const size_t num_ranks, const size_t num_nodes_by_rank, const double &rank_sigma)
Definition random_net.H:136
void connect_ranks(size_t src_idx_rank, size_t tgt_idx_rank, const double &cap_mean, const double &cap_sigma, const double &density)
Definition random_net.H:83
void create_forward_arcs_in_rank(size_t curr_rank_idx, const double &cap_mean, const double &cap_sigma, const double &forward_density)
Definition random_net.H:114
void create_backward_arcs_in_rank(const size_t curr_rank_idx, const double &cap_mean, const double &cap_sigma, const double &backward_density)
Definition random_net.H:124
Random_Network_Flow(const unsigned int seed=time(nullptr))
Definition random_net.H:71
std::unique_ptr< Net > net
Definition random_net.H:68
void create_arcs(const double &cap_mean, const double &cap_sigma, const double &forward_density, const double &backward_density)
After created ranks and their nodes, this routine creates the arcs.
Definition random_net.H:160
DynArray< DynArray< Node * > > rank
Definition random_net.H:66
Net * operator()(const size_t num_ranks, const size_t num_nodes_by_rank, const double &rank_sigma=0.2, const double &cap_mean=100, const double &cap_sigma=0.9, const double &forward_density=0.3, const double &backward_density=0.01)
The number of nodes per rank is determined by a distribution normal with half num_nodes_by_rank and s...
Definition random_net.H:213
#define N
Definition fib.C:294
DynArray< Graph::Arc * > arcs
Definition graphpic.C:408
DynList< T > maps(const C &c, Op op)
Classic map operation.
Flow network implemented with adjacency lists.
Definition tpl_net.H:261
ArcT Arc
Arc type.
Definition tpl_net.H:272
NodeT Node
Node type.
Definition tpl_net.H:275
void reset()
Reset all arc flows to zero.
Definition tpl_net.H:794
Lazy and scalable dynamic array implementation.
Dynamic set implementations based on balanced binary search trees.
Arc indexing for fast lookup by endpoint nodes.
Maximum flow minimum cost network algorithms.