// ----------------------------------------------------------------------------- void btm::Set::GenerateRandomScore(std::mt19937 & rng) { std::uniform_int_distribution<int> genWinner(1, 2); std::uniform_int_distribution<int> genPoints(0, nb_points_to_win); int winner = genWinner(rng); if (winner == 1) { team1_points = nb_points_to_win; team2_points = genPoints(rng); int diff = team1_points-team2_points; if (diff<2) team1_points = team2_points+2; } if (winner == 2) { team2_points = nb_points_to_win; team1_points = genPoints(rng); int diff = team2_points-team1_points; if (diff<2) team2_points = team1_points+2; } }
extern void run(OpenCVWindow * window) try{ std::vector< cv::Point2d > testPoints = genPoints(); OpenCVScatterItem * item_ = window->insertScatter(testPoints.cbegin(),testPoints.cend()); /*令每个点减去均值*/ { cv::Point2d mean_= std::accumulate(testPoints.begin(),testPoints.end(),cv::Point2d{ 0,0 }); mean_.x/=testPoints.size(); mean_.y/=testPoints.size(); for (auto & point:testPoints) { point-=mean_; } } /*用opencv mat包装数据*/ cv::Mat diff( testPoints.size(),2, CV_64FC1, const_cast<cv::Point2d *>(testPoints.data())); /*保存成MATLAB格式*/ { std::stringstream data_diff; data_diff<<"data_diff" <<cv::format(diff,cv::Formatter::FMT_MATLAB)<<std::endl; QFile outPutFile ( qApp->applicationDirPath() + "/simple_pca_data.m"); outPutFile.open(QFile::Text|QFile::WriteOnly); const auto data__= readAll(data_diff) ; outPutFile.write(data__.first,data__.second); outPutFile.write("\n"); /*MATLAB 使用 princomp进行主成分分析*/ } cv::Mat transpose_diff; cv::transpose(diff,transpose_diff); cv::Mat A=transpose_diff*diff; cv::Mat eigenvalues,eigenvectors; /*计算特征值特征向量*/ cv::eigen(A,eigenvalues,eigenvectors); std::cout<<"eigen values:"<<std::endl <<eigenvalues<<std::endl; /*注意:opencv特征向量按行存储*/ std::cout<<"eigen vectors"<<std::endl <<eigenvectors<<std::endl; std::shared_ptr< std::function<void(QPainter *)> > c_draw_{ new std::function<void(QPainter *)>{ [eigenvectors](QPainter * painter) { draw_axis_array_(painter, eigenvectors.at<double>(0,0),eigenvectors.at<double>(0,1), eigenvectors.at<double>(1,0),eigenvectors.at<double>(1,1), 100, 30,15); }}}; item_->setCentrePointPaint(c_draw_); item_->setWindowTitle(u8"绘制特征向量"_qs); } catch (const cv::Exception &e) { opencv_exception::error(e,"get opencv exception",opencv_line(),opencv_file(),opencv_func()); }
int main(int argc, char** argv) { uint32_t dim = 2; uint32_t n = 8; uint32_t N = 100; /*read static data set*/ vector <Point> P; P = genPoints(dim, N, 1, 0); // displayPset(P); try { IStorageManager* memfile = StorageManager::createNewMemoryStorageManager(); StorageManager::IBuffer* file = StorageManager::createNewRandomEvictionsBuffer(*memfile, 10, false); id_type indexIdentifier; ISpatialIndex* tree = RTree::createNewRTree(*file, 0.7, CAPACITY, CAPACITY, dim, SpatialIndex::RTree::RV_RSTAR, indexIdentifier); id_type id = 0; for(uint32_t i = 0; i < N; ++i) { std::ostringstream os; os << P[i]; std::string data = os.str(); tree->insertData(data.size() + 1, reinterpret_cast<const byte*>(data.c_str()), P[i], id); id++; } for(uint32_t loop = 1; loop <= LOOPNUM; ++loop) { cout << "/**************** BEGIN " << loop << " ***************/" << endl; /*generate query set*/ vector <Point> Q; Q = genPoints(dim, n, 1, loop); /*double pdata1[] = {0, 0}; double pdata2[] = {0, 1}; double pdata3[] = {1, 1}; Point q1(pdata1, dim), q2(pdata2, dim), q3(pdata3, dim); Q.push_back(q1); Q.push_back(q2); Q.push_back(q3); displayPset(Q); */ /*************** BEGIN BF MBM method ******************/ /* MBM method for finding ANN of Q */ CATCH mbmcost; mbmcost.catch_time(); Region M = getMBR(Q, dim, n); MyQueryStrategy qs(M, Q, FUN); tree->queryStrategy(qs); mbmcost.catch_time(); cout << "MBM: cpu cost is " << mbmcost.get_cost(2) << " millisecond(s)" << endl; cout << "MBM: best_dist is " << qs.best_dist << endl; cout << "MBM: best_NN is "; displayCoordinates(qs.best_NN); cout << "MBM: leafIO = " << qs.mbm_leafIO << "; indexIO = " << qs.mbm_indexIO << endl << endl; /*************** END BF MBM method *******************/ cout << "/**************** END " << loop << " ****************/" << endl << endl; } // end loop delete tree; delete file; delete memfile; } catch(Tools::Exception& e) { cerr << "*********ERROR**********" << endl; std::string s = e.what(); cerr << s << endl; return -1; } catch(...) { cerr << "**********ERROR********" << endl; return -1; } return 1; }