-
Notifications
You must be signed in to change notification settings - Fork 0
/
MSLFQ_Test.py
119 lines (88 loc) · 3.01 KB
/
MSLFQ_Test.py
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
'''
Created on Apr 8, 2013
@author: vima
'''
import unittest
from DummyEnvironment import DummyEnvironment
from MSLFQ import MSLFQ
import numpy as np
class Test(unittest.TestCase):
def setUp(self):
# Set up environment
actions = ["Drug_A", "Drug_B"]
num_features = 2
gamma = 0.99
stages = 1
self.env = DummyEnvironment(feature_size=num_features, stages=1, gamma=gamma, actions=actions)
#set up data
self.data = []
s = [1, 1]
a = "Drug_A"
r = 1.0
ns = [0, 0]
self.data.append([0, s, a, r, ns])
s = [1, 1]
a = "Drug_B"
r = 0.0
ns = [0, 0]
self.data.append([0, s, a, r, ns])
self.learner = MSLFQ(stages=stages, gamma=gamma)
def testDummyEnvironment(self):
state = [1.0, 1.0]
action = "Drug_A"
stage = 1
features = self.env.phi(stage, state, action)
result_list = [1.0, 1.0, 1.0, 0, 0, 0]
self.assertListEqual(list(features), result_list)
action = "Drug_B"
result_list = [0, 0, 0, 1.0, 1.0, 1.0]
features = self.env.phi(stage, state, action)
self.assertListEqual(list(features), result_list)
def testProcessDataFit(self):
self.learner.process_data(self.data, self.env)
n_data = self.learner.data_
result_list = [1.0, 1.0, 1.0, 0, 0, 0]
features = n_data[0][0]
target = n_data[0][1]
self.assertListEqual(list(features[0]), result_list)
self.assertEqual(target[0], 1.0)
result_list = [0, 0, 0, 1.0, 1.0, 1.0]
self.assertListEqual(list(features[1]), result_list)
self.assertEqual(target[1], 0.0)
weights = self.learner.fit(features, target)
self.assertEqual(np.sign(weights[0]), 1)
self.assertEqual(np.sign(weights[3]), -1)
def testLongFit(self):
weights = self.learner.fit_data(self.data, self.env)
self.assertEqual(len(weights), 1)
self.assertEqual(len(weights[0]), 6)
def testMultiStage(self):
#set up data
stages = 2
data = []
s = [1, 1]
a = "Drug_A"
r = 1.0
ns = [1, 1]
data.append([1, s, a, r, ns])
a = "Drug_B"
r = 0.0
ns = [1, 1]
data.append([1, s, a, r, ns])
s = [0, 0]
a = "Drug_B"
r = 0.0
ns = [1, 1]
data.append([0, s, a, r, ns])
a = "Drug_A"
r = 0.0
ns = [0, 0]
data.append([0, s, a, r, ns])
learner = MSLFQ(stages=stages, gamma=1.0)
weights = learner.fit_data(data, self.env)
self.assertEqual(len(weights), 2)
self.assertEqual(len(weights[0]), 6)
print weights
if __name__ == "__main__":
#import sys;sys.argv = ['', 'Test.testName']
unittest.main()