示例#1
0
int rtree_insert_test() {
    ISpatialIndex *rtree;
	try {
        // Create a new storage manager with the provided base name and a 4K page size.
        //std::string baseName = "rtree";
        //IStorageManager* diskfile = StorageManager::createNewDiskStorageManager(baseName, 4096);
        memfile = StorageManager::createNewMemoryStorageManager();

        //StorageManager::IBuffer* file = StorageManager::createNewRandomEvictionsBuffer(*memfile, 100, false);
        // applies a main memory random buffer on top of the persistent storage manager
        // (LRU buffer, etc can be created the same way).

        // Create a new, empty, RTree with dimensionality 2, minimum load 70%, using "file" as
        // the StorageManager and the RSTAR splitting policy.
        id_type indexIdentifier;
        rtree = RTree::createNewRTree(*memfile, 0.7, 100, 100, MAX_DIM_NUM, SpatialIndex::RTree::RV_RSTAR, indexIdentifier);

        id_type id;
		uint32_t op;
		double x1, x2, y1, y2, z1, z2;
		double plow[3], phigh[3];

        std::ifstream fin("/root/mbj/data/data");
		while (fin)
		{
			fin >> op >> id >> x1 >> y1 >> z1 >> x2 >> y2 >> z2;
			if (! fin.good()) continue; // skip newlines, etc.

			if (op == RTREE_INSERT)
			{
				plow[0] = x1; plow[1] = y1; plow[2] = z1;
				phigh[0] = x2; phigh[1] = y2; phigh[2] = z2;

				Region r = Region(plow, phigh, 3);

				std::ostringstream os;
				os << r;
				std::string data = os.str();

				rtree->insertData(data.size() + 1, reinterpret_cast<const byte*>(data.c_str()), r, id);

			}
		}

    } catch (Tools::Exception& e) {
		std::cerr << "******ERROR******" << std::endl;
		std::string s = e.what();
		std::cerr << s << std::endl;
		return FALSE;
	}
    return TRUE;
}
示例#2
0
int main(int argc, const char* argv[])
{
	/*
	 *  Build 48 Index with Links
	 */


	// Load Circuit
    Experiment experiment;
    experiment.open(blue_config_filename);
    Microcircuit & microcircuit = experiment.microcircuit();
    const Targets & targets = experiment.targets();
    const Cell_Target target = targets.cell_target("Column");
    microcircuit.load(target, NEURONS | MORPHOLOGIES);

    //Make Neuron Rtrees
    ISpatialIndex *neuronTrees[MORPHOLOGIES_COUNT];
    string *morphologyLabels[MORPHOLOGIES_COUNT];

    int cm=0;
    Morphologies & myMorphologies = microcircuit.morphologies();
    Morphologies::iterator myMorphologiesEnd = myMorphologies.end();
      for (Morphologies::iterator i = myMorphologies.begin(); i != myMorphologiesEnd; ++i)
      {
    	  morphologyLabels[cm] = i->label();
    	  neuronTrees[cm] = RTree::createNewRTree (createNewMemoryStorageManager(), 0.7, 127, 127, 3,RTree::RV_RSTAR,indexIdentifier);
    	  cm++;
      }

    Neurons & myNeurons = microcircuit.neurons();
    Neurons::iterator myNeuronsEnd = myNeurons.end();
    for (Neurons::iterator i = myNeurons.begin(); i != myNeuronsEnd; ++i)
    {
    	cm=0;
    	for (cm=0;cm<MORPHOLOGIES_COUNT;cm++)
    		if (strcmp(i->morphology().label(),morphologyLabels[cm])==0) break;

    	Transform_3D<Micron> trafo = i->global_transform();
    	Sections mySections = i->morphology().all_sections();
    	Sections::iterator mySectionsEnd = mySections.end();
    	for (Sections::iterator s = mySections.begin(); s != mySectionsEnd; ++s)
    	    {
    		 Segments segments = s->segments();
    		 Segments::const_iterator segments_end = segments.end();
    		 for (Segments::const_iterator j = segments.begin(); j != segments_end ; ++j)
    		     {
     			 vect plow, phigh;
     			 get_segment_mbr (*j, trafo, &plow, &phigh);
     			 SpatialIndex::Region mbr = SpatialIndex::Region(plow.data(),phigh.data(),3);

     			 std::stringstream strStream;
     			 strStream << i->gid() <<"-"<< s->id()<< "-" << j->id();
     			 neuronTrees[cm]->insertData (strStream.str().length(), (byte*)(strStream.str().c_str()), mbr, segmentid);
    		     }
    	    }
    }

    // Make Morphology Rtrees
    Morphologies & myMorphologies = microcircuit.morphologies();
    Morphologies::iterator myMorphologiesEnd = myMorphologies.end();
      for (Morphologies::iterator i = myMorphologies.begin(); i != myMorphologiesEnd; ++i)
      {
      	cout << "Indexing Morphology: " << i->label();
      	string baseName = i->label();
        IStorageManager* diskfile = StorageManager::createNewDiskStorageManager(baseName, 4096);
        ISpatialIndex *tree = RTree::createNewRTree (*diskfile, 0.7, 127, 127, 3,RTree::RV_RSTAR,indexIdentifier);
        indexIdentifier++; segmentid=0;

      	Sections mySections = i->all_sections();
      	Sections::iterator mySectionsEnd = mySections.end();
      	for (Sections::iterator s = mySections.begin(); s != mySectionsEnd; ++s)
      	{
            Segments segments = s->segments();
      		Segments::const_iterator segments_end = segments.end();
      		for (Segments::const_iterator j = segments.begin(); j != segments_end ; ++j)
      		{

      			Box<bbp::Micron> Mbr = AABBCylinder::calculateAABBForCylinder(j->begin().center(),
      								  j->begin().radius(),j->end().center(),j->begin().radius());

      			vect plow, phigh;

      			plow[0] = Mbr.center().x() - Mbr.dimensions().x() / 2;
      			phigh[0] = Mbr.center().x() + Mbr.dimensions().x() / 2;
      			plow[1] = Mbr.center().y() - Mbr.dimensions().y() / 2;
      			phigh[1] = Mbr.center().y() + Mbr.dimensions().y() / 2;
      			plow[2] = Mbr.center().z() - Mbr.dimensions().z() / 2;
      			phigh[2] = Mbr.center().z() + Mbr.dimensions().z() / 2;

				SpatialIndex::Region mbr = SpatialIndex::Region(plow.data(),phigh.data(),3);

      			std::stringstream strStream;
      			strStream << s->id()<< "-" << j->id();

      			tree->insertData (strStream.str().length(), (byte*)(strStream.str().c_str()), mbr, segmentid);
      			segmentid++;
      		}
      	}
      	cout << ".. Total Segments: " << segmentid << "\n";
        tree->~ISpatialIndex();
        diskfile->~IStorageManager();
      }


    // PRELOAD the Trees amd Neuron Morphology Mapping


    ISpatialIndex *neurons[NEURONS_COUNT];
    global_transformer *transforms[NEURONS_COUNT];

    int cm=0;
    int cn=0;
    string baseName = "";
    Morphologies & myMorphologies = microcircuit.morphologies();
    Neurons & myNeurons = microcircuit.neurons();

    cout << "PreLoading Mappings \n";
    Morphologies::iterator myMorphologiesEnd = myMorphologies.end();
      for (Morphologies::iterator m = myMorphologies.begin(); m != myMorphologiesEnd; ++m)
      {
    	  baseName = m->label();
    	  m->
          IStorageManager* diskfile = StorageManager::loadDiskStorageManager(baseName);
          trees[cm] = RTree::loadRTree(*diskfile, 1);

          std::cout << "Checking R-tree structure... ";
          if (!trees[cm]->isIndexValid()) std::cerr << "R-tree internal checks failed!\n"; else std::cout << "OK\n";
          IStatistics * tree_stats;
          trees[cm]->getStatistics (&tree_stats);
          cout << *tree_stats;

          Neurons::iterator myNeuronsEnd = myNeurons.end();
          for (Neurons::iterator n = myNeurons.begin(); n != myNeuronsEnd; ++n)
              {
        	  if (strcmp(n->morphology().label().c_str(),m->label().c_str())==0)
        		  {
        		  transforms[cn] = n->global_transform().inverse();
        		  neurons[cn] = trees[cm];
        		  }
        	  cn++;
        	  if (cn>=NEURONS_COUNT) break;
              }
          cn=0;cm++;
      }




	/*
	 *  Query the Index
	 */








}
示例#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
文件: epsMQM.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++;
		}

	//for(uint32_t loop = 1; loop <= LOOPNUM; ++loop)
	for(uint32_t loop = 55; 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 ******************/
		//double err_arr[] = {0, 0.001, 0.005, 0.01, 0.05, 0.1};
		double err_arr[] = {0, 0.01, 0.1};
		for (uint32_t i = 0; i < sizeof(err_arr) / sizeof(double); ++i)
		{
			MQM(tree, Q, n, FUN, err_arr[i]); // MQM method for finding ANN of Q
		}
		/*************** END MQM 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(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;
}
示例#5
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;
}