Package teamwork :: Package policy :: Module generic
[hide private]
[frames] | no frames]

Source Code for Module teamwork.policy.generic

  1   ########################################################################### 
  2   # 11/5/2001: David V. Pynadath, USC Information Sciences Institute 
  3   #            pynadath@isi.edu 
  4   # 
  5   # Policy: generic class for a policy of behavior 
  6   # CompositePolicy: a generic class for a policy that merges a list of 
  7   #           policy functions 
  8   # GenericPolicy: a generic class for a policy that is specified as a 
  9   #           table 
 10   ########################################################################### 
 11   
12 -def strip(state):
13 s = {} 14 for key in state.keys(): 15 if key[0] != '_': 16 s[key] = state[key] 17 return s
18
19 -class Policy:
20 """Generic class for a policy of behavior""" 21
22 - def __init__(self,choices,type=None):
23 self.choices = choices
24
25 - def execute(self,state,choices=[],debug=0):
26 raise NotImplementedError
27
28 -class CompositePolicy(Policy):
29 """Generic class for a policy that merges a list of policy 30 functions"""
31 - def __init__(self,choices,policyList):
32 Policy.__init__(self,choices,'composite') 33 self.subpolicies = policyList
34
35 - def extend(self,subpolicy):
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
45 - def actionValue(self,state,actStruct,debug=0):
46 """Return expected value of performing action""" 47 value = None 48 for subpolicy in self.subpolicies: 49 try: 50 delta = subpolicy.actionValue(state,actStruct,debug) 51 if value: 52 value = value + delta 53 else: 54 value = delta 55 except AttributeError: 56 pass 57 return value
58
59 - def copy(self):
60 newPolicy = CompositePolicy(self.choices,[]) 61 for subpolicy in self.subpolicies: 62 newPolicy.extend(subpolicy.copy()) 63 return newPolicy
64
65 - def __str__(self):
66 str = '' 67 for subpolicy in self.subpolicies: 68 str = str + '\nSubpolicy:\n\t'+`subpolicy` 69 return str[1:]
70 71 # Example actionTable: 72 # {'action':'listen', 73 # 'table':[{'key':{'Tiger':'left'}, 74 # 'action':'openright', 75 # 'table':[...]}, 76 # {'key':{'Tiger':'right'}, 77 # 'action':'openleft', 78 # 'table':[{'key':{'Tiger':'reset'}, 79 # 'action':'listen', 80 # 'table':[...]},...]}, 81 # ...] 82 # }
83 -class GenericPolicy(Policy):
84 """Generic class for a policy that is specified as a table"""
85 - def __init__(self,actionTable):
86 self.table = actionTable 87 Policy.__init__(self,[],'generic')
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 # Policy doesn't specify anything for this belief 101 return None 102 return entry['action']
103
104 - def getNodes(self,depth=-1,node=None):
105 # If depth < 0, returns leaf nodes; 106 # otherwise, returns nodes at depth specified 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 # Decrement depth to search 117 depth = depth - 1 118 nodeList = [] 119 for node in table: 120 nodeList = nodeList + self.getNodes(depth,node) 121 return nodeList
122
123 - def __str__(self):
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
132 - def printTable(self,table,depth):
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