forked from SafeRoboticsLab/safety_rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
genValSamples.py
126 lines (110 loc) · 4.39 KB
/
genValSamples.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
120
121
122
123
124
125
126
"""
Please contact the author(s) of this library if you have any questions.
Authors: Kai-Chieh Hsu ( [email protected] )
Generate samples to compute approximation error.
1. It supports SIX sample types:
0-6 corresponds to ['TN', 'TP', 'FN', 'FP', 'POS', 'NEG', 'ALL'].
2. This script uses `{args.modelFolder}/data/{args.dataFile}.npy` to load the
ddqn-predicted values and rollout values of the sampled initial states.
Then, it generates the samples for validation under
`{args.modelFolder}/data/{sampleType}/{args.outFile}{sampleType}.npy`
EXAMPLES
TN: python3 genValSamples.py -t 0 -mf <model path>
FP: python3 genValSamples.py -t 3 -mf <model path>
"""
import argparse
import os
from warnings import simplefilter
import numpy as np
simplefilter(action='ignore', category=FutureWarning)
def run(args):
# == Getting states to be tested ==
print('\n== Getting states to be tested ==')
dataFolder = os.path.join(args.modelFolder, 'data')
dataFile = os.path.join(dataFolder, args.dataFile + '.npy')
print('Load from {:s} ...'.format(dataFile))
read_dictionary = np.load(dataFile, allow_pickle='TRUE').item()
print(read_dictionary.keys())
ddqnValue = read_dictionary['ddqnValue']
rolloutValue = read_dictionary['rolloutValue']
samples = read_dictionary['samples']
[samplesAtt, samplesDef, thetas] = samples
print(rolloutValue.shape)
if args.sampleType == 0:
pickMtx = np.logical_and((rolloutValue <= 0), (ddqnValue <= 0))
elif args.sampleType == 1:
pickMtx = np.logical_and((rolloutValue > 0), (ddqnValue > 0))
elif args.sampleType == 2:
pickMtx = np.logical_and((rolloutValue > 0), (ddqnValue <= 0))
elif args.sampleType == 3:
pickMtx = np.logical_and((rolloutValue <= 0), (ddqnValue > 0))
elif args.sampleType == 4:
pickMtx = (rolloutValue > 0)
elif args.sampleType == 5:
pickMtx = (rolloutValue <= 0)
elif args.sampleType == 6:
pickMtx = np.full(shape=rolloutValue.shape, fill_value=True)
sampleTypeList = ['TN', 'TP', 'FN', 'FP', 'POS', 'NEG', 'ALL']
sampleType = sampleTypeList[args.sampleType]
print('Type of sampled states:', sampleType)
pickIndices = np.argwhere(pickMtx)
length = pickIndices.shape[0]
indices = np.random.randint(low=0, high=length, size=(args.numTest,))
states = np.empty(shape=(args.numTest, 6), dtype=float)
ddqnList = np.empty(shape=(args.numTest), dtype=float)
rolloutPolicyValueList = np.empty(shape=(args.numTest), dtype=float)
idxList = []
for cnt, i in enumerate(indices):
idx = tuple(pickIndices[i])
ddqnList[cnt] = ddqnValue[idx]
rolloutPolicyValueList[cnt] = rolloutValue[idx]
states[cnt, 0:2] = samplesAtt[idx[0], :]
states[cnt, 2] = thetas[idx[1]]
states[cnt, 3:5] = samplesDef[idx[2], :]
states[cnt, 5] = thetas[idx[3]]
idxList.append(idx)
print("The first five indices picked: ")
endIdx = 10
print('indices:', idxList[:5])
print('states:', states[:5, :])
# print(np.all(ddqnList[:] <= 0))
# print(np.all(rolloutPolicyValueList[:] <= 0))
print('DDQN:', ddqnList[:endIdx])
print('rollout:', rolloutPolicyValueList[:endIdx])
finalDict = {}
finalDict['states'] = states
finalDict['idxList'] = idxList
finalDict['ddqnList'] = ddqnList
finalDict['rolloutPolicyValueList'] = rolloutPolicyValueList
outFolder = os.path.join(dataFolder, sampleType)
os.makedirs(outFolder, exist_ok=True)
outFile = os.path.join(outFolder, args.outFile + sampleType + '.npy')
np.save('{:s}'.format(outFile), finalDict)
print('--> Save to {:s} ...'.format(outFile))
if __name__ == '__main__':
# == Arguments ==
parser = argparse.ArgumentParser()
# Simulation Parameters
parser.add_argument(
"-rnd", "--randomSeed", help="random seed", default=0, type=int
)
parser.add_argument(
"-t", "--sampleType", help="type of sampled states", default=0, type=int
)
parser.add_argument("-nt", "--numTest", help="#tests", default=100, type=int)
# File Parameters
parser.add_argument(
"-of", "--outFile", help="output file", default='samples', type=str
)
parser.add_argument("-mf", "--modelFolder", help="model folder", type=str)
parser.add_argument(
"-df", "--dataFile", help="estimation error file", default='estError',
type=str
)
args = parser.parse_args()
print("== Arguments ==")
print(args)
# == Execution ==
np.random.seed(args.randomSeed)
np.set_printoptions(precision=3, suppress=True, floatmode='fixed')
run(args)