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

Source Code for Module teamwork.policy.LookupAheadPolicy

 1  from xml.dom.minidom import * 
 2  from teamwork.policy.LookupPolicy import * 
 3  from teamwork.policy.LookaheadPolicy import * 
 4   
5 -class LookupAheadPolicy(LookupPolicy,LookaheadPolicy):
6 lookupClass = LookupPolicy 7 lookaheadClass = LookaheadPolicy 8
9 - def __init__(self,entity,actions,size,depth=1):
10 self.lookaheadClass.__init__(self,entity,actions,depth) 11 self.lookupClass.__init__(self,entity=entity,actions=actions, 12 span=size)
13
14 - def setHorizon(self,depth=1):
15 self.lookaheadClass.setDepth(self,depth)
16
17 - def tryLookup(self,state,choices,debug,explain):
18 if len(choices) > 0: 19 newChoices = choices + [self.entity.actionClass({'type':'lookahead'})] 20 else: 21 newChoices = choices 22 act,exp = self.lookupClass.execute(self,state,newChoices,debug) 23 return act,exp
24
25 - def execute(self,state,choices=[],debug=Debugger(),explain=False):
26 act,exp = self.tryLookup(state,choices,debug,explain) 27 if not act: 28 act,exp = self.lookaheadClass.execute(self,state,choices,debug, 29 explain=explain) 30 elif act[0]['type'] == 'lookahead': 31 depth = -1 32 for value in act.values(): 33 if value != 'lookahead' and value: 34 depth = value 35 break 36 act,exp = self.lookaheadClass.execute(self,state,choices,debug, 37 depth,explain=explain) 38 return act,exp
39
40 - def __copy__(self):
41 newPolicy = self.__class__(self.entity,self.choices, 42 self.span,self.horizon) 43 newPolicy.entries = self.entries[:] 44 return newPolicy
45
46 - def __deepcopy__(self,memo):
47 newPolicy = self.__class__(self.entity,self.choices, 48 self.span,self.horizon) 49 newPolicy.entries = copy.deepcopy(self.entries,memo) 50 return newPolicy
51
52 - def actionValue(self,state,actStruct,debug=Debugger()):
53 action,explanation = self.lookupClass.execute(self,state=state, 54 debug=debug) 55 if not action or action['type'] == 'lookahead': 56 return self.lookaheadClass.actionValue(self,state,actStruct,debug) 57 else: 58 return self.lookupClass.actionValue(self,state,actStruct,debug)
59
60 - def __contains__(self,value):
61 return self.lookupClass.__contains__(self,value) or \ 62 self.lookaheadClass.__contains__(self,value)
63
64 - def __xml__(self):
65 doc = LookaheadPolicy(self) 66 root.setAttribute('span',str(self.span)) 67 return doc
68
69 - def parse(self,element):
70 LookaheadPolicy.parse(self,element) 71 try: 72 self.span = int(element.getAttribute('span')) 73 except ValueError: 74 self.span = 1
75