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

# /Users/fpy/titan-1-5-MBARI/Mers/Estimation/Discrete/Estimate.cpp

    00001 //==============================================================================
    00002 // Copyright (c) 2004           Model-based Embedded and Robotic Systems Group
    00003 // All Rights Reserved          Massachusetts Institute of Technology
    00004 //
    00005 // This software is provided as is. Model-based Embedded and Robotic Systems
    00006 // (MERS) group does not assume any responsibility.
    00007 //==============================================================================
    00008
    00009 #include "Estimate.h"
    00010
    00011 #ifdef _MSC_VER
    00012 #pragma warning( disable : 4786 )
    00013 #endif
    00014
    00015 #include <iomanip>
    00016 #include <Mers/Models/Base/BaseModelAssignment.h>
    00017 #include <Mers/Models/Base/BaseModelAssignmentArray.h>
    00018 #include <Mers/Models/Base/BaseModelVariable.h>
    00019 #include <cassert>
    00020
    00021 using namespace Mers::Estimation::Discrete;
    00022 using Mers::Models::Base::BaseModelAssignment;
    00023 using Mers::Models::Base::BaseModelAssignmentArray;
    00024 using Mers::Models::Base::BaseModelVariable;
    00025 using std::ostream;
    00026 using std::setprecision;
    00027 using std::cerr;
    00028 using std::endl;
    00029
    00030         //-----------------------------------------------
    00031         //      CONSTRUCTOR
    00032         //-----------------------------------------------
    00033
    00034
    00035 Estimate::Estimate(BaseModelAssignmentArray const * assigns, const double p,
    00036                                    const unsigned int oTN, const unsigned short db)
    00037 : componentAssignments(assigns), posteriori(p), originatedTrajectoryNumber(oTN), debug(db)
    00038 {
    00039         if(p > 1.0 || p < 0.0)
    00040                 cerr << "<Estimate> ERROR: Estimate probably must be [0,1]! (" << p << ")" << endl;
    00041
    00042         // Simple error checking to make sure all assignments are state variables
    00043         bool badVar = false;
    00044         BaseModelAssignmentArray::ConstantIterator bmaaItr;
    00045
    00046         for(bmaaItr = assigns->begin(); bmaaItr != assigns->end(); bmaaItr++)
    00047         {
    00048                 const BaseModelVariable var = *bmaaItr->getVariable();
    00049                 if(var.getVariableType() != BaseModelVariable::STATE && var.getVariableType() != BaseModelVariable::State)
    00050                         badVar = true;
    00051         }
    00052         if(badVar)
    00053                 cerr << "<Estimate> ERROR: Invalid Variable Type! Mode assignments should be State variables." << endl;
    00054 }
    00055
    00056         //-----------------------------------------------
    00057         //      COPY CONSTRUCTOR
    00058         //-----------------------------------------------
    00059
    00060
    00061 Estimate::Estimate(const Estimate &est)
    00062 : componentAssignments(est.componentAssignments), posteriori(est.posteriori),
    00063         originatedTrajectoryNumber(est.originatedTrajectoryNumber), debug(est.debug)
    00064 {}
    00065
    00066 Estimate::Estimate(const Estimate * est)
    00067 : componentAssignments(est->componentAssignments), posteriori(est->posteriori),
    00068 originatedTrajectoryNumber(est->originatedTrajectoryNumber), debug(est->debug)
    00069 {}
    00070
    00071         //-----------------------------------------------
    00072         //      DESTRUCTOR
    00073         //-----------------------------------------------
    00074
    00075
    00076 Estimate::~Estimate()
    00077 {
    00078         delete componentAssignments;
    00079 }
    00080
    00081
    00082
    00083         //-----------------------------------------------
    00084         //      PUBLIC METHODS
    00085         //-----------------------------------------------
    00086
    00087 double
    00088 Estimate::getProbability() const
    00089 {
    00090         return posteriori;
    00091 }
    00092
    00093
    00094 void
    00095 Estimate::setProbability(double p)
    00096 {
    00097         posteriori = p;
    00098         return;
    00099 }
    00100
    00101
    00102 const unsigned int
    00103 Estimate::getOriginatedTrajectoryNumber() const
    00104 {
    00105         return originatedTrajectoryNumber;
    00106 }
    00107
    00108
    00109 BaseModelAssignmentArray const *
    00110 Estimate::getComponentAssignments() const
    00111 {
    00112         return componentAssignments;
    00113 }
    00114
    00115
    00116 BaseModelAssignment const *
    00117 Estimate::getAssignment(const unsigned int index) const
    00118 {
    00119         return (*componentAssignments)[index];
    00120 }
    00121
    00122
    00123 BaseModelAssignment const *
    00124 Estimate::getAssignmentByUID(const unsigned int uid) const
    00125 {
    00126         for(unsigned short i=0; i<componentAssignments->getLength(); i++)
    00127         {
    00128                 if((*componentAssignments)[i]->getVariable()->getUID() == uid)
    00129                         return (*componentAssignments)[i];
    00130         }
    00131         return NULL;
    00132 }
    00133
    00135
    00136 void Estimate::addAssignment( BaseModelAssignment * asg )
    00137 {
    00138         assert( asg->getVariable()->getVariableType() == BaseModelVariable::State );
    00139         const_cast< BaseModelAssignmentArray * >( componentAssignments )->pushBack( asg );
    00140 }
    00141
    00143
    00144 ostream&
    00145 Mers::Estimation::Discrete::operator<<(ostream& os, Estimate const & est)
    00146 {
    00147         int p = os.precision();
    00148
    00149         os << "{" << setprecision(4) << est.posteriori << ": " << setprecision(p);
    00150         for(unsigned int i=0; i<est.componentAssignments->getLength(); i++)
    00151         {
    00152                 os << "\n\t(" << (*est.componentAssignments)[i]->getVariable()->getName() << "[" << (*est.componentAssignments)[i]->getVariable()->getUID() << "]";
    00153                 os << " = " << (*est.componentAssignments)[i]->getValue()->getName() << "[" << (*est.componentAssignments)[i]->getValue()->getUID() << "])";
    00154                 if(i != est.componentAssignments->getLength()-1)
    00155                         os << " ";
    00156         }
    00157         os << "}";
    00158
    00159         return os;
    00160 }
    00161         //-----------------------------------------------
    00162         //      PRIVATE METHODS
    00163         //-----------------------------------------------
    00164