LCOV - code coverage report
Current view: top level - builds/barbot/Cosmos/src/Simulator - BatchR.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 115 129 89.1 %
Date: 2021-06-16 15:43:28 Functions: 7 8 87.5 %

          Line data    Source code
       1             : /*******************************************************************************
       2             :  *                                                                             *
       3             :  * Cosmos:(C)oncept et (O)utils (S)tatistique pour les (Mo)deles               *
       4             :  * (S)tochastiques                                                             *
       5             :  *                                                                             *
       6             :  * Copyright (C) 2009-2012 LSV & LACL                                          *
       7             :  * Authors: Paolo Ballarini BenoƮt Barbot & Hilal Djafri                       *
       8             :  * Website: http://www.lsv.ens-cachan.fr/Software/cosmos                       *
       9             :  *                                                                             *
      10             :  * This program is free software; you can redistribute it and/or modify        *
      11             :  * it under the terms of the GNU General Public License as published by        *
      12             :  * the Free Software Foundation; either version 3 of the License, or           *
      13             :  * (at your option) any later version.                                         *
      14             :  *                                                                             *
      15             :  * This program is distributed in the hope that it will be useful,             *
      16             :  * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
      17             :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
      18             :  * GNU General Public License for more details.                                *
      19             :  *                                                                             *
      20             :  * You should have received a copy of the GNU General Public License along     *
      21             :  * with this program; if not, write to the Free Software Foundation, Inc.,     *
      22             :  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.                 *
      23             :  * file BatchR.cpp created by Benoit Barbot.                                   *
      24             :  *******************************************************************************
      25             :  */
      26             : 
      27             : 
      28             : #include <unistd.h>
      29             : #include <math.h>
      30             : #include <float.h>
      31             : #include <stdlib.h>
      32             : 
      33             : #include "BatchR.hpp"
      34             : 
      35             : using namespace std;
      36             : 
      37             : 
      38             : /**
      39             :  *Initialize the batch with zeros
      40             :  * @param i is the number of formula evaluated by the automaton.
      41             :  */
      42         521 : BatchR::BatchR(size_t i,size_t j) :
      43             : I(0) ,
      44             : Isucc(0),
      45             : IsBernoulli(vector<bool>(i,true)),
      46             : Mean(vector<double>(i,0.0)),
      47             : M2(vector<double>(i,0.0)),
      48             : M3(vector<double>(i,0.0)),
      49             : M4(vector<double>(i,0.0)),
      50        1042 : Min(vector<double>(i,DBL_MAX)),
      51        1042 : Max(vector<double>(i,-DBL_MAX)),
      52             : bernVar(j,false),
      53        2605 : simTime(0.0)
      54         521 : {}
      55             : 
      56             : /**
      57             :  * Add a simulation to the batch
      58             :  * If the simulation is a success then The Mean and second Moment are updated
      59             :  * @param Result the result of one trajectory in the simulator.
      60             :  */
      61      222102 : void BatchR::addSim(const SimOutput &Result){
      62      222102 :     I++;
      63      222102 :     if (Result.accept) {
      64      148441 :         Isucc++;
      65             : 
      66      148441 :         for(size_t i =0; i< bernVar.size(); i++){
      67           0 :             if(Result.qualR[i])bernVar[i]++;
      68             :         }
      69             : 
      70     1237487 :         for(size_t i =0; i< Mean.size(); i++){
      71             :             
      72     1089046 :             if (Result.quantR[i] * (1 - Result.quantR[i]) != 0){
      73      894174 :                 IsBernoulli[i] = false;
      74             :             }
      75             :                         
      76     1089046 :                         double temp = Result.quantR[i];
      77     1089046 :             Mean[i] += temp;
      78     1089046 :                         temp *= Result.quantR[i];
      79     1089046 :             M2[i] += temp;
      80     1089046 :                         temp *= Result.quantR[i];
      81     1089046 :                         M3[i] += temp;
      82     1089046 :                         temp *= Result.quantR[i];
      83     1089046 :                         M4[i] += temp;
      84     1089046 :                         Min[i] = min(Min[i],Result.quantR[i]);
      85     1089046 :                         Max[i] = max(Max[i],Result.quantR[i]);
      86             :         }
      87             :     }
      88      222102 : }
      89             : 
      90             : /**
      91             :  * Take the union of two batch of result.
      92             :  * @param batch is a batch of result wich is added to the current batch.
      93             :  */
      94         233 : void BatchR::unionR(const BatchR &batch){
      95             :     
      96         233 :     I += batch.I;
      97         233 :     Isucc += batch.Isucc;
      98             :     
      99         233 :     if (batch.Mean.size() > Mean.size()){
     100           2 :                 size_t TableLength = batch.Mean.size();
     101           2 :                 IsBernoulli.resize(TableLength,true);
     102           2 :                 Mean.resize(TableLength,0.0);
     103           2 :                 M2.resize(TableLength,0.0);
     104           2 :                 M3.resize(TableLength,0.0);
     105           2 :                 M4.resize(TableLength,0.0);
     106           2 :                 Min.resize(TableLength,DBL_MAX);
     107           2 :                 Max.resize(TableLength,-DBL_MAX);
     108             :     }
     109        2293 :     for(size_t i =0; i< Mean.size(); i++){
     110        2060 :         IsBernoulli[i] = IsBernoulli[i] && batch.IsBernoulli[i];
     111             :         
     112        2060 :         Mean[i] += batch.Mean[i];
     113        2060 :         M2[i] += batch.M2[i];
     114        2060 :                 M3[i] += batch.M3[i];
     115        2060 :                 M4[i] += batch.M4[i];
     116        2060 :                 Min[i] = fmin(Min[i],batch.Min[i]);
     117        2060 :                 Max[i] = fmax(Max[i],batch.Max[i]);
     118             :     }
     119         233 :     if(batch.bernVar.size() > bernVar.size())
     120           0 :         bernVar.resize(batch.bernVar.size(),0);
     121             : 
     122         233 :     for(size_t i = 0; i< bernVar.size();i++)
     123           0 :         bernVar[i] += batch.bernVar[i];
     124             : 
     125         233 :     simTime += batch.simTime;
     126         233 : }
     127             : 
     128             : /**
     129             :  * This function write a batch on the standart output it is suposed to
     130             :  * be read by the function BatchR::inputR
     131             :  */
     132         285 : void BatchR::outputR(ostream &f) {
     133             :     //boost::archive::text_oarchive foa(f);
     134             :     
     135             :     //foa << *this;
     136             :     
     137         285 :     unsigned long int check = 8427;   //magic value
     138         285 :     f.write(reinterpret_cast<char*>(&check),sizeof(check));
     139         285 :     f.write(reinterpret_cast<char*>(&I),sizeof(I));
     140         285 :     f.write(reinterpret_cast<char*>(&Isucc),sizeof(Isucc));
     141         285 :     size_t v = Mean.size();
     142         285 :         f.write(reinterpret_cast<char*>(&v),sizeof(size_t));
     143         285 :     v = bernVar.size();
     144         285 :     f.write(reinterpret_cast<char*>(&v),sizeof(size_t));
     145             :         
     146        2740 :     for(unsigned int i =0; i< Mean.size(); i++){
     147        2455 :         bool tmpbool = IsBernoulli[i];
     148        2455 :         f.write(reinterpret_cast<char*>(&tmpbool),sizeof(bool));
     149        2455 :         f.write(reinterpret_cast<char*>(&Mean[i]),sizeof(Mean[0]));
     150        2455 :         f.write(reinterpret_cast<char*>(&M2[i]),sizeof(Mean[0]));
     151        2455 :         f.write(reinterpret_cast<char*>(&M3[i]),sizeof(Mean[0]));
     152        2455 :         f.write(reinterpret_cast<char*>(&M4[i]),sizeof(Mean[0]));
     153        2455 :                 f.write(reinterpret_cast<char*>(&Min[i]),sizeof(Mean[0]));
     154        2455 :         f.write(reinterpret_cast<char*>(&Max[i]),sizeof(Mean[0]));
     155             :     }
     156         285 :     for(unsigned int i =0; i< bernVar.size(); i++)
     157           0 :         f.write(reinterpret_cast<char*>(&bernVar[i]),sizeof(bernVar[0]));
     158         285 :     f.write(reinterpret_cast<char*>(&simTime),sizeof(simTime));
     159             : 
     160         285 :     f.flush();
     161         285 : }
     162             : 
     163             : 
     164             : /**
     165             :  * Read a batch from a file.
     166             :  * The batch should be print with BatchR::outputR
     167             :  * @param f an opened for reading file.
     168             :  * @return true if the read was succesfull and false if it was not complete.
     169             :  */
     170         236 : bool BatchR::inputR(FILE* f) {
     171             :     bool readb;
     172             :         size_t readbyte;
     173         236 :     bool ok = true;
     174             : 
     175         236 :     unsigned long int check = 0;
     176         236 :     readbyte = fread(reinterpret_cast<char*>( &check ), sizeof check ,1, f);
     177         236 :     ok &= (check==8427);   //check magic value
     178         236 :     ok &= (readbyte == 1);
     179             : 
     180         236 :         readbyte = fread(reinterpret_cast<char*>( &I ), sizeof I ,1, f);
     181         236 :         ok &= (readbyte == 1);
     182             :         
     183         236 :     readbyte = fread(reinterpret_cast<char*>( &Isucc), sizeof Isucc ,1, f);
     184         236 :     ok &= (readbyte == 1);
     185             : 
     186             :     size_t TableLength;
     187         236 :         readbyte = fread(reinterpret_cast<char*>( &TableLength), sizeof TableLength ,1, f);
     188         236 :     ok &= (readbyte == 1);
     189             : 
     190             :     size_t nbBernouilli;
     191         236 :     readbyte = fread(reinterpret_cast<char*>( &nbBernouilli), sizeof nbBernouilli ,1, f);
     192         236 :     ok &= (readbyte == 1);
     193         236 :     if(!ok)return false;
     194             : 
     195         233 :         if(Mean.size() != TableLength){
     196             :         //cerr << "TableLength:" << TableLength << "resize from " << Mean.size() << endl;
     197           3 :                 IsBernoulli.resize(TableLength);
     198           3 :                 Mean.resize(TableLength);
     199           3 :                 M2.resize(TableLength);
     200           3 :                 M3.resize(TableLength);
     201           3 :                 M4.resize(TableLength);
     202           3 :                 Min.resize(TableLength);
     203           3 :                 Max.resize(TableLength);
     204             :         }
     205             :         
     206        2292 :     for(unsigned int i =0; i< TableLength; i++){
     207        2059 :         readbyte = fread(reinterpret_cast<char*>( &readb ), sizeof readb ,1, f);
     208        2059 :         IsBernoulli[i]=readb;
     209        2059 :                 ok &= (readbyte == 1);
     210        2059 :         readbyte = fread(reinterpret_cast<char*>( &(Mean[i]) ), sizeof Mean[i] ,1, f);
     211        2059 :         ok &= (readbyte == 1);
     212        2059 :         readbyte = fread(reinterpret_cast<char*>( &(M2[i]) ), sizeof M2[i] ,1, f);
     213        2059 :         ok &= (readbyte == 1);
     214        2059 :                 readbyte = fread(reinterpret_cast<char*>( &(M3[i]) ), sizeof M2[i] ,1, f);
     215        2059 :         ok &= (readbyte == 1);
     216        2059 :                 readbyte = fread(reinterpret_cast<char*>( &(M4[i]) ), sizeof M2[i] ,1, f);
     217        2059 :         ok &= (readbyte == 1);
     218        2059 :                 readbyte = fread(reinterpret_cast<char*>( &(Min[i]) ), sizeof Min[i] ,1, f);
     219        2059 :         ok &= (readbyte == 1);
     220        2059 :                 readbyte = fread(reinterpret_cast<char*>( &(Max[i]) ), sizeof Max[i] ,1, f);
     221        2059 :         ok &= (readbyte == 1);
     222             :     }
     223             : 
     224         233 :     if(bernVar.size() != nbBernouilli)bernVar.resize(nbBernouilli);
     225         233 :     for( size_t i =0; i< nbBernouilli; i++){
     226           0 :         readbyte = fread(reinterpret_cast<char*>( &bernVar[i]), sizeof bernVar[0] ,1, f);
     227           0 :         ok &= (readbyte == 1);
     228             :     }
     229             : 
     230         233 :     readbyte = fread(reinterpret_cast<char*>( &simTime), sizeof simTime ,1, f);
     231         233 :     ok &= (readbyte == 1);
     232             : 
     233         233 :     return ok;
     234             : }
     235             : 
     236             : /*
     237             : template<class Archive>
     238             : void BatchR::serialize(Archive & ar, const unsigned int)
     239             : {
     240             :     ar & I & Isucc & IsBernoulli & Mean & M2 & M3 & M4 & Min & Max & bernVar & simTime;
     241             : }*/
     242             : 
     243           0 : ostream& operator<<(ostream& os, const BatchR& b){
     244           0 :   os << "I:\t" << b.I << endl << "Isucc:\t" << b.Isucc << endl << "TableLength:\t" << b.Mean.size() << endl;
     245           0 :     for(size_t i =0; i< b.Mean.size(); i++){
     246           0 :         os << "Mean:\t" << b.Mean[i] << endl << "M2:\t" << b.M2[i] << endl << "M3:\t" << b.M3[i] << endl <<"M4:\t" << b.M4[i] << "Min:\t" << b.Min[i] << endl <<"Max:\t" << b.Max[i] << endl << "IsBernoulli:\t" << b.IsBernoulli[i] << endl;
     247             :     }
     248           0 :     for(size_t i =0; i< b.bernVar.size(); i++){
     249           0 :         os << "Bernouilli:\t" << b.bernVar[i] << endl;
     250             :     }
     251           0 :     os << "Simulation Time:\t" << b.simTime << endl;
     252           0 :     return os;
     253         189 : }

Generated by: LCOV version 1.13