1
2
3
4
5
6
7
8
9
10
11
13 s = {}
14 for key in state.keys():
15 if key[0] != '_':
16 s[key] = state[key]
17 return s
18
20 """Generic class for a policy of behavior"""
21
23 self.choices = choices
24
25 - def execute(self,state,choices=[],debug=0):
26 raise NotImplementedError
27
29 """Generic class for a policy that merges a list of policy
30 functions"""
32 Policy.__init__(self,choices,'composite')
33 self.subpolicies = policyList
34
36 self.subpolicies.append(subpolicy)
37
38 - def execute(self,state,choices,debug=0):
39 for subpolicy in self.subpolicies:
40 choice = subpolicy.execute(state,choices,debug)
41 if choice:
42 return choice
43 return None
44
58
60 newPolicy = CompositePolicy(self.choices,[])
61 for subpolicy in self.subpolicies:
62 newPolicy.extend(subpolicy.copy())
63 return newPolicy
64
66 str = ''
67 for subpolicy in self.subpolicies:
68 str = str + '\nSubpolicy:\n\t'+`subpolicy`
69 return str[1:]
70
71
72
73
74
75
76
77
78
79
80
81
82
84 """Generic class for a policy that is specified as a table"""
88
89 - def execute(self,state,choices,debug=0):
90 entry = self.table
91 beliefList = state[:]
92 beliefList.reverse()
93 for belief in beliefList:
94 table = entry['table']
95 for entry in table:
96 if entry['key'] == strip(belief):
97 if entry['action'] in choices:
98 break
99 else:
100
101 return None
102 return entry['action']
103
105
106
107 if not node:
108 node = self.table
109 try:
110 table = node['table']
111 except KeyError:
112 return [node]
113 if depth == 0:
114 return [node]
115 elif depth > 0:
116
117 depth = depth - 1
118 nodeList = []
119 for node in table:
120 nodeList = nodeList + self.getNodes(depth,node)
121 return nodeList
122
124 result = ''
125 entry = self.table
126 try:
127 result = result + self.printTable(entry['table'],0)
128 except KeyError:
129 result = '<Empty policy>'
130 return result
131
133 str = ''
134 for entry in table:
135 for index in range(depth):
136 str = str + '\t'
137 str = str + `entry['key']` + ' -> ' + entry['action'] + '\n'
138 try:
139 str = str + self.printTable(entry['table'],depth+1)
140 except KeyError:
141 pass
142 return str
143