Example #1
0
def doBattle(world, turn, config, logfile, session=1):
    buf = StringIO.StringIO()
    user = world.agents[config.get('Game', 'user')]
    if len(world.history) == 0:
        print >> buf, '<h2>No previous battle.</h2>'
        outcomes = []
    else:
        outcomes = getOutcomes(world)
        if not outcomes[-1].has_key(user.name):
            outcomes = outcomes[:-1]
        attackers = getAttacks(world, outcomes[-1], config)
        if attackers:
            if config.get('Game', 'battle') == 'mandatory':
                print >> buf, '<h2>Battle results</h2>'
            else:
                msg = ' and '.join(attackers)
                print >> buf, '<h2>%s attacked</h2>' % (msg)
            state = world.state.expectation()
            territory = stateKey(user.name, 'territory')
            if state[territory] < 1:
                print >> buf, '<h5>You lost the war.</h5>'
            elif state[territory] > 99:
                print >> buf, '<h5>You won the war.</h5>'
            else:
                delta = state - outcomes[-1]['old']
                if delta[stateKey(user.name, 'position')] > 0:
                    print >> buf, '<h5>You won the last battle.</h5>'
                elif delta[stateKey(user.name, 'position')] < 0:
                    print >> buf, '<h5>You lost the last battle.</h5>'
                else:
                    print >> buf, '<h5>The last battle was indecisive.</h5>'
        elif config.get('Game', 'battle') == 'mandatory':
            print >> buf, '<h2>The results from the battle are not in yet.</h2>'
        else:
            print >> buf, '<h2>There was no battle.</h2>'
    print >> buf, '<br/>'
    print >> buf, '<h3>Battle History</h3>'
    print >> buf, '<select size=8>'
    for day in range(len(outcomes)):
        print >> buf, '<option>%02d)' % (day + 1)
        attackers = getAttacks(world, outcomes[day], config)
        if attackers:
            print >> buf, '<h2>'
            if config.get('Game', 'battle') == 'optional':
                msg = ' and '.join(attackers)
                print >> buf, '%s attacked.' % (msg)
            try:
                delta = outcomes[day + 1]['old'] - outcomes[day]['old']
            except IndexError:
                delta = state - outcomes[day]['old']
            print >> buf, '%s</h2>' % (getDelta(world, delta, config))
        elif config.get('Game', 'battle') == 'optional':
            print >> buf, '<h2>No battle.</h2>'
        else:
            print >> buf, '<h2>Results not in yet.</h2>'
        print >> buf, '</option>'
    print >> buf, '</select>'
    contents = buf.getvalue()
    buf.close()
    return contents
    def modeltest(self, trueModels, davidBeliefAboutStacy,
                  stacyBeliefAboutDavid, strongerBelief):
        agts = self.world.agents.values()
        for i in range(2):
            me = agts[i]
            other = agts[1 - i]
            for model in me.models.keys():
                if model is True:
                    name = trueModels[me.name]
                else:
                    name = model
                if name == 'Capitalist':
                    me.setReward(maximizeFeature(stateKey(me.name, 'money')),
                                 1.0, model)
                elif name == 'Christian':
                    me.setReward(maximizeFeature(stateKey(me.name, 'money')),
                                 1.0, model)
                    me.setReward(
                        maximizeFeature(stateKey(other.name, 'money')), 1.0,
                        model)

        weakBelief = 1.0 - strongerBelief
        belief = {'Christian': weakBelief, 'Capitalist': weakBelief}
        belief[davidBeliefAboutStacy] = strongerBelief
        self.world.setMentalModel('David', 'Stacy', belief)
        belief = {'Christian': weakBelief, 'Capitalist': weakBelief}
        belief[stacyBeliefAboutDavid] = strongerBelief
        self.world.setMentalModel('Stacy', 'David', belief)
def doBattle(world,turn,config,logfile,session=1):
    buf = StringIO.StringIO()
    user = world.agents[config.get('Game','user')]
    if len(world.history) == 0:
        print >> buf,'<h2>No previous battle.</h2>'
        outcomes = []
    else:
        outcomes = getOutcomes(world)
        if not outcomes[-1].has_key(user.name):
            outcomes = outcomes[:-1]
        attackers = getAttacks(world,outcomes[-1],config)
        if attackers:
            if config.get('Game','battle') == 'mandatory':
                print >> buf,'<h2>Battle results</h2>'
            else:
                msg = ' and '.join(attackers)
                print >> buf,'<h2>%s attacked</h2>' % (msg)
            state = world.state.expectation()
            territory = stateKey(user.name,'territory')
            if state[territory] < 1:
                print >> buf,'<h5>You lost the war.</h5>'
            elif state[territory] > 99:
                print >> buf,'<h5>You won the war.</h5>'
            else:
                delta = state - outcomes[-1]['old']
                if delta[stateKey(user.name,'position')] > 0:
                    print >> buf,'<h5>You won the last battle.</h5>'
                elif delta[stateKey(user.name,'position')] < 0:
                    print >> buf,'<h5>You lost the last battle.</h5>'
                else:
                    print >> buf,'<h5>The last battle was indecisive.</h5>'
        elif config.get('Game','battle') == 'mandatory':
            print >> buf,'<h2>The results from the battle are not in yet.</h2>'
        else:
            print >> buf,'<h2>There was no battle.</h2>'
    print >> buf,'<br/>'
    print >> buf,'<h3>Battle History</h3>'
    print >> buf,'<select size=8>'
    for day in range(len(outcomes)):
        print >> buf,'<option>%02d)' % (day+1)
        attackers = getAttacks(world,outcomes[day],config)
        if attackers:
            print >> buf,'<h2>'
            if config.get('Game','battle') == 'optional':
                msg = ' and '.join(attackers)
                print >> buf,'%s attacked.' % (msg)
            try:
                delta = outcomes[day+1]['old'] - outcomes[day]['old']
            except IndexError:
                delta = state - outcomes[day]['old']
            print >> buf, '%s</h2>' % (getDelta(world,delta,config))
        elif config.get('Game','battle') == 'optional':
            print >> buf,'<h2>No battle.</h2>'
        else:
            print >> buf,'<h2>Results not in yet.</h2>'
        print >> buf,'</option>'
    print >> buf,'</select>'
    contents = buf.getvalue()
    buf.close()
    return contents
def getDelta(world,delta,config):
    """
    Translate a delta vector into a string representation
    """
    user = config.get('Game','user')
    phrases = []
    for name in world.agents.keys():
        for feature in world.features[name].keys():
            if config.has_option('Change',feature):
                key = stateKey(name,feature)
                value = delta[key]
                if feature == 'position':
                    if name == user:
                        if value < 0:
                            phrases.insert(0,'You lost the battle.')
                        elif value > 0:
                            phrases.insert(0,'You won the battle.')
                        else:
                            phrases.insert(0,'The battle was indecisive.')
                    continue
                elif value < 0:
                    phrase = '%s lost %s %s.' % (name,locale.format('%d',-delta[key],True),feature)
                elif value > 0:
                    phrase = '%s gained %s %s.' % (name,locale.format('%d',delta[key],True),feature)
                phrases.append(phrase.replace(user,'You'))
    return ' '.join(phrases)
def doState(world,turn,config,logfile,session=1):
    buf = StringIO.StringIO()
    print >> buf,'<h5>'
    print >> buf,'<table cellpadding="5" width="100%">'
    state = world.state.domain()[0]
    user = world.agents[config.get('Game','user')]
    features = world.features[user.name].keys()
    features.sort()
    for feature in filter(lambda f: config.has_option('Visible',f) and config.has_option('Visible',f),features):
        value = state[stateKey(user.name,feature)]
        desc = world.getDescription(user.name,feature)
        print >> buf,'<tr><td colspan="2">%s</td></tr>' % (desc)
        print >> buf,'<tr><td width="5%%" align="right">%s</td>' % \
            (locale.format('%d',value,True))
        span = world.features[user.name][feature]['hi'] - \
            world.features[user.name][feature]['lo'] 
        pct = 100*(value-world.features[user.name][feature]['lo'])/span
        print >> buf,'<td><table width="93%" border="1" frame="box" rules="none"><tr>'
        if pct > 0:
            print >> buf,'<td bgcolor="white" height="12" width="%d%%"/>' % (pct)
        else:
            print >> buf,'<td height="12"/>'
        print >> buf,'<td/></tr></table></td>'
        print >> buf,'</tr>'
    print >> buf,'</table>'
    print >> buf,'</h5>'
    contents = buf.getvalue()
    buf.close()
    return contents
Example #6
0
def getDelta(world, delta, config):
    """
    Translate a delta vector into a string representation
    """
    user = config.get('Game', 'user')
    phrases = []
    for name in world.agents.keys():
        for feature in world.features[name].keys():
            if config.has_option('Change', feature):
                key = stateKey(name, feature)
                value = delta[key]
                if feature == 'position':
                    if name == user:
                        if value < 0:
                            phrases.insert(0, 'You lost the battle.')
                        elif value > 0:
                            phrases.insert(0, 'You won the battle.')
                        else:
                            phrases.insert(0, 'The battle was indecisive.')
                    continue
                elif value < 0:
                    phrase = '%s lost %s %s.' % (
                        name, locale.format('%d', -delta[key], True), feature)
                elif value > 0:
                    phrase = '%s gained %s %s.' % (
                        name, locale.format('%d', delta[key], True), feature)
                phrases.append(phrase.replace(user, 'You'))
    return ' '.join(phrases)
Example #7
0
def doState(world, turn, config, logfile, session=1):
    buf = StringIO.StringIO()
    print >> buf, '<h5>'
    print >> buf, '<table cellpadding="5" width="100%">'
    state = world.state.domain()[0]
    user = world.agents[config.get('Game', 'user')]
    features = world.features[user.name].keys()
    features.sort()
    for feature in filter(
            lambda f: config.has_option('Visible', f) and config.has_option(
                'Visible', f), features):
        value = state[stateKey(user.name, feature)]
        desc = world.getDescription(user.name, feature)
        print >> buf, '<tr><td colspan="2">%s</td></tr>' % (desc)
        print >> buf,'<tr><td width="5%%" align="right">%s</td>' % \
            (locale.format('%d',value,True))
        span = world.features[user.name][feature]['hi'] - \
            world.features[user.name][feature]['lo']
        pct = 100 * (value - world.features[user.name][feature]['lo']) / span
        print >> buf, '<td><table width="93%" border="1" frame="box" rules="none"><tr>'
        if pct > 0:
            print >> buf, '<td bgcolor="white" height="12" width="%d%%"/>' % (
                pct)
        else:
            print >> buf, '<td height="12"/>'
        print >> buf, '<td/></tr></table></td>'
        print >> buf, '</tr>'
    print >> buf, '</table>'
    print >> buf, '</h5>'
    contents = buf.getvalue()
    buf.close()
    return contents
 def buildPayoff(self,rnd,key,payoff):
     if (rnd == self.maxRounds - 1):
         return setToConstantMatrix(key,payoff[rnd])
     else:
         return {'if': equalRow(stateKey(None,'round'),rnd),
                 True: setToConstantMatrix(key,payoff[rnd]),
                 False: self.buildPayoff(rnd+1,key,payoff)}
 def buildPayoff(self, rnd, key, payoff):
     if (rnd == self.maxRounds - 1):
         return setToConstantMatrix(key, payoff[rnd])
     else:
         return {
             'if': equalRow(stateKey(None, 'round'), rnd),
             True: setToConstantMatrix(key, payoff[rnd]),
             False: self.buildPayoff(rnd + 1, key, payoff)
         }
Example #10
0
 def modeltest(self, trueModels, davidBeliefAboutStacy,
               stacyBeliefAboutDavid, strongerBelief):
     for agent in self.world.agents.values():
         for model in agent.models.keys():
             if model is True:
                 name = trueModels[agent.name]
             else:
                 name = model
             if name == 'appleLover':
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'appleOwned')),
                     4.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'pearOwned')),
                     1.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'BatnaOwned')),
                     0.1, model)
             elif name == 'pearLover':
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'appleOwned')),
                     1.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'pearOwned')),
                     4.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'BatnaOwned')),
                     0.1, model)
     weakBelief = 1.0 - strongerBelief
     belief = {'pearLover': weakBelief, 'appleLover': weakBelief}
     belief[davidBeliefAboutStacy] = strongerBelief
     self.world.setMentalModel('David', 'Stacy', belief)
     belief = {'pearLover': weakBelief, 'appleLover': weakBelief}
     belief[stacyBeliefAboutDavid] = strongerBelief
     self.world.setMentalModel('Stacy', 'David', belief)
    def modeltest(self,trueModels,davidBeliefAboutStacy,stacyBeliefAboutDavid,strongerBelief):
        agts = self.world.agents.values()
        for i in range(2):
            me = agts[i]
            other = agts[1-i]
            for model in me.models.keys():
                if model is True:
                    name = trueModels[me.name]
                else:
                    name = model
                if name == 'Capitalist':
                    me.setReward(maximizeFeature(stateKey(me.name,'money')),1.0,model)
                elif name == 'Christian':
                    me.setReward(maximizeFeature(stateKey(me.name,'money')),1.0,model)
                    me.setReward(maximizeFeature(stateKey(other.name,'money')),1.0,model)

        weakBelief = 1.0 - strongerBelief
        belief = {'Christian': weakBelief,'Capitalist': weakBelief}
        belief[davidBeliefAboutStacy] = strongerBelief
        self.world.setMentalModel('David','Stacy',belief)
        belief = {'Christian': weakBelief,'Capitalist': weakBelief}
        belief[stacyBeliefAboutDavid] = strongerBelief
        self.world.setMentalModel('Stacy','David',belief)
Example #12
0
def play(world, debug=1):
    """
    Modify Freedonia to play autonomously and simulate
    """
    for agent in world.agents.values():
        if agent.name == 'Freedonia':
            free = agent
        else:
            sylv = agent
    for amount in range(10, 100, 20):
        action = Action({
            'verb': 'offer',
            'object': sylv.name,
            'amount': amount
        })
        free.addAction(action)
        action = Action({
            'verb': 'offer',
            'object': free.name,
            'amount': amount
        })
        sylv.addAction(action)
    for action in filterActions({'verb': 'offer'},
                                free.actions | sylv.actions):
        actor = world.agents[action['subject']]
        if not actor.legal.has_key(action):
            actor.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
    model = world.getState(None, 'model').domain()[0]
    start = world.getState(free.name, 'territory').expectation()
    print model, start
    scenarioSimulationUseCase(world,
                              offer=0,
                              rounds=15,
                              debug=debug,
                              model=model)
Example #13
0
def play(world,debug=1):
    """
    Modify Freedonia to play autonomously and simulate
    """
    for agent in world.agents.values():
        if agent.name == 'Freedonia':
            free = agent
        else:
            sylv = agent
    for amount in range(10,100,20):
        action = Action({'verb': 'offer','object': sylv.name,'amount': amount})
        free.addAction(action)
        action = Action({'verb': 'offer','object': free.name,'amount': amount})
        sylv.addAction(action)
    for action in filterActions({'verb': 'offer'},free.actions | sylv.actions):
        actor = world.agents[action['subject']]
        if not actor.legal.has_key(action):
            actor.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
    model = world.getState(None,'model').domain()[0]
    start = world.getState(free.name,'territory').expectation()
    print model,start
    scenarioSimulationUseCase(world,offer=0,rounds=15,debug=debug,model=model)
 def modeltest(self,trueModels,davidBeliefAboutStacy,stacyBeliefAboutDavid,strongerBelief):
     for agent in self.world.agents.values():
         for model in agent.models.keys():
             if model is True:
                 name = trueModels[agent.name]
             else:
                 name = model
             if name == 'appleLover':
                 agent.setReward(maximizeFeature(stateKey(agent.name,'appleOwned')),4.0,model)
                 agent.setReward(maximizeFeature(stateKey(agent.name,'pearOwned')),1.0,model)
                 agent.setReward(maximizeFeature(stateKey(agent.name,'BatnaOwned')),0.1,model)
             elif name == 'pearLover':
                 agent.setReward(maximizeFeature(stateKey(agent.name,'appleOwned')),1.0,model)
                 agent.setReward(maximizeFeature(stateKey(agent.name,'pearOwned')),4.0,model)
                 agent.setReward(maximizeFeature(stateKey(agent.name,'BatnaOwned')),0.1,model)
     weakBelief = 1.0 - strongerBelief          
     belief = {'pearLover': weakBelief,'appleLover': weakBelief}
     belief[davidBeliefAboutStacy] = strongerBelief
     self.world.setMentalModel('David','Stacy',belief)
     belief = {'pearLover': weakBelief,'appleLover': weakBelief}
     belief[stacyBeliefAboutDavid] = strongerBelief
     self.world.setMentalModel('Stacy','David',belief)
Example #15
0
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 __init__(self, turnOrder, maxRounds, payoff):

        self.maxRounds = maxRounds
        self.payoff = payoff
        print self.payoff
        self.world = World()
        stacy = Agent('Stacy')
        david = Agent('David')
        agts = [stacy, david]

        # Player state, actions and parameters common to both players
        for i in range(2):
            me = agts[i]
            other = agts[1 - i]
            self.world.addAgent(me)
            # State
            self.world.defineState(me.name, 'money', int)
            me.setState('money', 0)
            mePass = me.addAction({'verb': 'pass', 'object': other.name})
            meTake = me.addAction({'verb': 'take', 'object': other.name})
            # Parameters
            me.setHorizon(6)
            me.setParameter('discount', 1.)
            # me.setParameter('discount',0.9)

            # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

        self.world.setOrder(turnOrder)
        # World state
        self.world.defineState(None,
                               'round',
                               int,
                               description='The current round')
        self.world.setState(None, 'round', 0)
        self.world.defineState(None,
                               'gameOver',
                               bool,
                               description='whether game is over')
        self.world.setState(None, 'gameOver', False)

        self.world.addTermination(
            makeTree({
                'if':
                thresholdRow(stateKey(None, 'round'), self.maxRounds),
                True:
                True,
                False: {
                    'if': trueRow(stateKey(None, 'gameOver')),
                    True: True,
                    False: False
                }
            }))

        # Dynamics
        for action in stacy.actions | david.actions:
            tree = makeTree(incrementMatrix(stateKey(None, 'round'), 1))
            self.world.setDynamics(None, 'round', action, tree)
            if (action['verb'] == 'take'):
                tree = makeTree(setTrueMatrix(stateKey(None, 'gameOver')))
                self.world.setDynamics(None, 'gameOver', action, tree)
                agts = ['Stacy', 'David']
                for i in range(2):
                    key = stateKey(agts[i], 'money')
                    tree = makeTree(
                        self.buildPayoff(0, key, self.payoff[agts[i]]))
                    self.world.setDynamics(agts[i], 'money', action, tree)
            elif action['verb'] == 'pass':
                agts = ['Stacy', 'David']
                for i in range(2):
                    key = stateKey(agts[i], 'money')
                    tree = makeTree({
                        'if':
                        equalRow(stateKey(None, 'round'), self.maxRounds - 1),
                        True:
                        setToConstantMatrix(
                            key, self.payoff[agts[i]][self.maxRounds]),
                        False:
                        noChangeMatrix(key)
                    })
                    self.world.setDynamics(agts[i], 'money', action, tree)

# really need to ask david about these levels - if adding modesl with levels, can
# the true model point to these but have a different level

        for agent in self.world.agents.values():
            agent.addModel('Christian',
                           R={},
                           level=2,
                           rationality=10.,
                           selection='distribution')
            agent.addModel('Capitalist',
                           R={},
                           level=2,
                           rationality=10.,
                           selection='distribution')
    world.defineState(david.name,'applesOffered',int,lo=0,hi=totals['apple'])
    david.setState('applesOffered',0)  
    world.defineState(david.name,'pearsOwned',int,lo=0,hi=totals['pear'])
    david.setState('pearsOwned',0)
    world.defineState(david.name,'pearsOffered',int,lo=0,hi=totals['pear'])
    david.setState('pearsOffered',0)  

    # World state
    world.defineState(None,'agreement',bool)
    world.setState(None,'agreement',False)
    world.defineState(None,'applesOffer',bool)
    world.setState(None,'applesOffer',False)
    world.defineState(None,'pearsOffer',bool)
    world.setState(None,'pearsOffer',False)

    world.termination.append(makeTree({'if': trueRow(stateKey(None,'agreement')),
                                       True: True, 
                                       False: False}))

    # Turn order: Uncomment the following if you want agents to act in parallel
#    world.setOrder([[stacy.name,david.name]])
    # Turn order: Uncomment the following if you want agents to act sequentially
    world.setOrder([stacy.name,david.name])

    # Stacy actions
    stacy.addAction({'verb': 'do nothing'})
    stacy.addAction({'verb': 'offerApple','object': david.name,'amount': 0})
    stacy.addAction({'verb': 'offerApple','object': david.name,'amount': 1})
    stacy.addAction({'verb': 'offerApple','object': david.name,'amount': 2})
    stacy.addAction({'verb': 'offerApple','object': david.name,'amount': 3})
    def __init__(self,turnOrder):

        self.maxRounds=8
        self.world = World()
        totals = {'apple':1,'pear':2} 
        batna_prePref = totals['apple'] + totals['pear']
        stacy = Agent('Stacy')
        david = Agent('David')
        agts = [stacy, david]

        # Player state, actions and parameters common to both players
        for i in range(2):
            me = agts[i]
            other = agts[1-i]
            self.world.addAgent(me)
            # State
            self.world.defineState(me.name,'appleOwned',int,lo=0,hi=totals['apple'])
            me.setState('appleOwned',0)
            self.world.defineState(me.name,'appleOffered',int,lo=0,hi=totals['apple'])
            me.setState('appleOffered',0)  
            self.world.defineState(me.name,'pearOwned',int,lo=0,hi=totals['pear'])
            me.setState('pearOwned',0)
            self.world.defineState(me.name,'pearOffered',int,lo=0,hi=totals['pear'])
            me.setState('pearOffered',0)  

            self.world.defineState(me.name,'Batna',int,lo=0,hi=10)
            me.setState('Batna', batna_prePref)
            self.world.defineState(me.name,'BatnaOwned',int,lo=0,hi=10)
            me.setState('BatnaOwned',0)  

            self.world.defineState(me.name,'agree',bool)
            me.setState('agree',False)  
            # Actions
            me.addAction({'verb': 'do nothing'})
            for amt in range(totals['apple'] + 1):
                tmp = me.addAction({'verb': 'offerApple','object': other.name,'amount': amt})
                me.setLegal(tmp,makeTree({'if': trueRow(stateKey(None, 'agreement')),
                                          False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                                  True: False,
                                                  False: True},
                                          True: False}))


            for amt in range(totals['pear'] + 1):
                tmp = me.addAction({'verb': 'offerPear','object': other.name,'amount': amt})
                me.setLegal(tmp,makeTree({'if': trueRow(stateKey(None, 'agreement')),
                                          False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                                  True: False,
                                                  False: True},
                                          True: False}))

            meReject = me.addAction({'verb': 'rejectNegotiation','object': other.name})
            me.setLegal(meReject,makeTree({'if': trueRow(stateKey(None, 'agreement')),
                                           False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                                   True: False,
                                                   False: True},
                                           True: False}))

            meAccept = me.addAction({'verb': 'accept offer','object': other.name})
            me.setLegal(meAccept,makeTree({'if': trueRow(stateKey(None, 'appleOffer')),
                                           True: {'if': trueRow(stateKey(None, 'pearOffer')),
                                                  True: {'if': trueRow(stateKey(None, 'agreement')),
                                                         False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                                                 True: False,
                                                                 False: True},
                                                         True: False},
                                                  False: False},
                                           False: False}))
            # Parameters
            me.setHorizon(6)
            me.setParameter('discount',0.9)
        
            # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

            # Turn order: Uncomment the following if you want agents to act in parallel
            # world.setOrder([{agts[0].name,agts[1].name}])
            # Turn order: Uncomment the following if you want agents to act sequentially

        self.world.setOrder(turnOrder)

        # World state
        self.world.defineState(None,'agreement',bool)
        self.world.setState(None,'agreement',False)
        self.world.defineState(None,'appleOffer',bool)
        self.world.setState(None,'appleOffer',False)
        self.world.defineState(None,'pearOffer',bool)
        self.world.setState(None,'pearOffer',False)
        self.world.defineState(None,'round',int,description='The current round of the negotiation')
        self.world.setState(None,'round',0)
        self.world.defineState(None,'rejectedNegotiation',bool,
                          description='Have one of the players walked out?')
        self.world.setState(None, 'rejectedNegotiation', False)


# dont terminate so agent sees benefit of early agreement
#    world.addTermination(makeTree({'if': trueRow(stateKey(None,'agreement')),
#                                   True: True, 
#                                   False: False}))

#    world.addTermination(makeTree({'if': trueRow(stateKey(None,'rejectedNegotiation')),
#                                   True: True, 
#                                   False: False}))

        self.world.addTermination(makeTree({'if': thresholdRow(stateKey(None,'round'),self.maxRounds),
                                   True: True, False: False}))

    # Dynamics of offers
        agents = [david.name,stacy.name]
        for i in range(2):
            for fruit in ['apple','pear']:
                atom = Action({'subject': agents[i],'verb': 'offer%s' % (fruit.capitalize()),
                               'object': agents[1-i]})
                parties = [atom['subject'], atom['object']]
                for j in range(2):
                    # Set offer amount
                    offer = stateKey(parties[j],'%sOffered' % (fruit))
                    amount = actionKey('amount') if j == 1 else '%d-%s' % (totals[fruit],actionKey('amount'))
                    tree = makeTree(setToConstantMatrix(offer,amount))
                    self.world.setDynamics(parties[j],'%sOffered' % (fruit),atom,tree)
                    # reset agree flags whenever an offer is made
                    agreeFlag = stateKey(parties[j],'agree')
                    tree = makeTree(setFalseMatrix(agreeFlag))
                    self.world.setDynamics(parties[j],'agree',atom,tree)
                # Offers set offer flag in world state
                tree = makeTree(setTrueMatrix(stateKey(None,'%sOffer' % (fruit))))
                self.world.setDynamics(None,'%sOffer' % (fruit) ,atom,tree)
 

    # agents = [david.name,stacy.name]
    # Dynamics of agreements
        for i in range(2):
            atom = Action({'subject': agents[i],'verb': 'accept offer', 'object': agents[1-i]})

            # accept offer sets accept
            tree = makeTree(setTrueMatrix(stateKey(atom['subject'],'agree')))
            self.world.setDynamics(atom['subject'],'agree',atom,tree)

            # accept offer sets agreement if object has accepted
            tree = makeTree({'if': trueRow(stateKey(atom['object'],'agree')),
                             True:  setTrueMatrix(stateKey(None,'agreement')),
                             False: noChangeMatrix(stateKey(None,'agreement'))})
            self.world.setDynamics(None,'agreement',atom,tree)

        # Accepting offer sets ownership
            parties = [atom['subject'], atom['object']]
            for fruit in ['apple','pear']:
                # atom = Action({'subject': agents[i],'verb': 'accept offer', 'object': agents[1-i]})
                for j in range(2):
                    offer = stateKey(parties[j],'%sOffered' % (fruit))
                    owned = stateKey(parties[j],'%sOwned' % (fruit))
                    tree = makeTree({'if': trueRow(stateKey(atom['object'],'agree')),
                                     False: noChangeMatrix(owned),
                                     True: setToFeatureMatrix(owned,offer)})
                    self.world.setDynamics(parties[j],'%sOwned' % (fruit),atom,tree)
        # rejecting give us batna and ends negotiation
            atom = Action({'subject': agents[i],'verb': 'rejectNegotiation',
                           'object': agents[1-i]})

            tree = makeTree(setToFeatureMatrix(stateKey(atom['subject'],'BatnaOwned') ,stateKey(atom['subject'], 'Batna')))
            self.world.setDynamics(atom['subject'],'BatnaOwned' ,atom,tree)

            tree = makeTree(setToFeatureMatrix(stateKey(atom['object'],'BatnaOwned') ,stateKey(atom['object'], 'Batna')))
            self.world.setDynamics(atom['object'],'BatnaOwned' ,atom,tree)

            tree = makeTree(setTrueMatrix(stateKey(None,'rejectedNegotiation')))
            self.world.setDynamics(None,'rejectedNegotiation' ,atom,tree)
 

        for action in stacy.actions | david.actions:
            tree = makeTree(incrementMatrix(stateKey(None,'round'),1))
            self.world.setDynamics(None,'round',action,tree)
        for agent in self.world.agents.values():
            agent.addModel('pearLover',R={},level=2,rationality=0.01)
            agent.addModel('appleLover',R={},level=2,rationality=0.01)
Example #19
0
        me.setState('pearOwned',0)
        world.defineState(me.name,'pearOffered',int,lo=0,hi=totals['pear'])
        me.setState('pearOffered',0)  

        world.defineState(me.name,'Batna',int,lo=0,hi=10)
        me.setState('Batna', batna_prePref)
        world.defineState(me.name,'BatnaOwned',int,lo=0,hi=10)
        me.setState('BatnaOwned',0)  

        world.defineState(me.name,'agree',bool)
        me.setState('agree',False)  
        # Actions
        me.addAction({'verb': 'do nothing'})
        for amt in range(totals['apple'] + 1):
            tmp = me.addAction({'verb': 'offerApple','object': other.name,'amount': amt})
            me.setLegal(tmp,makeTree({'if': trueRow(stateKey(None, 'agreement')),
                                      False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                              True: False,
                                              False: True},
                                      True: False}))


        for amt in range(totals['pear'] + 1):
            tmp = me.addAction({'verb': 'offerPear','object': other.name,'amount': amt})
            me.setLegal(tmp,makeTree({'if': trueRow(stateKey(None, 'agreement')),
                                      False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                              True: False,
                                              False: True},
                                      True: False}))

        meReject = me.addAction({'verb': 'rejectNegotiation','object': other.name})
        me.setState('pearOwned',0)
        world.defineState(me.name,'pearOffered',int,lo=0,hi=totals['pear'])
        me.setState('pearOffered',0)  

        world.defineState(me.name,'Batna',int,lo=0,hi=10)
        me.setState('Batna', batna_prePref)
        world.defineState(me.name,'BatnaOwned',int,lo=0,hi=10)
        me.setState('BatnaOwned',0)  

        world.defineState(me.name,'agree',bool)
        me.setState('agree',False)  
        # Actions
        me.addAction({'verb': 'do nothing'})
        for amt in range(totals['apple'] + 1):
            tmp = me.addAction({'verb': 'offerApple','object': other.name,'amount': amt})
            me.setLegal(tmp,makeTree({'if': trueRow(stateKey(None, 'agreement')),
                                      False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                              True: False,
                                              False: True},
                                      True: False}))


        for amt in range(totals['pear'] + 1):
            tmp = me.addAction({'verb': 'offerPear','object': other.name,'amount': amt})
            me.setLegal(tmp,makeTree({'if': trueRow(stateKey(None, 'agreement')),
                                      False: {'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                                              True: False,
                                              False: True},
                                      True: False}))

        meReject = me.addAction({'verb': 'rejectNegotiation','object': other.name})
Example #21
0
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
Example #22
0
    def __init__(self):
        self.world = World()
        stacy = Agent('Stacy')
        david = Agent('David')
        agts = [stacy, david]
        totalAmt = 4
        # Player state, actions and parameters common to both players
        for i in range(2):
            me = agts[i]
            other = agts[1-i]
            self.world.addAgent(me)
            self.world.defineState(me.name,'offered',int,lo=0,hi=totalAmt)
            self.world.defineState(me.name,'money',int,lo=0,hi=totalAmt)
            me.setState('offered',0)  
            me.setState('money',0)  
            if (me.name == 'Stacy'):
                for amt in range(totalAmt + 1):
                    me.addAction({'verb': 'offer','object': other.name,'amount': amt})
            else:
                mePass = me.addAction({'verb': 'accept','object': other.name})
                mePass = me.addAction({'verb': 'reject','object': other.name})
            # Parameters
            me.setHorizon(2)
            me.setParameter('discount',0.9)
            # me.setParameter('discount',1.0)
        
            # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

        self.world.setOrder(['Stacy','David'])

        # World state
        self.world.defineState(None,'gameOver',bool,description='The current round')
        self.world.setState(None,'gameOver',False)

        self.world.addTermination(makeTree({'if': trueRow(stateKey(None,'gameOver')),
                                            True: True, False: False}))
        # offer dynamics
        atom = Action({'subject': 'Stacy','verb': 'offer', 'object': 'David'})
        parties = [atom['subject'], atom['object']]
        for j in range(2):
            offer = stateKey(parties[j],'offered')
            amount = actionKey('amount') if j == 1 else '%d-%s' % (totalAmt,actionKey('amount'))
            tree = makeTree(setToConstantMatrix(offer,amount))
            self.world.setDynamics(parties[j],'offered',atom,tree)
        # accept dynamics
        atom = Action({'subject': 'David','verb': 'accept', 'object': 'Stacy'})
        parties = [atom['subject'], atom['object']]
        for j in range(2):
            offer = stateKey(parties[j],'offered')
            money = stateKey(parties[j],'money')
            tree = makeTree(setToFeatureMatrix(money,offer))
            self.world.setDynamics(parties[j],'money',atom,tree)
        tree=makeTree(setTrueMatrix(stateKey(None,'gameOver')))
        self.world.setDynamics(None,'gameOver',atom,tree)
        # reject dynamics
        atom = Action({'subject': 'David','verb': 'reject', 'object': 'Stacy'})
        tree=makeTree(setTrueMatrix(stateKey(None,'gameOver')))
        self.world.setDynamics(None,'gameOver',atom,tree)

# really need to ask david about these levels - if adding modesl with levels, can
# the true model point to these but have a different level
        for agent in self.world.agents.values():
            agent.addModel('Christian',R={},level=2,rationality=25.,selection='distribution')
            agent.addModel('Capitalist',R={},level=2,rationality=25.,selection='distribution')
Example #23
0
            # trying to avoid psychsim thinking in terms x,y coordinates
            # so smartbody will have to maintain these values
            world.defineState(me.name,'door_dist',float)
            world.setState(me.name,'door_dist',3)
            world.defineState(me.name,'fire_dist',float)
            world.setState(me.name,'fire_dist',5)
            world.defineState(me.name,'closest_dist',float)
            world.setState(me.name,'closest_dist',4)
            
            # Actions
            me.addAction({'verb': 'do nothing'})
            me.addAction({'verb': 'runAway','object': 'fire'})
            me.addAction({'verb': 'runTowards','object': 'door'})
            me.addAction({'verb': 'followClosest'})
            # goals
            goal = maximizeFeature(stateKey(me.name,'fire_dist'))
            me.setReward(goal,rewardWeights[base]['fire'])
            goal = minimizeFeature(stateKey(me.name,'door_dist'))
            me.setReward(goal,rewardWeights[base]['door'])
            goal = minimizeFeature(stateKey(me.name,'closest_dist'))
            me.setReward(goal,rewardWeights[base]['follow'])
            # Parameters
            me.setHorizon(1)
            # me.setParameter('discount',0.9)
            me.setParameter('discount',0.2)

    # Turn order: Uncomment the following if you want agents to act in parallel

    actors = world.agents.keys()
    # actors = set(actors)
    # print actors
Example #24
0
        world.defineState(me.name,'scotchOffered',int,lo=0,hi=totals['scotch'])
        me.setState('scotchOffered',0)  
        world.defineState(me.name,'tequilaOwned',int,lo=0,hi=totals['tequila'])
        me.setState('tequilaOwned',0)
        world.defineState(me.name,'tequilaOffered',int,lo=0,hi=totals['tequila'])
        me.setState('tequilaOffered',0)  
        world.defineState(me.name,'agree',bool)
        me.setState('agree',False)  
        # Actions
        me.addAction({'verb': 'do nothing'})
        for amt in range(totals['scotch'] + 1):
            me.addAction({'verb': 'offerScotch','object': other.name,'amount': amt})
        for amt in range(totals['tequila'] + 1):
            me.addAction({'verb': 'offerTequila','object': other.name,'amount': amt})
        meAccept = me.addAction({'verb': 'accept offer','object': other.name})
        me.setLegal(meAccept,makeTree({'if': trueRow(stateKey(None, 'scotchOffer')),
                                         True: {'if': trueRow(stateKey(None, 'tequilaOffer')),
                                                True: True,
                                                False: False},
                                         False: False}))
        # Parameters
        me.setHorizon(4)
        # me.setParameter('discount',0.9)
        me.setParameter('discount',0.9)
        # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

    # Turn order: Uncomment the following if you want agents to act in parallel
    #world.setOrder([{agts[0].name,agts[1].name}])
    # Turn order: Uncomment the following if you want agents to act sequentially
    def __init__(self,turnOrder,maxRounds,payoff):

        self.maxRounds=maxRounds
        self.payoff = payoff
        print self.payoff
        self.world = World()
        stacy = Agent('Stacy')
        david = Agent('David')
        agts = [stacy, david]

        # Player state, actions and parameters common to both players
        for i in range(2):
            me = agts[i]
            other = agts[1-i]
            self.world.addAgent(me)
            # State
            self.world.defineState(me.name,'money',int)
            me.setState('money',0)
            mePass = me.addAction({'verb': 'pass','object': other.name})
            meTake = me.addAction({'verb': 'take','object': other.name})
            # Parameters
            me.setHorizon(6)
            me.setAttribute('discount',1.)
            # me.setAttribute('discount',0.9)

            # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

        self.world.setOrder(turnOrder)
        # World state
        self.world.defineState(None,'round',int,description='The current round')
        self.world.setState(None,'round',0)
        self.world.defineState(None,'gameOver',bool,description='whether game is over')
        self.world.setState(None,'gameOver',False)

        self.world.addTermination(makeTree({'if': thresholdRow(stateKey(None,'round'),self.maxRounds),
                                            True: True,
                                            False: {'if': trueRow(stateKey(None,'gameOver')),
                                                    True: True,
                                                    False: False}}))

        # Dynamics
        for action in stacy.actions | david.actions:
            tree = makeTree(incrementMatrix(stateKey(None,'round'),1))
            self.world.setDynamics(stateKey(None,'round'),action,tree)
            if (action['verb'] == 'take'):
                tree = makeTree(setTrueMatrix(stateKey(None,'gameOver')))
                self.world.setDynamics(stateKey(None,'gameOver'),action,tree)
                agts = ['Stacy','David']
                for i in range(2):
                    key = stateKey(agts[i],'money')
                    tree = makeTree(self.buildPayoff(0, key, self.payoff[agts[i]]))
                    self.world.setDynamics(stateKey(agts[i],'money'),action,tree)
            elif action['verb'] == 'pass':
                agts = ['Stacy','David']
                for i in range(2):
                    key = stateKey(agts[i],'money')
                    tree = makeTree({'if': equalRow(stateKey(None,'round'),self.maxRounds-1),
                                     True: setToConstantMatrix(key,self.payoff[agts[i]][self.maxRounds]),
                                     False: noChangeMatrix(key)})
                    self.world.setDynamics(stateKey(agts[i],'money'),action,tree)


# really need to ask david about these levels - if adding modesl with levels, can
# the true model point to these but have a different level

        for agent in self.world.agents.values():
            agent.addModel('Christian',R={},level=2,rationality=10.,selection='distribution')
            agent.addModel('Capitalist',R={},level=2,rationality=10.,selection='distribution')
Example #26
0
    def __init__(self, turnOrder):

        self.maxRounds = 8
        self.world = World()
        totals = {'apple': 1, 'pear': 2}
        batna_prePref = totals['apple'] + totals['pear']
        stacy = Agent('Stacy')
        david = Agent('David')
        agts = [stacy, david]

        # Player state, actions and parameters common to both players
        for i in range(2):
            me = agts[i]
            other = agts[1 - i]
            self.world.addAgent(me)
            # State
            self.world.defineState(me.name,
                                   'appleOwned',
                                   int,
                                   lo=0,
                                   hi=totals['apple'])
            me.setState('appleOwned', 0)
            self.world.defineState(me.name,
                                   'appleOffered',
                                   int,
                                   lo=0,
                                   hi=totals['apple'])
            me.setState('appleOffered', 0)
            self.world.defineState(me.name,
                                   'pearOwned',
                                   int,
                                   lo=0,
                                   hi=totals['pear'])
            me.setState('pearOwned', 0)
            self.world.defineState(me.name,
                                   'pearOffered',
                                   int,
                                   lo=0,
                                   hi=totals['pear'])
            me.setState('pearOffered', 0)

            self.world.defineState(me.name, 'Batna', int, lo=0, hi=10)
            me.setState('Batna', batna_prePref)
            self.world.defineState(me.name, 'BatnaOwned', int, lo=0, hi=10)
            me.setState('BatnaOwned', 0)

            self.world.defineState(me.name, 'agree', bool)
            me.setState('agree', False)
            # Actions
            me.addAction({'verb': 'do nothing'})
            for amt in range(totals['apple'] + 1):
                tmp = me.addAction({
                    'verb': 'offerApple',
                    'object': other.name,
                    'amount': amt
                })
                me.setLegal(
                    tmp,
                    makeTree({
                        'if': trueRow(stateKey(None, 'agreement')),
                        False: {
                            'if': trueRow(stateKey(None,
                                                   'rejectedNegotiation')),
                            True: False,
                            False: True
                        },
                        True: False
                    }))

            for amt in range(totals['pear'] + 1):
                tmp = me.addAction({
                    'verb': 'offerPear',
                    'object': other.name,
                    'amount': amt
                })
                me.setLegal(
                    tmp,
                    makeTree({
                        'if': trueRow(stateKey(None, 'agreement')),
                        False: {
                            'if': trueRow(stateKey(None,
                                                   'rejectedNegotiation')),
                            True: False,
                            False: True
                        },
                        True: False
                    }))

            meReject = me.addAction({
                'verb': 'rejectNegotiation',
                'object': other.name
            })
            me.setLegal(
                meReject,
                makeTree({
                    'if': trueRow(stateKey(None, 'agreement')),
                    False: {
                        'if': trueRow(stateKey(None, 'rejectedNegotiation')),
                        True: False,
                        False: True
                    },
                    True: False
                }))

            meAccept = me.addAction({
                'verb': 'accept offer',
                'object': other.name
            })
            me.setLegal(
                meAccept,
                makeTree({
                    'if': trueRow(stateKey(None, 'appleOffer')),
                    True: {
                        'if': trueRow(stateKey(None, 'pearOffer')),
                        True: {
                            'if': trueRow(stateKey(None, 'agreement')),
                            False: {
                                'if':
                                trueRow(stateKey(None, 'rejectedNegotiation')),
                                True:
                                False,
                                False:
                                True
                            },
                            True: False
                        },
                        False: False
                    },
                    False: False
                }))
            # Parameters
            me.setHorizon(6)
            me.setParameter('discount', 0.9)

            # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

        # Turn order: Uncomment the following if you want agents to act in parallel
        # world.setOrder([{agts[0].name,agts[1].name}])
        # Turn order: Uncomment the following if you want agents to act sequentially

        self.world.setOrder(turnOrder)

        # World state
        self.world.defineState(None, 'agreement', bool)
        self.world.setState(None, 'agreement', False)
        self.world.defineState(None, 'appleOffer', bool)
        self.world.setState(None, 'appleOffer', False)
        self.world.defineState(None, 'pearOffer', bool)
        self.world.setState(None, 'pearOffer', False)
        self.world.defineState(
            None,
            'round',
            int,
            description='The current round of the negotiation')
        self.world.setState(None, 'round', 0)
        self.world.defineState(
            None,
            'rejectedNegotiation',
            bool,
            description='Have one of the players walked out?')
        self.world.setState(None, 'rejectedNegotiation', False)

        # dont terminate so agent sees benefit of early agreement
        #    world.addTermination(makeTree({'if': trueRow(stateKey(None,'agreement')),
        #                                   True: True,
        #                                   False: False}))

        #    world.addTermination(makeTree({'if': trueRow(stateKey(None,'rejectedNegotiation')),
        #                                   True: True,
        #                                   False: False}))

        self.world.addTermination(
            makeTree({
                'if':
                thresholdRow(stateKey(None, 'round'), self.maxRounds),
                True:
                True,
                False:
                False
            }))

        # Dynamics of offers
        agents = [david.name, stacy.name]
        for i in range(2):
            for fruit in ['apple', 'pear']:
                atom = Action({
                    'subject': agents[i],
                    'verb': 'offer%s' % (fruit.capitalize()),
                    'object': agents[1 - i]
                })
                parties = [atom['subject'], atom['object']]
                for j in range(2):
                    # Set offer amount
                    offer = stateKey(parties[j], '%sOffered' % (fruit))
                    amount = actionKey('amount') if j == 1 else '%d-%s' % (
                        totals[fruit], actionKey('amount'))
                    tree = makeTree(setToConstantMatrix(offer, amount))
                    self.world.setDynamics(parties[j], '%sOffered' % (fruit),
                                           atom, tree)
                    # reset agree flags whenever an offer is made
                    agreeFlag = stateKey(parties[j], 'agree')
                    tree = makeTree(setFalseMatrix(agreeFlag))
                    self.world.setDynamics(parties[j], 'agree', atom, tree)
                # Offers set offer flag in world state
                tree = makeTree(
                    setTrueMatrix(stateKey(None, '%sOffer' % (fruit))))
                self.world.setDynamics(None, '%sOffer' % (fruit), atom, tree)

    # agents = [david.name,stacy.name]
    # Dynamics of agreements
        for i in range(2):
            atom = Action({
                'subject': agents[i],
                'verb': 'accept offer',
                'object': agents[1 - i]
            })

            # accept offer sets accept
            tree = makeTree(setTrueMatrix(stateKey(atom['subject'], 'agree')))
            self.world.setDynamics(atom['subject'], 'agree', atom, tree)

            # accept offer sets agreement if object has accepted
            tree = makeTree({
                'if': trueRow(stateKey(atom['object'], 'agree')),
                True: setTrueMatrix(stateKey(None, 'agreement')),
                False: noChangeMatrix(stateKey(None, 'agreement'))
            })
            self.world.setDynamics(None, 'agreement', atom, tree)

            # Accepting offer sets ownership
            parties = [atom['subject'], atom['object']]
            for fruit in ['apple', 'pear']:
                # atom = Action({'subject': agents[i],'verb': 'accept offer', 'object': agents[1-i]})
                for j in range(2):
                    offer = stateKey(parties[j], '%sOffered' % (fruit))
                    owned = stateKey(parties[j], '%sOwned' % (fruit))
                    tree = makeTree({
                        'if':
                        trueRow(stateKey(atom['object'], 'agree')),
                        False:
                        noChangeMatrix(owned),
                        True:
                        setToFeatureMatrix(owned, offer)
                    })
                    self.world.setDynamics(parties[j], '%sOwned' % (fruit),
                                           atom, tree)
        # rejecting give us batna and ends negotiation
            atom = Action({
                'subject': agents[i],
                'verb': 'rejectNegotiation',
                'object': agents[1 - i]
            })

            tree = makeTree(
                setToFeatureMatrix(stateKey(atom['subject'], 'BatnaOwned'),
                                   stateKey(atom['subject'], 'Batna')))
            self.world.setDynamics(atom['subject'], 'BatnaOwned', atom, tree)

            tree = makeTree(
                setToFeatureMatrix(stateKey(atom['object'], 'BatnaOwned'),
                                   stateKey(atom['object'], 'Batna')))
            self.world.setDynamics(atom['object'], 'BatnaOwned', atom, tree)

            tree = makeTree(
                setTrueMatrix(stateKey(None, 'rejectedNegotiation')))
            self.world.setDynamics(None, 'rejectedNegotiation', atom, tree)

        for action in stacy.actions | david.actions:
            tree = makeTree(incrementMatrix(stateKey(None, 'round'), 1))
            self.world.setDynamics(None, 'round', action, tree)
        for agent in self.world.agents.values():
            agent.addModel('pearLover', R={}, level=2, rationality=0.01)
            agent.addModel('appleLover', R={}, level=2, rationality=0.01)
Example #27
0
    world.defineState(david.name, 'pearsOwned', int, lo=0, hi=totals['pear'])
    david.setState('pearsOwned', 0)
    world.defineState(david.name, 'pearsOffered', int, lo=0, hi=totals['pear'])
    david.setState('pearsOffered', 0)

    # World state
    world.defineState(None, 'agreement', bool)
    world.setState(None, 'agreement', False)
    world.defineState(None, 'applesOffer', bool)
    world.setState(None, 'applesOffer', False)
    world.defineState(None, 'pearsOffer', bool)
    world.setState(None, 'pearsOffer', False)

    world.termination.append(
        makeTree({
            'if': trueRow(stateKey(None, 'agreement')),
            True: True,
            False: False
        }))

    # Turn order: Uncomment the following if you want agents to act in parallel
    #    world.setOrder([[stacy.name,david.name]])
    # Turn order: Uncomment the following if you want agents to act sequentially
    world.setOrder([stacy.name, david.name])

    # Stacy actions
    stacy.addAction({'verb': 'do nothing'})
    stacy.addAction({'verb': 'offerApple', 'object': david.name, 'amount': 0})
    stacy.addAction({'verb': 'offerApple', 'object': david.name, 'amount': 1})
    stacy.addAction({'verb': 'offerApple', 'object': david.name, 'amount': 2})
    stacy.addAction({'verb': 'offerApple', 'object': david.name, 'amount': 3})
Example #28
0
    def __init__(self):
        self.world = World()
        stacy = Agent('Stacy')
        david = Agent('David')
        agts = [stacy, david]
        totalAmt = 4
        # Player state, actions and parameters common to both players
        for i in range(2):
            me = agts[i]
            other = agts[1 - i]
            self.world.addAgent(me)
            self.world.defineState(me.name, 'offered', int, lo=0, hi=totalAmt)
            self.world.defineState(me.name, 'money', int, lo=0, hi=totalAmt)
            me.setState('offered', 0)
            me.setState('money', 0)
            if (me.name == 'Stacy'):
                for amt in range(totalAmt + 1):
                    me.addAction({
                        'verb': 'offer',
                        'object': other.name,
                        'amount': amt
                    })
            else:
                mePass = me.addAction({'verb': 'accept', 'object': other.name})
                mePass = me.addAction({'verb': 'reject', 'object': other.name})
            # Parameters
            me.setHorizon(2)
            me.setParameter('discount', 0.9)
            # me.setParameter('discount',1.0)

            # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

        self.world.setOrder(['Stacy', 'David'])

        # World state
        self.world.defineState(None,
                               'gameOver',
                               bool,
                               description='The current round')
        self.world.setState(None, 'gameOver', False)

        self.world.addTermination(
            makeTree({
                'if': trueRow(stateKey(None, 'gameOver')),
                True: True,
                False: False
            }))
        # offer dynamics
        atom = Action({'subject': 'Stacy', 'verb': 'offer', 'object': 'David'})
        parties = [atom['subject'], atom['object']]
        for j in range(2):
            offer = stateKey(parties[j], 'offered')
            amount = actionKey('amount') if j == 1 else '%d-%s' % (
                totalAmt, actionKey('amount'))
            tree = makeTree(setToConstantMatrix(offer, amount))
            self.world.setDynamics(parties[j], 'offered', atom, tree)
        # accept dynamics
        atom = Action({
            'subject': 'David',
            'verb': 'accept',
            'object': 'Stacy'
        })
        parties = [atom['subject'], atom['object']]
        for j in range(2):
            offer = stateKey(parties[j], 'offered')
            money = stateKey(parties[j], 'money')
            tree = makeTree(setToFeatureMatrix(money, offer))
            self.world.setDynamics(parties[j], 'money', atom, tree)
        tree = makeTree(setTrueMatrix(stateKey(None, 'gameOver')))
        self.world.setDynamics(None, 'gameOver', atom, tree)
        # reject dynamics
        atom = Action({
            'subject': 'David',
            'verb': 'reject',
            'object': 'Stacy'
        })
        tree = makeTree(setTrueMatrix(stateKey(None, 'gameOver')))
        self.world.setDynamics(None, 'gameOver', atom, tree)

        # really need to ask david about these levels - if adding modesl with levels, can
        # the true model point to these but have a different level
        for agent in self.world.agents.values():
            agent.addModel('Christian',
                           R={},
                           level=2,
                           rationality=25.,
                           selection='distribution')
            agent.addModel('Capitalist',
                           R={},
                           level=2,
                           rationality=25.,
                           selection='distribution')
Example #29
0
            me.addAction({
                'verb': 'offerScotch',
                'object': other.name,
                'amount': amt
            })
        for amt in range(totals['tequila'] + 1):
            me.addAction({
                'verb': 'offerTequila',
                'object': other.name,
                'amount': amt
            })
        meAccept = me.addAction({'verb': 'accept offer', 'object': other.name})
        me.setLegal(
            meAccept,
            makeTree({
                'if': trueRow(stateKey(None, 'scotchOffer')),
                True: {
                    'if': trueRow(stateKey(None, 'tequilaOffer')),
                    True: True,
                    False: False
                },
                False: False
            }))
        # Parameters
        me.setHorizon(6)
        # me.setParameter('discount',0.9)
        #me.setParameter('discount',0.9)
        # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)
Example #30
0
        world.defineState(me.name,'scotchOffered',int,lo=0,hi=totals['scotch'])
        me.setState('scotchOffered',0)  
        world.defineState(me.name,'tequilaOwned',int,lo=0,hi=totals['tequila'])
        me.setState('tequilaOwned',0)
        world.defineState(me.name,'tequilaOffered',int,lo=0,hi=totals['tequila'])
        me.setState('tequilaOffered',0)  
        world.defineState(me.name,'agree',bool)
        me.setState('agree',False)  
        # Actions
        me.addAction({'verb': 'do nothing'})
        for amt in range(totals['scotch'] + 1):
            me.addAction({'verb': 'offerScotch','object': other.name,'amount': amt})
        for amt in range(totals['tequila'] + 1):
            me.addAction({'verb': 'offerTequila','object': other.name,'amount': amt})
        meAccept = me.addAction({'verb': 'accept offer','object': other.name})
        me.setLegal(meAccept,makeTree({'if': trueRow(stateKey(None, 'scotchOffer')),
                                         True: {'if': trueRow(stateKey(None, 'tequilaOffer')),
                                                True: True,
                                                False: False},
                                         False: False}))
        # Parameters
        me.setHorizon(4)
        # me.setParameter('discount',0.9)
        me.setParameter('discount',0.9)
        # Levels of belief
        david.setRecursiveLevel(3)
        stacy.setRecursiveLevel(3)

    # Turn order: Uncomment the following if you want agents to act in parallel
    #world.setOrder([{agts[0].name,agts[1].name}])
    # Turn order: Uncomment the following if you want agents to act sequentially