-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_utils.py
49 lines (36 loc) · 1.13 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
from torch.distributions.categorical import Categorical
from Params import configs
import torch
device = torch.device(configs.device)
def select_action_mch(p, candidate, memory):
dist = Categorical(p.squeeze())
s = dist.sample()
if memory is not None: memory.job_mch_logprobs.append(dist.log_prob(s))
row = s // configs.n_m
col = s % configs.n_m
action = candidate[row].item()
mch = col.item()
return action, mch, s
# evaluate the actions
def eval_actions_mchs(p, actions):
dist = Categorical(p)
log_a = dist.log_prob(actions).reshape(-1)
entropy_a = dist.entropy().mean()
return log_a, entropy_a
# select action method for test
def sample_select_action_mch(p, candidate, n_m):
dist = Categorical(p.squeeze())
s = dist.sample()
row = s // n_m
col = s % n_m
action = candidate[row].item()
mch = col.item()
return action, mch
# select action method for test
def greedy_select_action_mch(p, candidate, n_m):
_, index = p.squeeze().max(0)
row = index // n_m
col = index % n_m
action = candidate[row].item()
mch = col.item()
return action, mch