00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
00079
00080
00081
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
00170
00171
00172
00173
00174
00175
00176 return(rng_->pareto(avg_ * (shape_ -1)/shape_, shape_));
00177 }
00178
00179
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
00268
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
00309
00310
00311
00312
00313
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_) {
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
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)
00394 return ceil(value);
00395 return value;
00396 }
00397
00398 int EmpiricalRandomVariable::lookup(double u)
00399 {
00400
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 }