User Manual, Developers Guide and API Documentation

dlreg.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/dlreg.hpp>
00029 #include <cmath>
00030 
00031 
00032 using namespace std;
00033 using namespace wns::evaluation::statistics;
00034 
00035 
00036 STATIC_FACTORY_REGISTER_WITH_CREATOR(DLREG,
00037                                      StatEvalInterface,
00038                                      "openwns.evaluation.statistics.DLREG",
00039                                      wns::PyConfigViewCreator);
00040 
00041 
00042 DLREG::DLREG(std::vector<double> xValuesArr,
00043              int level,
00044              double error,
00045              double preFirst,
00046              std::string name,
00047              std::string description,
00048              bool forceRMinusAOk,
00049              double gMin,
00050              int maxNrv,
00051              int skipInterval,
00052              formatType format)
00053     : DLRE(xValuesArr, level, error, preFirst, name, description, forceRMinusAOk, maxNrv, skipInterval, format),
00054       gMin_(gMin)
00055 {
00056     curLevelIndex_ = indexMin_;
00057 }
00058 
00059 
00061 DLREG::DLREG(double xMin,
00062              double xMax,
00063              double intSize,
00064              double error,
00065              double preFirst,
00066              std::string name,
00067              std::string description,
00068              bool forceRMinusAOk,
00069              double gMin,
00070              int maxNrv,
00071              int skipInterval,
00072              formatType aFormat)
00073     : DLRE(xMin, xMax, intSize, error, preFirst, name, description, forceRMinusAOk, maxNrv, skipInterval, aFormat),
00074       gMin_(gMin)
00075 {
00076     curLevelIndex_ = indexMin_;
00077 }
00078 
00079 
00081 DLREG::DLREG(const wns::pyconfig::View& config) :
00082     DLRE(config),
00083     gMin_(config.get<double>("minLevel"))
00084 {
00085     curLevelIndex_ = indexMin_;
00086 }
00087 
00089 DLREG::~DLREG()
00090 {}
00091 
00092 
00094 void
00095 DLREG::print(std::ostream& stream) const
00096 {
00097     printAll(stream, cdf, gMin_);
00098 }
00099 
00100 
00102 void
00103 DLREG::put(double value)
00104 {
00105     if (numTrials_ + 1 < maxNrv_)
00106     {
00107         curIndex_ = getIndex(value);
00108 
00109         if (curIndex_ == noIndex)
00110         {
00111             throw wns::Exception("Warning: Wrong x value in DLREG::put !");
00112             return;
00113         }
00114 
00115         StatEval::put(value);
00116         ++h_;
00117 
00118         if (curIndex_ == lower)
00119         {
00120             ++wastedLeft_;
00121             if (preRv_ > value)
00122             {
00123                 for (int i = preIndex_; i >= indexMin_ ; --i)
00124                 {
00125                     ++(results_[i].c_);
00126                 }
00127             }
00128 
00129             preRv_ = xMin_ - 1.0;
00130             preIndex_ = indexMin_;
00131             return;
00132         }
00133         else if (curIndex_ == greater)
00134         {
00135             ++wastedRight_;
00136             preRv_ = xMax_ + 1.0;
00137             preIndex_ = indexMax_ - 1;
00138             return;
00139         }
00140         else if (preRv_ > value)
00141         {
00142             for (int i = preIndex_; i > curIndex_; i--)
00143             {
00144                 ++(results_[i].c_);
00145             }
00146         }
00147 
00148         results_[curIndex_].h_++;
00149         // Increment number of sorted values counters
00150         for (int i = 0; i <= curIndex_; i++)
00151         {
00152             ++(results_[i].sumh_);
00153         }
00154 
00155         // check if ready
00156         if (h_ >= skipInterval_)
00157         {
00158             phase_ = rtc();
00159             h_ = 0;
00160         }
00161 
00162         // save current values
00163         preRv_ = value;
00164         preIndex_ = curIndex_;
00165     }
00166     else
00167     {
00168         phase_ = finish;
00169     }
00170 }
00171 
00172 
00174 double
00175 DLREG::curGLev() const
00176 {
00177     if (not (checkLargeSample(curLevelIndex_)))
00178     {
00179         return 1.0;
00180     }
00181     else
00182     {
00183         // subtract all values that are 'left' from the current
00184         // level (including the underflows)
00185 
00186         int vf = numTrials_ - wastedLeft_;
00187         for (int i = indexMin_; i <= curLevelIndex_; i++)
00188         {
00189             vf -= results_[i].h_;
00190         }
00191         return (double)vf / double(numTrials_);
00192     }
00193 }
00194 
00195 
00197 double
00198 DLREG::g(double xt) const
00199 {
00200     if (numTrials_ < 1000)
00201     {
00202         return 1.0;
00203     }
00204     else
00205     {
00206         int i;
00207         int vf = numTrials_ - results_[indexMin_].h_ - wastedLeft_;
00208 
00209         for (i = indexMin_;
00210              (i < (indexMax_ - 1) and (fabs(results_[i].x_ - xt) > getMaxError<double>()));
00211              ++i)
00212         {
00213             vf -= results_[i + 1].h_;
00214         }
00215 
00216         if (fabs(results_[i].x_ - xt) < getMaxError<double>())
00217         {
00218             return (double)vf / (double)numTrials_;
00219         }
00220         else
00221         {
00222             return -1.0;
00223         }
00224     }
00225 }
00226 
00227 
00229 void
00230 DLREG::getResultLine(const int index, ResultLine& line) const
00231 {
00232     if ((index < minIndex()) or (index > maxIndex()))
00233     {
00234         throw wns::Exception("DLREG::getResult(): index out of range.");
00235         return;
00236     }
00237 
00238     double nf = double(numTrials_);
00239     double vf = wastedRight_;
00240     int i;
00241     for (i = indexMax_ - 1; i >= index; i--)
00242     {
00243         vf += double(results_[i].h_);
00244     }
00245 
00246     double G = vf/nf;
00247     double cf = double(results_[index].c_);
00248 
00249     // large sample conditions not fulfilled ?
00250     if (not checkLargeSample(index))
00251     {
00252         line.rho_    = 0.0;
00253         line.sigRho_ = 0.0;
00254         line.relErr_ = 0.0;
00255     }
00256     else
00257     {
00258         if((fabs(G) < getMaxError<double>()) or (fabs(vf) < getMaxError<double>()))
00259         {
00260             line.rho_ = 0.0;
00261         }
00262         else
00263         {
00264             line.rho_ = 1.0 - cf/vf/(1.0 - G);
00265         }
00266 
00267         double uf = nf - vf;
00268 
00269         if((fabs(vf) < getMaxError<double>()) or (fabs(uf) < getMaxError<double>()))
00270         {
00271             line.sigRho_ = 0.0;
00272         }
00273         else
00274         {
00275             line.sigRho_ =  sqrt(cf * (((1 - cf/vf)/(vf*vf)) + ((1 - cf/uf)/(uf*uf))));
00276         }
00277 
00278         if((fabs(vf) < getMaxError<double>()) or (fabs(line.rho_ - 1.0) < getMaxError<double>()))
00279         {
00280             line.relErr_ = 0.0;
00281         }
00282         else
00283         {
00284             line.relErr_ = sqrt((1.0 - vf/nf)/vf * (1.0 + line.rho_)/(1.0 - line.rho_));
00285         }
00286     }
00287 
00288     line.vf_ = G * base_;
00289     line.nx_ = results_[index].h_;
00290     line.x_  = results_[index].x_;
00291 }
00292 
00293 
00295 void
00296 DLREG::changeError(double newError)
00297 {
00298     if (newError < relErrMax_)
00299     {
00300         curLevelIndex_ = indexMin_;
00301     }
00302     relErrMax_ = newError;
00303     phase_ = rtc();
00304     h_ = 0;
00305 }
00306 
00310 DLRE::Phase
00311 DLREG::rtc()
00312 {
00313     if (numTrials_ < 1000)
00314     {
00315         phase_ = initialize;
00316         return phase_;
00317     }
00318 
00319     if (curGLev() <= gMin_)
00320     {
00321         phase_ = finish;
00322         reason_ = minimum;
00323         return phase_;
00324     }
00325     else
00326     {
00327         double cf;
00328         double dSquare;
00329         double rho;
00330         double nf = double(numTrials_);
00331         double vf;
00332         double max_error_square = relErrMax_ * relErrMax_;
00333 
00334         // check the next level, which is the current + 1
00335         int i = curLevelIndex_ + 1;
00336 
00337         while (i < indexMax_ - 1)
00338         {
00339             cf = results_[i + 1].c_;
00340             vf = (double)(results_[i].sumh_ - wastedLeft_);
00341 
00342             if (not checkLargeSample(i))
00343             {
00344                 // We are still waiting for the large sample conditions
00345                 // to be fulfilled
00347                 return iterate;
00348             }
00349             else
00350             {
00351                 // The large sample conditions are fulfilled, so check
00352                 // the estimated number of samples
00353 
00354                 // special treatment for last level (index indexMax_)
00355                 if (i == (indexMax_ - 2) and
00356                     (fabs(vf - cf) < getMaxError<double>()) and
00357                     (fabs(maxValue_ - xMax_) < getMaxError<double>()))
00358                 {
00359                     phase_ = finish;
00360                     reason_ = last;
00361                     return phase_;
00362                 }
00363 
00364                 if((fabs(vf) < getMaxError<double>()) or
00365                    (fabs(nf) < getMaxError<double>()) or
00366                    (fabs(vf - nf) < getMaxError<double>()))
00367                 {
00368                     rho = 0.0;
00369                 }
00370                 else
00371                 {
00372                     rho =  1.0 - (cf / vf) / (1.0 - (vf / nf));
00373                 }
00374 
00375                 if((fabs(vf) < getMaxError<double>()) or
00376                    (fabs(nf) < getMaxError<double>()) or
00377                    (fabs(rho - 1.0) < getMaxError<double>()))
00378                 {
00379                     dSquare = 0.0;
00380                 }
00381                 else
00382                 {
00383                     dSquare = (1.0 - (vf / nf)) / vf * (1.0 + rho) / (1.0 - rho);
00384                 }
00385 
00386                 if (phase_ == initialize or dSquare > max_error_square)
00387                 {
00388                     phase_ = iterate;
00390                     return phase_;
00391                 }
00392             }
00393             curLevelIndex_ = i;
00394             ++i;
00395         }
00396 
00397         phase_ = finish;
00398         reason_ = ok;
00399         return phase_;
00400     }
00401 }

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