asim.h
上传用户:rrhhcc
上传日期:2015-12-11
资源大小:54129k
文件大小:3k
源码类别:

通讯编程

开发平台:

Visual C++

  1. /*
  2.  * asim.h
  3.  * Copyright (C) 2000 by the University of Southern California
  4.  * $Id: asim.h,v 1.3 2005/08/25 18:58:01 johnh Exp $
  5.  *
  6.  * This program is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU General Public License,
  8.  * version 2, as published by the Free Software Foundation.
  9.  *
  10.  * This program is distributed in the hope that it will be useful,
  11.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.  * GNU General Public License for more details.
  14.  *
  15.  * You should have received a copy of the GNU General Public License along
  16.  * with this program; if not, write to the Free Software Foundation, Inc.,
  17.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  18.  *
  19.  *
  20.  * The copyright of this module includes the following
  21.  * linking-with-specific-other-licenses addition:
  22.  *
  23.  * In addition, as a special exception, the copyright holders of
  24.  * this module give you permission to combine (via static or
  25.  * dynamic linking) this module with free software programs or
  26.  * libraries that are released under the GNU LGPL and with code
  27.  * included in the standard release of ns-2 under the Apache 2.0
  28.  * license or under otherwise-compatible licenses with advertising
  29.  * requirements (or modified versions of such code, with unchanged
  30.  * license).  You may copy and distribute such a system following the
  31.  * terms of the GNU GPL for this module and the licenses of the
  32.  * other code concerned, provided that you include the source code of
  33.  * that other code when and as the GNU GPL requires distribution of
  34.  * source code.
  35.  *
  36.  * Note that people who make modified versions of this module
  37.  * are not obligated to grant this special exception for their
  38.  * modified versions; it is their choice whether to do so.  The GNU
  39.  * General Public License gives permission to release a modified
  40.  * version without this exception; this exception also makes it
  41.  * possible to release a modified version which carries forward this
  42.  * exception.
  43.  *
  44.  */
  45. // author: Debojyoti Dutta
  46. #ifndef _RED_ROUTER_H_
  47. #define _RED_ROUTER_H_
  48. #include <assert.h>
  49. #include <stdio.h>
  50. #include <math.h>
  51. class RedRouter {
  52.   double MinTh, MaxTh, MaxP;
  53.   double Lambda_L;
  54.   double Lambda_H;
  55.   void Populate();
  56.  public:
  57.   RedRouter(int mTh, int MTh, double MP) {
  58.     MinTh = mTh;
  59.     MaxTh = MTh;
  60.     MaxP = MP; 
  61.     Populate();
  62.   }
  63.   double ComputeProbability(double Lambda, double &Delay);
  64.   short Identical(int mTh, int MTh, double MP) {
  65.     return (mTh == MinTh &&
  66.     MTh == MaxTh &&
  67.     MP  == MaxP);
  68.   }
  69. };
  70. void RedRouter::Populate() {
  71.   // rho = Lambda_L: p = 0 => rho/(1-rho) = MinTh
  72.   Lambda_L = ((double)MinTh)/((double)(1+MinTh));
  73.   // rho = Lambda_H: p = Max_p => rho(1-Max_p)/(1-rho(1-Max_p)) = MaxTh;
  74.   if (MaxP < 1)
  75.     Lambda_H = ((double)MaxTh)/((double)(1+MaxTh))/(1-MaxP);
  76. }
  77. double RedRouter::ComputeProbability(double Lambda, double &delay) {
  78.   double p;
  79.   
  80.   if (Lambda <= Lambda_L) {
  81.     delay = Lambda/(1-Lambda);
  82.     return 0;
  83.   }
  84.   if (MaxP < 1 && Lambda > Lambda_H) {
  85.     delay = MaxTh;
  86.     p = (Lambda - Lambda_H*(1 - MaxP))/Lambda;
  87.     return p;
  88.   }
  89.   // Solve the quadratic a.p^2 + b.p + c = 0
  90.   double a, b, c;
  91.   a = Lambda * (MaxTh - MinTh)/(MaxP);
  92.   b = (MaxTh - MinTh)*(1-Lambda)/MaxP + MinTh * Lambda + Lambda;
  93.   c = MinTh*(1-Lambda)-Lambda;
  94.   p = (-b + sqrt(b*b - 4 * a * c))/(2 * a);
  95.   delay = Lambda*(1-p)/(1-(Lambda*(1-p)));
  96.   return p;
  97. }
  98. #endif