Пример #1
0
vector<double>	dcMatrix::extractRow_cond_minElement(unsigned int j_col)
{
	if (j_col >= nbCols) {
		cout << endl << " ERROR [dcMatrix::extractRow_cond_minElement]:cannot extract row("<< j_col;
		cout << ") greater than size (0--"<< nbCols-1<< ")!"<<endl;
		exit(1);
	}
	
	vector<double> col_select = extractColumn(j_col);
	
	//argminElementVector(col_select); DOESN'T WORK WHEN #include "dcTools.h" ???
	
	double min = 999999999;
	unsigned int argmin = 0;
    
    for (unsigned int i=0; i<col_select.size(); i++) 
	{
        if (min > col_select[i]) 
		{
            min = col_select[i];
			argmin = i;
        }
    }
	
	return extractRow(argmin);
}
/*
  Extract the current version of the row or column from the CoinPostsolveMatrix
  into a CoinPackedVector, sort it, and compare it to the stored
  CoinPackedVector. Differences are reported to std::cout.
*/
void CoinPresolveMonitor::checkAndTell (const CoinPostsolveMatrix *mtx)
{
  CoinPackedVector *curVec = 0 ;
  const double *lbs = 0 ;
  const double *ubs = 0 ;
  if (isRow_) {
    lbs = mtx->getRowLower() ;
    ubs = mtx->getRowUpper() ;
    curVec = extractRow(ndx_,mtx) ;
  } else {
    curVec = extractCol(ndx_,mtx) ;
    lbs = mtx->getColLower() ;
    ubs = mtx->getColUpper() ;
  }
  checkAndTell(curVec,lbs[ndx_],ubs[ndx_]) ;
}
/*
  Constructor to initialise from a CoinPostsolveMatrix
*/
CoinPresolveMonitor::CoinPresolveMonitor (const CoinPostsolveMatrix *mtx,
					  bool isRow,
					  int k)
{
  ndx_ = k ;
  isRow_ = isRow ;

  if (isRow) {
    origVec_ = extractRow(k,mtx) ;
    const double *blow = mtx->getRowLower() ;
    lb_ = blow[k] ;
    const double *b = mtx->getRowUpper() ;
    ub_ = b[k] ;
  } else {
    origVec_ = extractCol(k,mtx) ;
    const double *lb = mtx->getColLower() ;
    lb_ = lb[k] ;
    const double *ub = mtx->getColUpper() ;
    ub_ = ub[k] ;
  }
  origVec_->sortIncrIndex() ;
}
Пример #4
0
    void PeakListCollection::applyMasterTimeScale(DBL_MATRIX mts)
    {
        int dim = c_.size();
        int n_points = mts.rowCount();

        if(dim != mts.columnCount()){
            MSPP_LOG(logERROR) << "PeakListCollection::applyMasterTimeScale(): The Master Time Scale's dimension differs from the number of Peak Lists!" << std::endl;
        }
        mspp_precondition(dim == mts.columnCount() , "PeakListCollection::applyMasterTimeScale(): The Master Time Scale's dimension differs from the number of Peak Lists!");

        if(n_points < 2){
            MSPP_LOG(logERROR) << "PeakListCollection::applyMasterTimeScale(): Master Time Scale does not provide enough sampling points!" << std::endl;
        }
        mspp_precondition(dim == mts.columnCount() , "PeakListCollection::applyMasterTimeScale(): Master Time Scale does not provide enough sampling points!");


        //Correct each dimension by mts
        for(int i = 0; i < dim; i++){

            int length = c_[i].size();

            //process each Peak in PeakList i
            for(int j = 0; j < length; j++){
                double oldRt = c_[i][j].getRt();

                //search for interval of MTS to do interpolation
                if(mts(0,i)>oldRt){
                    MSPP_LOG(logWARNING) << "PeakListCollection::applyMasterTimeScale(): Could not correct Peak " << j << " in PeakList " << i << "! Rt-Value not covered by Master Time Scale." << std::endl;
                    continue;
                }
                int mts_index = 0;
                while(true){
                    mts_index++;
                    if(mts_index>=n_points){
                        MSPP_LOG(logWARNING) << "PeakListCollection::applyMasterTimeScale(): Could not correct Peak " << j << " in PeakList " << i << "! Rt-Value not covered by Master Time Scale." << std::endl;
                        break;
                    }
                    if(mts(mts_index,i)>oldRt){
                        break;
                    }
                }
                if(mts_index>=n_points){
                    continue;
                }

                //interval is now [mts(mts_index-1) , mts(mts_index)] =: [x0 , x1]
                //now do linear interpolation: x = x0 + (x1-x0)/(x1[i]-x0[i])*(oldRt-x0[i])

                //calculate interpolated point x on MTS with x[i] = oldRt
                DBL_MATRIX x0 = extractRow(mts,mts_index-1).transpose();
                DBL_MATRIX x1 = extractRow(mts,mts_index).transpose();
                DBL_MATRIX x = x0 + (x1 - x0)/(x1(i,0) - x0(i,0))*(oldRt - x0(i,0));


                //create unit vector in bisectional line direction
                DBL_MATRIX uBisec (dim,1,1./sqrt(double(dim)));

                //calculate distance vector to bisectional line
                DBL_MATRIX tmpProd = uBisec.transpose()*x;
                double dotProd = tmpProd(0,0);
                DBL_MATRIX dist = dotProd*uBisec - x;

                //update Rt value of current Peak: rt = oldRt + dist[i]
                c_[i][j].setRt(oldRt + dist(i,0));
            }
        }
        return;
    }
Пример #5
0
    DBL_MATRIX Alignment::lineDistort_(DBL_MATRIX& linePoints, DBL_MATRIX& y, DBL_MATRIX& rt_vec, double c1, double c2,  DBL_MATRIX& alpha)
    {
        if(alpha.columnCount() != 1){
            MSPP_LOG(logERROR) << "Alignment::lineDistort_(): Wrong dimensionality of input - alpha must be a column vector!" << std::endl;
        }
        mspp_precondition(alpha.columnCount() == 1 , "Alignment::lineDistort_(): Wrong dimensionality of input - alpha must be a column vector!");
        
        if(linePoints.columnCount() != y.columnCount()-1){
            MSPP_LOG(logERROR) << "Alignment::lineDistort_(): Dimension mismatch - the dimension of linePoints must be one less than the dimension of y!" << std::endl;
        }
        mspp_precondition(alpha.columnCount() == 1 , "Alignment::lineDistort_(): Dimension mismatch - the dimension of linePoints must be one less than the dimension of y!");
        
        if(alpha.rowCount() != linePoints.rowCount()){
            MSPP_LOG(logERROR) << "Alignment::lineDistort_(): Dimension mismatch - the length of alpha does not match the given data!" << std::endl;
        }
        mspp_precondition(alpha.columnCount() == 1 , "Alignment::lineDistort_(): Dimension mismatch - the length of alpha does not match the given data!");

        if(linePoints.columnCount() != rt_vec.columnCount()-1){
            MSPP_LOG(logERROR) << "Alignment::lineDistort_(): Dimension mismatch - rt_vec must be 1 dimension larger than linePoints!" << std::endl;
        }
        mspp_precondition(linePoints.columnCount() == rt_vec.columnCount()-1 , "Alignment::lineDistort_(): Dimension mismatch - rt_vec must be 1 dimension larger than linePoints!");

        if(y.columnCount() != rt_vec.columnCount()){
            MSPP_LOG(logERROR) << "Alignment::lineDistort_(): Dimension mismatch - rt_vec and y must have equal dimensionality!" << std::endl;
        }
        mspp_precondition(y.columnCount() == rt_vec.columnCount() , "Alignment::lineDistort_(): Dimension mismatch - rt_vec and y must have equal dimensionality!");

        //get number of dimensions
        int dim = rt_vec.columnCount(); 
        int size = linePoints.rowCount();


        //go through points in line
        for(int u = 0; u < size; u++){
            MSPP_LOG(logINFO) << "Alignment: Processing line point " << u+1 << "/" << size << std::endl;
            //calculate distance vector between current point u and next point u+1
            std::vector<double> dist (dim-1);
            if(u < size-1){
                for(int i = 0; i < dim-1; i++){
                    dist[i] = linePoints(u+1,i) - linePoints(u,i);
                }
            }

            //init stepsize lambda
            double lambda = 1;

            //get current support vector
            DBL_MATRIX y_curr = extractRow(y,u).transpose();
            
            //get current line points
            DBL_MATRIX linePoints_curr = extractRow(linePoints,u).transpose();
            
            //init iteration counter
            int ITER = 0;

            while(ITER < 10000){
                ITER++;
                if(ITER==1000){
                    MSPP_LOG(logDEBUG) << "Alignment::lineDistort_(): Algorithm probably does not converge!" << std::endl;
                }
                if(ITER==9999){
                    MSPP_LOG(logERROR) << "Alignment::lineDistort_(): Too many iterations. Algorithm does not converge!" << std::endl;
                    
                }

                //calculate negative gradient on hyperplane
                DBL_MATRIX proj_vec = -Alignment::gradOnHPlane_(linePoints_curr,y_curr,rt_vec,alpha(u,0));

                //calculate vector with length 1 pointing in gradient direction
                double proj_vec_norm = 0;
                for(int i = 0; i<proj_vec.rowCount(); i++){
                    proj_vec_norm += proj_vec(i,0)*proj_vec(i,0);
                }
                proj_vec_norm = sqrt(proj_vec_norm);

                DBL_MATRIX d (proj_vec.rowCount(),1);
                for(int i = 0; i<proj_vec.rowCount(); i++){
                    d(i,0) = proj_vec(i,0)/proj_vec_norm;
                }
               
                //compute new stepsize
                lambda = Alignment::stepSizeND_(linePoints_curr,y_curr,d,lambda,c1,c2,rt_vec,alpha(u,0));
                
                //new position of current point u
                DBL_MATRIX x_new (d.rowCount(),1);
                for(int i = 0; i<d.rowCount(); i++){
                    x_new(i,0) = linePoints_curr(i,0) + lambda*d(i,0);
                }
                
                //termination condition
                double mean_change = 0;
                for(int i = 0; i<x_new.size(); i++){
                    mean_change += abs(x_new(i,0) - linePoints_curr(i,0));
                } 
                
                //update current linepoints
                linePoints_curr = x_new;

                //double cond = (0.1*lambda>1e-5 ? 0.1*lambda : 1e-5);
                double cond = 1e-3;
                
                if(mean_change < cond){
                    break;
                };                   

            }
            
            for(int i = 0; i<linePoints_curr.rowCount(); i++){
                linePoints(u,i) = linePoints_curr(i,0);
            }

            //shift next point u+1 to a better start position
            if(u < linePoints.rowCount()-1){
                for(unsigned int i = 0; i<dist.size(); i++){
                    linePoints(u+1,i) = (linePoints(u+1,i) + linePoints(u,i) + dist[i])/2.;
                }                    
            }
        }
        

        //Output of line distortion
        DBL_MATRIX newLinePoints (linePoints.rowCount(),linePoints.columnCount()+1);

        //calculate last component
        for(int i = 0; i<linePoints.rowCount(); i++){
            double sum_y = 0;
            for(int k = 0; k<y.columnCount();k++){
                sum_y += y(i,k);
            }
            
            double sum_lp = 0;
            for(int k = 0; k<linePoints.columnCount();k++){
                sum_lp += linePoints(i,k);
            }
            
            double z = sum_y - sum_lp;
            
            for(int k = 0; k<newLinePoints.columnCount()-1;k++){
                newLinePoints(i,k) = linePoints(i,k);
            }
            
            newLinePoints(i,newLinePoints.columnCount()-1) = z;

        }

        //check monotonicity of MTS
        bool sane = true;
        for(int i = 0; i < newLinePoints.columnCount(); i++){
            for(int j = 1; j < newLinePoints.rowCount(); j++){
                if(newLinePoints(j,i) < newLinePoints(j-1,i)){
                    sane = false;
                }
            }
        }
        if(!sane){
            std::cout << "WARNING: Master Time Scale is not monotonous. Results may be incorrect!" << std::endl;
        }

        return newLinePoints;

    }