def createWorld(username='******', level=0, ability='good', explanation='none', embodiment='robot', acknowledgment='no', sequence=False, root='.', ext='xml', beliefs=True): """ Creates the initial PsychSim scenario and saves it @param username: name of user ID to use in filenames @param level: robot mission level to use as template @param ability: the level of the robot's ability - good or C{True}: perfect sensors and sensor model - badSensor or C{False}: noisy sensors, but perfect model of noisy sensors - badModel: perfect sensors, but imperfect model of sensors @type ability: bool @param explanation: the type of explanation to use - none: No explanations - ability: Explanation based on robot ability provided. - abilitybenevolence: Explanation based on both robot's ability and benevolence provided. @type explanation: str @param embodiment: the robot's embodiment - robot: The robot looks like a robot - dog: The robot looks like a dog @type embodiment: str @param acknowledgment: the robot's behavior regarding the acknowledgment of errors - no: The robot does not acknowledge its errors - yes: The robot acknowledges its errors @type acknowledgment: str @param root: the root directory to use for files (default is current working directory) @param ext: the file extension for the PsychSim scenario file - xml: Save as uncompressed XML - psy: Save as bzipped XML @type ext: str @param beliefs: if C{True}, store robot's uncertain beliefs in scenario file, rather than compute them on the fly. Storing in scenario file makes the scenario a more complete model, but greatly increases file sixe. Default is C{True} @type beliefs: bool """ print "**************************createWorld***********************" print 'Username:\t%s\nLevel:\t\t%s' % (username, level + 1) print 'Ability\t\t%s\nExplanation:\t%s\nEmbodiment:\t%s\nAcknowledge:\t%s' % \ (ability,explanation,embodiment,acknowledgment) # Pre-compute symbols for this level's waypoints for point in WAYPOINTS[level]: if not point.has_key('symbol'): point['symbol'] = point['name'].replace(' ', '') world = World() world.defineState( None, 'level', int, lo=0, hi=len(WAYPOINTS) - 1, description='Static variable indicating what mission level') world.setState(None, 'level', level) world.defineState(None, 'time', float) world.setState(None, 'time', 0.) world.defineState(None, 'complete', bool) world.setState(None, 'complete', False) world.addTermination( makeTree({ 'if': trueRow('complete'), True: True, False: False })) # Buildings threats = ['none', 'NBC', 'armed'] for waypoint in WAYPOINTS[level]: if not waypoint.has_key('symbol'): waypoint['symbol'] = waypoint['name'].replace(' ', '') world.addAgent(Agent(waypoint['symbol'])) # Have we visited this waypoint? key = world.defineState(waypoint['symbol'], 'visited', bool) world.setFeature(key, False) # Are there dangerous chemicals or armed people here? key = world.defineState(waypoint['symbol'], 'danger', list, threats[:]) if waypoint.has_key('NBC') and waypoint['NBC']: world.setFeature(key, 'NBC') elif waypoint.has_key('armed') and waypoint['armed']: world.setFeature(key, 'armed') else: world.setFeature(key, 'none') key = world.defineState(waypoint['symbol'], 'recommendation', list, ['none', 'protected', 'unprotected']) world.setFeature(key, 'none') # Human human = Agent('human') world.addAgent(human) world.defineState(human.name, 'alive', bool) human.setState('alive', True) world.defineState(human.name, 'deaths', int) human.setState('deaths', 0) # Robot robot = Agent('robot') world.addAgent(robot) # Robot states world.defineState(robot.name, 'waypoint', list, [point['symbol'] for point in WAYPOINTS[level]]) robot.setState('waypoint', WAYPOINTS[level][getStart(level)]['symbol']) world.defineState(robot.name, 'explanation', list, [ 'none', 'ability', 'abilitybenevolence', 'abilityconfidence', 'confidence' ]) robot.setState('explanation', explanation) world.defineState(robot.name, 'embodiment', list, ['robot', 'dog']) robot.setState('embodiment', embodiment) world.defineState(robot.name, 'acknowledgment', list, ['no', 'yes']) robot.setState('acknowledgment', acknowledgment) world.defineState(robot.name, 'ability', list, ['badSensor', 'badModel', 'good']) if ability is True: # Backward compatibility with boolean ability ability = 'good' elif ability is False: ability = 'badSensor' robot.setState('ability', ability) # State of the robot's sensors world.defineState(robot.name, 'sensorModel', list, ['good', 'bad']) robot.setState('sensorModel', 'good') world.defineState(robot.name, 'command', list, ['none'] + [point['symbol'] for point in WAYPOINTS[level]]) robot.setState('command', 'none') # Actions for end in range(len(WAYPOINTS[level])): symbol = WAYPOINTS[level][end]['symbol'] # Robot movement action = robot.addAction({'verb': 'moveto', 'object': symbol}) # Legal if no contradictory command tree = makeTree({ 'if': equalRow(stateKey(robot.name, 'command'), 'none'), True: True, False: { 'if': equalRow(stateKey(robot.name, 'command'), symbol), True: True, False: False } }) robot.setLegal(action, tree) # Dynamics of robot's location tree = makeTree( setToConstantMatrix(stateKey(action['subject'], 'waypoint'), symbol)) world.setDynamics(stateKey(action['subject'], 'waypoint'), action, tree) # Dynamics of visited flag key = stateKey(symbol, 'visited') tree = makeTree(setTrueMatrix(key)) world.setDynamics(key, action, tree) # Dynamics of time key = stateKey(None, 'time') tree = setToConstantMatrix(key, 0.) for start in range(len(WAYPOINTS[level])): if start != end: startsymbol = WAYPOINTS[level][start]['symbol'] if sequence: # Distance is measured by level sequence distance = abs(start - end) * 50 else: try: distance = DISTANCES[WAYPOINTS[level][start]['name']][ WAYPOINTS[level][end]['name']] except KeyError: try: distance = DISTANCES[WAYPOINTS[level][end][ 'name']][WAYPOINTS[level][start]['name']] except KeyError: distance = 250 tree = { 'if': equalRow(stateKey(action['subject'], 'waypoint'), startsymbol), True: setToConstantMatrix(key, float(distance) / 1000.), False: tree } world.setDynamics(key, action, makeTree(tree)) # Human entry: Dead or alive if unprotected? key = stateKey(human.name, 'alive') action = robot.addAction({ 'verb': 'recommend unprotected', 'object': symbol }) tree = makeTree({ 'if': equalRow(stateKey(symbol, 'danger'), 'none'), True: setTrueMatrix(key), False: setFalseMatrix(key) }) world.setDynamics(key, action, tree) robot.setLegal(action, makeTree(False)) # Human entry: How much "time" if protected? action = robot.addAction({ 'verb': 'recommend protected', 'object': symbol }) key = stateKey(None, 'time') world.setDynamics(key, action, makeTree(setToConstantMatrix(key, 0.25))) robot.setLegal(action, makeTree(False)) # Robot goals goal = minimizeFeature(stateKey(None, 'time')) robot.setReward(goal, 2.) goal = maximizeFeature(stateKey(human.name, 'alive')) robot.setReward(goal, 1.) for point in WAYPOINTS[level]: robot.setReward(maximizeFeature(stateKey(point['symbol'], 'visited')), 1.) if beliefs: # omega = 'danger' world.defineVariable(robot.name, ActionSet) # Robot beliefs world.setModel(robot.name, True) value = 1. / float(len(WAYPOINTS[level])) # tree = KeyedVector({CONSTANT: world.value2float(omega,'none')}) for index in range(len(WAYPOINTS[level])): waypoint = WAYPOINTS[level][index] key = stateKey(waypoint['symbol'], 'danger') # if index > 0: # Starting state is safe robot.setBelief( key, psychsim.probability.Distribution({ 'NBC': value / 2., 'armed': value / 2., 'none': 1. - value })) # Observation function # tree = {'if': equalRow(stateKey(robot.name,'waypoint'),waypoint['symbol']), # True: generateO(world,key), # False: tree} # robot.defineObservation(omega,makeTree(tree),domain=list,lo=['none','NBC','armed']) robot.defineObservation('microphone', makeTree(None), None, domain=list, lo=['nobody', 'friendly', 'suspicious']) robot.defineObservation('NBCsensor', makeTree(None), None, domain=bool) robot.defineObservation('camera', makeTree(None), None, domain=bool) else: robot.defineObservation('microphone', makeTree(None), None, domain=list, lo=['nobody', 'friendly', 'suspicious']) robot.defineObservation('NBCsensor', makeTree(None), None, domain=bool) robot.defineObservation('camera', makeTree(None), None, domain=bool) robot.setAttribute('horizon', 1) world.setOrder([robot.name]) filename = getFilename(username, level, ext, root) world.save(filename, ext == 'psy') WriteLogData('%s user %s, level %d, ability %s, explanation %s' % \ (CREATE_TAG,username,level,ability,explanation),username,level,root=root) return world
class TestAgents(unittest.TestCase): def setUp(self): # Create world self.world = World() # Create agents self.tom = Agent('Tom') self.world.addAgent(self.tom) self.jerry = Agent('Jerry') self.world.addAgent(self.jerry) def addStates(self): """Create state features""" self.world.defineState(self.tom.name,'health',int,lo=0,hi=100, description='%s\'s wellbeing' % (self.tom.name)) self.world.setState(self.tom.name,'health',50) self.world.defineState(self.jerry.name,'health',int,lo=0,hi=100, description='%s\'s wellbeing' % (self.jerry.name)) self.world.setState(self.jerry.name,'health',50) def addActions(self): """Create actions""" self.chase = self.tom.addAction({'verb': 'chase','object': self.jerry.name}) self.hit = self.tom.addAction({'verb': 'hit','object': self.jerry.name}) self.run = self.jerry.addAction({'verb': 'run away'}) self.trick = self.jerry.addAction({'verb': 'trick','object': self.tom.name}) def addDynamics(self): """Create dynamics""" tree = makeTree(incrementMatrix(stateKey(self.jerry.name,'health'),-10)) self.world.setDynamics(stateKey(self.jerry.name,'health'),self.hit,tree,enforceMin=True) def addModels(self,rationality=1.): self.tom.addModel('friend',rationality=rationality,parent=True) self.tom.setReward(maximizeFeature(stateKey(self.jerry.name,'health')),1.,'friend') self.tom.addModel('foe',rationality=rationality,parent=True) self.tom.setReward(minimizeFeature(stateKey(self.jerry.name,'health')),1.,'foe') def saveload(self): """Write scenario to file and then load from scratch""" self.world.save('/tmp/psychsim_test.psy') self.world = World('/tmp/psychsim_test.psy') self.tom = self.world.agents[self.tom.name] self.jerry = self.world.agents[self.jerry.name] def testEnumeratedState(self): self.addActions() self.world.defineVariable(self.tom.name,ActionSet) self.world.defineState(self.tom.name,'status',list,['dead','injured','healthy']) self.world.setState(self.tom.name,'status','healthy') goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'healthy') self.tom.setReward(goal,1.) goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'injured') self.jerry.setReward(goal,1.) self.saveload() self.assertEqual(len(self.world.state[None]),1) vector = self.world.state[None].domain()[0] tVal = self.tom.reward(vector) self.assertAlmostEqual(tVal,1.,8) jVal = self.jerry.reward(vector) self.assertAlmostEqual(jVal,0.,8) for action in self.tom.actions: encoding = self.world.value2float(self.tom.name,action) self.assertEqual(action,self.world.float2value(self.tom.name,encoding)) def testBeliefModels(self): self.addStates() self.addActions() self.addDynamics() self.world.setOrder([self.tom.name]) self.tom.addModel('optimist') self.tom.setBelief(stateKey(self.jerry.name,'health'),20,'optimist') self.tom.addModel('pessimist') self.world.setModel(self.jerry.name,True) self.world.setMentalModel(self.jerry.name,self.tom.name,{'optimist': 0.5,'pessimist': 0.5}) actions = {self.tom.name: self.hit} self.world.step(actions) vector = self.world.state[None].domain()[0] beliefs = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector)) for belief in beliefs.domain(): model = self.world.getModel(self.tom.name,belief) if self.tom.models[model].has_key('beliefs'): nested = self.tom.models[model]['beliefs'] self.assertEqual(len(nested),1) nested = nested.domain()[0] self.assertEqual(len(nested),1) self.assertAlmostEqual(nested[stateKey(self.jerry.name,'health')],10.,8) def testObservation(self): self.addStates() self.addActions() self.addDynamics() self.world.setOrder([self.tom.name]) self.world.setModel(self.jerry.name,True) key = stateKey(self.jerry.name,'health') self.jerry.setBelief(key,Distribution({20: 0.5, 50: 0.5})) tree = makeTree({'if': thresholdRow(key,40), True: {'distribution': [(KeyedVector({CONSTANT: 50}),.8), (KeyedVector({CONSTANT: 20}),.2)]}, False: {'distribution': [(KeyedVector({CONSTANT: 50}),.2), (KeyedVector({CONSTANT: 20}),.8)]}}) self.jerry.defineObservation(key,tree) actions = {self.tom.name: self.hit} vector = self.world.state[None].domain()[0] omegaDist = self.jerry.observe(vector,actions) for omega in omegaDist.domain(): new = KeyedVector(vector) model = self.jerry.index2model(self.jerry.stateEstimator(vector,new,omega)) beliefs = self.jerry.models[model]['beliefs'] if omega[key] > 30: # We observed a high value, so we should have a stronger belief in the higher value # which is now 40 after the hit for belief in beliefs.domain(): if beliefs[belief] > 0.5: self.assertAlmostEqual(belief[key],40,8) else: self.assertAlmostEqual(belief[key],10,8) else: # We observed a low value, so we should have a stronger belief in the lower value # which is now 10 after the hit for belief in beliefs.domain(): if beliefs[belief] < 0.5: self.assertAlmostEqual(belief[key],40,8) else: self.assertAlmostEqual(belief[key],10,8) def testUnobservedAction(self): self.addStates() self.addActions() self.addDynamics() self.addModels() self.world.setOrder([self.tom.name]) self.world.setModel(self.jerry.name,True) self.jerry.setBelief(stateKey(self.jerry.name,'health'),50) self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) tree = makeTree(True) self.jerry.defineObservation(self.tom.name,tree,self.hit,domain=ActionSet) tree = makeTree({'distribution': [(True,0.25),(False,0.75)]}) self.jerry.defineObservation(self.tom.name,tree,self.chase,domain=ActionSet) vector = self.world.state[None].domain()[0] self.saveload() self.world.step({self.tom.name: self.hit}) vector = self.world.state[None].domain()[0] def testRewardModels(self): self.addStates() self.addActions() self.addDynamics() self.addModels() self.world.setOrder([self.tom.name]) # Add Jerry's model to the world (so that it gets updated) self.world.setModel(self.jerry.name,True) # Give Jerry uncertainty about Tom self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) self.saveload() # Hitting should make Jerry think Tom is more of a foe actions = {self.tom.name: self.hit} self.world.step(actions) vector = self.world.state[None].domain()[0] belief01 = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector)) key = modelKey(self.tom.name) for belief in belief01.domain(): if self.tom.index2model(belief[key]) == 'foe': prob01 = belief01[belief] break self.assertGreater(prob01,0.5) # If we think of Tom as even more of an optimizer, then our update should be stronger self.tom.setAttribute('rationality',10.,'foe') self.tom.setAttribute('rationality',10.,'friend') self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) self.world.step(actions) vector = self.world.state[None].domain()[0] model = self.world.getModel(self.jerry.name,vector) belief10 = self.jerry.getAttribute('beliefs',model) key = modelKey(self.tom.name) for belief in belief10.domain(): if self.tom.index2model(belief[key]) == 'foe': prob10 = belief10[belief] break self.assertGreater(prob10,prob01) # If we keep the same models, but get another observation, we should update even more self.world.step(actions) vector = self.world.state[None].domain()[0] model = self.world.getModel(self.jerry.name,vector) belief1010 = self.jerry.getAttribute('beliefs',model) key = modelKey(self.tom.name) for belief in belief1010.domain(): if self.tom.index2model(belief[key]) == 'foe': prob1010 = belief1010[belief] break self.assertGreater(prob1010,prob10) def testDynamics(self): self.world.setOrder([self.tom.name]) self.addStates() self.addActions() self.addDynamics() key = stateKey(self.jerry.name,'health') self.assertEqual(len(self.world.state[None]),1) vector = self.world.state[None].domain()[0] self.assertTrue(vector.has_key(stateKey(self.tom.name,'health'))) self.assertTrue(vector.has_key(turnKey(self.tom.name))) self.assertTrue(vector.has_key(key)) self.assertTrue(vector.has_key(CONSTANT)) self.assertEqual(len(vector),4) self.assertEqual(vector[stateKey(self.tom.name,'health')],50) self.assertEqual(vector[key],50) outcome = self.world.step({self.tom.name: self.chase}) for i in range(7): self.assertEqual(len(self.world.state[None]),1) vector = self.world.state[None].domain()[0] self.assertTrue(vector.has_key(stateKey(self.tom.name,'health'))) self.assertTrue(vector.has_key(turnKey(self.tom.name))) self.assertTrue(vector.has_key(key)) self.assertTrue(vector.has_key(CONSTANT)) self.assertEqual(len(vector),4) self.assertEqual(vector[stateKey(self.tom.name,'health')],50) self.assertEqual(vector[key],max(50-10*i,0)) outcome = self.world.step({self.tom.name: self.hit}) self.saveload() def testRewardOnOthers(self): self.addStates() self.addActions() self.addDynamics() self.world.setOrder([self.tom.name]) vector = self.world.state[None].domain()[0] # Create Jerry's goals goal = maximizeFeature(stateKey(self.jerry.name,'health')) self.jerry.setReward(goal,1.) jVal = -self.jerry.reward(vector) # Create Tom's goals from scratch minGoal = minimizeFeature(stateKey(self.jerry.name,'health')) self.tom.setReward(minGoal,1.) self.saveload() tRawVal = self.tom.reward(vector) self.assertAlmostEqual(jVal,tRawVal,8) # Create Tom's goals as a function of Jerry's self.tom.models[True]['R'].clear() self.tom.setReward(self.jerry.name,-1.) self.saveload() tFuncVal = self.tom.reward(vector) self.assertAlmostEqual(tRawVal,tFuncVal,8) # Test effect of functional reward on value function self.tom.setHorizon(1) self.saveload() vHit = self.tom.value(vector,self.hit)['V'] vChase = self.tom.value(vector,self.chase)['V'] self.assertAlmostEqual(vHit,vChase+.1,8) def testReward(self): self.addStates() key = stateKey(self.jerry.name,'health') goal = makeTree({'if': thresholdRow(key,5), True: KeyedVector({key: -2}), False: KeyedVector({key: -1})}) goal = goal.desymbolize(self.world.symbols) self.jerry.setReward(goal,1.) R = self.jerry.models[True]['R'] self.assertEqual(len(R),1) newGoal = R.keys()[0] self.assertEqual(newGoal,goal) self.assertAlmostEqual(R[goal],1.,8) self.jerry.setReward(goal,2.) self.assertEqual(len(R),1) self.assertEqual(R.keys()[0],goal) self.assertAlmostEqual(R[goal],2.,8) def testTurnDynamics(self): self.addStates() self.addActions() self.world.setOrder([self.tom.name,self.jerry.name]) self.assertEqual(self.world.maxTurn,1) self.saveload() vector = self.world.state[None].domain()[0] jTurn = turnKey(self.jerry.name) tTurn = turnKey(self.tom.name) self.assertEqual(self.world.next(),[self.tom.name]) self.assertEqual(vector[tTurn],0) self.assertEqual(vector[jTurn],1) self.world.step() vector = self.world.state[None].domain()[0] self.assertEqual(self.world.next(),[self.jerry.name]) self.assertEqual(vector[tTurn],1) self.assertEqual(vector[jTurn],0) self.world.step() vector = self.world.state[None].domain()[0] self.assertEqual(self.world.next(),[self.tom.name]) self.assertEqual(vector[tTurn],0) self.assertEqual(vector[jTurn],1) # Try some custom dynamics self.world.setTurnDynamics(self.tom.name,self.hit,makeTree(noChangeMatrix(tTurn))) self.world.setTurnDynamics(self.jerry.name,self.hit,makeTree(noChangeMatrix(tTurn))) self.world.step() vector = self.world.state[None].domain()[0] self.assertEqual(self.world.next(),[self.tom.name]) self.assertEqual(vector[tTurn],0) self.assertEqual(vector[jTurn],1) self.world.step({self.tom.name: self.chase}) vector = self.world.state[None].domain()[0] self.assertEqual(self.world.next(),[self.jerry.name]) self.assertEqual(vector[tTurn],1) self.assertEqual(vector[jTurn],0) def testStatic(self): self.addStates() self.addActions() self.addDynamics() self.addModels() self.world.setModel(self.jerry.name,True) self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) self.world.setOrder([self.tom.name]) vector = self.world.state[None].domain()[0] model = self.world.getModel(self.jerry.name,vector) belief0 = self.jerry.models[model]['beliefs'] result = self.world.step({self.tom.name: self.hit}) vector = self.world.state[None].domain()[0] model = self.world.getModel(self.jerry.name,vector) belief1 = self.jerry.models[model]['beliefs'] key = modelKey(self.tom.name) for vector in belief0.domain(): if self.tom.index2model(vector[key]) == 'friend': self.assertGreater(belief0[vector],belief1[vector]) else: self.assertGreater(belief1[vector],belief0[vector]) # Now with the static beliefs self.jerry.setAttribute('static',True,model) self.saveload() self.world.step() vector = self.world.state[None].domain()[0] model = self.world.getModel(self.jerry.name,vector) belief2 = self.jerry.models[model]['beliefs'] for vector in belief1.domain(): self.assertAlmostEqual(belief1[vector],belief2[vector],8)
def scenarioCreationUseCase(enemy='Sylvania',model='powell',web=False, fCollapse=None,sCollapse=None,maxRounds=15): """ An example of how to create a scenario @param enemy: the name of the agent-controlled side, i.e., Freedonia's opponent (default: Sylvania) @type enemy: str @param model: which model do we use (default is "powell") @type model: powell or slantchev @param web: if C{True}, then create the web-based experiment scenario (default: C{False}) @type web: bool @param fCollapse: the probability that Freedonia collapses (under powell, default: 0.1) or loses battle (under slantchev, default: 0.7) @type fCollapse: float @param sCollapse: the probability that Sylvania collapses, under powell (default: 0.1) @type sCollapse: float @param maxRounds: the maximum number of game rounds (default: 15) @type maxRounds: int @return: the scenario created @rtype: L{World} """ # Handle defaults for battle probabilities, under each model posLo = 0 posHi = 10 if fCollapse is None: if model == 'powell': fCollapse = 0.1 elif model == 'slantchev': fCollapse = 0.7 if sCollapse is None: sCollapse = 0.1 # Create scenario world = World() # Agents free = Agent('Freedonia') world.addAgent(free) sylv = Agent(enemy) world.addAgent(sylv) # User state world.defineState(free.name,'troops',int,lo=0,hi=50000, description='Number of troops you have left') free.setState('troops',40000) world.defineState(free.name,'territory',int,lo=0,hi=100, description='Percentage of disputed territory owned by you') free.setState('territory',15) world.defineState(free.name,'cost',int,lo=0,hi=50000, description='Number of troops %s loses in an attack' % (free.name)) free.setState('cost',2000) world.defineState(free.name,'position',int,lo=posLo,hi=posHi, description='Current status of war (%d=%s is winner, %d=you are winner)' % (posLo,sylv.name,posHi)) free.setState('position',5) world.defineState(free.name,'offered',int,lo=0,hi=100, description='Percentage of disputed territory that %s last offered to you' % (sylv.name)) free.setState('offered',0) if model == 'slantchev': # Compute new value for territory only *after* computing new value for position world.addDependency(stateKey(free.name,'territory'),stateKey(free.name,'position')) # Agent state world.defineState(sylv.name,'troops',int,lo=0,hi=500000, description='Number of troops %s has left' % (sylv.name)) sylv.setState('troops',30000) world.defineState(sylv.name,'cost',int,lo=0,hi=50000, description='Number of troops %s loses in an attack' % (sylv.name)) sylv.setState('cost',2000) world.defineState(sylv.name,'offered',int,lo=0,hi=100, description='Percentage of disputed territory that %s last offered to %s' % (free.name,sylv.name)) sylv.setState('offered',0) # World state world.defineState(None,'treaty',bool, description='Have the two sides reached an agreement?') world.setState(None,'treaty',False) # Stage of negotiation, illustrating the use of an enumerated state feature world.defineState(None,'phase',list,['offer','respond','rejection','end','paused','engagement'], description='The current stage of the negotiation game') world.setState(None,'phase','paused') # Game model, static descriptor world.defineState(None,'model',list,['powell','slantchev'], description='The model underlying the negotiation game') world.setState(None,'model',model) # Round of negotiation world.defineState(None,'round',int,description='The current round of the negotiation') world.setState(None,'round',0) if not web: # Relationship value key = world.defineRelation(free.name,sylv.name,'trusts') world.setFeature(key,0.) # Game over if there is a treaty world.addTermination(makeTree({'if': trueRow(stateKey(None,'treaty')), True: True, False: False})) # Game over if Freedonia has no territory world.addTermination(makeTree({'if': thresholdRow(stateKey(free.name,'territory'),1), True: False, False: True}) ) # Game over if Freedonia has all the territory world.addTermination(makeTree({'if': thresholdRow(stateKey(free.name,'territory'),99), True: True, False: False})) # Game over if number of rounds exceeds limit world.addTermination(makeTree({'if': thresholdRow(stateKey(None,'round'),maxRounds), True: True, False: False})) # Turn order: Uncomment the following if you want agents to act in parallel # world.setOrder([set(world.agents.keys())]) # Turn order: Uncomment the following if you want agents to act sequentially world.setOrder([free.name,sylv.name]) # User actions freeBattle = free.addAction({'verb': 'attack','object': sylv.name}) for amount in range(20,100,20): free.addAction({'verb': 'offer','object': sylv.name,'amount': amount}) if model == 'powell': # Powell has null stages freeNOP = free.addAction({'verb': 'continue'}) elif model == 'slantchev': # Slantchev has both sides receiving offers free.addAction({'verb': 'accept offer','object': sylv.name}) free.addAction({'verb': 'reject offer','object': sylv.name}) # Agent actions sylvBattle = sylv.addAction({'verb': 'attack','object': free.name}) sylvAccept = sylv.addAction({'verb': 'accept offer','object': free.name}) sylvReject = sylv.addAction({'verb': 'reject offer','object': free.name}) if model == 'powell': # Powell has null stages sylvNOP = sylv.addAction({'verb': 'continue'}) elif model == 'slantchev': # Slantchev has both sides making offers for amount in range(10,100,10): sylv.addAction({'verb': 'offer','object': free.name,'amount': amount}) # Restrictions on when actions are legal, based on phase of game for action in filterActions({'verb': 'offer'},free.actions | sylv.actions): agent = world.agents[action['subject']] agent.setLegal(action,makeTree({'if': equalRow(stateKey(None,'phase'),'offer'), True: True, # Offers are legal in the offer phase False: False})) # Offers are illegal in all other phases if model == 'powell': # Powell has a special rejection phase for action in [freeNOP,freeBattle]: free.setLegal(action,makeTree({'if': equalRow(stateKey(None,'phase'),'rejection'), True: True, # Attacking and doing nothing are legal only in rejection phase False: False})) # Attacking and doing nothing are illegal in all other phases # Once offered, agent can respond if model == 'powell': # Under Powell, only Sylvania has to respond, and it can attack responses = [sylvBattle,sylvAccept,sylvReject] elif model == 'slantchev': # Under Slantchev, only accept/reject responses = filterActions({'verb': 'accept offer'},free.actions | sylv.actions) responses += filterActions({'verb': 'reject offer'},free.actions | sylv.actions) for action in responses: agent = world.agents[action['subject']] agent.setLegal(action,makeTree({'if': equalRow(stateKey(None,'phase'),'respond'), True: True, # Offeree must act in the response phase False: False})) # Offeree cannot act in any other phase if model == 'powell': # NOP is legal in exactly opposite situations to all other actions sylv.setLegal(sylvNOP,makeTree({'if': equalRow(stateKey(None,'phase'),'end'), True: True, # Sylvania does not do anything in the null phase after Freedonia responds to rejection False: False})) # Sylvania must act in its other phases if model == 'slantchev': # Attacking legal only under engagement phase for action in filterActions({'verb': 'attack'},free.actions | sylv.actions): agent = world.agents[action['subject']] agent.setLegal(action,makeTree({'if': equalRow(stateKey(None,'phase'),'engagement'), True: True, # Attacking legal only in engagement False: False})) # Attacking legal every other phase # Goals for Freedonia goalFTroops = maximizeFeature(stateKey(free.name,'troops')) free.setReward(goalFTroops,1.) goalFTerritory = maximizeFeature(stateKey(free.name,'territory')) free.setReward(goalFTerritory,1.) # Goals for Sylvania goalSTroops = maximizeFeature(stateKey(sylv.name,'troops')) sylv.setReward(goalSTroops,1.) goalSTerritory = minimizeFeature(stateKey(free.name,'territory')) sylv.setReward(goalSTerritory,1.) # Possible goals applicable to both goalAgreement = maximizeFeature(stateKey(None,'treaty')) # Silly goal, provided as an example of an achievement goal goalAchieve = achieveFeatureValue(stateKey(None,'phase'),'respond') # Horizons if model == 'powell': free.setAttribute('horizon',4) sylv.setAttribute('horizon',4) elif model == 'slantchev': free.setAttribute('horizon',6) sylv.setAttribute('horizon',6) # Discount factors free.setAttribute('discount',-1) sylv.setAttribute('discount',-1) # Levels of belief free.setRecursiveLevel(2) sylv.setRecursiveLevel(2) # Dynamics of battle freeTroops = stateKey(free.name,'troops') freeTerr = stateKey(free.name,'territory') sylvTroops = stateKey(sylv.name,'troops') # Effect of fighting for action in filterActions({'verb': 'attack'},free.actions | sylv.actions): # Effect on troops (cost of battle) tree = makeTree(addFeatureMatrix(freeTroops,stateKey(free.name,'cost'),-1.)) world.setDynamics(freeTroops,action,tree,enforceMin=not web) tree = makeTree(addFeatureMatrix(sylvTroops,stateKey(sylv.name,'cost'),-1.)) world.setDynamics(sylvTroops,action,tree,enforceMin=not web) if model == 'powell': # Effect on territory (probability of collapse) tree = makeTree({'distribution': [ ({'distribution': [(setToConstantMatrix(freeTerr,100),1.-fCollapse), # Sylvania collapses, Freedonia does not (noChangeMatrix(freeTerr), fCollapse)]}, # Both collapse sCollapse), ({'distribution': [(setToConstantMatrix(freeTerr,0),fCollapse), # Freedonia collapses, Sylvania does not (noChangeMatrix(freeTerr), 1.-fCollapse)]}, # Neither collapses 1.-sCollapse)]}) world.setDynamics(freeTerr,action,tree) elif model == 'slantchev': # Effect on position pos = stateKey(free.name,'position') tree = makeTree({'distribution': [(incrementMatrix(pos,1),1.-fCollapse), # Freedonia wins battle (incrementMatrix(pos,-1),fCollapse)]}) # Freedonia loses battle world.setDynamics(pos,action,tree) # Effect on territory tree = makeTree({'if': thresholdRow(pos,posHi-.5), True: setToConstantMatrix(freeTerr,100), # Freedonia won False: {'if': thresholdRow(pos,posLo+.5), True: noChangeMatrix(freeTerr), False: setToConstantMatrix(freeTerr,0)}}) # Freedonia lost world.setDynamics(freeTerr,action,tree) # Dynamics of offers for index in range(2): atom = Action({'subject': world.agents.keys()[index],'verb': 'offer', 'object': world.agents.keys()[1-index]}) if atom['subject'] == free.name or model != 'powell': offer = stateKey(atom['object'],'offered') amount = actionKey('amount') tree = makeTree({'if': trueRow(stateKey(None,'treaty')), True: noChangeMatrix(offer), False: setToConstantMatrix(offer,amount)}) world.setDynamics(offer,atom,tree,enforceMax=not web) # Dynamics of treaties for action in filterActions({'verb': 'accept offer'},free.actions | sylv.actions): # Accepting an offer means that there is now a treaty key = stateKey(None,'treaty') tree = makeTree(setTrueMatrix(key)) world.setDynamics(key,action,tree) # Accepting offer sets territory offer = stateKey(action['subject'],'offered') territory = stateKey(free.name,'territory') if action['subject'] == free.name: # Freedonia accepts sets territory to last offer tree = makeTree(setToFeatureMatrix(territory,offer)) world.setDynamics(freeTerr,action,tree) else: # Sylvania accepts sets territory to 1-last offer tree = makeTree(setToFeatureMatrix(territory,offer,pct=-1.,shift=100.)) world.setDynamics(freeTerr,action,tree) # Dynamics of phase phase = stateKey(None,'phase') roundKey = stateKey(None,'round') # OFFER -> RESPOND for index in range(2): action = Action({'subject': world.agents.keys()[index],'verb': 'offer', 'object': world.agents.keys()[1-index]}) if action['subject'] == free.name or model != 'powell': tree = makeTree(setToConstantMatrix(phase,'respond')) world.setDynamics(phase,action,tree) # RESPOND -> REJECTION or ENGAGEMENT for action in filterActions({'verb': 'reject offer'},free.actions | sylv.actions): if model == 'powell': tree = makeTree(setToConstantMatrix(phase,'rejection')) elif model == 'slantchev': tree = makeTree(setToConstantMatrix(phase,'engagement')) world.setDynamics(phase,action,tree) # accepting -> OFFER for action in filterActions({'verb': 'accept offer'},free.actions | sylv.actions): tree = makeTree(setToConstantMatrix(phase,'offer')) world.setDynamics(phase,action,tree) # attacking -> OFFER for action in filterActions({'verb': 'attack'},free.actions | sylv.actions): tree = makeTree(setToConstantMatrix(phase,'offer')) world.setDynamics(phase,action,tree) if action['subject'] == sylv.name or model == 'slantchev': tree = makeTree(incrementMatrix(roundKey,1)) world.setDynamics(roundKey,action,tree) if model == 'powell': # REJECTION -> END for atom in [freeNOP,freeBattle]: tree = makeTree(setToConstantMatrix(phase,'end')) world.setDynamics(phase,atom,tree) # END -> OFFER atom = Action({'subject': sylv.name,'verb': 'continue'}) tree = makeTree(setToConstantMatrix(phase,'offer')) world.setDynamics(phase,atom,tree) tree = makeTree(incrementMatrix(roundKey,1)) world.setDynamics(roundKey,atom,tree) if not web: # Relationship dynamics: attacking is bad for trust atom = Action({'subject': sylv.name,'verb': 'attack','object': free.name}) key = binaryKey(free.name,sylv.name,'trusts') tree = makeTree(approachMatrix(key,0.1,-1.)) world.setDynamics(key,atom,tree) # Handcrafted policy for Freedonia # free.setPolicy(makeTree({'if': equalRow('phase','respond'), # # Accept an offer greater than 50 # True: {'if': thresholdRow(stateKey(free.name,'offered'),50), # True: Action({'subject': free.name,'verb': 'accept offer','object': sylv.name}), # False: Action({'subject': free.name,'verb': 'reject offer','object': sylv.name})}, # False: {'if': equalRow('phase','engagement'), # # Attack during engagement phase # True: Action({'subject': free.name,'verb': 'attack','object': sylv.name}), # # Agent decides how what to do otherwise # False: False}})) # Mental models of enemy # Example of creating a model with incorrect reward all at once (a version of Freedonia who cares about reaching agreement as well) # sylv.addModel('false',R={goalSTroops: 10.,goalSTerritory: 1.,goalAgreement: 1.}, # rationality=1.,selection='distribution',parent=True) # Example of creating a model with incorrect beliefs sylv.addModel('false',rationality=10.,selection='distribution',parent=True) key = stateKey(free.name,'position') # Sylvania believes position to be fixed at 3 sylv.setBelief(key,3,'false') # Freedonia is truly unsure about position (50% chance of being 7, 50% of being 3) world.setModel(free.name,True) free.setBelief(key,Distribution({7: 0.5,3: 0.5}),True) # Observations about military position tree = makeTree({'if': thresholdRow(key,1), True: {'if': thresholdRow(key,9), True: {'distribution': [(KeyedVector({key: 1}),0.9), (KeyedVector({key: 1,CONSTANT: -1}),0.1)]}, False: {'distribution': [(KeyedVector({key: 1}),0.8), (KeyedVector({key: 1,CONSTANT: -1}),0.1), (KeyedVector({key: 1,CONSTANT: 1}),0.1)]}}, False: {'distribution': [(KeyedVector({key: 1}),0.9), (KeyedVector({key: 1,CONSTANT: 1}),0.1)]}}) free.defineObservation(key,tree) # Example of setting model parameters separately sylv.addModel('true',parent=True) sylv.setAttribute('rationality',10.,'true') # Override real agent's rationality with this value sylv.setAttribute('selection','distribution','true') world.setMentalModel(free.name,sylv.name,{'false': 0.9,'true': 0.1}) # Goal of fooling Sylvania goalDeception = achieveFeatureValue(modelKey(sylv.name),sylv.model2index('false')) return world
def scenarioCreationUseCase(enemy='Sylvania', model='powell', web=False, fCollapse=None, sCollapse=None, maxRounds=15): """ An example of how to create a scenario @param enemy: the name of the agent-controlled side, i.e., Freedonia's opponent (default: Sylvania) @type enemy: str @param model: which model do we use (default is "powell") @type model: powell or slantchev @param web: if C{True}, then create the web-based experiment scenario (default: C{False}) @type web: bool @param fCollapse: the probability that Freedonia collapses (under powell, default: 0.1) or loses battle (under slantchev, default: 0.7) @type fCollapse: float @param sCollapse: the probability that Sylvania collapses, under powell (default: 0.1) @type sCollapse: float @param maxRounds: the maximum number of game rounds (default: 15) @type maxRounds: int @return: the scenario created @rtype: L{World} """ # Handle defaults for battle probabilities, under each model posLo = 0 posHi = 10 if fCollapse is None: if model == 'powell': fCollapse = 0.1 elif model == 'slantchev': fCollapse = 0.7 if sCollapse is None: sCollapse = 0.1 # Create scenario world = World() # Agents free = Agent('Freedonia') world.addAgent(free) sylv = Agent(enemy) world.addAgent(sylv) # User state world.defineState(free.name, 'troops', int, lo=0, hi=50000, description='Number of troops you have left') free.setState('troops', 40000) world.defineState( free.name, 'territory', int, lo=0, hi=100, description='Percentage of disputed territory owned by you') free.setState('territory', 15) world.defineState(free.name, 'cost', int, lo=0, hi=50000, description='Number of troops %s loses in an attack' % (free.name)) free.setState('cost', 2000) world.defineState( free.name, 'position', int, lo=posLo, hi=posHi, description='Current status of war (%d=%s is winner, %d=you are winner)' % (posLo, sylv.name, posHi)) free.setState('position', 5) world.defineState( free.name, 'offered', int, lo=0, hi=100, description= 'Percentage of disputed territory that %s last offered to you' % (sylv.name)) free.setState('offered', 0) if model == 'slantchev': # Compute new value for territory only *after* computing new value for position world.addDependency(stateKey(free.name, 'territory'), stateKey(free.name, 'position')) # Agent state world.defineState(sylv.name, 'troops', int, lo=0, hi=500000, description='Number of troops %s has left' % (sylv.name)) sylv.setState('troops', 30000) world.defineState(sylv.name, 'cost', int, lo=0, hi=50000, description='Number of troops %s loses in an attack' % (sylv.name)) sylv.setState('cost', 2000) world.defineState( sylv.name, 'offered', int, lo=0, hi=100, description= 'Percentage of disputed territory that %s last offered to %s' % (free.name, sylv.name)) sylv.setState('offered', 0) # World state world.defineState(None, 'treaty', bool, description='Have the two sides reached an agreement?') world.setState(None, 'treaty', False) # Stage of negotiation, illustrating the use of an enumerated state feature world.defineState( None, 'phase', list, ['offer', 'respond', 'rejection', 'end', 'paused', 'engagement'], description='The current stage of the negotiation game') world.setState(None, 'phase', 'paused') # Game model, static descriptor world.defineState(None, 'model', list, ['powell', 'slantchev'], description='The model underlying the negotiation game') world.setState(None, 'model', model) # Round of negotiation world.defineState(None, 'round', int, description='The current round of the negotiation') world.setState(None, 'round', 0) if not web: # Relationship value key = world.defineRelation(free.name, sylv.name, 'trusts') world.setFeature(key, 0.) # Game over if there is a treaty world.addTermination( makeTree({ 'if': trueRow(stateKey(None, 'treaty')), True: True, False: False })) # Game over if Freedonia has no territory world.addTermination( makeTree({ 'if': thresholdRow(stateKey(free.name, 'territory'), 1), True: False, False: True })) # Game over if Freedonia has all the territory world.addTermination( makeTree({ 'if': thresholdRow(stateKey(free.name, 'territory'), 99), True: True, False: False })) # Game over if number of rounds exceeds limit world.addTermination( makeTree({ 'if': thresholdRow(stateKey(None, 'round'), maxRounds), True: True, False: False })) # Turn order: Uncomment the following if you want agents to act in parallel # world.setOrder([set(world.agents.keys())]) # Turn order: Uncomment the following if you want agents to act sequentially world.setOrder([free.name, sylv.name]) # User actions freeBattle = free.addAction({'verb': 'attack', 'object': sylv.name}) for amount in range(20, 100, 20): free.addAction({ 'verb': 'offer', 'object': sylv.name, 'amount': amount }) if model == 'powell': # Powell has null stages freeNOP = free.addAction({'verb': 'continue'}) elif model == 'slantchev': # Slantchev has both sides receiving offers free.addAction({'verb': 'accept offer', 'object': sylv.name}) free.addAction({'verb': 'reject offer', 'object': sylv.name}) # Agent actions sylvBattle = sylv.addAction({'verb': 'attack', 'object': free.name}) sylvAccept = sylv.addAction({'verb': 'accept offer', 'object': free.name}) sylvReject = sylv.addAction({'verb': 'reject offer', 'object': free.name}) if model == 'powell': # Powell has null stages sylvNOP = sylv.addAction({'verb': 'continue'}) elif model == 'slantchev': # Slantchev has both sides making offers for amount in range(10, 100, 10): sylv.addAction({ 'verb': 'offer', 'object': free.name, 'amount': amount }) # Restrictions on when actions are legal, based on phase of game for action in filterActions({'verb': 'offer'}, free.actions | sylv.actions): agent = world.agents[action['subject']] agent.setLegal( action, makeTree({ 'if': equalRow(stateKey(None, 'phase'), 'offer'), True: True, # Offers are legal in the offer phase False: False })) # Offers are illegal in all other phases if model == 'powell': # Powell has a special rejection phase for action in [freeNOP, freeBattle]: free.setLegal( action, makeTree({ 'if': equalRow(stateKey(None, 'phase'), 'rejection'), True: True, # Attacking and doing nothing are legal only in rejection phase False: False }) ) # Attacking and doing nothing are illegal in all other phases # Once offered, agent can respond if model == 'powell': # Under Powell, only Sylvania has to respond, and it can attack responses = [sylvBattle, sylvAccept, sylvReject] elif model == 'slantchev': # Under Slantchev, only accept/reject responses = filterActions({'verb': 'accept offer'}, free.actions | sylv.actions) responses += filterActions({'verb': 'reject offer'}, free.actions | sylv.actions) for action in responses: agent = world.agents[action['subject']] agent.setLegal( action, makeTree({ 'if': equalRow(stateKey(None, 'phase'), 'respond'), True: True, # Offeree must act in the response phase False: False })) # Offeree cannot act in any other phase if model == 'powell': # NOP is legal in exactly opposite situations to all other actions sylv.setLegal( sylvNOP, makeTree({ 'if': equalRow(stateKey(None, 'phase'), 'end'), True: True, # Sylvania does not do anything in the null phase after Freedonia responds to rejection False: False })) # Sylvania must act in its other phases if model == 'slantchev': # Attacking legal only under engagement phase for action in filterActions({'verb': 'attack'}, free.actions | sylv.actions): agent = world.agents[action['subject']] agent.setLegal( action, makeTree({ 'if': equalRow(stateKey(None, 'phase'), 'engagement'), True: True, # Attacking legal only in engagement False: False })) # Attacking legal every other phase # Goals for Freedonia goalFTroops = maximizeFeature(stateKey(free.name, 'troops')) free.setReward(goalFTroops, 1.) goalFTerritory = maximizeFeature(stateKey(free.name, 'territory')) free.setReward(goalFTerritory, 1.) # Goals for Sylvania goalSTroops = maximizeFeature(stateKey(sylv.name, 'troops')) sylv.setReward(goalSTroops, 1.) goalSTerritory = minimizeFeature(stateKey(free.name, 'territory')) sylv.setReward(goalSTerritory, 1.) # Possible goals applicable to both goalAgreement = maximizeFeature(stateKey(None, 'treaty')) # Silly goal, provided as an example of an achievement goal goalAchieve = achieveFeatureValue(stateKey(None, 'phase'), 'respond') # Horizons if model == 'powell': free.setAttribute('horizon', 4) sylv.setAttribute('horizon', 4) elif model == 'slantchev': free.setAttribute('horizon', 6) sylv.setAttribute('horizon', 6) # Discount factors free.setAttribute('discount', -1) sylv.setAttribute('discount', -1) # Levels of belief free.setRecursiveLevel(2) sylv.setRecursiveLevel(2) # Dynamics of battle freeTroops = stateKey(free.name, 'troops') freeTerr = stateKey(free.name, 'territory') sylvTroops = stateKey(sylv.name, 'troops') # Effect of fighting for action in filterActions({'verb': 'attack'}, free.actions | sylv.actions): # Effect on troops (cost of battle) tree = makeTree( addFeatureMatrix(freeTroops, stateKey(free.name, 'cost'), -1.)) world.setDynamics(freeTroops, action, tree, enforceMin=not web) tree = makeTree( addFeatureMatrix(sylvTroops, stateKey(sylv.name, 'cost'), -1.)) world.setDynamics(sylvTroops, action, tree, enforceMin=not web) if model == 'powell': # Effect on territory (probability of collapse) tree = makeTree({ 'distribution': [ ( { 'distribution': [ (setToConstantMatrix(freeTerr, 100), 1. - fCollapse ), # Sylvania collapses, Freedonia does not (noChangeMatrix(freeTerr), fCollapse) ] }, # Both collapse sCollapse), ( { 'distribution': [ (setToConstantMatrix(freeTerr, 0), fCollapse ), # Freedonia collapses, Sylvania does not (noChangeMatrix(freeTerr), 1. - fCollapse) ] }, # Neither collapses 1. - sCollapse) ] }) world.setDynamics(freeTerr, action, tree) elif model == 'slantchev': # Effect on position pos = stateKey(free.name, 'position') tree = makeTree({ 'distribution': [ (incrementMatrix(pos, 1), 1. - fCollapse), # Freedonia wins battle (incrementMatrix(pos, -1), fCollapse) ] }) # Freedonia loses battle world.setDynamics(pos, action, tree) # Effect on territory tree = makeTree({ 'if': thresholdRow(pos, posHi - .5), True: setToConstantMatrix(freeTerr, 100), # Freedonia won False: { 'if': thresholdRow(pos, posLo + .5), True: noChangeMatrix(freeTerr), False: setToConstantMatrix(freeTerr, 0) } }) # Freedonia lost world.setDynamics(freeTerr, action, tree) # Dynamics of offers for index in range(2): atom = Action({ 'subject': world.agents.keys()[index], 'verb': 'offer', 'object': world.agents.keys()[1 - index] }) if atom['subject'] == free.name or model != 'powell': offer = stateKey(atom['object'], 'offered') amount = actionKey('amount') tree = makeTree({ 'if': trueRow(stateKey(None, 'treaty')), True: noChangeMatrix(offer), False: setToConstantMatrix(offer, amount) }) world.setDynamics(offer, atom, tree, enforceMax=not web) # Dynamics of treaties for action in filterActions({'verb': 'accept offer'}, free.actions | sylv.actions): # Accepting an offer means that there is now a treaty key = stateKey(None, 'treaty') tree = makeTree(setTrueMatrix(key)) world.setDynamics(key, action, tree) # Accepting offer sets territory offer = stateKey(action['subject'], 'offered') territory = stateKey(free.name, 'territory') if action['subject'] == free.name: # Freedonia accepts sets territory to last offer tree = makeTree(setToFeatureMatrix(territory, offer)) world.setDynamics(freeTerr, action, tree) else: # Sylvania accepts sets territory to 1-last offer tree = makeTree( setToFeatureMatrix(territory, offer, pct=-1., shift=100.)) world.setDynamics(freeTerr, action, tree) # Dynamics of phase phase = stateKey(None, 'phase') roundKey = stateKey(None, 'round') # OFFER -> RESPOND for index in range(2): action = Action({ 'subject': world.agents.keys()[index], 'verb': 'offer', 'object': world.agents.keys()[1 - index] }) if action['subject'] == free.name or model != 'powell': tree = makeTree(setToConstantMatrix(phase, 'respond')) world.setDynamics(phase, action, tree) # RESPOND -> REJECTION or ENGAGEMENT for action in filterActions({'verb': 'reject offer'}, free.actions | sylv.actions): if model == 'powell': tree = makeTree(setToConstantMatrix(phase, 'rejection')) elif model == 'slantchev': tree = makeTree(setToConstantMatrix(phase, 'engagement')) world.setDynamics(phase, action, tree) # accepting -> OFFER for action in filterActions({'verb': 'accept offer'}, free.actions | sylv.actions): tree = makeTree(setToConstantMatrix(phase, 'offer')) world.setDynamics(phase, action, tree) # attacking -> OFFER for action in filterActions({'verb': 'attack'}, free.actions | sylv.actions): tree = makeTree(setToConstantMatrix(phase, 'offer')) world.setDynamics(phase, action, tree) if action['subject'] == sylv.name or model == 'slantchev': tree = makeTree(incrementMatrix(roundKey, 1)) world.setDynamics(roundKey, action, tree) if model == 'powell': # REJECTION -> END for atom in [freeNOP, freeBattle]: tree = makeTree(setToConstantMatrix(phase, 'end')) world.setDynamics(phase, atom, tree) # END -> OFFER atom = Action({'subject': sylv.name, 'verb': 'continue'}) tree = makeTree(setToConstantMatrix(phase, 'offer')) world.setDynamics(phase, atom, tree) tree = makeTree(incrementMatrix(roundKey, 1)) world.setDynamics(roundKey, atom, tree) if not web: # Relationship dynamics: attacking is bad for trust atom = Action({ 'subject': sylv.name, 'verb': 'attack', 'object': free.name }) key = binaryKey(free.name, sylv.name, 'trusts') tree = makeTree(approachMatrix(key, 0.1, -1.)) world.setDynamics(key, atom, tree) # Handcrafted policy for Freedonia # free.setPolicy(makeTree({'if': equalRow('phase','respond'), # # Accept an offer greater than 50 # True: {'if': thresholdRow(stateKey(free.name,'offered'),50), # True: Action({'subject': free.name,'verb': 'accept offer','object': sylv.name}), # False: Action({'subject': free.name,'verb': 'reject offer','object': sylv.name})}, # False: {'if': equalRow('phase','engagement'), # # Attack during engagement phase # True: Action({'subject': free.name,'verb': 'attack','object': sylv.name}), # # Agent decides how what to do otherwise # False: False}})) # Mental models of enemy # Example of creating a model with incorrect reward all at once (a version of Freedonia who cares about reaching agreement as well) # sylv.addModel('false',R={goalSTroops: 10.,goalSTerritory: 1.,goalAgreement: 1.}, # rationality=1.,selection='distribution',parent=True) # Example of creating a model with incorrect beliefs sylv.addModel('false', rationality=10., selection='distribution', parent=True) key = stateKey(free.name, 'position') # Sylvania believes position to be fixed at 3 sylv.setBelief(key, 3, 'false') # Freedonia is truly unsure about position (50% chance of being 7, 50% of being 3) world.setModel(free.name, True) free.setBelief(key, Distribution({7: 0.5, 3: 0.5}), True) # Observations about military position tree = makeTree({ 'if': thresholdRow(key, 1), True: { 'if': thresholdRow(key, 9), True: { 'distribution': [(KeyedVector({key: 1}), 0.9), (KeyedVector({ key: 1, CONSTANT: -1 }), 0.1)] }, False: { 'distribution': [(KeyedVector({key: 1}), 0.8), (KeyedVector({ key: 1, CONSTANT: -1 }), 0.1), (KeyedVector({ key: 1, CONSTANT: 1 }), 0.1)] } }, False: { 'distribution': [(KeyedVector({key: 1}), 0.9), (KeyedVector({ key: 1, CONSTANT: 1 }), 0.1)] } }) free.defineObservation(key, tree) # Example of setting model parameters separately sylv.addModel('true', parent=True) sylv.setAttribute( 'rationality', 10., 'true') # Override real agent's rationality with this value sylv.setAttribute('selection', 'distribution', 'true') world.setMentalModel(free.name, sylv.name, {'false': 0.9, 'true': 0.1}) # Goal of fooling Sylvania goalDeception = achieveFeatureValue(modelKey(sylv.name), sylv.model2index('false')) return world
if name == 'survival': tree = {'if': equalRow(stateKey(resident.name,'location',True),'Seattle'), True: tree, False: setTrueMatrix(objective['key'])} world.setDynamics(objective['key'],option,makeTree(tree)) # Movement dynamics world.setDynamics(location,behaviors['leave']['action'],makeTree(setToConstantMatrix(location,'beyond'))) world.setDynamics(location,behaviors['return']['action'],makeTree(setToConstantMatrix(location,'Seattle'))) # Phase dynamics world.setDynamics('phase',True,makeTree({'if': equalRow('phase','where'), True: setToConstantMatrix('phase','how'), False: setToConstantMatrix('phase','where')})) # Decision-making parameters resident.setAttribute('horizon',2) resident.setAttribute('selection','distribution') world.save('anthrax.psy') # world.printState() for tree,weight in resident.getAttribute('R').items(): print weight,tree decision = Distribution() for vector in world.state[None].domain(): world.printVector(vector) result = resident.decide(vector,selection='distribution') for action in result['action'].domain(): decision.addProb(action,world.state[None][vector]*result['action'][action]) print 'Choice:',action
if __name__ == '__main__': # sets up log to screen logging.basicConfig(format='%(message)s', level=logging.DEBUG if DEBUG else logging.INFO) # create world and add agents world = World() ag_producer = Agent('Producer') world.addAgent(ag_producer) ag_consumer = Agent('Consumer') world.addAgent(ag_consumer) agents = [ag_producer, ag_consumer] # agent settings ag_producer.setAttribute('discount', 1) ag_producer.setHorizon(HORIZON) ag_consumer.setAttribute('discount', 1) ag_consumer.setHorizon(HORIZON) # add variables (capacity and asked/received amounts) var_half_cap = world.defineState(ag_producer.name, 'half capacity', bool) world.setFeature(var_half_cap, False) var_ask_amnt = world.defineState(ag_producer.name, 'asked amount', int, lo=0, hi=100) world.setFeature(var_ask_amnt, 0) var_rcv_amnt = world.defineState(ag_consumer.name, 'received amount',
'i.e., in its mind the position is changing, but is not aligned with the real/true position.' # parameters HORIZON = 3 DISCOUNT = 1 MAX_STEPS = 3 if __name__ == '__main__': # create world and add agent world = World() agent = Agent('Agent') world.addAgent(agent) # set parameters agent.setAttribute('discount', DISCOUNT) agent.setHorizon(HORIZON) # add position variable pos = world.defineState(agent.name, 'position', int, lo=-100, hi=100) world.setFeature(pos, 0) # define agents' actions (stay 0, left -1 and right +1) action = agent.addAction({'verb': 'move', 'action': 'nowhere'}) tree = makeTree(setToFeatureMatrix(pos, pos)) world.setDynamics(pos, action, tree) action = agent.addAction({'verb': 'move', 'action': 'left'}) tree = makeTree(incrementMatrix(pos, -1)) world.setDynamics(pos, action, tree) action = agent.addAction({'verb': 'move', 'action': 'right'}) tree = makeTree(incrementMatrix(pos, 1))
def createWorld(username='******',level=0,ability='good',explanation='none', embodiment='robot',acknowledgment='no',sequence=False, root='.',ext='xml',beliefs=True): """ Creates the initial PsychSim scenario and saves it @param username: name of user ID to use in filenames @param level: robot mission level to use as template @param ability: the level of the robot's ability - good or C{True}: perfect sensors and sensor model - badSensor or C{False}: noisy sensors, but perfect model of noisy sensors - badModel: perfect sensors, but imperfect model of sensors @type ability: bool @param explanation: the type of explanation to use - none: No explanations - ability: Explanation based on robot ability provided. - abilitybenevolence: Explanation based on both robot's ability and benevolence provided. @type explanation: str @param embodiment: the robot's embodiment - robot: The robot looks like a robot - dog: The robot looks like a dog @type embodiment: str @param acknowledgment: the robot's behavior regarding the acknowledgment of errors - no: The robot does not acknowledge its errors - yes: The robot acknowledges its errors @type acknowledgment: str @param root: the root directory to use for files (default is current working directory) @param ext: the file extension for the PsychSim scenario file - xml: Save as uncompressed XML - psy: Save as bzipped XML @type ext: str @param beliefs: if C{True}, store robot's uncertain beliefs in scenario file, rather than compute them on the fly. Storing in scenario file makes the scenario a more complete model, but greatly increases file sixe. Default is C{True} @type beliefs: bool """ print "**************************createWorld***********************" print 'Username:\t%s\nLevel:\t\t%s' % (username,level+1) print 'Ability\t\t%s\nExplanation:\t%s\nEmbodiment:\t%s\nAcknowledge:\t%s' % \ (ability,explanation,embodiment,acknowledgment) # Pre-compute symbols for this level's waypoints for point in WAYPOINTS[level]: if not point.has_key('symbol'): point['symbol'] = point['name'].replace(' ','') world = World() world.defineState(None,'level',int,lo=0,hi=len(WAYPOINTS)-1, description='Static variable indicating what mission level') world.setState(None,'level',level) world.defineState(None,'time',float) world.setState(None,'time',0.) world.defineState(None,'complete',bool) world.setState(None,'complete',False) world.addTermination(makeTree({'if': trueRow('complete'), True: True, False: False})) # Buildings threats = ['none','NBC','armed'] for waypoint in WAYPOINTS[level]: if not waypoint.has_key('symbol'): waypoint['symbol'] = waypoint['name'].replace(' ','') world.addAgent(Agent(waypoint['symbol'])) # Have we visited this waypoint? key = world.defineState(waypoint['symbol'],'visited',bool) world.setFeature(key,False) # Are there dangerous chemicals or armed people here? key = world.defineState(waypoint['symbol'],'danger',list,threats[:]) if waypoint.has_key('NBC') and waypoint['NBC']: world.setFeature(key,'NBC') elif waypoint.has_key('armed') and waypoint['armed']: world.setFeature(key,'armed') else: world.setFeature(key,'none') key = world.defineState(waypoint['symbol'],'recommendation',list, ['none','protected','unprotected']) world.setFeature(key,'none') # Human human = Agent('human') world.addAgent(human) world.defineState(human.name,'alive',bool) human.setState('alive',True) world.defineState(human.name,'deaths',int) human.setState('deaths',0) # Robot robot = Agent('robot') world.addAgent(robot) # Robot states world.defineState(robot.name,'waypoint',list,[point['symbol'] for point in WAYPOINTS[level]]) robot.setState('waypoint',WAYPOINTS[level][getStart(level)]['symbol']) world.defineState(robot.name,'explanation',list,['none','ability','abilitybenevolence','abilityconfidence','confidence']) robot.setState('explanation',explanation) world.defineState(robot.name,'embodiment',list,['robot','dog']) robot.setState('embodiment',embodiment) world.defineState(robot.name,'acknowledgment',list,['no','yes']) robot.setState('acknowledgment',acknowledgment) world.defineState(robot.name,'ability',list,['badSensor','badModel','good']) if ability is True: # Backward compatibility with boolean ability ability = 'good' elif ability is False: ability = 'badSensor' robot.setState('ability',ability) # State of the robot's sensors world.defineState(robot.name,'sensorModel',list,['good','bad']) robot.setState('sensorModel','good') world.defineState(robot.name,'command',list,['none']+[point['symbol'] for point in WAYPOINTS[level]]) robot.setState('command','none') # Actions for end in range(len(WAYPOINTS[level])): symbol = WAYPOINTS[level][end]['symbol'] # Robot movement action = robot.addAction({'verb': 'moveto','object': symbol}) # Legal if no contradictory command tree = makeTree({'if': equalRow(stateKey(robot.name,'command'),'none'), True: True, False: {'if': equalRow(stateKey(robot.name,'command'),symbol), True: True, False: False}}) robot.setLegal(action,tree) # Dynamics of robot's location tree = makeTree(setToConstantMatrix(stateKey(action['subject'],'waypoint'),symbol)) world.setDynamics(stateKey(action['subject'],'waypoint'),action,tree) # Dynamics of visited flag key = stateKey(symbol,'visited') tree = makeTree(setTrueMatrix(key)) world.setDynamics(key,action,tree) # Dynamics of time key = stateKey(None,'time') tree = setToConstantMatrix(key,0.) for start in range(len(WAYPOINTS[level])): if start != end: startsymbol = WAYPOINTS[level][start]['symbol'] if sequence: # Distance is measured by level sequence distance = abs(start-end)*50 else: try: distance = DISTANCES[WAYPOINTS[level][start]['name']][WAYPOINTS[level][end]['name']] except KeyError: try: distance = DISTANCES[WAYPOINTS[level][end]['name']][WAYPOINTS[level][start]['name']] except KeyError: distance = 250 tree = {'if': equalRow(stateKey(action['subject'],'waypoint'),startsymbol), True: setToConstantMatrix(key,float(distance)/1000.), False: tree} world.setDynamics(key,action,makeTree(tree)) # Human entry: Dead or alive if unprotected? key = stateKey(human.name,'alive') action = robot.addAction({'verb': 'recommend unprotected','object': symbol}) tree = makeTree({'if': equalRow(stateKey(symbol,'danger'),'none'), True: setTrueMatrix(key), False: setFalseMatrix(key)}) world.setDynamics(key,action,tree) robot.setLegal(action,makeTree(False)) # Human entry: How much "time" if protected? action = robot.addAction({'verb': 'recommend protected','object': symbol}) key = stateKey(None,'time') world.setDynamics(key,action,makeTree(setToConstantMatrix(key,0.25))) robot.setLegal(action,makeTree(False)) # Robot goals goal = minimizeFeature(stateKey(None,'time')) robot.setReward(goal,2.) goal = maximizeFeature(stateKey(human.name,'alive')) robot.setReward(goal,1.) for point in WAYPOINTS[level]: robot.setReward(maximizeFeature(stateKey(point['symbol'],'visited')),1.) if beliefs: # omega = 'danger' world.defineVariable(robot.name,ActionSet) # Robot beliefs world.setModel(robot.name,True) value = 1./float(len(WAYPOINTS[level])) # tree = KeyedVector({CONSTANT: world.value2float(omega,'none')}) for index in range(len(WAYPOINTS[level])): waypoint = WAYPOINTS[level][index] key = stateKey(waypoint['symbol'],'danger') # if index > 0: # Starting state is safe robot.setBelief(key,psychsim.probability.Distribution({'NBC': value/2., 'armed': value/2.,'none': 1.-value})) # Observation function # tree = {'if': equalRow(stateKey(robot.name,'waypoint'),waypoint['symbol']), # True: generateO(world,key), # False: tree} # robot.defineObservation(omega,makeTree(tree),domain=list,lo=['none','NBC','armed']) robot.defineObservation('microphone',makeTree(None),None,domain=list, lo=['nobody','friendly','suspicious']) robot.defineObservation('NBCsensor',makeTree(None),None,domain=bool) robot.defineObservation('camera',makeTree(None),None,domain=bool) else: robot.defineObservation('microphone',makeTree(None),None,domain=list, lo=['nobody','friendly','suspicious']) robot.defineObservation('NBCsensor',makeTree(None),None,domain=bool) robot.defineObservation('camera',makeTree(None),None,domain=bool) robot.setAttribute('horizon',1) world.setOrder([robot.name]) filename = getFilename(username,level,ext,root) world.save(filename,ext=='psy') WriteLogData('%s user %s, level %d, ability %s, explanation %s' % \ (CREATE_TAG,username,level,ability,explanation),username,level,root=root) return world
world.setDynamics(location, behaviors['leave']['action'], makeTree(setToConstantMatrix(location, 'beyond'))) world.setDynamics(location, behaviors['return']['action'], makeTree(setToConstantMatrix(location, 'Seattle'))) # Phase dynamics world.setDynamics( 'phase', True, makeTree({ 'if': equalRow('phase', 'where'), True: setToConstantMatrix('phase', 'how'), False: setToConstantMatrix('phase', 'where') })) # Decision-making parameters resident.setAttribute('horizon', 2) resident.setAttribute('selection', 'distribution') world.save('anthrax.psy') # world.printState() for tree, weight in list(resident.getAttribute('R').items()): print(weight, tree) decision = Distribution() for vector in world.state[None].domain(): world.printVector(vector) result = resident.decide(vector, selection='distribution') for action in result['action'].domain(): decision.addProb( action, world.state[None][vector] * result['action'][action])
def setup(): global args np.random.seed(args.seed) # create world and add agents world = World() world.memory = False world.parallel = args.parallel agents = [] agent_features = {} for ag in range(args.agents): agent = Agent('Agent' + str(ag)) world.addAgent(agent) agents.append(agent) # set agent's params agent.setAttribute('discount', 1) agent.setHorizon(args.horizon) # add features, initialize at random features = [] agent_features[agent] = features for f in range(args.features_agent): feat = world.defineState(agent.name, 'Feature{}'.format(f), int, lo=0, hi=1000) world.setFeature(feat, np.random.randint(0, MAX_FEATURE_VALUE)) features.append(feat) # set random reward function agent.setReward(maximizeFeature(np.random.choice(features), agent.name), 1) # add mental copy of true model and make it static (we do not have beliefs in the models) agent.addModel(get_fake_model_name(agent), parent=get_true_model_name(agent)) agent.setAttribute('static', True, get_fake_model_name(agent)) # add actions for ac in range(args.actions): action = agent.addAction({'verb': '', 'action': 'Action{}'.format(ac)}) i = ac while i + args.features_action < args.features_agent: weights = {} for j in range(args.features_action): weights[features[i + j + 1]] = 1 tree = makeTree(multi_set_matrix(features[i], weights)) world.setDynamics(features[i], action, tree) i += args.features_action # define order world.setOrder([set(ag.name for ag in agents)]) for agent in agents: # test belief update: # - set a belief in one feature to the actual initial value (should not change outcomes) # world.setModel(agent.name, Distribution({True: 1.0})) rand_feat = np.random.choice(agent_features[agent]) agent.setBelief(rand_feat, world.getValue(rand_feat)) print('{} will always observe {}={}'.format(agent.name, rand_feat, world.getValue(rand_feat))) # set mental model of each agent in all other agents for i in range(args.agents): for j in range(i + 1, args.agents): world.setMentalModel(agents[i].name, agents[j].name, Distribution({get_fake_model_name(agents[j]): 1})) world.setMentalModel(agents[j].name, agents[i].name, Distribution({get_fake_model_name(agents[i]): 1})) return world
class TestAgents(unittest.TestCase): def setUp(self): # Create world self.world = World() # Create agents self.tom = Agent('Tom') self.world.addAgent(self.tom) self.jerry = Agent('Jerry') self.world.addAgent(self.jerry) def addStates(self): """Create state features""" self.world.defineState(self.tom.name,'health',int,lo=0,hi=100, description='%s\'s wellbeing' % (self.tom.name)) self.world.setState(self.tom.name,'health',50) self.world.defineState(self.jerry.name,'health',int,lo=0,hi=100, description='%s\'s wellbeing' % (self.jerry.name)) self.world.setState(self.jerry.name,'health',50) def addActions(self): """Create actions""" self.chase = self.tom.addAction({'verb': 'chase','object': self.jerry.name}) self.hit = self.tom.addAction({'verb': 'hit','object': self.jerry.name}) self.run = self.jerry.addAction({'verb': 'run away'}) self.trick = self.jerry.addAction({'verb': 'trick','object': self.tom.name}) def addDynamics(self): """Create dynamics""" tree = makeTree(incrementMatrix(stateKey(self.jerry.name,'health'),-10)) self.world.setDynamics(stateKey(self.jerry.name,'health'),self.hit,tree,enforceMin=True) def addModels(self,rationality=1.): self.tom.addModel('friend',rationality=rationality,parent=True) self.tom.setReward(maximizeFeature(stateKey(self.jerry.name,'health')),1.,'friend') self.tom.addModel('foe',rationality=rationality,parent=True) self.tom.setReward(minimizeFeature(stateKey(self.jerry.name,'health')),1.,'foe') def saveload(self): """Write scenario to file and then load from scratch""" self.world.save('/tmp/psychsim_test.psy') self.world = World('/tmp/psychsim_test.psy') self.tom = self.world.agents[self.tom.name] self.jerry = self.world.agents[self.jerry.name] def testEnumeratedState(self): self.addActions() self.world.defineVariable(self.tom.name,ActionSet) self.world.defineState(self.tom.name,'status',list,['dead','injured','healthy']) self.world.setState(self.tom.name,'status','healthy') goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'healthy') self.tom.setReward(goal,1.) goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'injured') self.jerry.setReward(goal,1.) self.saveload() self.assertEqual(len(self.world.state),1) vector = self.world.state.domain()[0] tVal = self.tom.reward(vector) self.assertAlmostEqual(tVal,1.,8) jVal = self.jerry.reward(vector) self.assertAlmostEqual(jVal,0.,8) for action in self.tom.actions: encoding = self.world.value2float(self.tom.name,action) self.assertEqual(action,self.world.float2value(self.tom.name,encoding)) def testBeliefModels(self): self.addStates() self.addActions() self.addDynamics() self.world.setOrder([self.tom.name]) self.tom.addModel('optimist') self.tom.setBelief(stateKey(self.jerry.name,'health'),20,'optimist') self.tom.addModel('pessimist') self.world.setModel(self.jerry.name,True) self.world.setMentalModel(self.jerry.name,self.tom.name,{'optimist': 0.5,'pessimist': 0.5}) actions = {self.tom.name: self.hit} self.world.step(actions) vector = self.world.state.domain()[0] beliefs = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector)) for belief in beliefs.domain(): model = self.world.getModel(self.tom.name,belief) if self.tom.models[model].has_key('beliefs'): nested = self.tom.models[model]['beliefs'] self.assertEqual(len(nested),1) nested = nested.domain()[0] self.assertEqual(len(nested),1) self.assertAlmostEqual(nested[stateKey(self.jerry.name,'health')],10.,8) def testObservation(self): self.addStates() self.addActions() self.addDynamics() self.world.setOrder([self.tom.name]) self.world.setModel(self.jerry.name,True) key = stateKey(self.jerry.name,'health') self.jerry.setBelief(key,Distribution({20: 0.5, 50: 0.5})) tree = makeTree({'if': thresholdRow(key,40), True: {'distribution': [(KeyedVector({CONSTANT: 50}),.8), (KeyedVector({CONSTANT: 20}),.2)]}, False: {'distribution': [(KeyedVector({CONSTANT: 50}),.2), (KeyedVector({CONSTANT: 20}),.8)]}}) self.jerry.defineObservation(key,tree) actions = {self.tom.name: self.hit} vector = self.world.state.domain()[0] omegaDist = self.jerry.observe(vector,actions) for omega in omegaDist.domain(): new = KeyedVector(vector) model = self.jerry.index2model(self.jerry.stateEstimator(vector,new,omega)) beliefs = self.jerry.models[model]['beliefs'] if omega[key] > 30: # We observed a high value, so we should have a stronger belief in the higher value # which is now 40 after the hit for belief in beliefs.domain(): if beliefs[belief] > 0.5: self.assertAlmostEqual(belief[key],40,8) else: self.assertAlmostEqual(belief[key],10,8) else: # We observed a low value, so we should have a stronger belief in the lower value # which is now 10 after the hit for belief in beliefs.domain(): if beliefs[belief] < 0.5: self.assertAlmostEqual(belief[key],40,8) else: self.assertAlmostEqual(belief[key],10,8) def testUnobservedAction(self): self.addStates() self.addActions() self.addDynamics() self.addModels() self.world.setOrder([self.tom.name]) self.world.setModel(self.jerry.name,True) self.jerry.setBelief(stateKey(self.jerry.name,'health'),50) self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) tree = makeTree(True) self.jerry.defineObservation(self.tom.name,tree,self.hit,domain=ActionSet) tree = makeTree({'distribution': [(True,0.25),(False,0.75)]}) self.jerry.defineObservation(self.tom.name,tree,self.chase,domain=ActionSet) vector = self.world.state.domain()[0] self.saveload() self.world.step({self.tom.name: self.hit}) vector = self.world.state.domain()[0] def testRewardModels(self): self.addStates() self.addActions() self.addDynamics() self.addModels() self.world.setOrder([self.tom.name]) # Add Jerry's model to the world (so that it gets updated) self.world.setModel(self.jerry.name,True) # Give Jerry uncertainty about Tom self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) self.saveload() # Hitting should make Jerry think Tom is more of a foe actions = {self.tom.name: self.hit} self.world.step(actions) vector = self.world.state.domain()[0] belief01 = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector)) key = modelKey(self.tom.name) for belief in belief01.domain(): if self.tom.index2model(belief[key]) == 'foe': prob01 = belief01[belief] break self.assertGreater(prob01,0.5) # If we think of Tom as even more of an optimizer, then our update should be stronger self.tom.setAttribute('rationality',10.,'foe') self.tom.setAttribute('rationality',10.,'friend') self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) self.world.step(actions) vector = self.world.state.domain()[0] model = self.world.getModel(self.jerry.name,vector) belief10 = self.jerry.getAttribute('beliefs',model) key = modelKey(self.tom.name) for belief in belief10.domain(): if self.tom.index2model(belief[key]) == 'foe': prob10 = belief10[belief] break self.assertGreater(prob10,prob01) # If we keep the same models, but get another observation, we should update even more self.world.step(actions) vector = self.world.state.domain()[0] model = self.world.getModel(self.jerry.name,vector) belief1010 = self.jerry.getAttribute('beliefs',model) key = modelKey(self.tom.name) for belief in belief1010.domain(): if self.tom.index2model(belief[key]) == 'foe': prob1010 = belief1010[belief] break self.assertGreater(prob1010,prob10) def testDynamics(self): self.world.setOrder([self.tom.name]) self.addStates() self.addActions() self.addDynamics() key = stateKey(self.jerry.name,'health') self.assertEqual(len(self.world.state),1) vector = self.world.state.domain()[0] self.assertTrue(vector.has_key(stateKey(self.tom.name,'health'))) self.assertTrue(vector.has_key(turnKey(self.tom.name))) self.assertTrue(vector.has_key(key)) self.assertTrue(vector.has_key(CONSTANT)) self.assertEqual(len(vector),4) self.assertEqual(vector[stateKey(self.tom.name,'health')],50) self.assertEqual(vector[key],50) outcome = self.world.step({self.tom.name: self.chase}) for i in range(7): self.assertEqual(len(self.world.state),1) vector = self.world.state.domain()[0] self.assertTrue(vector.has_key(stateKey(self.tom.name,'health'))) self.assertTrue(vector.has_key(turnKey(self.tom.name))) self.assertTrue(vector.has_key(key)) self.assertTrue(vector.has_key(CONSTANT)) self.assertEqual(len(vector),4) self.assertEqual(vector[stateKey(self.tom.name,'health')],50) self.assertEqual(vector[key],max(50-10*i,0)) outcome = self.world.step({self.tom.name: self.hit}) self.saveload() def testRewardOnOthers(self): self.addStates() self.addActions() self.addDynamics() self.world.setOrder([self.tom.name]) vector = self.world.state.domain()[0] # Create Jerry's goals goal = maximizeFeature(stateKey(self.jerry.name,'health')) self.jerry.setReward(goal,1.) jVal = -self.jerry.reward(vector) # Create Tom's goals from scratch minGoal = minimizeFeature(stateKey(self.jerry.name,'health')) self.tom.setReward(minGoal,1.) self.saveload() tRawVal = self.tom.reward(vector) self.assertAlmostEqual(jVal,tRawVal,8) # Create Tom's goals as a function of Jerry's self.tom.models[True]['R'].clear() self.tom.setReward(self.jerry.name,-1.) self.saveload() tFuncVal = self.tom.reward(vector) self.assertAlmostEqual(tRawVal,tFuncVal,8) # Test effect of functional reward on value function self.tom.setHorizon(1) self.saveload() vHit = self.tom.value(vector,self.hit)['V'] vChase = self.tom.value(vector,self.chase)['V'] self.assertAlmostEqual(vHit,vChase+.1,8) def testReward(self): self.addStates() key = stateKey(self.jerry.name,'health') goal = makeTree({'if': thresholdRow(key,5), True: KeyedVector({key: -2}), False: KeyedVector({key: -1})}) self.jerry.setReward(goal,1.) R = self.jerry.models[True]['R'] self.assertEqual(len(R),1) self.assertEqual(R.keys()[0],goal) self.assertAlmostEqual(R[goal],1.,8) self.jerry.setReward(goal,2.) self.assertEqual(len(R),1) self.assertEqual(R.keys()[0],goal) self.assertAlmostEqual(R[goal],2.,8) def testTurnDynamics(self): self.addStates() self.addActions() self.world.setOrder([self.tom.name,self.jerry.name]) self.assertEqual(self.world.maxTurn,1) self.saveload() vector = self.world.state.domain()[0] jTurn = turnKey(self.jerry.name) tTurn = turnKey(self.tom.name) self.assertEqual(self.world.next(),[self.tom.name]) self.assertEqual(vector[tTurn],0) self.assertEqual(vector[jTurn],1) self.world.step() vector = self.world.state.domain()[0] self.assertEqual(self.world.next(),[self.jerry.name]) self.assertEqual(vector[tTurn],1) self.assertEqual(vector[jTurn],0) self.world.step() vector = self.world.state.domain()[0] self.assertEqual(self.world.next(),[self.tom.name]) self.assertEqual(vector[tTurn],0) self.assertEqual(vector[jTurn],1) # Try some custom dynamics self.world.setTurnDynamics(self.tom.name,self.hit,makeTree(noChangeMatrix(tTurn))) self.world.setTurnDynamics(self.jerry.name,self.hit,makeTree(noChangeMatrix(tTurn))) self.world.step() vector = self.world.state.domain()[0] self.assertEqual(self.world.next(),[self.tom.name]) self.assertEqual(vector[tTurn],0) self.assertEqual(vector[jTurn],1) self.world.step({self.tom.name: self.chase}) vector = self.world.state.domain()[0] self.assertEqual(self.world.next(),[self.jerry.name]) self.assertEqual(vector[tTurn],1) self.assertEqual(vector[jTurn],0) def testStatic(self): self.addStates() self.addActions() self.addDynamics() self.addModels() self.world.setModel(self.jerry.name,True) self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5}) self.world.setOrder([self.tom.name]) vector = self.world.state.domain()[0] model = self.world.getModel(self.jerry.name,vector) belief0 = self.jerry.models[model]['beliefs'] self.world.step() vector = self.world.state.domain()[0] model = self.world.getModel(self.jerry.name,vector) belief1 = self.jerry.models[model]['beliefs'] key = modelKey(self.tom.name) for vector in belief0.domain(): if self.tom.index2model(vector[key]) == 'friend': self.assertGreater(belief0[vector],belief1[vector]) else: self.assertGreater(belief1[vector],belief0[vector]) # Now with the static beliefs self.jerry.setAttribute('static',True,model) self.saveload() self.world.step() vector = self.world.state.domain()[0] model = self.world.getModel(self.jerry.name,vector) belief2 = self.jerry.models[model]['beliefs'] for vector in belief1.domain(): self.assertAlmostEqual(belief1[vector],belief2[vector],8)