ranvar.cc

Go to the documentation of this file.
00001 /* -*-  Mode:C++; c-basic-offset:8; tab-width:8; indent-tabs-mode:t -*- */
00002 /*
00003  * Copyright (c) Xerox Corporation 1997. All rights reserved.
00004  *
00005  * This program is free software; you can redistribute it and/or modify it
00006  * under the terms of the GNU General Public License as published by the
00007  * Free Software Foundation; either version 2 of the License, or (at your
00008  * option) any later version.
00009  *
00010  * This program is distributed in the hope that it will be useful, but
00011  * WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public License along
00016  * with this program; if not, write to the Free Software Foundation, Inc.,
00017  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00018  *
00019  * Linking this file statically or dynamically with other modules is making
00020  * a combined work based on this file.  Thus, the terms and conditions of
00021  * the GNU General Public License cover the whole combination.
00022  *
00023  * In addition, as a special exception, the copyright holders of this file
00024  * give you permission to combine this file with free software programs or
00025  * libraries that are released under the GNU LGPL and with code included in
00026  * the standard release of ns-2 under the Apache 2.0 license or under
00027  * otherwise-compatible licenses with advertising requirements (or modified
00028  * versions of such code, with unchanged license).  You may copy and
00029  * distribute such a system following the terms of the GNU GPL for this
00030  * file and the licenses of the other code concerned, provided that you
00031  * include the source code of that other code when and as the GNU GPL
00032  * requires distribution of source code.
00033  *
00034  * Note that people who make modified versions of this file are not
00035  * obligated to grant this special exception for their modified versions;
00036  * it is their choice whether to do so.  The GNU General Public License
00037  * gives permission to release a modified version without this exception;
00038  * this exception also makes it possible to release a modified version
00039  * which carries forward this exception.
00040  */
00041 
00042 #ifndef lint
00043 static const char rcsid[] =
00044     "@(#) $Header: /nfs/jade/vint/CVSROOT/ns-2/tools/ranvar.cc,v 1.19 2005/08/26 05:05:31 tomh Exp $ (Xerox)";
00045 #endif
00046 
00047 #include <stdio.h>
00048 #include "ranvar.h"
00049 
00050 RandomVariable::RandomVariable()
00051 {
00052     rng_ = RNG::defaultrng(); 
00053 }
00054 
00055 int RandomVariable::command(int argc, const char*const* argv)
00056 {
00057     Tcl& tcl = Tcl::instance();
00058 
00059     if (argc == 2) {
00060         if (strcmp(argv[1], "value") == 0) {
00061             tcl.resultf("%6e", value());
00062             return(TCL_OK);
00063         }
00064     }
00065     if (argc == 3) {
00066         if (strcmp(argv[1], "use-rng") == 0) {
00067             rng_ = (RNG*)TclObject::lookup(argv[2]);
00068             if (rng_ == 0) {
00069                 tcl.resultf("no such RNG %s", argv[2]);
00070                 return(TCL_ERROR);
00071             }
00072             return(TCL_OK);
00073         }
00074     }
00075     return(TclObject::command(argc, argv));
00076 }
00077 
00078 // Added by Debojyoti Dutta 12 October 2000
00079 // This allows us to seed a randomvariable with 
00080 // our own RNG object. This command is called from 
00081 // expoo.cc and pareto.cc 
00082 
00083 int  RandomVariable::seed(char *x){
00084         
00085         Tcl& tcl = Tcl::instance();
00086 
00087                 rng_ = (RNG*)TclObject::lookup(x);
00088                 if (rng_ == 0) {
00089                         tcl.resultf("no such RNG %s", x);
00090                         return(TCL_ERROR);
00091                 }
00092                 return(TCL_OK);
00093  
00094 }
00095 
00096 
00097 static class UniformRandomVariableClass : public TclClass {
00098 public:
00099     UniformRandomVariableClass() : TclClass("RandomVariable/Uniform"){}
00100     TclObject* create(int, const char*const*) {
00101          return(new UniformRandomVariable());
00102      }
00103 } class_uniformranvar;
00104 
00105 UniformRandomVariable::UniformRandomVariable()
00106 {
00107     bind("min_", &min_);
00108     bind("max_", &max_); 
00109 }
00110 
00111 UniformRandomVariable::UniformRandomVariable(double min, double max)
00112 {
00113     min_ = min;
00114     max_ = max;
00115 }
00116 
00117 double UniformRandomVariable::value()
00118 {
00119     return(rng_->uniform(min_, max_));
00120 }
00121 
00122 
00123 static class ExponentialRandomVariableClass : public TclClass {
00124 public:
00125     ExponentialRandomVariableClass() : TclClass("RandomVariable/Exponential") {}
00126     TclObject* create(int, const char*const*) {
00127         return(new ExponentialRandomVariable());
00128     }
00129 } class_exponentialranvar;
00130 
00131 ExponentialRandomVariable::ExponentialRandomVariable()
00132 {
00133     bind("avg_", &avg_);
00134 }
00135 
00136 ExponentialRandomVariable::ExponentialRandomVariable(double avg)
00137 {
00138     avg_ = avg;
00139 }
00140 
00141 double ExponentialRandomVariable::value()
00142 {
00143     return(rng_->exponential(avg_));
00144 }
00145 
00146 
00147 static class ParetoRandomVariableClass : public TclClass {
00148  public:
00149     ParetoRandomVariableClass() : TclClass("RandomVariable/Pareto") {}
00150     TclObject* create(int, const char*const*) {
00151         return(new ParetoRandomVariable());
00152     }
00153 } class_paretoranvar;
00154 
00155 ParetoRandomVariable::ParetoRandomVariable()
00156 {
00157     bind("avg_", &avg_);
00158     bind("shape_", &shape_);
00159 }
00160 
00161 ParetoRandomVariable::ParetoRandomVariable(double avg, double shape)
00162 {
00163     avg_ = avg;
00164     shape_ = shape;
00165 }
00166 
00167 double ParetoRandomVariable::value()
00168 {
00169     /* yuck, user wants to specify shape and avg, but the
00170      * computation here is simpler if we know the 'scale'
00171      * parameter.  right thing is to probably do away with
00172      * the use of 'bind' and provide an API such that we
00173      * can update the scale everytime the user updates shape
00174      * or avg.
00175      */
00176     return(rng_->pareto(avg_ * (shape_ -1)/shape_, shape_));
00177 }
00178 
00179 /* Pareto distribution of the second kind, aka. Lomax distribution */
00180 static class ParetoIIRandomVariableClass : public TclClass {
00181  public:
00182         ParetoIIRandomVariableClass() : TclClass("RandomVariable/ParetoII") {}
00183         TclObject* create(int, const char*const*) {
00184                 return(new ParetoIIRandomVariable());
00185         }
00186 } class_paretoIIranvar;
00187 
00188 ParetoIIRandomVariable::ParetoIIRandomVariable()
00189 {
00190         bind("avg_", &avg_);
00191         bind("shape_", &shape_);
00192 }
00193 
00194 ParetoIIRandomVariable::ParetoIIRandomVariable(double avg, double shape)
00195 {
00196         avg_ = avg;
00197         shape_ = shape;
00198 }
00199 
00200 double ParetoIIRandomVariable::value()
00201 {
00202         return(rng_->paretoII(avg_ * (shape_ - 1), shape_));
00203 }
00204 
00205 static class NormalRandomVariableClass : public TclClass {
00206  public:
00207         NormalRandomVariableClass() : TclClass("RandomVariable/Normal") {}
00208         TclObject* create(int, const char*const*) {
00209                 return(new NormalRandomVariable());
00210         }
00211 } class_normalranvar;
00212  
00213 NormalRandomVariable::NormalRandomVariable()
00214 { 
00215         bind("avg_", &avg_);
00216         bind("std_", &std_);
00217 }
00218  
00219 double NormalRandomVariable::value()
00220 {
00221         return(rng_->normal(avg_, std_));
00222 }
00223 
00224 static class LogNormalRandomVariableClass : public TclClass {
00225  public:
00226         LogNormalRandomVariableClass() : TclClass("RandomVariable/LogNormal") {}
00227         TclObject* create(int, const char*const*) {
00228                 return(new LogNormalRandomVariable());
00229         }
00230 } class_lognormalranvar;
00231  
00232 LogNormalRandomVariable::LogNormalRandomVariable()
00233 { 
00234         bind("avg_", &avg_);
00235         bind("std_", &std_);
00236 }
00237  
00238 double LogNormalRandomVariable::value()
00239 {
00240         return(rng_->lognormal(avg_, std_));
00241 }
00242 
00243 static class ConstantRandomVariableClass : public TclClass {
00244  public:
00245     ConstantRandomVariableClass() : TclClass("RandomVariable/Constant"){}
00246     TclObject* create(int, const char*const*) {
00247         return(new ConstantRandomVariable());
00248     }
00249 } class_constantranvar;
00250 
00251 ConstantRandomVariable::ConstantRandomVariable()
00252 {
00253     bind("val_", &val_);
00254 }
00255 
00256 ConstantRandomVariable::ConstantRandomVariable(double val)
00257 {
00258     val_ = val;
00259 }
00260 
00261 double ConstantRandomVariable::value()
00262 {
00263     return(val_);
00264 }
00265 
00266 
00267 /* Hyperexponential distribution code adapted from code provided
00268  * by Ion Stoica.
00269  */
00270 
00271 static class HyperExponentialRandomVariableClass : public TclClass {
00272 public:
00273     HyperExponentialRandomVariableClass() : 
00274     TclClass("RandomVariable/HyperExponential") {}
00275     TclObject* create(int, const char*const*) {
00276         return(new HyperExponentialRandomVariable());
00277     }
00278 } class_hyperexponentialranvar;
00279 
00280 HyperExponentialRandomVariable::HyperExponentialRandomVariable()
00281 {
00282     bind("avg_", &avg_);
00283     bind("cov_", &cov_);
00284     alpha_ = .95;
00285 }
00286 
00287 HyperExponentialRandomVariable::HyperExponentialRandomVariable(double avg, double cov)
00288 {
00289     alpha_ = .95;
00290     avg_ = avg;
00291     cov_ = cov;
00292 }
00293 
00294 double HyperExponentialRandomVariable::value()
00295 {
00296     double temp, res;
00297     double u = Random::uniform();
00298 
00299     temp = sqrt((cov_ * cov_ - 1.0)/(2.0 * alpha_ * (1.0 - alpha_)));
00300     if (u < alpha_)
00301         res = Random::exponential(avg_ - temp * (1.0 - alpha_) * avg_);
00302     else
00303         res = Random::exponential(avg_ + temp * (alpha_) * avg_);
00304     return(res);
00305 }
00306 
00307 /*
00308 // Empirical Random Variable:
00309 //  CDF input from file with the following column
00310 //   1.  Possible values in a distrubutions
00311 //   2.  Number of occurances for those values
00312 //   3.  The CDF for those value
00313 //  code provided by Giao Nguyen
00314 */
00315 
00316 static class EmpiricalRandomVariableClass : public TclClass {
00317 public:
00318     EmpiricalRandomVariableClass() : TclClass("RandomVariable/Empirical"){}
00319     TclObject* create(int, const char*const*) {
00320         return(new EmpiricalRandomVariable());
00321     }
00322 } class_empiricalranvar;
00323 
00324 EmpiricalRandomVariable::EmpiricalRandomVariable() : minCDF_(0), maxCDF_(1), maxEntry_(32), table_(0)
00325 {
00326     bind("minCDF_", &minCDF_);
00327     bind("maxCDF_", &maxCDF_);
00328     bind("interpolation_", &interpolation_);
00329     bind("maxEntry_", &maxEntry_);
00330 }
00331 
00332 int EmpiricalRandomVariable::command(int argc, const char*const* argv)
00333 {
00334     Tcl& tcl = Tcl::instance();
00335     if (argc == 3) {
00336         if (strcmp(argv[1], "loadCDF") == 0) {
00337             if (loadCDF(argv[2]) == 0) {
00338                 tcl.resultf("%s loadCDF %s: invalid file",
00339                         name(), argv[2]);
00340                 return (TCL_ERROR);
00341             }
00342             return (TCL_OK);
00343         }
00344     }
00345     return RandomVariable::command(argc, argv);
00346 }
00347 
00348 int EmpiricalRandomVariable::loadCDF(const char* filename)
00349 {
00350     FILE* fp;
00351     char line[256];
00352     CDFentry* e;
00353 
00354     fp = fopen(filename, "r");
00355     if (fp == 0) 
00356         return 0;
00357 
00358 
00359     if (table_ == 0)
00360         table_ = new CDFentry[maxEntry_];
00361     for (numEntry_=0;  fgets(line, 256, fp);  numEntry_++) {
00362         if (numEntry_ >= maxEntry_) {   // resize the CDF table
00363             maxEntry_ *= 2;
00364             e = new CDFentry[maxEntry_];
00365             for (int i=numEntry_-1; i >= 0; i--)
00366                 e[i] = table_[i];
00367             delete table_;
00368             table_ = e;
00369         }
00370         e = &table_[numEntry_];
00371         // Use * and l together raises a warning
00372         sscanf(line, "%lf %*f %lf", &e->val_, &e->cdf_);
00373     }
00374         fclose(fp);
00375     return numEntry_;
00376 }
00377 
00378 double EmpiricalRandomVariable::value()
00379 {
00380     if (numEntry_ <= 0)
00381         return 0;
00382     double u = rng_->uniform(minCDF_, maxCDF_);
00383     int mid = lookup(u);
00384     if (mid && interpolation_ && u < table_[mid].cdf_)
00385         return interpolate(u, table_[mid-1].cdf_, table_[mid-1].val_,
00386                    table_[mid].cdf_, table_[mid].val_);
00387     return table_[mid].val_;
00388 }
00389 
00390 double EmpiricalRandomVariable::interpolate(double x, double x1, double y1, double x2, double y2)
00391 {
00392     double value = y1 + (x - x1) * (y2 - y1) / (x2 - x1);
00393     if (interpolation_ == INTER_INTEGRAL)   // round up
00394         return ceil(value);
00395     return value;
00396 }
00397 
00398 int EmpiricalRandomVariable::lookup(double u)
00399 {
00400     // always return an index whose value is >= u
00401     int lo, hi, mid;
00402     if (u <= table_[0].cdf_)
00403         return 0;
00404     for (lo=1, hi=numEntry_-1;  lo < hi; ) {
00405         mid = (lo + hi) / 2;
00406         if (u > table_[mid].cdf_)
00407             lo = mid + 1;
00408         else hi = mid;
00409     }
00410     return lo;
00411 }

Generated on Tue Mar 6 16:47:49 2007 for ns2 Network Simulator 2.29 by  doxygen 1.4.6