int main(int argc, char* argv[])
{
    if (argc < 2)
    {
        cerr << "arguments: {greedy,tau-first,bootstrap,softmax,generic}" << endl;
        exit(1);
    }
    
    // Arguments for individual explorers
    if (strcmp(argv[1], "greedy") == 0)
    {
        // Initialize Epsilon-Greedy explore algorithm using MyPolicy

        // Creates a recorder of built-in StringRecorder type for string serialization
        StringRecorder<SimpleContext> recorder;

        // Creates a policy that interacts with SimpleContext type
        MySimplePolicy default_policy;

        // Creates an MwtExplorer instance using the recorder above
        MwtExplorer<SimpleContext> mwt("appid", recorder);

        u32 num_actions = 10;
        float epsilon = .2f;
        // Creates an Epsilon-Greedy explorer using the specified settings
        EpsilonGreedyExplorer<SimpleContext> explorer(default_policy, epsilon, num_actions);

        // Creates a context of built-in SimpleContext type
        vector<Feature> features;
        features.push_back({ 0.5f, 1 });
        features.push_back({ 1.3f, 11 });
        features.push_back({ -.95f, 413 });

        SimpleContext context(features);

        // Performs exploration by passing an instance of the Epsilon-Greedy exploration algorithm into MwtExplorer
        // using a sample string to uniquely identify this event
        string unique_key = "eventid";
        u32 action = mwt.Choose_Action(explorer, unique_key, context);

        cout << "Chosen action = " << action << endl;
        cout << "Exploration record = " << recorder.Get_Recording();
    }
    else if (strcmp(argv[1], "tau-first") == 0)
    {
        // Initialize Tau-First explore algorithm using MyPolicy
        MyRecorder recorder;
        MwtExplorer<MyContext> mwt("appid", recorder);

        int num_actions = 10;
        u32 tau = 5;
        MyPolicy default_policy;
        TauFirstExplorer<MyContext> explorer(default_policy, tau, num_actions);
        MyContext ctx;
        string unique_key = "eventid";
        u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

        cout << "action = " << action << endl;
    }
    else if (strcmp(argv[1], "bootstrap") == 0)
    {
        // Initialize Bootstrap explore algorithm using MyPolicy
        MyRecorder recorder;
        MwtExplorer<MyContext> mwt("appid", recorder);

        u32 num_bags = 2;

        // Create a vector of smart pointers to default policies using the built-in type PolicyPtr<Context>
        vector<unique_ptr<IPolicy<MyContext>>> policy_functions;
        for (size_t i = 0; i < num_bags; i++)
        {
          policy_functions.push_back(unique_ptr<IPolicy<MyContext>>(new MyPolicy()));
        }
        int num_actions = 10;
        BootstrapExplorer<MyContext> explorer(policy_functions, num_actions);
        MyContext ctx;
        string unique_key = "eventid";
        u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

        cout << "action = " << action << endl;
    }
    else if (strcmp(argv[1], "softmax") == 0)
    {
        // Initialize Softmax explore algorithm using MyScorer 
        MyRecorder recorder;
        MwtExplorer<MyContext> mwt("salt", recorder);

        u32 num_actions = 10;
        MyScorer scorer(num_actions);
        float lambda = 0.5f;
        SoftmaxExplorer<MyContext> explorer(scorer, lambda, num_actions);

        MyContext ctx;
        string unique_key = "eventid";
        u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

        cout << "action = " << action << endl;
    }
    else if (strcmp(argv[1], "generic") == 0)
    {
        // Initialize Generic explore algorithm using MyScorer 
        MyRecorder recorder;
        MwtExplorer<MyContext> mwt("appid", recorder);

        int num_actions = 10;
        MyScorer scorer(num_actions);
        GenericExplorer<MyContext> explorer(scorer, num_actions);
        MyContext ctx;
        string unique_key = "eventid";
        u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

        cout << "action = " << action << endl;
    }
    else
    {
        cerr << "unknown exploration type: " << argv[1] << endl;
        exit(1);
    }

    return 0;
}
Exemplo n.º 2
0
int main(int argc, char* argv[])
{
	if (argc < 2)
	{
		cerr << "arguments: {greedy,tau-first,bagging,softmax,generic} [stateful]" << endl;
		exit(1);
	}
	
	//arguments for individual explorers
	if (strcmp(argv[1], "greedy") == 0)
	{
		//Initialize Epsilon-Greedy explore algorithm using MyPolicy
		vector<Feature> features;
		features.push_back({ 0.5f, 1 });
		features.push_back({ 1.3f, 11 });
		features.push_back({ -.95f, 413 });
		SimpleContext context(features);

		StringRecorder<SimpleContext> recorder;
		MySimplePolicy default_policy; 
		MwtExplorer<SimpleContext> mwt("appid", recorder);

		u32 num_actions = 10;
		float epsilon = .2f;
		EpsilonGreedyExplorer<SimpleContext> explorer(default_policy, epsilon, num_actions);

		string unique_key = "eventid";
		u32 action = mwt.Choose_Action(explorer, unique_key, context);

		cout << "action = " << action << " recorder = " << recorder.Get_Recording();
	}
	else if (strcmp(argv[1], "tau-first") == 0)
	{
		//Initialize Tau-First explore algorithm using MyPolicy
		MyRecorder recorder;
		MwtExplorer<MyContext> mwt("appid", recorder);

		int num_actions = 10;
		u32 tau = 5;
		MyPolicy default_policy;
		TauFirstExplorer<MyContext> explorer(default_policy, tau, num_actions);
		MyContext ctx;
		string unique_key = "eventid";
		u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

		cout << "action = " << action << endl;
	}
	else if (strcmp(argv[1], "bagging") == 0)
	{
		//Initialize Bagging explore algorithm using MyPolicy
		MyRecorder recorder;
		MwtExplorer<MyContext> mwt("appid", recorder);

		u32 num_bags = 2;
		vector<unique_ptr<IPolicy<MyContext>>> policy_functions;
		for (size_t i = 0; i < num_bags; i++)
		{
			policy_functions.push_back(unique_ptr<IPolicy<MyContext>>(new MyPolicy()));
		}
		int num_actions = 10;
		BaggingExplorer<MyContext> explorer(policy_functions, num_bags, num_actions);
		MyContext ctx;
		string unique_key = "eventid";
		u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

		cout << "action = " << action << endl;
	}
	else if (strcmp(argv[1], "softmax") == 0)
	{
		//Initialize Softmax explore algorithm using MyScorer 
		MyRecorder recorder;
		MwtExplorer<MyContext> mwt("salt", recorder);

		u32 num_actions = 10;
		MyScorer scorer(num_actions);
		float lambda = 0.5f;
		SoftmaxExplorer<MyContext> explorer(scorer, lambda, num_actions);

		MyContext ctx;
		string unique_key = "eventid";
		u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

		cout << "action = " << action << endl;
	}
	else if (strcmp(argv[1], "generic") == 0)
	{
		//Initialize Generic explore algorithm using MyScorer 
		MyRecorder recorder;
		MwtExplorer<MyContext> mwt("appid", recorder);

		int num_actions = 10;
		MyScorer scorer(num_actions);
		GenericExplorer<MyContext> explorer(scorer, num_actions);
		MyContext ctx;
		string unique_key = "eventid";
		u32 action = mwt.Choose_Action(explorer, unique_key, ctx);

		cout << "action = " << action << endl;
	}
	else
	{
		cerr << "unknown exploration type: " << argv[1] << endl;
		exit(1);
	}

	return 0;
}