示例#1
0
void process_input(struct partition_op &partop) {
	// build in memory Tree
	VecStreamReader vecstream(&vec);
	IStorageManager* memoryFile = StorageManager::createNewMemoryStorageManager();
	id_type indexIdentifier = 0;

	ISpatialIndex* tree = RTree::createAndBulkLoadNewRTree(RTree::BLM_STR, 
		vecstream, *memoryFile, fillFactor, indexCapacity, partop.bucket_size, 
		2, SpatialIndex::RTree::RV_RSTAR, indexIdentifier);

	// Traverse through each leaf node which is a partition
	MyQueryStrategy qs(&partop);
	tree->queryStrategy(qs);

	#ifdef DEBUGAREA
	Rectangle *tmp;
	double span[2];
	double area_total;

	for (int k = 0; k < 2; k++) {
		span[k] = partop.high[k] - partop.low[k];
	}

	// Normalize the data
	for (vector<Rectangle*>::iterator it = list_rec.begin() ; it != list_rec.end(); ++it) {
		tmp = *it;
		tmp->low[0] = (tmp->low[0] - partop.low[0]) / span[0];
        	tmp->low[1] = (tmp->low[1] - partop.low[1]) / span[1];
        	tmp->high[0] = (tmp->high[0] - partop.low[0]) / span[0];
        	tmp->high[1] = (tmp->high[1] - partop.low[1]) / span[1];
        	area_total += (tmp->high[0] - tmp->low[0]) * (tmp->high[1] - tmp->low[1]);
	}

	cerr << "Area total: " << area_total << endl;
        if (list_rec.size() > 0) {
                cerr << "Area covered: " << findarea(list_rec) << endl;
        }

	for (vector<Rectangle*>::iterator it = list_rec.begin() ; it != list_rec.end(); ++it) {
		delete *it;
	}
	list_rec.clear();
	#endif 
	/* This will fail the code the Data is already removed by VecStreamReader
	for (vector<RTree::Data*>::iterator it = vec.begin() ; it != vec.end(); ++it) {
		delete *it;
	}*/
	vec.clear();
	delete tree;
	delete memoryFile;
}
示例#2
0
文件: main.cpp 项目: Dongyuyang/ann
int main(int argc, char** argv)
{
	if (argc != 4)
	{
		cerr << "Usage: " << argv[0] << " dim n area." << endl;
		return -1;
	}

	int dim = atol(argv[1]);
	int n = atol(argv[2]);
	double area = atof(argv[3]);
	if(dim <= 0)
	{
		cerr << "Dimension should be larger than 0." << endl;
		return -1;
	}
	if(n <= 0)
	{
		cerr << "The number of query points should be larger than 0." << endl;
		return -1;
	}
	if(area <= 0 || area > 1)
	{
		cerr << "the area of query points should be in (0, 1]." << endl;
		return -1;
	}

	/*read static data set*/
	vector <Point> P;
	ifstream in("../data.ini");
	if(!in)
	{
		cerr << "Cannot open file data.ini.\n";
		return -1;
	}
 	P = readPoints(in, dim);
	uint32_t N = P.size();

	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++;
		}
		/*std::cerr << "Operations: " << N << std::endl;
		std::cerr << *tree;
		std::cerr << "Buffer hits: " << file->getHits() << std::endl;
		std::cerr << "Index ID: " << indexIdentifier << std::endl; 
		bool ret = tree->isIndexValid();
		if (ret == false) std::cerr << "ERROR: Structure is invalid!" << std::endl;
		else std::cerr << "The stucture seems O.K." << std::endl; */


	for(uint32_t loop = 1; loop <= LOOPNUM; ++loop)
	{
		cout << "/**************** BEGIN " << loop << " ***************/" << endl;

		/*generate query set*/
		vector <Point> Q;
		//Q = genPoints(dim, n, area, loop);
		stringstream ss;
		ss << "../query/n" << n << "M" << area << "/loop" << loop;
		cout << ss.str().c_str() << endl;
		ifstream qin(ss.str().c_str());
		if(!qin)
		{
			cerr << "Cannot open query file";
			return -1;
		}
		Q = readPoints(qin, dim);
		
		/*************** BEGIN MQM method ******************/
		MQM(tree, Q, n, FUN); // MQM method for finding ANN of Q
		/*************** END MQM method *******************/


		/*************** BEGIN ADM method ******************/
		CATCH cost1;
		cost1.catch_time();

		vector <uint32_t> nnIDs = nearestNeighborSet(tree, Q, n); // find the NN for every qi in Q as qi'
		vector <Point> newQ;
		for(uint32_t i = 0; i < n; ++i)
		{
			newQ.push_back(P[nnIDs[i]]);
		}
		cost1.catch_time();
		cout << "proposal method: cpu cost for finding NNs of Q as Q' is " << cost1.get_cost(2) << " millisecond(s)" << endl;

		/***** read dist-matrix index for Q' ********/
		uint32_t maxK = P.size() / RATIO; // the length of dist-matrix index
		uint32_t * dmindex[n];
		for(uint32_t i = 0; i < n; ++i)
	 	{ // read the dist-matrix index of qi'
			dmindex[i] = readDMIndex(nnIDs[i], maxK);
			if (dmindex[i] == NULL)
			{
				cerr << "error for loading Dist-Matrix Index." << endl;
				return -1;
			}
		}

		double minadist  = 0;
		/* ADM method for finding approxiamte ANN  */
		Point adm_ANN = ADM(newQ, n, P, N, dmindex, maxK, FUN, minadist, ERROR_RATE);
		cout << "ADM: best_dist is " << getAdist(adm_ANN, Q, n, FUN) << endl << endl; 
		/*************** END ADM method *******************/

		/*************** BEGIN approxiamte vp-ANN method ******************/
		/* approximate vp-ANN method for finding ANN of Q'*/
		CATCH cost2;
		cost2.catch_time();

		Point centroid = findCentroid(Q, dim, n);
		minadist = getAdist(centroid, Q, n, FUN);
		uint32_t vpID = nearestNeighbor(tree, centroid);
		uint32_t best_id_Q = epsilonANN(Q, n, P, N, vpID, dmindex, maxK, FUN); 

		cost2.catch_time();
		cout << "approxiamte vp-ANN method: cpu cost is " << cost2.get_cost(2) << " millisecond(s)" << endl;
		cout << "approximate vp-ANN method: best_dist is " << getAdist(P[best_id_Q], Q, n, FUN) << endl;
		cout << "approxiamte vp-ANN method: best_NN is ";
		displayCoordinates(P[best_id_Q]); 
		cout << endl;
		/*************** END approxiamte vp-ANN method *******************/


		/*************** BEGIN BF MBM method ******************/
		/* MBM method for finding ANN of Q */
		CATCH mbmcost;
		mbmcost.catch_time();

		Region M = getMBR(Q, dim, n);
		MyQueryStrategy qs = MyQueryStrategy(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 *******************/


		/*************** BEGIN brute method ******************/
		/* brute method for finding ANN of Q*/
		CATCH brute_cost;
		brute_cost.catch_time();

		uint32_t ANNid = brute_ANN(Q, n, P, N, FUN);

		brute_cost.catch_time();
		cout << "brute method: cpu cost is " << brute_cost.get_cost(2) << " millisecond(s)" << endl;
		double adist = getAdist(P[ANNid], Q, n, FUN);
		cout << "brute method: best_dist is " << adist << endl;
		cout << "brute method: best_NN is ";
		displayCoordinates(P[ANNid]);
		/*************** END brute 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;
}
示例#3
0
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;
}
示例#4
0
int main(int argc, const char * argv[]) {
    ParsePointFile();

    try
    {
        string basename("RTREE.DATA");
        IStorageManager *diskfile = StorageManager::loadDiskStorageManager(basename);
        StorageManager::IBuffer* file = StorageManager::createNewRandomEvictionsBuffer(*diskfile, 10, false);
        ISpatialIndex* tree = RTree::loadRTree(*file, 1);


        if (!tree->isIndexValid()) {
            cout << "Bad Tree" <<endl;
            return -1;
        }

        IStatistics *sts;
        tree->getStatistics(&sts);

        int MAX_LEVEL = dynamic_cast<RTree::Statistics*>(sts)->getTreeHeight();
        //Operation on MBR in RTree level by level
        for (int m_level = 0; m_level < MAX_LEVEL; ++m_level) {
            LevelStrategy ls(m_level);
            tree->queryStrategy(ls);
        }

        delete sts;

#ifdef DEBUG_TEST
        CheckStrategy cs;
        tree->queryStrategy(cs);
#endif

        ////========== Start Multi-Treads =========================================
        ////if the number of trajectories(points) is small, thread may be suspended
        boost::thread_group threads;
        for (int i=0; i<QUERY_SIZE; ++i) {
            threads.create_thread(boost::bind(&NN_RST::Put, rst_vector[i],  dynamic_cast<RTree::RTree*>(tree)));
        }

        Taxi_Point p;
        double time;

        for (int i=0; i< QUERY_SIZE; ++i) {
            p = point_set[rst_vector[i]->Get()];
            time = p.getMinimumDistance(*rst_vector[i]->query) /  p.m_speed;
            Q.push(QEntry(i, p.traj_id, time));
        }

        // No need to invoke AllCoverTest in every loop, so check_flag is set
        int check_flag=0;
        while(true)
        {
            QEntry entry = Q.top();
            Q.pop();

            p = point_set[rst_vector[entry.q_id]->Get()];
            time = ComputePTime(p, *dynamic_cast<Point*>(rst_vector[entry.q_id]->query));
            Q.push(QEntry(entry.q_id, p.traj_id, time));


            if (!CTestHelper[entry.t_id].test(entry.q_id)) {
                if (Candidates.count(entry.t_id) == 0)
                    Candidates[entry.t_id] = time;
                else
                    Candidates[entry.t_id] += time;
            }

            //        C.push_back(entry.t_id);
            if (CTestHelper.count(entry.t_id) == 0)
            {
                bitset<QUERY_SIZE> bt;
                bt.set(entry.q_id);
                CTestHelper[entry.t_id] = bt;
            }
            else
            {
                CTestHelper[entry.t_id].set(entry.q_id);
            }

            ++check_flag;

            if (check_flag > QUERY_K*100) {
                if (AllCoverTest())
                    break;
                else
                    check_flag = 0;

            }
        }

        //  set continued to false to stop all the other threads
        {
            boost::mutex::scoped_lock lock(bool_mutex);
            continued = false;

        }

        for (int i=0; i<QUERY_SIZE; ++i)
            rst_vector[i]->cond.notify_one();

        threads.join_all();

        //======= Multi-Threads are stopped ==================================

        //    cout << Q.size() <<endl;
        //    while (!Q.empty()) {
        //        cout << Q.top().time<<endl;
        //        Q.pop();
        //    }

        //=====   Refine && Verification =====================================

        double LargestK = -1.0;
        int LargestPos = -1;

        ComputeLargestK(LargestK, LargestPos);

        for (int traj_id : partial_covering)
        {
            double time = LowBound(traj_id);
            if (time < LargestK) {
                Candidates[LargestPos] = traj_id;
                ComputeLargestK(LargestK, LargestPos);
            }
        }

        // now all_covering is the final correct answer
        for(int traj_id : all_covering)
            cout << traj_id << endl;


        delete tree;
        delete file;
        delete diskfile;
        return 0;
    }
    catch (Tools::Exception& e)
    {
        cerr << "******ERROR******" << endl;
        std::string s = e.what();
        cerr << s << endl;
        return -1;
    }
    catch (...)
    {
        cerr << "******ERROR******" << endl;
        cerr << "other exception" << endl;
        return -1;
    }
}