Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/moses-smt/vowpal_wabbit.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLuong Hoang <lhoang@live.com>2014-11-08 12:15:34 +0300
committerLuong Hoang <lhoang@live.com>2014-11-08 12:15:34 +0300
commitd8cfd4ecee10c0dac914538ab54637b552f3a14e (patch)
tree90b7f167b4edf4019485257e6a9d3b876e5dda7b /cs_test
parent13f3acee647f0e9510e19db1755c4889a97e3729 (diff)
#85: added new interface for c# tau-first & generic explorers. cleaned up sample code in both c++ and c#
Diffstat (limited to 'cs_test')
-rw-r--r--cs_test/ExploreOnlySample.cs137
1 files changed, 25 insertions, 112 deletions
diff --git a/cs_test/ExploreOnlySample.cs b/cs_test/ExploreOnlySample.cs
index 6b2a19eb..16e7cc64 100644
--- a/cs_test/ExploreOnlySample.cs
+++ b/cs_test/ExploreOnlySample.cs
@@ -8,54 +8,8 @@ namespace cs_test
{
class ExploreOnlySample
{
- private static UInt32 SampleStatefulPolicyFunc(int policyParams, BaseContext appContext)
- {
- return (uint)((policyParams + appContext.GetFeatures().Length) % 10 + 1);
- }
-
- private static UInt32 SampleStatefulPolicyFunc2(int policyParams, BaseContext appContext)
- {
- return (uint)((policyParams + appContext.GetFeatures().Length) % 10 + 2);
- }
-
- private static UInt32 SampleStatefulPolicyFunc(CustomParams policyParams, BaseContext appContext)
- {
- return (uint)((policyParams.Value1 + policyParams.Value2 + appContext.GetFeatures().Length) % 10 + 1);
- }
-
- private static UInt32 SampleStatelessPolicyFunc(BaseContext appContext)
- {
- return (UInt32)appContext.GetFeatures().Length;
- }
-
- private static UInt32 SampleStatelessPolicyFunc2(BaseContext appContext)
- {
- return (UInt32)appContext.GetFeatures().Length + 1;
- }
-
- private static void SampleStatefulScorerFunc(int policyParams, BaseContext appContext, float[] scores)
- {
- for (uint i = 0; i < scores.Length; i++)
- {
- scores[i] = (int)policyParams + i;
- }
- }
-
- private static void SampleStatelessScorerFunc(BaseContext appContext, float[] scores)
- {
- for (uint i = 0; i < scores.Length; i++)
- {
- scores[i] = appContext.GetFeatures().Length + i;
- }
- }
-
- class CustomParams
- {
- public int Value1;
- public int Value2;
- }
-
class MyContext { }
+
class MyRecorder : IRecorder<MyContext>
{
public void Record(MyContext context, UInt32 action, float probability, string uniqueKey)
@@ -64,6 +18,7 @@ namespace cs_test
}
private List<uint> actions = new List<uint>();
}
+
class MyPolicy : IPolicy<MyContext>
{
public MyPolicy() : this(-1) { }
@@ -96,36 +51,13 @@ namespace cs_test
public static void Run()
{
- string exploration_type = "greedy";
- bool stateful = true;
-
- MwtExplorer mwt = new MwtExplorer("test");
-
- uint numActions = 10;
-
- float epsilon = 0.2f;
- uint tau = 0;
- uint numbags = 2;
- float lambda = 0.5f;
-
- int policyParams = 1003;
- int policyParams2 = 1004;
- CustomParams customParams = new CustomParams() { Value1 = policyParams, Value2 = policyParams2 };
- StatefulPolicyDelegate<int>[] bags =
- {
- new StatefulPolicyDelegate<int>(SampleStatefulPolicyFunc),
- new StatefulPolicyDelegate<int>(SampleStatefulPolicyFunc2)
- };
- int[] parameters = { policyParams, policyParams };
- StatelessPolicyDelegate[] statelessbags =
- {
- new StatelessPolicyDelegate(SampleStatelessPolicyFunc),
- new StatelessPolicyDelegate(SampleStatelessPolicyFunc2)
- };
+ string exploration_type = "generic";
if (exploration_type == "greedy")
{
// Initialize Epsilon-Greedy explore algorithm using custom Recorder, Policy & Context types
+ uint numActions = 10;
+ float epsilon = 0.2f;
MyRecorder mc = new MyRecorder();
MyPolicy mp = new MyPolicy();
MWT<MyContext> mwtt = new MWT<MyContext>("mwt", mc);
@@ -134,20 +66,20 @@ namespace cs_test
}
else if (exploration_type == "tau-first")
{
- if (stateful)
- {
- /*** Initialize Tau-First explore algorithm using a default policy function that accepts parameters ***/
- mwt.InitializeTauFirst<CustomParams>(tau, new StatefulPolicyDelegate<CustomParams>(SampleStatefulPolicyFunc), customParams, numActions);
- }
- else
- {
- /*** Initialize Tau-First explore algorithm using a stateless default policy function ***/
- mwt.InitializeTauFirst(tau, new StatelessPolicyDelegate(SampleStatelessPolicyFunc), numActions);
- }
+ // Initialize Tau-First explore algorithm using custom Recorder, Policy & Context types
+ uint numActions = 10;
+ uint tau = 0;
+ MyRecorder mc = new MyRecorder();
+ MyPolicy mp = new MyPolicy();
+ MWT<MyContext> mwtt = new MWT<MyContext>("mwt", mc);
+ uint action = mwtt.Choose_Action(new TauFirstExplorer<MyContext>(mp, tau, numActions), "key", new MyContext());
+ return;
}
else if (exploration_type == "bagging")
{
// Initialize Bagging explore algorithm using custom Recorder, Policy & Context types
+ uint numActions = 10;
+ uint numbags = 2;
MyRecorder mc = new MyRecorder();
MyPolicy[] mps = new MyPolicy[numbags];
for (int i = 0; i < numbags; i++)
@@ -162,6 +94,8 @@ namespace cs_test
else if (exploration_type == "softmax")
{
// Initialize Softmax explore algorithm using custom Recorder, Scorer & Context types
+ uint numActions = 10;
+ float lambda = 0.5f;
MyRecorder mc = new MyRecorder();
MyScorer ms = new MyScorer(numActions);
@@ -171,41 +105,20 @@ namespace cs_test
}
else if (exploration_type == "generic")
{
- if (stateful)
- {
- /*** Initialize Generic explore algorithm using a default policy function that accepts parameters ***/
- mwt.InitializeGeneric<int>(new StatefulScorerDelegate<int>(SampleStatefulScorerFunc), policyParams, numActions);
- }
- else
- {
- /*** Initialize Generic explore algorithm using a stateless default policy function ***/
- mwt.InitializeGeneric(new StatelessScorerDelegate(SampleStatelessScorerFunc), numActions);
- }
+ // Initialize Generic explore algorithm using custom Recorder, Scorer & Context types
+ uint numActions = 10;
+ MyRecorder mc = new MyRecorder();
+ MyScorer ms = new MyScorer(numActions);
+
+ MWT<MyContext> mwtt = new MWT<MyContext>("mwt", mc);
+ uint action = mwtt.Choose_Action(new GenericExplorer<MyContext>(ms, numActions), "key", new MyContext());
+ return;
}
else
{ //add error here
}
- Feature[] f = new Feature[2];
- f[0].Value = 0.5f;
- f[0].Id = 1;
- f[1].Value = 0.9f;
- f[1].Id = 2;
-
- string otherContext = "Some other context data that might be helpful to log";
- OldSimpleContext appContext = new OldSimpleContext(f, otherContext);
-
- UInt32 chosenAction = mwt.ChooseAction("myId", appContext);
-
- Interaction[] interactions = mwt.GetAllInteractions();
- // string interactions = mwt.GetAllInteractionsAsString();
-
- mwt.Uninitialize();
-
- Console.WriteLine(chosenAction);
- Console.WriteLine(interactions);
-
}
}
}