Beispiel #1
0
//' rcpp_neutral_hotspots_ntests
//'
//' Performs repeated neutral tests to yield average distributions of both
//' hotspot values and spatial autocorrelation statistics.
//'
//' @param nbs An \code{spdep} \code{nb} object listing all neighbours of each
//' point. 
//' @param wts Weighting factors for each neighbour; must have same length as
//' nbs. 
//' @param nbsi List of matrices as returned from \code{get_nbsi}. each element
//' of which contains the i-th nearest neighbour to each point.
//' @param alpha Strength of spatial autocorrelation
//' @param sd0 Standard deviation of truncated normal distribution used to model
//' environmental variation (with mean of 1)
//' @param nt Number of successive layers of temporal and spatial autocorrelation
//' used to generate final modelled values
//' @param ntests Number of tests used to obtain average values
//' @param ac_type Character string specifying type of aucorrelation
//' (\code{moran}, \code{geary}, or code{getis-ord}).
//'
//' @return A matrix of dimension (size, 2), with first column containing
//' sorted and re-scaled hotspot values, and second column containing sorted and
//' re-scaled spatial autocorrelation statistics.
//'
// [[Rcpp::export]]
Rcpp::NumericMatrix rcpp_neutral_hotspots_ntests (Rcpp::List nbs, 
        Rcpp::List wts, Rcpp::List nbsi, double alpha, double sd0, int niters,
        std::string ac_type, bool log_scale, int ntests)
{
    const int size = nbs.size ();

    Rcpp::NumericMatrix hs1;
    Rcpp::NumericVector z (size), z1 (size), ac (size), ac1 (size);
    std::fill (ac.begin (), ac.end (), 0.0);
    std::fill (z.begin (), z.end (), 0.0);

    for (int n=0; n<ntests; n++)
    {
        hs1 = rcpp_neutral_hotspots (nbs, wts, nbsi, alpha, sd0, log_scale,
                niters, ac_type);

        z += hs1 (Rcpp::_, 0);
        ac += hs1 (Rcpp::_, 1);
    }

    Rcpp::NumericMatrix result (size, 2);
    result (Rcpp::_, 0) = z / (double) ntests;
    result (Rcpp::_, 1) = ac / (double) ntests;
    Rcpp::colnames (result) = Rcpp::CharacterVector::create ("z", "ac");

    return result;
}
Beispiel #2
0
//creates accounts and cards in the system
void DataStorage::initATM(){
    Account ac("Petro","Petrov","16.03.1984");
    _data.accounts().push_back(ac);
    _data.accounts().back().insertCard("1234567890123651",md5("7561"));
    _data.accounts().back().insertCard("4984654351484864",md5("5768"));
    _data.accounts().back().insertCard("1218998165132318",md5("3201"));

    _data.cardToAmount().insert(pair<string,int>("1234567890123651",1500));
    _data.cardToAmount().insert(pair<string,int>("4984654351484864",0));
    _data.cardToAmount().insert(pair<string,int>("1218998165132318",210));

    Account ac1("Ivan","Ivanov","27.05.1975");
    _data.accounts().push_back(ac1);
    _data.accounts().back().insertCard("4327463724623746",md5("1234"));
    _data.accounts().back().insertCard("1334234234342212",md5("9368"));
    _data.accounts().back().insertCard("9456452312334123",md5("9057"));


    _data.cardToAmount().insert(pair<string,int>("4327463724623746",350));
    _data.cardToAmount().insert(pair<string,int>("1334234234342212",0));
    _data.cardToAmount().insert(pair<string,int>("9456452312334123",12000));

    Account ac2("Sergiy","Sergeev","22.11.1989");
    _data.accounts().push_back(ac2);
    _data.accounts().back().insertCard("1643234324234123",md5("1573"));
    _data.accounts().back().insertCard("9657433242341234",md5("9374"));
    _data.accounts().back().insertCard("1235234324124334",md5("2668"));


    _data.cardToAmount().insert(pair<string,int>("1643234324234123",20));
    _data.cardToAmount().insert(pair<string,int>("9657433242341234",150));
    _data.cardToAmount().insert(pair<string,int>("1235234324124334",90));

}
void LeaderFollowerPoolTest::NoReactorTest() {
	StartTrace(LeaderFollowerPoolTest.NoReactorTest);
	LeaderFollowerPool lfp(0);

	Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
	Anything lfpConfig;
	lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
	if (t_assertm( !lfp.Init(2, lfpConfig), "no reactor is configured; init should fail")) {
		t_assertm(lfp.GetPoolSize() == 0, "expected no threads in pool");
		t_assertm(lfp.Start(true, 1000, 11) == -1, "expected Start to fail");
	}
}
int main(int argc, char **argv) {
	ros::init(argc, argv, "spiri_go_client");

	//It's better to wait sonme time, for this file has no NodeHandle, we should add ros::Time::init()
	ros::Time::init();
	ros::Duration(3).sleep();
	
	actionlib::SimpleActionClient<spiri_go::TakeoffAction> ac("spiri_take_off", true);      //first param: server name, second param: true means a new thread
	actionlib::SimpleActionClient<spiri_go::LandHereAction> ac1("spiri_land_here", true);  //first param: server name, second param: false means not to use threads

	ROS_INFO("Waiting for action server(takeOff) to start.");

	ac.waitForServer();	
	
	ROS_INFO("Action server started--Taking off!");

	spiri_go::TakeoffGoal goal;

	//set the target height to take off!
	goal.height = 3;
	ac.sendGoal(goal);

	//wait for action(takeOff) to return
	bool is_takeOff_finished = ac.waitForResult(ros::Duration(60.0));

	if(is_takeOff_finished ) {
		ROS_INFO("reached target height!");

		//do something
		ros::Duration(20).sleep();
	}
	else {
		ROS_INFO("should set mode Land!");
	}

	//land
	ROS_INFO("Waiting for action server(land) to start.");
	ac1.waitForServer();	
	ROS_INFO("Start to land!.");
	
	spiri_go::LandHereGoal goal_1;
	goal_1.height = 0;
	ac1.sendGoal(goal_1);

	//wait for action(land) to return
	bool is_land_finished = ac1.waitForResult(ros::Duration(60.0));
	if(is_land_finished ) {
		ROS_INFO("Land success!");
	}

	return 0;

}
void LeaderFollowerPoolTest::InvalidAcceptorTest() {
	StartTrace(LeaderFollowerPoolTest.InvalidAcceptorTest);

	LeaderFollowerPool lfp(new TestReactor(this));

	Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port"].AsLong(), 5, 0);
	Anything lfpConfig;
	lfpConfig[String("Accept") << GetConfig()["Testhost"]["port"].AsString()] = (IFAObject *) &ac1;
	if (t_assertm( !lfp.Init(3, lfpConfig), "should not bind to already bound port and needs root privilege anyway")) {
		t_assertm(lfp.GetPoolSize() == 0, "expected no threads in pool");
		t_assertm(lfp.Start(true, 1000, 11) == -1, "expected Start to fail");
	}
}
void LeaderFollowerPoolTest::OneAcceptorTest() {
	StartTrace(LeaderFollowerPoolTest.OneAcceptorTest);

	LeaderFollowerPool lfp(new TestReactor(this));

	Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
	Anything lfpConfig;
	lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
	if (t_assertm( lfp.Init(3, lfpConfig), "some port maybe already bound")) {
		if (t_assert(lfp.Start(true, 1000, 11) == 0 )) {
			ProcessOneEvent();
			lfp.RequestTermination();
			t_assertm(lfp.Join() == 0, "expected Join to succeed");
		}
	}
}
void LeaderFollowerPoolTest::ManyAcceptorsTest() {
	StartTrace(LeaderFollowerPoolTest.ManyAcceptorsTest);
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( !lfp.Init(0, lfpConfig), "should not allow LFPool to run without threads")) {
			t_assertm(lfp.GetPoolSize() == 0, "expected no threads in pool");
			t_assertm(lfp.Start(true, 1000, 11) == -1, "expected Start to fail");
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(1, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(false, 1000, 10) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(2, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(false, 1000, 10) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(3, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(true, 1000, 11) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
	{
		LeaderFollowerPool lfp(new TestReactor(this));

		Acceptor ac1(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port1"].AsLong(), 5, 0);
		Acceptor ac2(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port2"].AsLong(), 5, 0);
		Acceptor ac3(GetConfig()["Testhost"]["ip"].AsString(), GetConfig()["Testhost"]["port3"].AsLong(), 5, 0);
		Anything lfpConfig;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port1"].AsString()] = (IFAObject *) &ac1;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port2"].AsString()] = (IFAObject *) &ac2;
		lfpConfig[String("Accept") << GetConfig()["Testhost"]["port3"].AsString()] = (IFAObject *) &ac3;
		if (t_assertm( lfp.Init(4, lfpConfig), "some port maybe already bound")) {
			if (t_assert(lfp.Start(true, 1000, 11) == 0 )) {
				ProcessManyEvents();
				lfp.RequestTermination();
				t_assertm(lfp.Join() == 0, "expected Join to succeed");
			}
		}
	}
}