User Manual, Developers Guide and API Documentation

dlref.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #include <WNS/evaluation/statistics/dlref.hpp>
00029 #include <cmath>
00030 
00031 using namespace std;
00032 using namespace wns::evaluation::statistics;
00033 
00034 STATIC_FACTORY_REGISTER_WITH_CREATOR(DLREF,
00035                                      StatEvalInterface,
00036                                      "openwns.evaluation.statistics.DLREF",
00037                                      wns::PyConfigViewCreator);
00038 
00039 DLREF::DLREF(std::vector<double> xValuesArrPtr,
00040              int level,
00041              double error,
00042              double preFirst,
00043              std::string name,
00044              std::string description,
00045              bool forceRMinusAOk,
00046              double fMin,
00047              int maxNrv,
00048              int skipInterval,
00049              formatType format)
00050     : DLRE(xValuesArrPtr, level, error, preFirst, name, description, forceRMinusAOk, maxNrv, skipInterval, format),
00051       fMin_(fMin)
00052 
00053 {
00054     curLevelIndex_ = indexMax_ - 1;
00055 }
00056 
00057 DLREF::DLREF(double xMin,
00058              double xMax,
00059              double intSize,
00060              double error,
00061              double preFirst,
00062              std::string name,
00063              std::string description,
00064              bool forceRMinusAOk,
00065              double fMin,
00066              int maxNrv,
00067              int skipInterval,
00068              formatType format)
00069     : DLRE(xMin, xMax, intSize, error, preFirst, name, description, forceRMinusAOk, maxNrv, skipInterval, format),
00070       fMin_(fMin)
00071 {
00072     curLevelIndex_ = indexMax_ - 1;
00073 }
00074 
00075 DLREF::DLREF(const wns::pyconfig::View& config) :
00076     DLRE(config),
00077     fMin_(config.get<double>("minLevel"))
00078 {
00079     curLevelIndex_ = indexMax_ - 1;
00080 }
00081 
00082 DLREF::~DLREF()
00083 {}
00084 
00085 void DLREF::print(ostream& stream) const
00086 {
00087     printAll(stream, df, fMin_);
00088 }
00089 
00090 void DLREF::put(double value)
00091 {
00092     if (numTrials_ + 1 < maxNrv_)
00093     {
00094         curIndex_ = getIndex(value);
00095 
00096         if (curIndex_ == noIndex)
00097         {
00098             throw wns::Exception("Warning: Wrong x value in DLREF::put !");
00099             return;
00100         }
00101         StatEval::put(value);
00102         ++h_;
00103 
00104         if (curIndex_ == lower)
00105         {
00106             ++wastedLeft_;
00107             preRv_ = xMin_ - 1.0;
00108             preIndex_ = indexMin_;
00109             return ;
00110         }
00111         else if (curIndex_ == greater)
00112         {
00113             ++wastedRight_;
00114             if (preRv_ < value)
00115             {
00116                 for (int i = preIndex_; i <= (indexMax_ - 1); i++)
00117                 {
00118                     ++(results_[i].c_);
00119                 }
00120             }
00121 
00122             preRv_ = xMax_ + 1.0;
00123             preIndex_ = indexMax_ - 1;
00124             return;
00125         }
00126         else if (preRv_ < value)
00127         {
00128             for (int i = preIndex_; i < curIndex_; i++)
00129             {
00130                 (results_[i].c_)++;
00131             }
00132         }
00133 
00134         ++(results_[curIndex_].h_);
00135         // Increment number of sorted values counters
00136         for (int i = 0; i <= curIndex_; i++)
00137         {
00138             ++(results_[i].sumh_);
00139         }
00140 
00141         // check if ready
00142         if (h_ >= skipInterval_)
00143         {
00144             phase_ = rtc();
00145             h_ = 0;
00146         }
00147 
00148         // save current values
00149         preRv_ = value;
00150         preIndex_ = curIndex_;
00151     }
00152     else
00153     {
00154         phase_ = finish;
00155     }
00156 }
00157 
00158 double
00159 DLREF::curFLev()
00160 {
00161     if (not checkLargeSample(curLevelIndex_))
00162     {
00163         return 1.0;
00164     }
00165     else
00166     {
00167         // subtract all values that are 'right' from the current
00168         // level (including the overflows)
00169 
00170         int vf = numTrials_ - wastedRight_;
00171         for (int i = indexMax_ - 1; i >= curLevelIndex_; --i)
00172         {
00173             vf -= results_[i].h_;
00174         }
00175         return (double)vf / double(numTrials_);
00176     }
00177 }
00178 
00179 double
00180 DLREF::f(double xt)
00181 {
00182     if (numTrials_ < 1000)
00183     {
00184         return 1.0;
00185     }
00186     else
00187     {
00188         int i;
00189         int vf = numTrials_ - results_[indexMin_].h_ - wastedRight_;
00190 
00191         for (i = indexMax_ - 1;
00192              (i > indexMin_) and (fabs(results_[i].x_ - xt) > getMaxError<double>());
00193              --i)
00194         {
00195             vf -= results_[i + 1].h_;
00196         }
00197 
00198         if (fabs(results_[i].x_ - xt) < getMaxError<double>())
00199         {
00200             return (double)vf / (double)numTrials_;
00201         }
00202         else
00203         {
00204             return -1.0;
00205         }
00206     }
00207 }
00208 
00210 void
00211 DLREF::getResultLine(int index, ResultLine& line) const
00212 {
00213     if ((index < minIndex()) || (index > maxIndex()))
00214     {
00215         throw wns::Exception("DLREF::getResult(): index out of range.");
00216         return;
00217     }
00218 
00219     double nf = double(numTrials_);
00220     double vf = wastedLeft_;
00221     int i;
00222     for (i = indexMin_; i <= index; i++)
00223     {
00224         vf += double(results_[i].h_);
00225     }
00226 
00227     double F = vf / nf;
00228     double cf = double(results_[ index ].c_);
00229     // large sample conditions not fulfilled ?
00230     if (not checkLargeSample(index))
00231     {
00232         line.rho_ = 0.0;
00233         line.sigRho_ = 0.0;
00234         line.relErr_ = 0.0;
00235     }
00236     else
00237     {
00238         if((fabs(F - 1.0) < getMaxError<double>()) or (fabs(vf) < getMaxError<double>()))
00239         {
00240             line.rho_ = 0.0;
00241         }
00242         else
00243         {
00244             line.rho_ = 1.0 - cf/vf/( 1.0 - F );
00245         }
00246 
00247         double uf = nf - vf;
00248 
00249         if((fabs(vf) < getMaxError<double>()) or (fabs(uf) < getMaxError<double>()))
00250         {
00251             line.sigRho_ = 0.0;
00252         }
00253         else
00254         {
00255             line.sigRho_ = sqrt(cf * (((1.0 - cf/vf)/(vf*vf)) + ((1.0 - cf/uf)/(uf*uf))));
00256         }
00257 
00258         if((fabs(vf) < getMaxError<double>()) or (fabs(line.rho_ - 1.0) < getMaxError<double>()))
00259         {
00260             line.relErr_ = 0.0;
00261         }
00262         else
00263         {
00264             line.relErr_ = sqrt((1.0 - vf/nf)/vf * (1.0 + line.rho_)/(1.0 - line.rho_));
00265         }
00266     }
00267     line.vf_ = F * base_;
00268     line.nx_ = results_[index].h_;
00269     line.x_  = results_[index].x_;
00270 }
00271 
00272 void
00273 DLREF::changeError(double newError)
00274 {
00275     if (newError < relErrMax_)
00276     {
00277         curLevelIndex_ = indexMax_ - 1;
00278     }
00279     relErrMax_ = newError;
00280     phase_ = rtc();
00281     h_ = 0;
00282 }
00283 
00287 DLRE::Phase
00288 DLREF::rtc()
00289 {
00290     if (numTrials_ < 1000)
00291     {
00292         phase_ = initialize;
00293         return phase_;
00294     }
00295 
00296     if (curFLev() <= fMin_)
00297     {
00298         phase_ = finish;
00299         reason_ = minimum;
00300         return phase_;
00301     }
00302     else
00303     {
00304         double cf;
00305         double dSquare;
00306         double rho;
00307         double nf = double(numTrials_);
00308         double vf;
00309         double maxErrorSquare = relErrMax_ * relErrMax_;
00310 
00311         // check the next level, which is the current - 1
00312         int i = curLevelIndex_ - 1;
00313 
00314         while (i > indexMin_)
00315         {
00316             cf = results_[i - 1].c_;
00317             vf = (double)(numTrials_ - wastedRight_ - results_[i].sumh_);
00318 
00319             if (not checkLargeSample(i))
00320             {
00321                 // We are still waiting for the large sample conditions
00322                 // to be fulfilled
00324                 return iterate;
00325             }
00326             else
00327             {
00328                 // The large sample conditions are fulfilled, so check
00329                 // the estimated number of samples
00330 
00331                 // special treatment for last level (index 0)
00332                 if ((i == indexMin_ + 1) and
00333                     (fabs(vf - cf) < getMaxError<double>()) and
00334                     (fabs(minValue_ - xMin_) < getMaxError<double>()))
00335                 {
00336                     phase_ = finish;
00337                     reason_ = last;
00338                     return phase_;
00339                 }
00340 
00341                 if((fabs(vf) < getMaxError<double>()) or
00342                    (fabs(nf) < getMaxError<double>()) or
00343                    (fabs(vf - nf) < getMaxError<double>()))
00344                 {
00345                     rho = 0.0;
00346                 }
00347                 else
00348                 {
00349                     rho = 1.0 - (cf / vf) / (1.0 - (vf / nf));
00350                 }
00351 
00352                 if((fabs(vf) < getMaxError<double>()) or
00353                    (fabs(nf) < getMaxError<double>()) or
00354                    (fabs(rho - 1.0) < getMaxError<double>()))
00355                 {
00356                     dSquare = 0.0;
00357                 }
00358                 else
00359                 {
00360                     dSquare = (1.0 - (vf / nf)) / vf * (1.0 + rho) / (1.0 - rho);
00361                 }
00362 
00363                 if (phase_ == initialize or dSquare > maxErrorSquare)
00364                 {
00365                     phase_ = iterate;
00367                     return phase_;
00368                 }
00369             }
00370             curLevelIndex_ = i;
00371             --i;
00372         }
00373 
00374         phase_ = finish;
00375         reason_ = ok;
00376         return phase_;
00377     }
00378 }

Generated on Sun May 27 03:31:39 2012 for openWNS by  doxygen 1.5.5