-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
91 lines (85 loc) · 3 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Blackjack.Game;
using Blackjack.ObjectModel;
using Blackjack.ObjectModel.Interfaces;
using System.Threading;
namespace Blackjack
{
public enum Mode { Train, Play };
public class Program
{
public static string FileName = "Blackjack.txt";
public const int Threshold = 500000;
public static Mode Mode;
public static double reward = 0;
public static void Main(string[] args)
{
System.Console.OutputEncoding = System.Text.Encoding.Unicode;
System.Console.WriteLine("Choose algorithm. Monte-Carlo or TD?");
Policy policy;
string read = System.Console.ReadLine().ToLower();
if (read.StartsWith("m"))
{
Program.FileName = "MC" + Program.FileName;
policy = new MonteCarlo();
}
else if (read.StartsWith("t"))
{
Program.FileName = "TD" + Program.FileName;
policy = new Sarsa();
}
else if (read.StartsWith("b"))
{
Program.FileName = "BS" + Program.FileName;
policy = new BackwardSarsa();
}
else
return;
bool shouldPrint = read.EndsWith("p");
Policy.Initialize();
System.Console.WriteLine("Train or Play?");
read = System.Console.ReadLine();
if (read.ToLower() == "t")
{
Mode = Mode.Train;
System.DateTime then = System.DateTime.Now;
int lastTimeActionUpdated = 0;
for (int i = 0; i < 100000000; i++)
{
Episode e = new Episode(policy);
if (e.Play())
{
lastTimeActionUpdated = i;
}
else if (i - lastTimeActionUpdated > Threshold)
{
System.Console.WriteLine("No need for more episodes, policy is optimal. Number of episode when action was updated last time: "
+ lastTimeActionUpdated);
break;
}
}
System.Console.WriteLine((System.DateTime.Now - then).TotalSeconds);
}
else if (read.ToLower() == "p")
{
Mode = Mode.Play;
while (read.ToLower() == "p")
{
Episode e = new Episode(policy);
e.Play();
e.Print();
System.Console.WriteLine("Train or Play?");
read = System.Console.ReadLine();
}
}
if (shouldPrint)
{
policy.Print();
}
Policy.FlushToDisk();
System.Console.WriteLine("Total reward: " + reward);
}
}
}