@@ -508,7 +508,7 @@ namespace dsm {
508508 } else {
509509 this ->removeAgent (agentId);
510510 }
511- return ;
511+ continue ;
512512 }
513513 auto const & nextStreet{m_graph.streetSet ()[m_agentNextStreetId[agentId]]};
514514 if (nextStreet->isFull ()) {
@@ -1073,43 +1073,64 @@ namespace dsm {
10731073 void Dynamics<Delay>::addAgentsRandomly(Size nAgents,
10741074 const TContainer& src_weights,
10751075 const TContainer& dst_weights) {
1076- // Check if the weights are normalized
1077- {
1078- auto const sum{std::accumulate (
1079- src_weights.begin (),
1080- src_weights.end (),
1081- 0 .,
1082- [](double sum, const std::pair<Id, double >& p) { return sum + p.second ; })};
1083- assert ((void (" The source weights are not normalized" ),
1084- std::abs (sum - 1 .) < std::numeric_limits<double >::epsilon ()));
1085- }
1086- {
1087- auto const sum{std::accumulate (
1088- dst_weights.begin (),
1089- dst_weights.end (),
1090- 0 .,
1091- [](double sum, const std::pair<Id, double >& p) { return sum + p.second ; })};
1092- assert ((void (" The destination weights are not normalized" ),
1093- std::abs (sum - 1 .) < std::numeric_limits<double >::epsilon ()));
1094- }
1076+ if (src_weights.size () == 1 && dst_weights.size () == 1 &&
1077+ src_weights.begin ()->first == dst_weights.begin ()->first ) {
1078+ throw std::invalid_argument (buildLog (
1079+ std::format (" The only source node {} is also the only destination node." ,
1080+ src_weights.begin ()->first )));
1081+ }
1082+ auto const srcSum{std::accumulate (
1083+ src_weights.begin (),
1084+ src_weights.end (),
1085+ 0 .,
1086+ [](double sum, const std::pair<Id, double >& p) {
1087+ if (p.second < 0 .) {
1088+ throw std::invalid_argument (buildLog (std::format (
1089+ " Negative weight ({}) for source node {}." , p.second , p.first )));
1090+ }
1091+ return sum + p.second ;
1092+ })};
1093+ auto const dstSum{std::accumulate (
1094+ dst_weights.begin (),
1095+ dst_weights.end (),
1096+ 0 .,
1097+ [](double sum, const std::pair<Id, double >& p) {
1098+ if (p.second < 0 .) {
1099+ throw std::invalid_argument (buildLog (std::format (
1100+ " Negative weight ({}) for destination node {}." , p.second , p.first )));
1101+ }
1102+ return sum + p.second ;
1103+ })};
1104+ std::uniform_real_distribution<double > srcUniformDist{0 ., srcSum};
1105+ std::uniform_real_distribution<double > dstUniformDist{0 ., dstSum};
10951106 while (nAgents > 0 ) {
10961107 Id srcId{0 }, dstId{0 };
1097- double dRand{this ->m_uniformDist (this ->m_generator )}, sum{0 .};
1098- for (const auto & [id, weight] : src_weights) {
1099- sum += weight;
1100- if (dRand < sum) {
1108+ if (dst_weights.size () == 1 ) {
1109+ dstId = dst_weights.begin ()->first ;
1110+ srcId = dstId;
1111+ }
1112+ double dRand, sum;
1113+ while (srcId == dstId) {
1114+ dRand = srcUniformDist (m_generator);
1115+ sum = 0 .;
1116+ for (const auto & [id, weight] : src_weights) {
11011117 srcId = id;
1102- break ;
1118+ sum += weight;
1119+ if (dRand < sum) {
1120+ break ;
1121+ }
11031122 }
11041123 }
1105- dstId = srcId;
1124+ if (src_weights.size () > 1 ) {
1125+ dstId = srcId;
1126+ }
11061127 while (dstId == srcId) {
1107- dRand = this -> m_uniformDist ( this -> m_generator );
1128+ dRand = dstUniformDist ( m_generator);
11081129 sum = 0 .;
11091130 for (const auto & [id, weight] : dst_weights) {
1131+ dstId = id;
11101132 sum += weight;
11111133 if (dRand < sum) {
1112- dstId = id;
11131134 break ;
11141135 }
11151136 }
0 commit comments