LCOV - code coverage report
Current view: top level - builds/DaniloBorquez/flex-net-sim/src - simulator.cpp (source / functions) Hit Total Coverage
Test: commit SHA1 Lines: 316 396 79.8 %
Date: 2025-08-25 21:30:02 Functions: 34 45 75.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : #include "simulator.hpp"
       2             : #include "unique_ptr.hpp"
       3             : #include "defragmentator.hpp"
       4             : #include <cmath>
       5             : #include <iostream>
       6             : #include <memory>
       7             : 
       8          12 : Simulator::Simulator(void) {
       9          12 :   this->defaultValues();
      10          12 :   this->controller = create_unique<Controller>();
      11          12 :   this->events = std::list<Event>();
      12          12 :   this->bitRatesDefault = std::vector<std::shared_ptr<BitRate>>();
      13          24 :   std::shared_ptr<BitRate> auxB = std::make_shared<BitRate>(10.0);
      14          12 :   auxB->addModulation(std::string("BPSK"), 1, 5520);
      15          12 :   this->bitRatesDefault.push_back(auxB);
      16          12 :   auxB = std::make_shared<BitRate>(40.0);
      17          12 :   auxB->addModulation(std::string("BPSK"), 4, 5520);
      18          12 :   this->bitRatesDefault.push_back(auxB);
      19          12 :   auxB = std::make_shared<BitRate>(100.0);
      20          12 :   auxB->addModulation(std::string("BPSK"), 8, 5520);
      21          12 :   this->bitRatesDefault.push_back(auxB);
      22          12 :   auxB = std::make_shared<BitRate>(400.0);
      23          12 :   auxB->addModulation(std::string("BPSK"), 32, 5520);
      24          12 :   this->bitRatesDefault.push_back(auxB);
      25          12 :   auxB = std::make_shared<BitRate>(1000.0);
      26          12 :   auxB->addModulation(std::string("BPSK"), 80, 5520);
      27          12 :   this->bitRatesDefault.push_back(auxB);
      28          12 :   this->allocatedConnections = 0;
      29          12 : }
      30             : 
      31           1 : Simulator::Simulator(std::string networkFilename, std::string pathFilename,
      32           1 :                      int networkType) {
      33           1 :   this->defaultValues();
      34           1 :   this->controller = create_unique<Controller>();
      35           1 :   this->controller->setNetwork(std::make_shared<Network>(networkFilename, networkType));
      36           1 :   this->controller->setPaths(pathFilename);
      37           1 :   this->events = std::list<Event>();
      38           1 :   this->bitRatesDefault = std::vector<std::shared_ptr<BitRate>>();
      39           2 :   std::shared_ptr<BitRate> auxB = std::make_shared<BitRate>(10.0);
      40           1 :   auxB->addModulation(std::string("BPSK"), 1, 5520);
      41           1 :   this->bitRatesDefault.push_back(auxB);
      42           1 :   auxB = std::make_shared<BitRate>(40.0);
      43           1 :   auxB->addModulation(std::string("BPSK"), 4, 5520);
      44           1 :   this->bitRatesDefault.push_back(auxB);
      45           1 :   auxB = std::make_shared<BitRate>(100.0);
      46           1 :   auxB->addModulation(std::string("BPSK"), 8, 5520);
      47           1 :   this->bitRatesDefault.push_back(auxB);
      48           1 :   auxB = std::make_shared<BitRate>(400.0);
      49           1 :   auxB->addModulation(std::string("BPSK"), 32, 5520);
      50           1 :   this->bitRatesDefault.push_back(auxB);
      51           1 :   auxB = std::make_shared<BitRate>(1000.0);
      52           1 :   auxB->addModulation(std::string("BPSK"), 80, 5520);
      53           1 :   this->bitRatesDefault.push_back(auxB);
      54           1 :   this->allocatedConnections = 0;
      55           1 : }
      56             : 
      57          15 : Simulator::Simulator(std::string networkFilename, std::string pathFilename,
      58          15 :                      std::string bitrateFilename, int networkType) {
      59          15 :   this->defaultValues();
      60          15 :   this->controller = create_unique<Controller>();
      61          15 :   this->controller->setNetwork(std::make_shared<Network>(networkFilename, networkType));
      62          15 :   this->controller->setPaths(pathFilename);
      63          15 :   this->events = std::list<Event>();
      64          15 :   this->bitRatesDefault = BitRate::selectBitrateMethod(bitrateFilename, networkType);
      65          15 :   this->allocatedConnections = 0;
      66          15 : }
      67             : 
      68             : Simulator& Simulator::operator=(Simulator&& other) noexcept = default;
      69             : 
      70             : 
      71             : Simulator::Simulator(Simulator&& other) noexcept = default;
      72             : 
      73             : // TODO: Add default values bit rates for other networkTypes.
      74             : 
      75          28 : Simulator::~Simulator() {}
      76             : 
      77           1 : void Simulator::setLambda(double lambda) {
      78           1 :   if (this->initReady) {
      79             :     throw std::runtime_error(
      80             :         "You can not set lambda parameter AFTER calling init simulator "
      81           0 :         "method.");
      82             :   }
      83           1 :   this->lambda = lambda;
      84           1 : }
      85             : 
      86           1 : void Simulator::setMu(double mu) {
      87           1 :   if (this->initReady) {
      88             :     throw std::runtime_error(
      89             :         "You can not set mu parameter AFTER calling init simulator "
      90           0 :         "method.");
      91             :   }
      92           1 :   this->mu = mu;
      93           1 : }
      94             : 
      95           1 : void Simulator::setSeedArrive(unsigned int seed) {
      96           1 :   if (this->initReady) {
      97             :     throw std::runtime_error(
      98             :         "You can not set seed arrive parameter AFTER calling init simulator "
      99           0 :         "method.");
     100             :   }
     101           1 :   this->seedArrive = seed;
     102           1 : }
     103             : 
     104           2 : void Simulator::setSeedDeparture(unsigned int seed) {
     105           2 :   if (this->initReady) {
     106             :     throw std::runtime_error(
     107             :         "You can not set seed departure parameter AFTER calling init simulator "
     108           0 :         "method.");
     109             :   }
     110           2 :   this->seedDeparture = seed;
     111           2 : }
     112             : 
     113           0 : void Simulator::setSeedBitRate(unsigned int seed) {
     114           0 :   if (this->initReady) {
     115             :     throw std::runtime_error(
     116             :         "You can not set seed bitrate parameter AFTER calling init simulator "
     117           0 :         "method.");
     118             :   }
     119           0 :   this->seedBitRate = seed;
     120           0 : }
     121             : 
     122           1 : void Simulator::setSeedSrc(unsigned int seed) {
     123           1 :   if (this->initReady) {
     124             :     throw std::runtime_error(
     125             :         "You can not set seed source parameter AFTER calling init simulator "
     126           0 :         "method.");
     127             :   }
     128           1 :   this->seedSrc = seed;
     129           1 : }
     130             : 
     131           1 : void Simulator::setSeedDst(unsigned int seed) {
     132           1 :   if (this->initReady) {
     133             :     throw std::runtime_error(
     134             :         "You can not set seed destination parameter AFTER calling init "
     135             :         "simulator "
     136           0 :         "method.");
     137             :   }
     138           1 :   this->seedDst = seed;
     139           1 : }
     140             : 
     141          12 : void Simulator::setGoalConnections(long long goal) {
     142          12 :   if (this->initReady) {
     143             :     throw std::runtime_error(
     144             :         "You can not set goal connections parameter AFTER calling init "
     145           0 :         "simulator method.");
     146             :   }
     147          12 :   this->goalConnections = goal;
     148          12 : }
     149             : 
     150           1 : void Simulator::setBitRates(std::vector<std::shared_ptr<BitRate>> bitRates) {
     151           1 :   if (this->initReady) {
     152             :     throw std::runtime_error(
     153             :         "You can not set bitrates parameter AFTER calling init simulator "
     154           0 :         "method.");
     155             :   }
     156           1 :   this->bitRatesDefault = bitRates;
     157           1 : }
     158             : 
     159          14 : void Simulator::setAllocator(std::unique_ptr<Allocator> newAllocator) {
     160          14 :   if (this->initReady) {
     161             :     throw std::runtime_error(
     162             :         "You can not set allocator parameter AFTER calling init simulator "
     163           0 :         "method.");
     164             :   }
     165          14 :   newAllocator->setNetwork(this->controller->getNetwork());
     166          14 :   newAllocator->setPaths(this->controller->getPaths());
     167          14 :   this->controller->setAllocator(std::move(newAllocator));
     168          14 : }
     169             : 
     170           0 : void Simulator::setCallDefragBefore(bool callDefragBefore) {
     171           0 :   if (this->initReady) {
     172             :     throw std::runtime_error(
     173             :         "You can not set callDefragBefore parameter AFTER calling init "
     174           0 :         "simulator method.");
     175             :   }
     176           0 :   std::unique_ptr<Allocator> &allocator = this->controller->getAllocator();
     177           0 :   if (allocator == nullptr) {
     178             :     throw std::runtime_error(
     179           0 :         "You must set an allocator before setting callDefragBefore.");
     180             :   }
     181           0 :   this->controller->getAllocator()->callDefragBefore = callDefragBefore;
     182           0 : }
     183             : 
     184             : 
     185             : 
     186           0 : void Simulator::setDefragmentator(std::unique_ptr<Defragmentator> newDefrag) {
     187           0 :   if (this->initReady) {
     188             :     throw std::runtime_error(
     189             :         "You can not set allocator parameter AFTER calling init simulator "
     190           0 :         "method.");
     191             :   }
     192           0 :   if (this->controller->getAllocator() == nullptr) {
     193             :     throw std::runtime_error(
     194           0 :         "You must set an allocator before setting a defragmentator.");
     195             :   }
     196           0 :   newDefrag->setPaths(this->controller->getPaths());
     197           0 :   this->controller->getAllocator()->setDefragmentator(std::move(newDefrag));
     198           0 : }
     199             : 
     200           0 : void Simulator::setConfidence(double c) {
     201           0 :   if (c <= 0 || c >= 1) {
     202             :     throw std::runtime_error(
     203             :         "You can't set a confidence interval with confidence equal/higher than "
     204           0 :         "1 or equal/lower than 0.");
     205             :   }
     206           0 :   this->confidence = c;
     207           0 : }
     208             : 
     209           2 : void Simulator::setNetworkType(int networkType) {
     210           2 :   if (this->initReady) {
     211             :     throw std::runtime_error(
     212             :         "You can not set 'networkType' parameter AFTER calling init simulator "
     213           1 :         "method.");
     214             :   }
     215           1 :   this->controller->getNetwork()->setNetworkType(networkType);
     216           1 : }
     217             : // Returns the int that represents the network type of the object
     218             : 
     219          28 : void Simulator::defaultValues() {
     220          28 :   this->initReady = false;
     221          28 :   this->lambda = 3;
     222          28 :   this->mu = 10;
     223          28 :   this->seedArrive = 12345;
     224          28 :   this->seedDeparture = 12345;
     225          28 :   this->seedSrc = 12345;
     226          28 :   this->seedDst = 12345;
     227          28 :   this->seedBitRate = 12345;
     228          28 :   this->numberOfConnections = -1;
     229          28 :   this->numberOfEvents = 0;
     230          28 :   this->goalConnections = 10000;
     231          28 :   this->columnWidth = 10;
     232          28 :   this->confidence = 0.95;
     233          28 : }
     234             : 
     235          12 : void Simulator::printInitialInfo() {
     236          12 :   std::cout << "\n--- Flex Net Sim (" << VERSION_MAJOR << "." << VERSION_MINOR
     237          12 :             << "." << VERSION_REVISION << ") ---"
     238          12 :             << "\n\n";
     239             : 
     240          12 :   std::cout << std::setfill(' ') << std::setw(20) << std::left << "Nodes:";
     241          12 :   std::cout << std::setw(30)
     242          12 :             << this->controller->getNetwork()->getNumberOfNodes() << "\n";
     243          12 :   std::cout << std::setw(20) << "Links:";
     244          12 :   std::cout << std::setw(30)
     245          12 :             << this->controller->getNetwork()->getNumberOfLinks() << "\n";
     246          12 :   std::cout << std::setw(20) << "Goal Connections:";
     247          12 :   std::cout << std::setw(30) << this->goalConnections << "\n";
     248          12 :   std::cout << std::setw(20) << "Lambda:";
     249          12 :   std::cout << std::setw(30) << this->lambda << "\n";
     250          12 :   std::cout << std::setw(20) << "Mu:";
     251          12 :   std::cout << std::setw(30) << this->mu << "\n";
     252          12 :   std::cout << std::setw(20) << "Algorithm:";
     253          24 :   std::cout << std::setw(30) << this->controller->getAllocator()->getName()
     254          24 :             << "\n";
     255          12 :   if (this->controller->getDefragmentator() != nullptr) {
     256           0 :     std::cout << std::setw(20) << "Defragmentator:";
     257           0 :     std::cout << std::setw(30) << this->controller->getDefragmentator()->getName()
     258           0 :               << "\n";
     259             :   }
     260          12 :   std::cout << "\n";
     261             : 
     262          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     263          12 :   std::cout << std::setw(11) << "+";
     264          12 :   std::cout << std::setw(11) << "+";
     265          12 :   std::cout << std::setw(11) << "+";
     266          12 :   std::cout << std::setw(11) << "+";
     267          12 :   std::cout << std::setw(11) << "+";
     268          12 :   std::cout << std::setw(11) << "+";
     269          12 :   std::cout << std::setw(1) << "+\n";
     270             : 
     271          12 :   std::cout << std::setfill(' ') << std::setw(11) << "| progress";
     272          12 :   std::cout << std::setw(11) << "| arrives";
     273          12 :   std::cout << std::setw(11) << "| blocking";
     274          12 :   std::cout << std::setw(11) << "| time(s)";
     275          12 :   std::cout << std::setw(11) << "| Wald CI";
     276          12 :   std::cout << std::setw(11) << "| A-C. CI";
     277          12 :   std::cout << std::setw(11) << "| Wilson CI";
     278          12 :   std::cout << std::setw(1) << "|\n";
     279             : 
     280          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     281          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     282          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     283          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     284          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     285          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     286          12 :   std::cout << std::setfill('-') << std::setw(11) << std::left << "+";
     287             :   // Nueva
     288          12 :   std::cout << std::setfill('-') << std::setw(1) << std::left << "+\n";
     289             : 
     290          12 :   this->startingTime = std::chrono::high_resolution_clock::now();
     291          12 : }
     292             : 
     293         240 : void Simulator::printRow(double percentage) {
     294         240 :   this->checkTime = std::chrono::high_resolution_clock::now();
     295         240 :   this->timeDuration =
     296             :       std::chrono::duration_cast<std::chrono::duration<double>>(
     297         480 :           this->checkTime - this->startingTime);
     298         240 :   std::cout << std::setprecision(1);
     299         240 :   std::cout << "|";
     300         240 :   std::cout << std::setfill(' ') << std::right << std::setw(7) << std::fixed
     301         240 :             << percentage << "%  |";
     302         240 :   std::cout << std::setfill(' ') << std::setw(7) << std::scientific
     303         240 :             << this->numberOfConnections - 1 << "   |";
     304         240 :   std::cout << std::setfill(' ') << std::setw(9) << std::right
     305         240 :             << std::scientific
     306         240 :             << 1 - this->allocatedConnections / this->numberOfConnections
     307         240 :             << " |";  // getBlockingProbability
     308         240 :   std::cout << std::setprecision(0) << std::setfill(' ') << std::setw(8)
     309         240 :             << std::right << std::fixed << this->timeDuration.count() << "  |";
     310             : 
     311         240 :   std::cout << std::setprecision(1) << std::setfill(' ') << std::setw(9)
     312         240 :             << std::right << std::scientific << this->waldCI() << " |";
     313             : 
     314         240 :   std::cout << std::setfill(' ') << std::setw(9) << std::right
     315         240 :             << std::scientific << this->agrestiCI() << " |";
     316             : 
     317         240 :   std::cout << std::setfill(' ') << std::setw(9) << std::right
     318         240 :             << std::scientific << this->wilsonCI() << " |";
     319             : 
     320         240 :   std::cout << std::setw(1) << "\n";
     321         240 : }
     322             : 
     323      106515 : int Simulator::eventRoutine(void) {
     324      106515 :   this->currentEvent = this->events.front();
     325      106515 :   this->rtnAllocation = N_A;
     326      106515 :   this->clock = this->currentEvent.getTime();
     327      106515 :   if (this->currentEvent.getType() == ARRIVE) {
     328       53412 :     nextEventTime = this->clock + this->arriveVariable.getNextValue();
     329       57001 :     for (std::list<Event>::reverse_iterator pos = this->events.rbegin();
     330       57001 :          pos != this->events.rend(); pos++) {
     331       57001 :       if ((pos)->getTime() < nextEventTime) {
     332      160236 :         this->events.insert((pos).base(), Event(ARRIVE, nextEventTime,
     333      160236 :                                                 this->numberOfConnections++));
     334       53412 :         break;
     335             :       }
     336             :     }
     337       53412 :     this->src = this->srcVariable.getNextIntValue();
     338       53412 :     this->dst = this->dstVariable.getNextIntValue();
     339       61512 :     while (this->src == this->dst) {
     340        4050 :       this->dst = this->dstVariable.getNextIntValue();
     341             :     }
     342       53412 :     this->bitRate = bitRateVariable.getNextIntValue();
     343       53412 :     this->rtnAllocation =
     344       53412 :         (this->controller.get()
     345      267060 :              ->*(this->controller->assignConnection))(  // TODO: No se que hice
     346             :                                                         // pero funciono
     347       53412 :             this->src, this->dst, this->bitRates[this->bitRate],
     348      106824 :             this->currentEvent.getIdConnection(), this->clock);
     349       53412 :     if (this->debugMode)
     350           0 :       debugPrintLinks(this->rtnAllocation == ALLOCATED);
     351       53412 :     if (this->rtnAllocation == ALLOCATED) {
     352       53118 :       nextEventTime = this->clock + this->departVariable.getNextValue();
     353      102885 :       for (std::list<Event>::reverse_iterator pos = this->events.rbegin();
     354      102885 :            pos != this->events.rend(); pos++) {
     355      102885 :         if ((pos)->getTime() < nextEventTime) {
     356      159354 :           this->events.insert((pos).base(),
     357      106236 :                               Event(DEPARTURE, nextEventTime,
     358      159354 :                                     this->currentEvent.getIdConnection()));
     359       53118 :           break;
     360             :         }
     361             :       }
     362       53118 :       this->allocatedConnections++;
     363             :     }
     364       53103 :   } else if (this->currentEvent.getType() == DEPARTURE) {
     365      159309 :     (this->controller.get()->*(this->controller->unassignConnection))(
     366      106206 :         this->currentEvent.getIdConnection(), this->clock);
     367             :   }
     368      106515 :   this->events.pop_front();
     369      106515 :   return this->rtnAllocation;
     370             : }
     371             : 
     372          13 : void Simulator::init(void) {
     373          13 :   this->initReady = true;
     374          13 :   this->clock = 0;
     375          13 :   this->arriveVariable = ExpVariable(this->seedArrive, this->lambda);
     376          13 :   this->departVariable = ExpVariable(this->seedDeparture, this->mu);
     377          26 :   this->srcVariable = UniformVariable(
     378          39 :       this->seedSrc, this->controller->getNetwork()->getNumberOfNodes() - 1);
     379          26 :   this->dstVariable = UniformVariable(
     380          39 :       this->seedDst, this->controller->getNetwork()->getNumberOfNodes() - 1);
     381          13 :   this->bitRateVariable =
     382          26 :       UniformVariable(this->seedBitRate, this->bitRatesDefault.size() - 1);
     383          26 :   this->events.push_back(Event(ARRIVE, this->arriveVariable.getNextValue(),
     384          13 :                                this->numberOfConnections++));
     385          13 :   this->bitRates = this->bitRatesDefault;
     386          13 :   this->initZScore();
     387          13 :   this->initZScoreEven();
     388          13 : }
     389             : 
     390          12 : void Simulator::run(void) {
     391          12 :   Simulator::run(20);
     392          12 : }
     393             : 
     394          12 : void Simulator::run(int timesToShow) {
     395          12 :   timesToShow = static_cast<float>(timesToShow);
     396          12 :   float arrivesByCycle = this->goalConnections / timesToShow;
     397          12 :   printInitialInfo();
     398         252 :   for (int i = 1; i <= timesToShow; i++) {
     399      213270 :     while (this->numberOfConnections <= i * arrivesByCycle) {
     400      106515 :       eventRoutine();
     401             :     }
     402         240 :     printRow((100 / timesToShow) * i);
     403             :   }
     404          12 :   if (this->debugMode) {
     405           0 :     std::cout << "Total connections: " << this->numberOfConnections << std::endl;
     406           0 :     std::cout << "Allocated connections: " << this->allocatedConnections << std::endl;
     407           0 :     std::cout << "Blocking Probability: " << this->getBlockingProbability() << std::endl;
     408             :   }
     409          12 : }
     410             : 
     411           0 : void Simulator::addDepartureEvent(long long idConnection) {
     412           0 :   double nextEventTime = this->clock + this->departVariable.getNextValue();
     413           0 :   for (std::list<Event>::reverse_iterator pos = this->events.rbegin();
     414           0 :        pos != this->events.rend(); pos++) {
     415           0 :     if ((pos)->getTime() < nextEventTime) {
     416           0 :       this->events.insert((pos).base(),
     417           0 :                           Event(DEPARTURE, nextEventTime, idConnection));
     418           0 :       break;
     419             :     }
     420             :   }
     421           0 : }
     422             : 
     423           1 : unsigned int Simulator::getTimeDuration(void) {
     424           1 :   return static_cast<unsigned int>(this->timeDuration.count());
     425             : }
     426             : 
     427           1 : double Simulator::getBlockingProbability(void) {
     428           1 :   return 1 - this->allocatedConnections / this->numberOfConnections;
     429             : }
     430             : 
     431         729 : double Simulator::getAllocatedProbability(void) {
     432         729 :   return this->allocatedConnections / this->numberOfConnections;
     433             : }
     434             : 
     435           3 : int Simulator::getNetworkType() {
     436           3 :   return this->controller->getNetwork()->getNetworkType();
     437             : }
     438             : 
     439         243 : double Simulator::waldCI() {
     440         243 :   double np = this->getAllocatedProbability();
     441         243 :   double p = 1 - np;
     442         243 :   int n = this->numberOfConnections;
     443         243 :   double sd = sqrt((np * p) / n);
     444             : 
     445         243 :   return this->zScore * sd;
     446             : }
     447             : 
     448         243 : double Simulator::agrestiCI() {
     449         243 :   double np = this->getAllocatedProbability();
     450         243 :   int n = this->numberOfConnections;
     451             : 
     452         486 :   np = np * ((n * (this->allocatedConnections + (this->zScoreEven / 2))) /
     453         243 :              (this->allocatedConnections * (n + this->zScoreEven)));
     454             : 
     455         243 :   double p = 1 - np;
     456         243 :   double sd = sqrt((np * p) / (n + this->zScoreEven));
     457             : 
     458         243 :   return this->zScore * sd;
     459             : }
     460             : 
     461         243 : double Simulator::wilsonCI() {
     462         243 :   double np = this->getAllocatedProbability();
     463         243 :   double p = 1 - np;
     464         243 :   int n = this->numberOfConnections;
     465             : 
     466         243 :   double denom = (1 + (pow(this->zScore, 2) / n));
     467             : 
     468         243 :   double k = p + pow(this->zScore, 2) / (2 * n);
     469         243 :   double sd = sqrt(((np * p) / n) + ((pow(this->zScore, 2)) / (4 * pow(n, 2))));
     470             : 
     471         243 :   return (this->zScore * sd) / denom;
     472             : }
     473             : 
     474          13 : void Simulator::initZScore(void) {
     475          13 :   double actual = 0.0;
     476          13 :   double step = 1.0;
     477          13 :   double covered = 0.0;
     478          13 :   double objective = this->confidence;
     479          13 :   double epsilon = 1e-6;
     480             : 
     481         481 :   while (fabs(objective - covered) > epsilon) {
     482         234 :     if (objective > covered) {
     483         143 :       actual += step;
     484         143 :       covered =
     485         143 :           ((1 + erf(actual / sqrt(2))) - (1 + erf(-actual / sqrt(2)))) / 2;
     486         143 :       if (covered > objective) {
     487          65 :         step /= 2;
     488             :       }
     489             :     } else {
     490          91 :       actual -= step;
     491          91 :       covered =
     492          91 :           ((1 + erf(actual / sqrt(2))) - (1 + erf(-actual / sqrt(2)))) / 2;
     493          91 :       if (covered < objective) {
     494          65 :         step /= 2;
     495             :       }
     496             :     }
     497             :   }
     498          13 :   this->zScore = actual;
     499          13 : }
     500             : 
     501          13 : void Simulator::initZScoreEven(void) {
     502          13 :   double zEven = pow(this->zScore, 2);
     503          13 :   zEven =
     504          13 :       floorf(zEven * 1000) / 1000;  // Redondeo a la centesima, similar a error
     505          13 :   zEven = ceil(zEven / 2) * 2;
     506             : 
     507          13 :   this->zScoreEven = zEven;
     508          13 : }
     509             : 
     510           0 : void Simulator::setUnassignCallback(void (*callbackFunction)(Connection, double,
     511             :                                                              std::shared_ptr<Network>)) {
     512           0 :   this->controller->setUnassignCallback(callbackFunction);
     513           0 : }
     514             : 
     515           0 : void Simulator::setUnassignSDM(void (*callbackFunction)(Connection, double,
     516             :                                                         std::shared_ptr<Network>)) {
     517           0 :   this->controller->setUnassignSDM(callbackFunction);
     518           0 : }
     519             : 
     520           0 : void Simulator::setUnassignMB() {
     521           0 :   this->controller->setUnassignMB();
     522           0 : }
     523             : 
     524           0 : void Simulator::debugPrintLinks(bool success) {
     525           0 :   std::shared_ptr<Network> network = this->controller->getNetwork();
     526           0 :   int linksNumber = network->getNumberOfLinks();
     527           0 :   int idConnection = this->currentEvent.getIdConnection();
     528           0 :   for (int i = 0; i < linksNumber; i ++) {
     529           0 :     std::shared_ptr<Link> link = network->getLink(i);
     530           0 :     int linkId = link->getId();
     531           0 :     int linkSrc = link->getSrc();
     532           0 :     int linkDst = link->getDst();
     533           0 :     int slotsNumber = link->getSlots();
     534           0 :     std::cout << "Link " << linkId << ": ";
     535           0 :     for (int j = 0; j < slotsNumber; j++) {
     536           0 :       bool slot = link->getSlot(j);
     537           0 :       std::cout << slot << " ";
     538             :     }
     539           0 :     std::cout << " [ " << linkSrc << " -> " << linkDst << " ]";
     540           0 :     std::cout << std::endl;
     541             :   }
     542           0 : }
     543             : 
     544           0 : void Simulator::setDebugMode(bool on) {
     545           0 :   this->debugMode = on;
     546           0 : }
     547             : 
     548             : 
     549           2 : std::vector<std::shared_ptr<BitRate>> Simulator::getBitRates(void) { return this->bitRates; }
     550             : 
     551           1 : std::shared_ptr<Paths> Simulator::getPaths() {
     552           1 :   return this->controller->getPaths();
     553             : }
     554             : 
     555           3 : std::unique_ptr<Controller>& Simulator::getController() { return this->controller; }

Generated by: LCOV version 1.13