-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaction_selection_module.py
More file actions
101 lines (70 loc) · 3.23 KB
/
action_selection_module.py
File metadata and controls
101 lines (70 loc) · 3.23 KB
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
#In action_selection_module.py the ActionSelectionModule class is implemented, which
#contains different methods for selecting an action from a categorical distribution
#over actions. In particular, the action can be chosen by sampling, as we propose
#for our approach, or by taking the action with the highest probability if it is
#above a specified threshold, as proposed by the compared approach by Cruz et al. (2018).
import numpy as np
class ActionSelectionModule:
def __init__(self, mdp, action_selection_method):
'''
constructor of ActionSelectionModule
:param mdp: instance of MDP
:param action_selection_method: name of the method to use for the action selection, 'all_probabilities', 'highest_probability', 'highest_probability_threshold'
'''
self.mdp = mdp
self.action_selection_method = action_selection_method
def select_action(self, dis, a_pi=None):
'''
selects action with highest probability directly from human input given as a distribution dis
:param dis: human action distribution
:param a_pi: action suggested by policy
:return: selected action, int
'''
if self.action_selection_method == 'argmax_threshold':
action = self.__select_action_with_highest_probability_threshold(dis, a_pi)
elif self.action_selection_method == 'sampling':
action = self.__select_action_from_distribution(dis)
else:
action = self.__select_action_with_highest_probability(dis)
return action
def __select_action_with_highest_probability(self, dis):
"""
selects the action with the highest probability
in contrast to standard argmax, if multiple classes have the same (highest) probability,
the choice among these is also random
:param np.array dis: distribution from which action should be selected
:return: selected action, int
"""
max = np.max(dis)
maxes = []
for i in range(dis.shape[0]):
if dis[i] == max:
maxes.append(i)
idx = np.random.choice(maxes)
return idx
def __select_action_with_highest_probability_threshold(self, dis, a_pi):
'''
selects the action with the highest probability, if probability smaller than threshold then return policy action
:param dis: distribution from which action should be selected
:param a_pi: action suggested by policy
:return: selected action, int
'''
a = self.__select_action_with_highest_probability(dis)
if dis[a] < 0.25:
# print 'Probability below Threshold: take policy action'
a = a_pi
return a
def __select_action_from_distribution(self, dis):
'''
selects action form distribution, samples over distribution
:param dis: distribution from which action should be selected
:return: selected action, int
'''
p = np.random.uniform(0, 1)
sum_up = 0
for i in range(dis.shape[0]):
if dis[i] != -np.Inf:
if sum_up <= p <= sum_up + dis[i]:
return i
else:
sum_up += dis[i]