Package teamwork :: Package examples :: Package Thespian :: Module testScript
[hide private]
[frames] | no frames]

Source Code for Module teamwork.examples.Thespian.testScript

  1  goals = {} 
  2  
 
3 -def testFitting():
4 storyPath = [\ 5 ## Action({'actor':'granny','type':'wait'}), 6 ## Action({'actor':'red','type':'escape','object':'wolf'}), 7 ## Action({'actor':'wolf','type':'move2'}), 8 9 Action({'actor':'granny','type':'wait'}), 10 Action({'actor':'red','type':'greet-init','object':'wolf'}), 11 Action({'actor':'wolf','type':'greet-resp','object':'red'}), 12 Action({'actor':'granny','type':'wait'}), 13 Action({'actor':'red','type':'wait'}), 14 Action({'actor':'wolf','type':'enquiry','object':'red'}), 15 Action({'actor':'granny','type':'wait'}), 16 ## 17 ## Action({'actor':'granny','type':'wait'}), 18 ## Action({'actor':'red','type':'move2'}), 19 ## Action({'actor':'wolf','type':'move2'}), 20 ## Action({'actor':'granny','type':'wait'}), 21 ## Action({'actor':'red','type':'tell-about-granny','object':'wolf'}), 22 ## 23 #### Action({'actor':'red','type':'enquiry','object':'wolf'}), 24 #### Action({'actor':'wolf','type':'inform','object':'red'}), 25 #### Action({'actor':'granny','type':'wait'}), 26 #### Action({'actor':'red','type':'move2'}), 27 #### Action({'actor':'wolf','type':'move2'}), 28 ] 29 ## print 30 res = terminal.fitSequence(storyPath,'wolf',['alive','sameLocation','actAlive','resp-norm','specialRule']) 31 if res == 1: 32 goals['wolf'] = terminal.entities['wolf'].getGoals() 33 pass 34 else: 35 print "path can not be fitted" 36 print storyPath 37 print 38 39 terminal.reSetScene() 40 41 res = terminal.fitSequence(storyPath,'red',['alive','sameLocation','actAlive','resp-norm','specialRule']) 42 if res == 1: 43 goals['red'] = terminal.entities['red'].getGoals() 44 45 ## construct necessory variables 46 fixedgoals=['alive','sameLocation','actAlive','resp-norm','specialRule'] 47 ## entity = terminal.entities['red'] 48 ## origoals = entity.getGoals() 49 ## adjgoals = copy.deepcopy(origoals) 50 ## removeList = [] 51 ## for goal in adjgoals: 52 ## if goal.key in fixedgoals: 53 ## removeList.append(goal) 54 ## for goal in removeList: 55 ## adjgoals.remove(goal) 56 ## 57 ## adjgoalKeys = [] 58 ## for goal in adjgoals: 59 ## adjgoalKeys.append(goal.toKey()) 60 ## 61 ## n = len(adjgoals) 62 ## ## construct the P matrix 63 ## P = matrix (0.0,(n,n),tc = 'd') 64 ## P [::n+1]+=2 65 66 ## terminal.executeCommand('step 15') 67 ## terminal.reSetScene() 68 69 70 ## test "FitToPlotPoint" 71 ## msg = Message('entities:red:state:location = .2') 72 ## msg['type'] = '_message' 73 ## msg.forceAccept('red') 74 ## 75 ## path = [\ 76 ## Action({'actor':'red','type':'move2'}), 77 #### msg, 78 #### Action({'actor':'wolf','type':'wait'}), 79 #### 80 #### Action({'actor':'red','type':'greet-init','object':'wolf'}), 81 #### Action({'actor':'wolf','type':'greet-resp','object':'red'}), 82 #### 83 #### Action({'actor':'wolf','type':'enquiry-about-granny','object':'red'}), 84 #### Action({'actor':'red','type':'inform-about-granny','object':'wolf'}), 85 #### Action({'actor':'red','type':'wait'}), 86 #### Action({'actor':'wolf','type':'wait'}), 87 #### 88 ## ] 89 ## terminal.executeCommand('step') 90 ## terminal.FitToPlotPoint(path,fixedgoals) 91 92 ## testFitting() 93 ## 94 ## terminal.reSetSceneWithGoals(goals) 95 96 ## print "generating all possible path" 97 ## terminal.generateAllPosPath('red',fixedgoals=fixedgoals,maxstep=5, 98 ## adjgoals=adjgoals,adjgoalKeys=adjgoalKeys,P=P) 99 100 ## terminal.reSetSceneWithGoals(goals) 101 ## 102 #### print "vary model" 103 #### terminal.varyModel('red',['hunter','granny','red'],models=['evil','good']) 104 #### 105 #### terminal.reSetSceneWithGoals(goals) 106 ## 107 ## print "vary goal weights" 108 ## terminal.varyGoal('red',fixedgoals) 109 ## 110 ## terminal.reSetSceneWithGoals(goals) 111 112 ## print "vary beliefs about initial state" 113 ## terminal.varyInitialState('red',varyList) 114 ## 115 ## terminal.reSetSceneWithGoals(goals) 116 ## terminal.reSetScene() 117 118 119 ## start = ['red-wait', 'red-bye-init-wolf', 'red-enquiry-wolf', 'red-enquiry-hunter', 'red-enquiry-hunter'] 120 ## print 'red\'s belief about wolf is set to evil and red wants to be alive' 121 ## terminal.generateAllPosPath('red',fixedgoals,5,adjgoals=adjgoals, adjgoalKeys=adjgoalKeys, P=P, start=start ) 122 ## terminal.reSetSceneWithGoals(goals) 123 124 ## testing what belief change to the agent is needed to maximize the objectives 125 ## however can not changed agent's beliefs about other 126 127 ## test "suggest" for wolf to choose wait instead of eatting red 128 ## terminal.execute('act red move2') 129 ## entity = terminal.entities['wolf'] 130 ## terminal.entities.objectives = [('wolf','wait','Maximize')] 131 132 ## turn = [{'name':'wolf'}] 133 ## 134 ## result = terminal.entities.microstep(turn,hypothetical=True,explain=True) 135 ## 136 ## for key,observed in result.items(): 137 ## if key != 'explanation': 138 ## try: 139 ## actor = terminal.entities[key] 140 ## except KeyError: 141 ## continue 142 ## 143 ## terminal.suggest(actor,observed['decision']) 144 145 146 ## testing for red to move even though wolf may eat her 147 ## now automated 148 149 ## terminal.entities.objectives = [('red','move2','Maximize')] 150 ## turn = [{'name':'red'}] 151 ## 152 ## result = terminal.entities.microstep(turn,hypothetical=True,explain=True) 153 ## 154 ## for key,observed in result.items(): 155 ## if key != 'explanation': 156 ## try: 157 ## actor = terminal.entities[key] 158 ## except KeyError: 159 ## continue 160 ## 161 ## terminal.suggest(actor,observed['decision']) 162 163 164 165 red = terminal.entities['red'] 166 wolfred = red.entities['wolf']
167 168 ## this part just tests the "multistepForSuggest" function, which supply 169 ## a fixed set of actions for agents in other agent's lookahead process 170 171 ## wolfred.actions.values = [] 172 ## wolfred.actions.extra = [Action({'actor':'wolf','type':'wait'})] 173 ## wolfred.actions._generated = None 174 175 ## action = Action({'actor':'red','type':'wait'}) 176 ## sequence = red.multistepForSuggest(2, 177 ## start={'red':action}, 178 ## state=copy.deepcopy(red.getAllBeliefs())) 179 180 ## sequence = red.multistep(2, 181 ## start={'red':action}, 182 ## state=copy.deepcopy(red.getAllBeliefs())) 183 184 185 ## this part tests if "red" can be fitted to an action with the speciall lookahead process "multistepForSuggest" 186 ## allConstraints = red.fitAllAction(suggest=True) 187 ## tmp = allConstraints['[red-move2]'] 188 ## 189 ## res = terminal.adjustGoalWeights(tmp,'red',fixedgoals) 190 ## print res 191 192 ## msg = PsychMessage('entities:red:state:location = .2') 193 ## msg['type'] = '_message' 194 ## msg.forceAccept('red') 195 ## result = terminal.entities.performMsg(msg,'wolf',['red'],[],terminal.debug) 196 197 198 ## terminal.executeCommand('act red move2') 199 ## wolfred.applyPolicy() 200