Advertisement
Deerenaros

aloha

May 14th, 2015
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.71 KB | None | 0 0
  1. tuple<float, float> aloha (
  2.     int M = 2, // количество абонентов
  3.     int N = 4, // размер потока (количество событий)
  4.     int W = 20, // количество окон для моделирования
  5.     float h = 0.6f, // плотность потока (общая)
  6.     float p = 0.5f, // вероятность, с которой абонент
  7.     // отправляет при наличии сообщения
  8.     type t = probabilistic, // вариант разрешения конфликта
  9.     bool debug = false ) // выводить само моделирование (номер окна и содержания буферов)
  10. {
  11.     auto subscribers = subscriber::several ( M, N, h, bernoulli );
  12.     auto windows = range ( 1, W+1 );
  13.    
  14.     float delay = 0, load = 0;
  15.     int sended = 0;
  16.  
  17.     for ( auto& w : windows ) {
  18.         if(debug) cout << w << "\t";
  19.         vector<subscriber*> senders;
  20.         for ( auto& sub : subscribers ) {
  21.             auto &flow = sub.flow, &arr = sub.arrived;
  22.  
  23.             copy_if ( flow.begin (), flow.end (), back_inserter ( arr ), [ = ] ( float& x ) { return x < w; } );
  24.             flow.erase ( remove_if ( flow.begin (), flow.end (), [ = ] ( float& x ) { return x < w; } ), flow.end () );
  25.  
  26.             if ( rnd () < p && sub.arrived.size () > 0 ) {
  27.                 senders.push_back ( &sub );
  28.             }
  29.  
  30.             load += sub.arrived.size ();
  31.  
  32.             if(debug) cout << sub << " ";
  33.         }
  34.        
  35.         if(debug) cout << endl;
  36.  
  37.         if ( senders.size () == 1 ) {
  38.             auto& mess = senders[ 0 ]->arrived.begin ();
  39.             delay += w - *mess;
  40.             sended += 1;
  41.             senders[ 0 ]->arrived.erase (mess);
  42.         } else {
  43.             for ( auto& s : senders ) {
  44.                 s->resolve_collision ( t, 2 * M );
  45.             }
  46.         }
  47.     }
  48.  
  49.     return make_tuple ( delay / sended, load / W );
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement