- Main Page
- Namespaces
- Classes
- Files
- Related Pages

# /Users/fpy/titan-1-5-MBARI/Mers/Interfaces/ProbDistribution.cpp

Go to the documentation of this file.

    00001
    00002
    00003
    00004
    00005
    00006
    00007 // ============================================================================
    00008
    00009 #include "ProbDistribution.h"
    00010 #include <Mers/Utils/Lang/CharUtils.h>
    00011
    00012 using namespace std;
    00013 using namespace Mers::Utils::Lang;
    00014 using namespace Mers::Interfaces;
    00015
    00016 // -------------------------- Constructors/Destructors ------------------------
    00017
    00019 ProbDistribution::ProbDistribution()
    00020 {
    00021         init();
    00022 }
    00023
    00024
    00026 ProbDistribution::ProbDistribution( string distributionString )
    00027 {
    00028         init();
    00029         parseDistributionName( distributionString );
    00030 }
    00031
    00032
    00034 ProbDistribution::ProbDistribution( string type, string param1, string param2 )
    00035 {
    00036         init();
    00037
    00038         // convert the parameters from strings to Time
    00039         Time time_param1( param1.c_str() );
    00040         Time time_param2( param2.c_str() );
    00041
    00042         if( type == "uniform" )
    00043         {
    00044                 distributionType = "uniform";
    00045                 set_bounds( time_param1, time_param2 );
    00046         }
    00047         else if ( type == "gaussian" )
    00048         {
    00049                 distributionType = "gaussian";
    00050                 mean = time_param1;
    00051                 std_dev = time_param2;
    00052         }
    00053 }
    00054
    00055
    00057 ProbDistribution::~ProbDistribution()
    00058 {
    00059 }
    00060
    00061
    00063 ProbDistribution::ProbDistribution( ProbDistribution const &other )
    00064 {
    00065         //cerr << "ProbDistribution: calling copy constructor" << endl;
    00066         *this  = other;
    00067 }
    00068
    00069
    00071 ProbDistribution& ProbDistribution::operator=( ProbDistribution const &other )
    00072 {
    00073         distributionType = other.distributionType;
    00074         lb = other.lb;
    00075         ub = other.ub;
    00076         mean = other.mean;
    00077         std_dev = other.std_dev;
    00078         return *this;
    00079 }
    00080
    00081
    00083 void
    00084 ProbDistribution::init()
    00085 {
    00086         // absolute time variables
    00087         distributionType = "uniform";   // probablity distribution type
    00088         lb = InfInt::NEG_INF;                                   // lower bound on probability distribution
    00089         ub = InfInt::POS_INF;                           // upper bound on probability distribution
    00090         mean = 0;                                           // mean of probablity distribution
    00091         std_dev = 1;                                    // standard deviation on execution time
    00092 }
    00093
    00094
    00095 // ------------------------------ Accessors ---------------------------------------
    00096
    00097 // common accessors
    00098 string ProbDistribution::get_distributionType() const { return distributionType; }
    00099 void   ProbDistribution::set_distributionType( string type ) { distributionType = type; }
    00100
    00101 // uniform accessors
    00102 Time ProbDistribution::get_lb() const { return lb; }
    00103 Time ProbDistribution::get_ub() const { return ub; }
    00104
    00105 void
    00106 ProbDistribution::set_bounds( Time lb_, Time ub_ )
    00107 {
    00108         // set lower bound, upper bound and mean
    00109         lb = lb_;
    00110         ub = ub_;
    00111         if( distributionType == "uniform" )
    00112                 mean = (ub.longval() - lb.longval() ) / 2;
    00113 }
    00114
    00115 // gaussian accessors
    00116 Time ProbDistribution::get_mean() const { return mean; }
    00117 void ProbDistribution::set_mean( Time mean_ ) { mean = mean_; }
    00118
    00119 Time ProbDistribution::get_std_dev() const { return std_dev; }
    00120 void ProbDistribution::set_std_dev( Time std_dev_ ) { std_dev = std_dev_; }
    00121
    00122
    00123 // ------------------------- Parsing Methods ----------------------------------
    00124
    00125 // The input distribution string contains the distribution name and two parameters
    00126 // examples: uniform(lb,ub) or gaussian(mean,std_dev)
    00127 // This function parses the string and initializes the distribution.
    00128 // returns true if parser succeeds, else returns false.
    00129 bool
    00130 ProbDistribution::parseDistributionName( string distributionString )
    00131 {
    00132         // initialize values
    00133         init();
    00134
    00135         // local variables
    00136         char *input, *distName, *param1, *param2;
    00137         string string1;
    00138
    00139         // extract the the two parameters
    00140         input = CharUtils::strdupnew( distributionString.c_str() );
    00141         distName = strtok( input, "(" );
    00142         param1 = strtok( NULL, "," );
    00143         param2 = strtok( NULL, ")" );
    00144
    00145         // convert parameters
    00146         Time time_param1( param1 );
    00147         Time time_param2( param2 );
    00148         string1 = distName;
    00149
    00150         // handle uniform and gausian cases separatly
    00151         if( string1 == "uniform" )
    00152         {
    00153                 distributionType = "uniform";
    00154                 set_bounds( time_param1, time_param2 );
    00155                 return true;
    00156         }
    00157
    00158         else if( string1 == "gaussian" )
    00159         {
    00160                 distributionType = "gaussian";
    00161                 set_mean( time_param1 );
    00162                 set_std_dev( time_param2 );
    00163                 return true;
    00164         }
    00165
    00166         // error case
    00167         cerr << "parseDistributionName(): Error, distribution type: "
    00168                  << distName << " unknown!" << endl;
    00169         return false;
    00170 }
    00171