// Inverts the permutation on a given b-vector. // Necessary to get a b-vector that match the original data static void _FinalizeBVector(vnl_vector<double> &b, vnl_vector<unsigned int> &perm, int cols) { vnl_vector<double> tempB(cols+1); tempB.fill(0); for (unsigned int c = 0; c < perm.size(); ++c) { tempB(perm(c)) = b(c); } b = tempB; }
void get_correlation2d(const cv::Mat &A, const cv::Mat &B, cv::Mat &corr) { IMP_LOG_VERBOSE("Computing 2D correlation " <<std::endl); IMP_USAGE_CHECK(((A.rows==B.rows) && (A.cols == B.cols)), "em2d:get_correlation2d: Matrices have different size."); // resize the output array if needed corr.create(A.rows,A.cols, A.type()); cv::Size dftSize; // compute the optimal size for faster DFT transform dftSize.width = cv::getOptimalDFTSize(A.cols); dftSize.height = cv::getOptimalDFTSize(A.rows); // temporary matrices cv::Mat tempA(dftSize, A.type(), cv::Scalar::all(0)); cv::Mat tempB(dftSize, B.type(), cv::Scalar::all(0)); // copy A and B to the top-left corners of tempA and tempB, respectively cv::Mat roiA(tempA, cv::Rect(0,0,A.cols,A.rows)); A.copyTo(roiA); cv::Mat roiB(tempB, cv::Rect(0,0,B.cols,B.rows)); B.copyTo(roiB); // FFT the padded A & B in-place; // use "nonzeroRows" hint for faster processing cv::dft(tempA, tempA, 0, A.rows); cv::dft(tempB, tempB, 0, B.rows); // multiply the spectrums; // the function handles packed spectrum representations well cv::mulSpectrums(tempA, tempB, tempA,0,true); // inverse transform // Even though all the result rows will be non-zero, // we need only the first corr.rows of them, and thus we // pass nonzeroRows == corr.rows cv::dft(tempA, tempA, cv::DFT_INVERSE + cv::DFT_SCALE, corr.rows); // now copy the result back to C. tempA(cv::Rect(0, 0, corr.cols, corr.rows)).copyTo(corr); do_matrix_to_image_flip(corr); }
ScenarioResult * ScenarioProblem::simProblem(){ ScenarioResult * sr = new ScenarioResult(_pd); int seed = _pd->getSeed() + _scenario*100000 + _trial*33000; int subSeed; if(!_pd->getSubProblemGenerated()){ _simSP.generateSubProblem(0,_pd,_scenario); } // IloEnv env; // _pd->setEnv(env); IloEnv env = _pd->getEnv(); IloModel mod(env); addSimProblem(mod); try { IloCplex cplex(mod); cplex.setOut(env.getNullStream()); Tree<Node> ref; ref.buildTree( _pd->getOutcomes(), _pd->getStages()); Grid* g = _pd->getGrid(); int E = _pd->getGrid()->getBranchSize(); int V = _pd->getGrid()->getBusSize(); IloNum temp; IloNumArray tempA(env); IloNumArray tempB(env); double R; double L = _pd->getEffectiveDistribution(); double U; double f; double fabs; double alpha; double x; int parent; double tempCount; int isDone; //fix x variables for(int j=0;j<E; j++){ _simSP.setOutage(j, false); } //add outages for(int j=0; j<_pd->getNumberOutages(_scenario) ; j++){ _simSP.setOutage(_pd->getOutages(_scenario,j), true); } for(int i=0;i<ref.getSize();i++){ subSeed = seed + i*1000; parent= ref[i]->getParent(); if( i!= 0 && _pd->getEnergyStorage() ){//storage logic //cout<<"\ni:"<<i<<",pnt:"<<parent<<",es:"<<sr->getETotal(parent); for(int j=0; j<V; j++){ _simSP.setPriorES(j, sr->getE( parent, j ) ); } } if( i!=0 ) isDone = (int)sr->getDone( parent ); else isDone = 0; if( isDone ){ sr->updateDone(i, 1); sr->updateDTotal(i,sr->getDTotal(parent)); sr->updatePDeltaTotal(i,0); sr->updateXTotal(i,sr->getXTotal(parent)); } else{ tempCount=0; for(int j=0; j<E; j++){ //set branch outages if( i!=0) { //OUTAGE LOGIC if not root node x = sr->getX(parent,j); if( x == 0 ){ //branch not out srand(subSeed + j); alpha = rand() / double(RAND_MAX); //uniform 0-1 U = g->getBranchCapacity(j); R = L + (1-L)*alpha; R = R*U; alpha = rand() / double(RAND_MAX); //uniform 0-1 if ( alpha > _pd->getProbability() ) R = U + 1; f = sr->getF(parent,j); if( f > 0 ) fabs = f; else fabs = -f; //outage branches if( fabs > R) { _simSP.setOutage(j,true); tempCount += 1; } else _simSP.setOutage(j,false); } else{ _simSP.setOutage(j,true); } } } if(tempCount>0) sr->updateDone(i,0); else if(tempCount==0 && i != 0) sr->updateDone(i,1); else sr->updateDone(i,0); if(cplex.solve()){ // temp = cplex.getObjValue(); temp = cplex.getValue( _simSP.getSumD() ); sr->updateDTotal(i,temp); cplex.getValues(tempA, _simSP.getD() ); tempCount=0; for(int j=0; j<V; j++){ sr->updateD(i, j,tempA[j]); //bus injects } cplex.getValues(tempA, _simSP.getP() ); tempCount=0; for(int j=0; j<V; j++){ sr->updateP(i, j,tempA[j]); //bus injects if( i!=0 ){ temp = tempA[j]; temp -= sr->getP( parent, j); sr->updatePDelta(i,j,temp); if(temp < 0) tempCount -= 0;//temp; else tempCount += temp; } } sr->updatePDeltaTotal(i,tempCount); cplex.getValues(tempA, _simSP.getF() ); for(int j=0; j<E; j++){ sr->updateF(i,j, tempA[j]); //branch flows } cplex.getValues(tempA, _simSP.getX() ); tempCount=0; for(int j=0; j<E; j++){ sr->updateX(i,j, tempA[j]); //branch flows tempCount+= tempA[j]; } sr->updateXTotal(i,tempCount); if(_pd->getEnergyStorage() ){ cplex.getValues(tempA, _simSP.getE() ); for(int j=0; j<V; j++){ sr->updateE(i,j, tempA[j]); //branch flows } temp = cplex.getValue( IloSum(_simSP.getEDeltaPlus() ) ); sr->updateEDeltaPlus(i, temp); temp = cplex.getValue( IloSum(_simSP.getEDeltaMinus() ) ); sr->updateEDeltaMinus(i, temp); temp = cplex.getValue( IloSum(_simSP.getE() ) ); sr->updateETotal(i, temp); } } } } // mod.end(); // env.end(); } catch (IloException e) { cout << "An exception occurred. Exception Nr. " << e << endl; } return sr; }