ConstraintSystem.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: ConstraintSystem.cpp 2939 2011-10-23 19:55:11Z yanweignss $"
00002 
00008 //============================================================================
00009 //
00010 //  This file is part of GPSTk, the GPS Toolkit.
00011 //
00012 //  The GPSTk is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU Lesser General Public License as published
00014 //  by the Free Software Foundation; either version 2.1 of the License, or
00015 //  any later version.
00016 //
00017 //  The GPSTk is distributed in the hope that it will be useful,
00018 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //  GNU Lesser General Public License for more details.
00021 //
00022 //  You should have received a copy of the GNU Lesser General Public
00023 //  License along with GPSTk; if not, write to the Free Software Foundation,
00024 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 //
00026 //  Wei Yan - Chinese Academy of Sciences . 2011
00027 //
00028 //============================================================================
00029 
00030 #include "ConstraintSystem.hpp"
00031 #include <vector>
00032 
00033 namespace gpstk
00034 {
00035       // Remove a single constraint
00036    ConstraintSystem& ConstraintSystem::removeConstraint(
00037                                                   const Constraint& constraint )
00038    {
00039       ConstraintList backupList;
00040       
00041       for(ConstraintList::iterator it= constraintList.begin();
00042          it!=constraintList.end();
00043          ++it)
00044       {
00045          bool isEqual(true);
00046 
00047          if(it->header.prefit!=constraint.header.prefit) isEqual=false;
00048          if(it->header.variance!=constraint.header.variance) isEqual=false;
00049          if(it->body != constraint.body) isEqual = false;
00050 
00051          if( !isEqual ) backupList.push_back(*it);
00052       }
00053 
00054       clearConstraint();
00055       
00056       for(ConstraintList::iterator it= backupList.begin();
00057          it!=backupList.end();
00058          ++it)
00059       {
00060          addConstraint(*it);
00061       }
00062       
00063       return (*this);
00064 
00065    }  // End of method 'ConstraintSystem::removeConstraint()'
00066 
00067 
00068       // Method to set multi-constraints
00069    ConstraintSystem& ConstraintSystem::setConstraint(const VariableSet& varSet, 
00070                                                    const Vector<double>& prefit)
00071    {
00072       // Fist, we check the size of inputs
00073       const int size = varSet.size();
00074 
00075       if( prefit.size()!=size )
00076       {
00077          Exception e("The input size doesn't match.");
00078          GPSTK_THROW(e);
00079       }
00080 
00081       clearConstraint();
00082 
00083       int i(0);
00084       
00085       for(VariableSet::const_iterator it = varSet.begin();
00086          it != varSet.end();
00087          ++it)
00088       {
00089          Constraint constraint;
00090          constraint.header.prefit = prefit[i];
00091          constraint.body[*it] = 1.0;
00092 
00093          addConstraint(constraint);
00094    
00095          i++;
00096       }
00097 
00098       return (*this);
00099 
00100    }  // End of method 'ConstraintSystem::setConstraint()'
00101 
00102 
00103       // Method to set multi-constraints
00104    ConstraintSystem& ConstraintSystem::setConstraint(const VariableSet& varSet,
00105                                                    const Vector<double>& prefit,
00106                                                    const Matrix<double>& design)
00107    {
00108       // Fist, we check the size of inputs
00109       const int size = varSet.size();
00110       
00111       if( (prefit.size()!=size) || design.rows()!=size || design.cols()!=size)
00112       {
00113          Exception e("The input size doesn't match.");
00114          GPSTK_THROW(e);
00115       }
00116       
00117       clearConstraint();
00118 
00119       std::vector<Variable> vars;
00120       for(VariableSet::const_iterator it = varSet.begin();
00121          it != varSet.end();
00122          ++it)
00123       {
00124          vars.push_back(*it);
00125       }
00126       
00127       for(int i=0; i<vars.size(); i++)
00128       {
00129          VariableDataMap dataMap;
00130          for(int k=0;k<size;k++)
00131          {
00132             if(design[i][k]!=0.0) dataMap[ vars[k] ] = design[i][k];
00133          }
00134 
00135          Constraint constraint;
00136          constraint.header.prefit = prefit[i];
00137          constraint.body = dataMap;
00138 
00139          addConstraint(constraint);
00140       }
00141       
00142       return (*this);
00143 
00144       
00145    }  // End of method 'ConstraintSystem::setConstraint()'
00146 
00147 
00148 
00149    ConstraintSystem& ConstraintSystem::constraintMatrix(
00150                                                     const VariableSet& allVar,
00151                                                     Vector<double>& prefit,
00152                                                     Matrix<double>& design,
00153                                                     Matrix<double>& covariance )
00154       throw(InvalidConstraintSystem)
00155    {
00156       const int rowSize = constraintList.size();
00157       const int colSize = allVar.size();
00158 
00159       prefit.resize(rowSize,0.0);
00160       design.resize(rowSize,colSize,0.0);
00161       covariance.resize(rowSize,rowSize,0.0);
00162       
00163       int irow(0);
00164 
00165       for(ConstraintList::iterator it=constraintList.begin();
00166          it!=constraintList.end();
00167          ++it)
00168       {
00169          prefit[irow] = it->header.prefit;
00170          covariance[irow][irow] = it->header.variance;
00171          
00172          VariableDataMap dataMap = it->body;
00173          
00174          
00175          for(VariableDataMap::iterator itv=dataMap.begin();
00176             itv!=dataMap.end();
00177             ++itv)
00178          {
00180             VariableSet::const_iterator itt = allVar.find(itv->first);
00181             if(itt==allVar.end())
00182             {
00183                InvalidConstraintSystem e("The variable not exist in the input");
00184                GPSTK_THROW(e);
00185             }
00186 
00187             int icol(0);
00188             for(VariableSet::const_iterator itt2=allVar.begin();
00189                itt2!=allVar.end();
00190                ++itt2)
00191             {
00192                if(itt == itt2) break;
00193                icol++;
00194             }
00195             
00196             design[irow][icol] = itv->second;
00197          }
00198 
00199 
00200          irow++;
00201       }
00202 
00203       return (*this);
00204 
00205    }  // End of method 'ConstraintSystem::constraintMatrix()'
00206 
00207 
00208       // Add a constraint list
00209    ConstraintSystem& ConstraintSystem::addConstraintList(
00210                                              const ConstraintList& equationList)
00211    {
00212       for(ConstraintList::const_iterator it = equationList.begin();
00213          it != equationList.end();
00214          ++it)
00215       {
00216          addConstraint(*it);
00217       }
00218 
00219       return (*this);
00220    }
00221 
00222 }  // End of namespace gpstk
00223 
00224 
00225 
00226 

Generated on Tue May 22 03:30:57 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1