forked from pengguo318/FJSPDRL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_utils.py
60 lines (47 loc) · 1.49 KB
/
agent_utils.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
from torch.distributions.categorical import Categorical
import torch
def select_action(p, cadidate, memory,log_prob):
dist = Categorical(p.squeeze())
s = dist.sample()
if memory is not None: log_prob.append(dist.log_prob(s).cpu().tolist())
action = []
for i in range(s.size(0)):
a = cadidate[i][s[i]].cpu().tolist()
action.append(a)
return action, s
def select_action1(p, cadidate):
dist = Categorical(p.squeeze())
s = dist.sample()
action = []
log_a = dist.log_prob(s)
for i in range(s.size(0)):
a = cadidate[i][s[i]]
action.append(a)
action = torch.stack(action,0)
return action, s,log_a
def select_action2(p):
dist = Categorical(p.squeeze())
s = dist.sample()
#if memory is not None: log_prob.append(dist.log_prob(s).cpu().tolist())
log_a = dist.log_prob(s)
return s,log_a
# evaluate the actions
def eval_actions(p, actions):
softmax_dist = Categorical(p)
ret = softmax_dist.log_prob(actions).reshape(-1)
entropy = softmax_dist.entropy().mean()
return ret, entropy
# select action method for test
def greedy_select_action(p,cadidate):
_, index = p.squeeze(-1).max(1)
action = []
for i in range(index.size(0)):
a = cadidate[i][index[i]]
action.append(a)
action = torch.stack(action, 0)
return action
# select action method for test
def sample_select_action(p, candidate):
dist = Categorical(p.squeeze())
s = dist.sample()
return candidate[s]