User Manual, Developers Guide and API Documentation

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

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