def run_univariate_function(name, symbol_fmt, func):
    print('\n*************************************')
    print('Testing {} function'.format(name))

    # PsychSim elements
    world = World()
    agent = Agent('The Agent')
    world.addAgent(agent)

    # gets samples from real non-linear function
    x_params, y_params, sample_values = \
        get_bivariate_samples(func, MIN_X, MAX_X, MIN_Y, MAX_Y, NUM_SAMPLES, NUM_SAMPLES)
    sample_mean = np.nanmean(sample_values)

    # create two features: one holding the variable, the other the result (dependent)
    var_x = world.defineState(agent.name, 'var_x', float, lo=MIN_X, hi=MAX_X)
    var_y = world.defineState(agent.name, 'var_y', float, lo=MIN_Y, hi=MAX_Y)
    result = world.defineState(agent.name,
                               'result',
                               float,
                               lo=np.min(sample_values),
                               hi=np.max(sample_values))
    world.setFeature(result, 0)

    # create action that is approximates the function, storing the result in the result feature
    action = agent.addAction({'verb': 'operation', 'action': name})
    tree = makeTree(
        tree_from_bivariate_samples(result, var_x, var_y, x_params, y_params,
                                    sample_values))
    world.setDynamics(result, action, tree)

    world.setOrder([agent.name])

    np.random.seed(SEED)
    values_original = []
    values_approx = []
    for i in range(NUM_TEST_SAMPLES):
        # gets random sample parameters
        x = MIN_X + np.random.rand() * (MAX_X - MIN_X)
        y = MIN_Y + np.random.rand() * (MAX_Y - MIN_Y)

        # sets variable and updates result
        world.setFeature(var_x, x)
        world.setFeature(var_y, y)
        world.step()

        real = func(x, y)
        psych = world.getValue(result)

        print('{:3}: {:30} | Expected: {:10.2f} | PsychSim: {:10.2f}'.format(
            i, symbol_fmt.format(x, y), real, psych))
        values_original.append(real)
        values_approx.append(psych)

    # gets error stats
    rmse = np.sqrt(np.mean((np.array(values_approx) - values_original)**2))
    print('=====================================')
    print('RMSE      = {:.3f}'.format(rmse))
    print('\nPress \'Enter\' to continue...')
    input()
Beispiel #2
0
    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})

    stacy.addAction({'verb': 'offerPear', 'object': david.name, 'amount': 0})
    stacy.addAction({'verb': 'offerPear', 'object': david.name, 'amount': 1})
    stacy.addAction({'verb': 'offerPear', 'object': david.name, 'amount': 2})

    stacyAccept = stacy.addAction({
        'verb': 'accept offer',
        'object': david.name
    })

    # David actions
Beispiel #3
0
def createWorld(username='******',
                level=0,
                ability='good',
                explanation='none',
                embodiment='robot',
                acknowledgment='no',
                sequence=False,
                root='.',
                ext='xml',
                beliefs=True):
    """
    Creates the initial PsychSim scenario and saves it
    @param username: name of user ID to use in filenames
    @param level: robot mission level to use as template
    @param ability: the level of the robot's ability
      - good or C{True}: perfect sensors and sensor model
      - badSensor or C{False}: noisy sensors, but perfect model of noisy sensors
      - badModel: perfect sensors, but imperfect model of sensors
    @type ability: bool
    @param explanation: the type of explanation to use
      - none: No explanations
      - ability: Explanation based on robot ability provided.
      - abilitybenevolence: Explanation based on both robot's ability and benevolence provided.
    @type explanation: str
    @param embodiment: the robot's embodiment
      - robot: The robot looks like a robot
      - dog: The robot looks like a dog
    @type embodiment: str
    @param acknowledgment: the robot's behavior regarding the acknowledgment of errors
      - no: The robot does not acknowledge its errors
      - yes: The robot acknowledges its errors
    @type acknowledgment: str
    @param root: the root directory to use for files (default is current working directory)
    @param ext: the file extension for the PsychSim scenario file
      - xml: Save as uncompressed XML
      - psy: Save as bzipped XML
    @type ext: str
    @param beliefs: if C{True}, store robot's uncertain beliefs in scenario file, rather than compute them on the fly. Storing in scenario file makes the scenario a more complete model, but greatly increases file sixe. Default is C{True}
    @type beliefs: bool
    """

    print "**************************createWorld***********************"
    print 'Username:\t%s\nLevel:\t\t%s' % (username, level + 1)
    print 'Ability\t\t%s\nExplanation:\t%s\nEmbodiment:\t%s\nAcknowledge:\t%s' % \
        (ability,explanation,embodiment,acknowledgment)

    # Pre-compute symbols for this level's waypoints
    for point in WAYPOINTS[level]:
        if not point.has_key('symbol'):
            point['symbol'] = point['name'].replace(' ', '')

    world = World()

    world.defineState(
        None,
        'level',
        int,
        lo=0,
        hi=len(WAYPOINTS) - 1,
        description='Static variable indicating what mission level')
    world.setState(None, 'level', level)

    world.defineState(None, 'time', float)
    world.setState(None, 'time', 0.)

    world.defineState(None, 'complete', bool)
    world.setState(None, 'complete', False)
    world.addTermination(
        makeTree({
            'if': trueRow('complete'),
            True: True,
            False: False
        }))

    # Buildings
    threats = ['none', 'NBC', 'armed']
    for waypoint in WAYPOINTS[level]:
        if not waypoint.has_key('symbol'):
            waypoint['symbol'] = waypoint['name'].replace(' ', '')
        world.addAgent(Agent(waypoint['symbol']))
        # Have we visited this waypoint?
        key = world.defineState(waypoint['symbol'], 'visited', bool)
        world.setFeature(key, False)
        # Are there dangerous chemicals or armed people here?
        key = world.defineState(waypoint['symbol'], 'danger', list, threats[:])
        if waypoint.has_key('NBC') and waypoint['NBC']:
            world.setFeature(key, 'NBC')
        elif waypoint.has_key('armed') and waypoint['armed']:
            world.setFeature(key, 'armed')
        else:
            world.setFeature(key, 'none')
        key = world.defineState(waypoint['symbol'], 'recommendation', list,
                                ['none', 'protected', 'unprotected'])
        world.setFeature(key, 'none')

    # Human
    human = Agent('human')
    world.addAgent(human)

    world.defineState(human.name, 'alive', bool)
    human.setState('alive', True)
    world.defineState(human.name, 'deaths', int)
    human.setState('deaths', 0)

    # Robot
    robot = Agent('robot')
    world.addAgent(robot)

    # Robot states
    world.defineState(robot.name, 'waypoint', list,
                      [point['symbol'] for point in WAYPOINTS[level]])
    robot.setState('waypoint', WAYPOINTS[level][getStart(level)]['symbol'])

    world.defineState(robot.name, 'explanation', list, [
        'none', 'ability', 'abilitybenevolence', 'abilityconfidence',
        'confidence'
    ])
    robot.setState('explanation', explanation)

    world.defineState(robot.name, 'embodiment', list, ['robot', 'dog'])
    robot.setState('embodiment', embodiment)

    world.defineState(robot.name, 'acknowledgment', list, ['no', 'yes'])
    robot.setState('acknowledgment', acknowledgment)

    world.defineState(robot.name, 'ability', list,
                      ['badSensor', 'badModel', 'good'])
    if ability is True:
        # Backward compatibility with boolean ability
        ability = 'good'
    elif ability is False:
        ability = 'badSensor'
    robot.setState('ability', ability)

    # State of the robot's sensors
    world.defineState(robot.name, 'sensorModel', list, ['good', 'bad'])
    robot.setState('sensorModel', 'good')

    world.defineState(robot.name, 'command', list, ['none'] +
                      [point['symbol'] for point in WAYPOINTS[level]])
    robot.setState('command', 'none')

    # Actions
    for end in range(len(WAYPOINTS[level])):
        symbol = WAYPOINTS[level][end]['symbol']
        # Robot movement
        action = robot.addAction({'verb': 'moveto', 'object': symbol})
        # Legal if no contradictory command
        tree = makeTree({
            'if': equalRow(stateKey(robot.name, 'command'), 'none'),
            True: True,
            False: {
                'if': equalRow(stateKey(robot.name, 'command'), symbol),
                True: True,
                False: False
            }
        })
        robot.setLegal(action, tree)
        # Dynamics of robot's location
        tree = makeTree(
            setToConstantMatrix(stateKey(action['subject'], 'waypoint'),
                                symbol))
        world.setDynamics(stateKey(action['subject'], 'waypoint'), action,
                          tree)
        # Dynamics of visited flag
        key = stateKey(symbol, 'visited')
        tree = makeTree(setTrueMatrix(key))
        world.setDynamics(key, action, tree)
        # Dynamics of time
        key = stateKey(None, 'time')
        tree = setToConstantMatrix(key, 0.)
        for start in range(len(WAYPOINTS[level])):
            if start != end:
                startsymbol = WAYPOINTS[level][start]['symbol']
                if sequence:
                    # Distance is measured by level sequence
                    distance = abs(start - end) * 50
                else:
                    try:
                        distance = DISTANCES[WAYPOINTS[level][start]['name']][
                            WAYPOINTS[level][end]['name']]
                    except KeyError:
                        try:
                            distance = DISTANCES[WAYPOINTS[level][end][
                                'name']][WAYPOINTS[level][start]['name']]
                        except KeyError:
                            distance = 250
                tree = {
                    'if':
                    equalRow(stateKey(action['subject'], 'waypoint'),
                             startsymbol),
                    True:
                    setToConstantMatrix(key,
                                        float(distance) / 1000.),
                    False:
                    tree
                }
        world.setDynamics(key, action, makeTree(tree))
        # Human entry: Dead or alive if unprotected?
        key = stateKey(human.name, 'alive')
        action = robot.addAction({
            'verb': 'recommend unprotected',
            'object': symbol
        })
        tree = makeTree({
            'if': equalRow(stateKey(symbol, 'danger'), 'none'),
            True: setTrueMatrix(key),
            False: setFalseMatrix(key)
        })
        world.setDynamics(key, action, tree)
        robot.setLegal(action, makeTree(False))
        # Human entry: How much "time" if protected?
        action = robot.addAction({
            'verb': 'recommend protected',
            'object': symbol
        })
        key = stateKey(None, 'time')
        world.setDynamics(key, action, makeTree(setToConstantMatrix(key,
                                                                    0.25)))
        robot.setLegal(action, makeTree(False))

    # Robot goals
    goal = minimizeFeature(stateKey(None, 'time'))
    robot.setReward(goal, 2.)

    goal = maximizeFeature(stateKey(human.name, 'alive'))
    robot.setReward(goal, 1.)

    for point in WAYPOINTS[level]:
        robot.setReward(maximizeFeature(stateKey(point['symbol'], 'visited')),
                        1.)

    if beliefs:
        #        omega = 'danger'
        world.defineVariable(robot.name, ActionSet)
        # Robot beliefs
        world.setModel(robot.name, True)
        value = 1. / float(len(WAYPOINTS[level]))
        #        tree = KeyedVector({CONSTANT: world.value2float(omega,'none')})
        for index in range(len(WAYPOINTS[level])):
            waypoint = WAYPOINTS[level][index]
            key = stateKey(waypoint['symbol'], 'danger')
            #        if index > 0:
            # Starting state is safe
            robot.setBelief(
                key,
                psychsim.probability.Distribution({
                    'NBC': value / 2.,
                    'armed': value / 2.,
                    'none': 1. - value
                }))
            # Observation function


#            tree = {'if': equalRow(stateKey(robot.name,'waypoint'),waypoint['symbol']),
#                    True: generateO(world,key),
#                    False: tree}
#        robot.defineObservation(omega,makeTree(tree),domain=list,lo=['none','NBC','armed'])
        robot.defineObservation('microphone',
                                makeTree(None),
                                None,
                                domain=list,
                                lo=['nobody', 'friendly', 'suspicious'])
        robot.defineObservation('NBCsensor', makeTree(None), None, domain=bool)
        robot.defineObservation('camera', makeTree(None), None, domain=bool)
    else:
        robot.defineObservation('microphone',
                                makeTree(None),
                                None,
                                domain=list,
                                lo=['nobody', 'friendly', 'suspicious'])
        robot.defineObservation('NBCsensor', makeTree(None), None, domain=bool)
        robot.defineObservation('camera', makeTree(None), None, domain=bool)
    robot.setAttribute('horizon', 1)

    world.setOrder([robot.name])

    filename = getFilename(username, level, ext, root)

    world.save(filename, ext == 'psy')
    WriteLogData('%s user %s, level %d, ability %s, explanation %s' % \
                     (CREATE_TAG,username,level,ability,explanation),username,level,root=root)
    return world
Beispiel #4
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
Beispiel #5
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
    var_ask_amnt = world.defineState(ag_producer.name,
                                     'asked amount',
                                     int,
                                     lo=0,
                                     hi=100)
    world.setFeature(var_ask_amnt, 0)
    var_rcv_amnt = world.defineState(ag_consumer.name,
                                     'received amount',
                                     int,
                                     lo=0,
                                     hi=100)
    world.setFeature(var_rcv_amnt, 0)

    # add producer actions
    # produce capacity: if half capacity then 0.5*asked amount else asked amount)
    act_prod = ag_producer.addAction({'verb': '', 'action': 'produce'})
    tree = makeTree({
        'if': equalRow(var_half_cap, True),
        True: multi_set_matrix(var_rcv_amnt, {var_ask_amnt: 0.5}),
        False: setToFeatureMatrix(var_rcv_amnt, var_ask_amnt)
    })
    world.setDynamics(var_rcv_amnt, act_prod, tree)

    # add consumer actions (ask more = 10 / less = 5)
    act_ask_more = ag_consumer.addAction({'verb': '', 'action': 'ask_more'})
    tree = makeTree(setToConstantMatrix(var_ask_amnt, 10))
    world.setDynamics(var_ask_amnt, act_ask_more, tree)

    act_ask_less = ag_consumer.addAction({'verb': '', 'action': 'ask_less'})
    tree = makeTree(setToConstantMatrix(var_ask_amnt, 5))
    world.setDynamics(var_ask_amnt, act_ask_less, tree)
class TestAgents(unittest.TestCase):

    def setUp(self):
        # Create world
        self.world = World()
        # Create agents
        self.tom = Agent('Tom')
        self.world.addAgent(self.tom)
        self.jerry = Agent('Jerry')
        self.world.addAgent(self.jerry)

    def addStates(self):
        """Create state features"""
        self.world.defineState(self.tom.name,'health',int,lo=0,hi=100,
                               description='%s\'s wellbeing' % (self.tom.name))
        self.world.setState(self.tom.name,'health',50)
        self.world.defineState(self.jerry.name,'health',int,lo=0,hi=100,
                               description='%s\'s wellbeing' % (self.jerry.name))
        self.world.setState(self.jerry.name,'health',50)

    def addActions(self):
        """Create actions"""
        self.chase = self.tom.addAction({'verb': 'chase','object': self.jerry.name})
        self.hit = self.tom.addAction({'verb': 'hit','object': self.jerry.name})
        self.run = self.jerry.addAction({'verb': 'run away'})
        self.trick = self.jerry.addAction({'verb': 'trick','object': self.tom.name})

    def addDynamics(self):
        """Create dynamics"""
        tree = makeTree(incrementMatrix(stateKey(self.jerry.name,'health'),-10))
        self.world.setDynamics(stateKey(self.jerry.name,'health'),self.hit,tree,enforceMin=True)

    def addModels(self,rationality=1.):
        self.tom.addModel('friend',rationality=rationality,parent=True)
        self.tom.setReward(maximizeFeature(stateKey(self.jerry.name,'health')),1.,'friend')
        self.tom.addModel('foe',rationality=rationality,parent=True)
        self.tom.setReward(minimizeFeature(stateKey(self.jerry.name,'health')),1.,'foe')

    def saveload(self):
        """Write scenario to file and then load from scratch"""
        self.world.save('/tmp/psychsim_test.psy')
        self.world = World('/tmp/psychsim_test.psy')
        self.tom = self.world.agents[self.tom.name]
        self.jerry = self.world.agents[self.jerry.name]

    def testEnumeratedState(self):
        self.addActions()
        self.world.defineVariable(self.tom.name,ActionSet)
        self.world.defineState(self.tom.name,'status',list,['dead','injured','healthy'])
        self.world.setState(self.tom.name,'status','healthy')
        goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'healthy')
        self.tom.setReward(goal,1.)
        goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'injured')
        self.jerry.setReward(goal,1.)
        self.saveload()
        self.assertEqual(len(self.world.state),1)
        vector = self.world.state.domain()[0]
        tVal = self.tom.reward(vector)
        self.assertAlmostEqual(tVal,1.,8)
        jVal = self.jerry.reward(vector)
        self.assertAlmostEqual(jVal,0.,8)
        for action in self.tom.actions:
            encoding = self.world.value2float(self.tom.name,action)
            self.assertEqual(action,self.world.float2value(self.tom.name,encoding))

    def testBeliefModels(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.world.setOrder([self.tom.name])
        self.tom.addModel('optimist')
        self.tom.setBelief(stateKey(self.jerry.name,'health'),20,'optimist')
        self.tom.addModel('pessimist')
        self.world.setModel(self.jerry.name,True)
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'optimist': 0.5,'pessimist': 0.5})
        actions = {self.tom.name: self.hit}
        self.world.step(actions)
        vector = self.world.state.domain()[0]
        beliefs = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector))
        for belief in beliefs.domain():
            model = self.world.getModel(self.tom.name,belief)
            if self.tom.models[model].has_key('beliefs'):
                nested = self.tom.models[model]['beliefs']
                self.assertEqual(len(nested),1)
                nested = nested.domain()[0]
                self.assertEqual(len(nested),1)
                self.assertAlmostEqual(nested[stateKey(self.jerry.name,'health')],10.,8)

    def testObservation(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.world.setOrder([self.tom.name])
        self.world.setModel(self.jerry.name,True)
        key = stateKey(self.jerry.name,'health')
        self.jerry.setBelief(key,Distribution({20: 0.5, 50: 0.5}))
        tree = makeTree({'if': thresholdRow(key,40),
                         True: {'distribution': [(KeyedVector({CONSTANT: 50}),.8),
                                                 (KeyedVector({CONSTANT: 20}),.2)]},
                         False: {'distribution': [(KeyedVector({CONSTANT: 50}),.2),
                                                  (KeyedVector({CONSTANT: 20}),.8)]}})
        self.jerry.defineObservation(key,tree)
        actions = {self.tom.name: self.hit}
        vector = self.world.state.domain()[0]
        omegaDist = self.jerry.observe(vector,actions)
        for omega in omegaDist.domain():
            new = KeyedVector(vector)
            model = self.jerry.index2model(self.jerry.stateEstimator(vector,new,omega))
            beliefs = self.jerry.models[model]['beliefs']
            if omega[key] > 30:
                # We observed a high value, so we should have a stronger belief in the higher value
                # which is now 40 after the hit
                for belief in beliefs.domain():
                    if beliefs[belief] > 0.5:
                        self.assertAlmostEqual(belief[key],40,8)
                    else:
                        self.assertAlmostEqual(belief[key],10,8)
            else:
                # We observed a low value, so we should have a stronger belief in the lower value
                # which is now 10 after the hit
                for belief in beliefs.domain():
                    if beliefs[belief] < 0.5:
                        self.assertAlmostEqual(belief[key],40,8)
                    else:
                        self.assertAlmostEqual(belief[key],10,8)

    def testUnobservedAction(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.addModels()
        self.world.setOrder([self.tom.name])
        self.world.setModel(self.jerry.name,True)
        self.jerry.setBelief(stateKey(self.jerry.name,'health'),50)
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        tree = makeTree(True)
        self.jerry.defineObservation(self.tom.name,tree,self.hit,domain=ActionSet)
        tree = makeTree({'distribution': [(True,0.25),(False,0.75)]})
        self.jerry.defineObservation(self.tom.name,tree,self.chase,domain=ActionSet)
        vector = self.world.state.domain()[0]
        self.saveload()
        self.world.step({self.tom.name: self.hit})
        vector = self.world.state.domain()[0]

    def testRewardModels(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.addModels()
        self.world.setOrder([self.tom.name])
        # Add Jerry's model to the world (so that it gets updated)
        self.world.setModel(self.jerry.name,True)
        # Give Jerry uncertainty about Tom
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        self.saveload()
        # Hitting should make Jerry think Tom is more of a foe
        actions = {self.tom.name: self.hit}
        self.world.step(actions)
        vector = self.world.state.domain()[0]
        belief01 = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector))
        key = modelKey(self.tom.name)
        for belief in belief01.domain():
            if self.tom.index2model(belief[key]) == 'foe':
                prob01 = belief01[belief]
                break
        self.assertGreater(prob01,0.5)
        # If we think of Tom as even more of an optimizer, then our update should be stronger
        self.tom.setAttribute('rationality',10.,'foe')
        self.tom.setAttribute('rationality',10.,'friend')
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        self.world.step(actions)
        vector = self.world.state.domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief10 = self.jerry.getAttribute('beliefs',model)
        key = modelKey(self.tom.name)
        for belief in belief10.domain():
            if self.tom.index2model(belief[key]) == 'foe':
                prob10 = belief10[belief]
                break
        self.assertGreater(prob10,prob01)
        # If we keep the same models, but get another observation, we should update even more
        self.world.step(actions)
        vector = self.world.state.domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief1010 = self.jerry.getAttribute('beliefs',model)
        key = modelKey(self.tom.name)
        for belief in belief1010.domain():
            if self.tom.index2model(belief[key]) == 'foe':
                prob1010 = belief1010[belief]
                break
        self.assertGreater(prob1010,prob10)

    def testDynamics(self):
        self.world.setOrder([self.tom.name])
        self.addStates()
        self.addActions()
        self.addDynamics()
        key = stateKey(self.jerry.name,'health')
        self.assertEqual(len(self.world.state),1)
        vector = self.world.state.domain()[0]
        self.assertTrue(vector.has_key(stateKey(self.tom.name,'health')))
        self.assertTrue(vector.has_key(turnKey(self.tom.name)))
        self.assertTrue(vector.has_key(key))
        self.assertTrue(vector.has_key(CONSTANT))
        self.assertEqual(len(vector),4)
        self.assertEqual(vector[stateKey(self.tom.name,'health')],50)
        self.assertEqual(vector[key],50)
        outcome = self.world.step({self.tom.name: self.chase})
        for i in range(7):
            self.assertEqual(len(self.world.state),1)
            vector = self.world.state.domain()[0]
            self.assertTrue(vector.has_key(stateKey(self.tom.name,'health')))
            self.assertTrue(vector.has_key(turnKey(self.tom.name)))
            self.assertTrue(vector.has_key(key))
            self.assertTrue(vector.has_key(CONSTANT))
            self.assertEqual(len(vector),4)
            self.assertEqual(vector[stateKey(self.tom.name,'health')],50)
            self.assertEqual(vector[key],max(50-10*i,0))
            outcome = self.world.step({self.tom.name: self.hit})
            self.saveload()

    def testRewardOnOthers(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.world.setOrder([self.tom.name])
        vector = self.world.state.domain()[0]
        # Create Jerry's goals
        goal = maximizeFeature(stateKey(self.jerry.name,'health'))
        self.jerry.setReward(goal,1.)
        jVal = -self.jerry.reward(vector)
        # Create Tom's goals from scratch
        minGoal = minimizeFeature(stateKey(self.jerry.name,'health'))
        self.tom.setReward(minGoal,1.)
        self.saveload()
        tRawVal = self.tom.reward(vector)
        self.assertAlmostEqual(jVal,tRawVal,8)
        # Create Tom's goals as a function of Jerry's
        self.tom.models[True]['R'].clear()
        self.tom.setReward(self.jerry.name,-1.)
        self.saveload()
        tFuncVal = self.tom.reward(vector)
        self.assertAlmostEqual(tRawVal,tFuncVal,8)
        # Test effect of functional reward on value function
        self.tom.setHorizon(1)
        self.saveload()
        vHit = self.tom.value(vector,self.hit)['V']
        vChase = self.tom.value(vector,self.chase)['V']
        self.assertAlmostEqual(vHit,vChase+.1,8)

    def testReward(self):
        self.addStates()
        key = stateKey(self.jerry.name,'health')
        goal = makeTree({'if': thresholdRow(key,5),
                         True: KeyedVector({key: -2}),
                         False: KeyedVector({key: -1})})
        self.jerry.setReward(goal,1.)
        R = self.jerry.models[True]['R']
        self.assertEqual(len(R),1)
        self.assertEqual(R.keys()[0],goal)
        self.assertAlmostEqual(R[goal],1.,8)
        self.jerry.setReward(goal,2.)
        self.assertEqual(len(R),1)
        self.assertEqual(R.keys()[0],goal)
        self.assertAlmostEqual(R[goal],2.,8)

    def testTurnDynamics(self):
        self.addStates()
        self.addActions()
        self.world.setOrder([self.tom.name,self.jerry.name])
        self.assertEqual(self.world.maxTurn,1)
        self.saveload()
        vector = self.world.state.domain()[0]
        jTurn = turnKey(self.jerry.name)
        tTurn = turnKey(self.tom.name)
        self.assertEqual(self.world.next(),[self.tom.name])
        self.assertEqual(vector[tTurn],0)
        self.assertEqual(vector[jTurn],1)
        self.world.step()
        vector = self.world.state.domain()[0]
        self.assertEqual(self.world.next(),[self.jerry.name])
        self.assertEqual(vector[tTurn],1)
        self.assertEqual(vector[jTurn],0)
        self.world.step()
        vector = self.world.state.domain()[0]
        self.assertEqual(self.world.next(),[self.tom.name])
        self.assertEqual(vector[tTurn],0)
        self.assertEqual(vector[jTurn],1)
        # Try some custom dynamics
        self.world.setTurnDynamics(self.tom.name,self.hit,makeTree(noChangeMatrix(tTurn)))
        self.world.setTurnDynamics(self.jerry.name,self.hit,makeTree(noChangeMatrix(tTurn)))
        self.world.step()
        vector = self.world.state.domain()[0]
        self.assertEqual(self.world.next(),[self.tom.name])
        self.assertEqual(vector[tTurn],0)
        self.assertEqual(vector[jTurn],1)
        self.world.step({self.tom.name: self.chase})
        vector = self.world.state.domain()[0]
        self.assertEqual(self.world.next(),[self.jerry.name])
        self.assertEqual(vector[tTurn],1)
        self.assertEqual(vector[jTurn],0)

    def testStatic(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.addModels()
        self.world.setModel(self.jerry.name,True)
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        self.world.setOrder([self.tom.name])
        vector = self.world.state.domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief0 = self.jerry.models[model]['beliefs']
        self.world.step()
        vector = self.world.state.domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief1 = self.jerry.models[model]['beliefs']
        key = modelKey(self.tom.name)
        for vector in belief0.domain():
            if self.tom.index2model(vector[key]) == 'friend':
                self.assertGreater(belief0[vector],belief1[vector])
            else:
                self.assertGreater(belief1[vector],belief0[vector])
        # Now with the static beliefs
        self.jerry.setAttribute('static',True,model)
        self.saveload()
        self.world.step()
        vector = self.world.state.domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief2 = self.jerry.models[model]['beliefs']
        for vector in belief1.domain():
            self.assertAlmostEqual(belief1[vector],belief2[vector],8)
    # create world and add agent
    world = World()
    agent = Agent('Agent')
    world.addAgent(agent)

    # set parameters
    agent.setAttribute('discount', DISCOUNT)
    agent.setHorizon(HORIZON)

    # add position variable
    pos = world.defineState(agent.name, 'position', int, lo=-100, hi=100)
    world.setFeature(pos, 0)

    # define agents' actions (stay 0, left -1 and right +1)
    action = agent.addAction({'verb': 'move', 'action': 'nowhere'})
    tree = makeTree(setToFeatureMatrix(pos, pos))
    world.setDynamics(pos, action, tree)
    action = agent.addAction({'verb': 'move', 'action': 'left'})
    tree = makeTree(incrementMatrix(pos, -1))
    world.setDynamics(pos, action, tree)
    action = agent.addAction({'verb': 'move', 'action': 'right'})
    tree = makeTree(incrementMatrix(pos, 1))
    world.setDynamics(pos, action, tree)

    # define rewards (maximize position, i.e., always go right)
    agent.setReward(maximizeFeature(pos, agent.name), 1)

    # set order
    world.setOrder([agent.name])
    resident.setState('location','Seattle')

    # Decisions
    for name,entry in behaviors.items():
        entry['action'] = Action({'subject': resident.name,'verb': name})

    # Generate possible combinations of actions
    options = {}
    
    # Lifestyle choices in Seattle
    joints = [name for name in behaviors.keys() if behaviors[name]['phase'] == 'how' and \
                     behaviors[name]['location'] == 'Seattle']
    for joint in powerset(joints):
        action = ActionSet([behaviors[name]['action'] for name in joint])
        label = ' '.join(joint)
        options[label] = resident.addAction(action)
        # Only legal during "how" phase and if living in Seattle
        resident.setLegal(options[label],makeTree({'if': equalRow('phase','how'),
                                                   True: {'if': equalRow(location,'Seattle'),
                                                          True: True, False: False},
                                                   False: False}))
    # Lifestyle choices outside Seattle
    for joint in [name for name in behaviors.keys() if behaviors[name]['phase'] == 'how' and \
                     behaviors[name]['location'] == 'beyond']:
        options[joint] = resident.addAction(behaviors[joint]['action'])
        # Only legal during "how" phase and if *not* living in Seattle
        resident.setLegal(options[joint],makeTree({'if': equalRow('phase','how'),
                                                   True: {'if': equalRow(location,'beyond'),
                                                          True: True, False: False},
                                                   False: False}))
    # Choices of where to live
    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})

    stacy.addAction({'verb': 'offerPear','object': david.name,'amount': 0})
    stacy.addAction({'verb': 'offerPear','object': david.name,'amount': 1})
    stacy.addAction({'verb': 'offerPear','object': david.name,'amount': 2})

    stacyAccept = stacy.addAction({'verb': 'accept offer','object': david.name})

    # David actions
    david.addAction({'verb': 'do nothing'})
    david.addAction({'verb': 'offerApple','object': stacy.name,'amount': 0})
    david.addAction({'verb': 'offerApple','object': stacy.name,'amount': 1})
Beispiel #11
0
        world.addAgent(agent2)

        # add variables
        var_counter = world.defineState(agent1.name,
                                        'counter',
                                        int,
                                        lo=0,
                                        hi=3)
        var_copy = world.defineState(agent2.name,
                                     'counter_copy',
                                     int,
                                     lo=0,
                                     hi=3)

        # define first agent's action (counter increment)
        action = agent1.addAction({'verb': '', 'action': 'increment'})
        tree = makeTree(
            multi_set_matrix(var_counter, {
                var_counter: 1,
                CONSTANT: 1
            }))
        world.setDynamics(var_counter, action, tree)

        # define second agent's action (var is copy from counter)
        action = agent2.addAction({'verb': '', 'action': 'copy'})
        tree = makeTree(setToFeatureMatrix(var_copy, var_counter))
        world.setDynamics(var_copy, action, tree)

        world.setOrder(turn_order)

        # resets vars
def createWorld(username='******',level=0,ability='good',explanation='none',
                embodiment='robot',acknowledgment='no',sequence=False,
                root='.',ext='xml',beliefs=True):
    """
    Creates the initial PsychSim scenario and saves it
    @param username: name of user ID to use in filenames
    @param level: robot mission level to use as template
    @param ability: the level of the robot's ability
      - good or C{True}: perfect sensors and sensor model
      - badSensor or C{False}: noisy sensors, but perfect model of noisy sensors
      - badModel: perfect sensors, but imperfect model of sensors
    @type ability: bool
    @param explanation: the type of explanation to use
      - none: No explanations
      - ability: Explanation based on robot ability provided.
      - abilitybenevolence: Explanation based on both robot's ability and benevolence provided.
    @type explanation: str
    @param embodiment: the robot's embodiment
      - robot: The robot looks like a robot
      - dog: The robot looks like a dog
    @type embodiment: str
    @param acknowledgment: the robot's behavior regarding the acknowledgment of errors
      - no: The robot does not acknowledge its errors
      - yes: The robot acknowledges its errors
    @type acknowledgment: str
    @param root: the root directory to use for files (default is current working directory)
    @param ext: the file extension for the PsychSim scenario file
      - xml: Save as uncompressed XML
      - psy: Save as bzipped XML
    @type ext: str
    @param beliefs: if C{True}, store robot's uncertain beliefs in scenario file, rather than compute them on the fly. Storing in scenario file makes the scenario a more complete model, but greatly increases file sixe. Default is C{True}
    @type beliefs: bool
    """

    print "**************************createWorld***********************"
    print 'Username:\t%s\nLevel:\t\t%s' % (username,level+1)
    print 'Ability\t\t%s\nExplanation:\t%s\nEmbodiment:\t%s\nAcknowledge:\t%s' % \
        (ability,explanation,embodiment,acknowledgment)

    # Pre-compute symbols for this level's waypoints
    for point in WAYPOINTS[level]:
        if not point.has_key('symbol'):
            point['symbol'] = point['name'].replace(' ','')

    world = World()

    world.defineState(None,'level',int,lo=0,hi=len(WAYPOINTS)-1,
                      description='Static variable indicating what mission level')
    world.setState(None,'level',level)

    world.defineState(None,'time',float)
    world.setState(None,'time',0.)

    world.defineState(None,'complete',bool)
    world.setState(None,'complete',False)
    world.addTermination(makeTree({'if': trueRow('complete'), True: True, False: False}))

    # Buildings
    threats = ['none','NBC','armed']
    for waypoint in WAYPOINTS[level]:
        if not waypoint.has_key('symbol'):
            waypoint['symbol'] = waypoint['name'].replace(' ','')
        world.addAgent(Agent(waypoint['symbol']))
        # Have we visited this waypoint?
        key = world.defineState(waypoint['symbol'],'visited',bool)
        world.setFeature(key,False)
        # Are there dangerous chemicals or armed people here?
        key = world.defineState(waypoint['symbol'],'danger',list,threats[:])
        if waypoint.has_key('NBC') and waypoint['NBC']:
            world.setFeature(key,'NBC')
        elif waypoint.has_key('armed') and waypoint['armed']:
            world.setFeature(key,'armed')
        else:
            world.setFeature(key,'none')
        key = world.defineState(waypoint['symbol'],'recommendation',list,
                                ['none','protected','unprotected'])
        world.setFeature(key,'none')

    # Human
    human = Agent('human')
    world.addAgent(human)

    world.defineState(human.name,'alive',bool)
    human.setState('alive',True)
    world.defineState(human.name,'deaths',int)
    human.setState('deaths',0)

    # Robot
    robot = Agent('robot')
    world.addAgent(robot)

    # Robot states
    world.defineState(robot.name,'waypoint',list,[point['symbol'] for point in WAYPOINTS[level]])
    robot.setState('waypoint',WAYPOINTS[level][getStart(level)]['symbol'])

    world.defineState(robot.name,'explanation',list,['none','ability','abilitybenevolence','abilityconfidence','confidence'])
    robot.setState('explanation',explanation)

    world.defineState(robot.name,'embodiment',list,['robot','dog'])
    robot.setState('embodiment',embodiment)

    world.defineState(robot.name,'acknowledgment',list,['no','yes'])
    robot.setState('acknowledgment',acknowledgment)

    world.defineState(robot.name,'ability',list,['badSensor','badModel','good'])
    if ability is True:
        # Backward compatibility with boolean ability
        ability = 'good'
    elif ability is False:
        ability = 'badSensor'
    robot.setState('ability',ability)

    # State of the robot's sensors
    world.defineState(robot.name,'sensorModel',list,['good','bad'])
    robot.setState('sensorModel','good')
    
    world.defineState(robot.name,'command',list,['none']+[point['symbol'] for point in WAYPOINTS[level]])
    robot.setState('command','none')

    # Actions
    for end in range(len(WAYPOINTS[level])):
        symbol = WAYPOINTS[level][end]['symbol']
        # Robot movement
        action = robot.addAction({'verb': 'moveto','object': symbol})
        # Legal if no contradictory command
        tree = makeTree({'if': equalRow(stateKey(robot.name,'command'),'none'),
                         True: True,
                         False: {'if': equalRow(stateKey(robot.name,'command'),symbol),
                                 True: True, False: False}})
        robot.setLegal(action,tree)
        # Dynamics of robot's location
        tree = makeTree(setToConstantMatrix(stateKey(action['subject'],'waypoint'),symbol))
        world.setDynamics(stateKey(action['subject'],'waypoint'),action,tree)
        # Dynamics of visited flag
        key = stateKey(symbol,'visited')
        tree = makeTree(setTrueMatrix(key))
        world.setDynamics(key,action,tree)
        # Dynamics of time
        key = stateKey(None,'time')
        tree = setToConstantMatrix(key,0.)
        for start in range(len(WAYPOINTS[level])):
            if start != end:
                startsymbol = WAYPOINTS[level][start]['symbol']
                if sequence:
                    # Distance is measured by level sequence
                    distance = abs(start-end)*50
                else:
                    try:
                        distance = DISTANCES[WAYPOINTS[level][start]['name']][WAYPOINTS[level][end]['name']]
                    except KeyError:
                        try:
                            distance = DISTANCES[WAYPOINTS[level][end]['name']][WAYPOINTS[level][start]['name']]
                        except KeyError:
                            distance = 250
                tree = {'if': equalRow(stateKey(action['subject'],'waypoint'),startsymbol),
                        True: setToConstantMatrix(key,float(distance)/1000.),
                        False: tree}
        world.setDynamics(key,action,makeTree(tree))
        # Human entry: Dead or alive if unprotected?
        key = stateKey(human.name,'alive')
        action = robot.addAction({'verb': 'recommend unprotected','object': symbol})
        tree = makeTree({'if': equalRow(stateKey(symbol,'danger'),'none'),
                         True: setTrueMatrix(key), False: setFalseMatrix(key)})
        world.setDynamics(key,action,tree)
        robot.setLegal(action,makeTree(False))
        # Human entry: How much "time" if protected?
        action = robot.addAction({'verb': 'recommend protected','object': symbol})
        key = stateKey(None,'time')
        world.setDynamics(key,action,makeTree(setToConstantMatrix(key,0.25)))
        robot.setLegal(action,makeTree(False))

    # Robot goals
    goal = minimizeFeature(stateKey(None,'time'))
    robot.setReward(goal,2.)

    goal = maximizeFeature(stateKey(human.name,'alive'))
    robot.setReward(goal,1.)

    for point in WAYPOINTS[level]:
        robot.setReward(maximizeFeature(stateKey(point['symbol'],'visited')),1.)

    if beliefs:
#        omega = 'danger'
        world.defineVariable(robot.name,ActionSet)
        # Robot beliefs
        world.setModel(robot.name,True)
        value = 1./float(len(WAYPOINTS[level]))
#        tree = KeyedVector({CONSTANT: world.value2float(omega,'none')})
        for index in range(len(WAYPOINTS[level])):
            waypoint = WAYPOINTS[level][index]
            key = stateKey(waypoint['symbol'],'danger')
    #        if index > 0:
                # Starting state is safe
            robot.setBelief(key,psychsim.probability.Distribution({'NBC': value/2., 'armed': value/2.,'none': 1.-value}))
            # Observation function
#            tree = {'if': equalRow(stateKey(robot.name,'waypoint'),waypoint['symbol']),
#                    True: generateO(world,key),
#                    False: tree}
#        robot.defineObservation(omega,makeTree(tree),domain=list,lo=['none','NBC','armed'])
        robot.defineObservation('microphone',makeTree(None),None,domain=list,
                                lo=['nobody','friendly','suspicious'])
        robot.defineObservation('NBCsensor',makeTree(None),None,domain=bool)
        robot.defineObservation('camera',makeTree(None),None,domain=bool)
    else:
        robot.defineObservation('microphone',makeTree(None),None,domain=list,
                                lo=['nobody','friendly','suspicious'])
        robot.defineObservation('NBCsensor',makeTree(None),None,domain=bool)
        robot.defineObservation('camera',makeTree(None),None,domain=bool)
    robot.setAttribute('horizon',1)

    world.setOrder([robot.name])

    filename = getFilename(username,level,ext,root)

    world.save(filename,ext=='psy')
    WriteLogData('%s user %s, level %d, ability %s, explanation %s' % \
                     (CREATE_TAG,username,level,ability,explanation),username,level,root=root)
    return world
Beispiel #13
0
    resident.setState('location', 'Seattle')

    # Decisions
    for name, entry in list(behaviors.items()):
        entry['action'] = Action({'subject': resident.name, 'verb': name})

    # Generate possible combinations of actions
    options = {}

    # Lifestyle choices in Seattle
    joints = [name for name in list(behaviors.keys()) if behaviors[name]['phase'] == 'how' and \
                     behaviors[name]['location'] == 'Seattle']
    for joint in powerset(joints):
        action = ActionSet([behaviors[name]['action'] for name in joint])
        label = ' '.join(joint)
        options[label] = resident.addAction(action)
        # Only legal during "how" phase and if living in Seattle
        resident.setLegal(
            options[label],
            makeTree({
                'if': equalRow('phase', 'how'),
                True: {
                    'if': equalRow(location, 'Seattle'),
                    True: True,
                    False: False
                },
                False: False
            }))
    # Lifestyle choices outside Seattle
    for joint in [name for name in list(behaviors.keys()) if behaviors[name]['phase'] == 'how' and \
                     behaviors[name]['location'] == 'beyond']:
def setup():
    global args

    np.random.seed(args.seed)
    # create world and add agents
    world = World()
    world.memory = False
    world.parallel = args.parallel
    agents = []
    agent_features = {}
    for ag in range(args.agents):
        agent = Agent('Agent' + str(ag))
        world.addAgent(agent)
        agents.append(agent)

        # set agent's params
        agent.setAttribute('discount', 1)
        agent.setHorizon(args.horizon)

        # add features, initialize at random
        features = []
        agent_features[agent] = features
        for f in range(args.features_agent):
            feat = world.defineState(agent.name, 'Feature{}'.format(f), int, lo=0, hi=1000)
            world.setFeature(feat, np.random.randint(0, MAX_FEATURE_VALUE))
            features.append(feat)

        # set random reward function
        agent.setReward(maximizeFeature(np.random.choice(features), agent.name), 1)

        # add mental copy of true model and make it static (we do not have beliefs in the models)
        agent.addModel(get_fake_model_name(agent), parent=get_true_model_name(agent))
        agent.setAttribute('static', True, get_fake_model_name(agent))

        # add actions
        for ac in range(args.actions):
            action = agent.addAction({'verb': '', 'action': 'Action{}'.format(ac)})
            i = ac
            while i + args.features_action < args.features_agent:

                weights = {}
                for j in range(args.features_action):
                    weights[features[i + j + 1]] = 1
                tree = makeTree(multi_set_matrix(features[i], weights))
                world.setDynamics(features[i], action, tree)

                i += args.features_action

    # define order
    world.setOrder([set(ag.name for ag in agents)])

    for agent in agents:
        # test belief update:
        # - set a belief in one feature to the actual initial value (should not change outcomes)
        # world.setModel(agent.name, Distribution({True: 1.0}))
        rand_feat = np.random.choice(agent_features[agent])
        agent.setBelief(rand_feat, world.getValue(rand_feat))
        print('{} will always observe {}={}'.format(agent.name, rand_feat, world.getValue(rand_feat)))

    # set mental model of each agent in all other agents
    for i in range(args.agents):
        for j in range(i + 1, args.agents):
            world.setMentalModel(agents[i].name, agents[j].name, Distribution({get_fake_model_name(agents[j]): 1}))
            world.setMentalModel(agents[j].name, agents[i].name, Distribution({get_fake_model_name(agents[i]): 1}))

    return world
Beispiel #15
0
class TestAgents(unittest.TestCase):

    def setUp(self):
        # Create world
        self.world = World()
        # Create agents
        self.tom = Agent('Tom')
        self.world.addAgent(self.tom)
        self.jerry = Agent('Jerry')
        self.world.addAgent(self.jerry)

    def addStates(self):
        """Create state features"""
        self.world.defineState(self.tom.name,'health',int,lo=0,hi=100,
                               description='%s\'s wellbeing' % (self.tom.name))
        self.world.setState(self.tom.name,'health',50)
        self.world.defineState(self.jerry.name,'health',int,lo=0,hi=100,
                               description='%s\'s wellbeing' % (self.jerry.name))
        self.world.setState(self.jerry.name,'health',50)

    def addActions(self):
        """Create actions"""
        self.chase = self.tom.addAction({'verb': 'chase','object': self.jerry.name})
        self.hit = self.tom.addAction({'verb': 'hit','object': self.jerry.name})
        self.run = self.jerry.addAction({'verb': 'run away'})
        self.trick = self.jerry.addAction({'verb': 'trick','object': self.tom.name})

    def addDynamics(self):
        """Create dynamics"""
        tree = makeTree(incrementMatrix(stateKey(self.jerry.name,'health'),-10))
        self.world.setDynamics(stateKey(self.jerry.name,'health'),self.hit,tree,enforceMin=True)

    def addModels(self,rationality=1.):
        self.tom.addModel('friend',rationality=rationality,parent=True)
        self.tom.setReward(maximizeFeature(stateKey(self.jerry.name,'health')),1.,'friend')
        self.tom.addModel('foe',rationality=rationality,parent=True)
        self.tom.setReward(minimizeFeature(stateKey(self.jerry.name,'health')),1.,'foe')

    def saveload(self):
        """Write scenario to file and then load from scratch"""
        self.world.save('/tmp/psychsim_test.psy')
        self.world = World('/tmp/psychsim_test.psy')
        self.tom = self.world.agents[self.tom.name]
        self.jerry = self.world.agents[self.jerry.name]

    def testEnumeratedState(self):
        self.addActions()
        self.world.defineVariable(self.tom.name,ActionSet)
        self.world.defineState(self.tom.name,'status',list,['dead','injured','healthy'])
        self.world.setState(self.tom.name,'status','healthy')
        goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'healthy')
        self.tom.setReward(goal,1.)
        goal = achieveFeatureValue(stateKey(self.tom.name,'status'),'injured')
        self.jerry.setReward(goal,1.)
        self.saveload()
        self.assertEqual(len(self.world.state[None]),1)
        vector = self.world.state[None].domain()[0]
        tVal = self.tom.reward(vector)
        self.assertAlmostEqual(tVal,1.,8)
        jVal = self.jerry.reward(vector)
        self.assertAlmostEqual(jVal,0.,8)
        for action in self.tom.actions:
            encoding = self.world.value2float(self.tom.name,action)
            self.assertEqual(action,self.world.float2value(self.tom.name,encoding))

    def testBeliefModels(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.world.setOrder([self.tom.name])
        self.tom.addModel('optimist')
        self.tom.setBelief(stateKey(self.jerry.name,'health'),20,'optimist')
        self.tom.addModel('pessimist')
        self.world.setModel(self.jerry.name,True)
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'optimist': 0.5,'pessimist': 0.5})
        actions = {self.tom.name: self.hit}
        self.world.step(actions)
        vector = self.world.state[None].domain()[0]
        beliefs = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector))
        for belief in beliefs.domain():
            model = self.world.getModel(self.tom.name,belief)
            if self.tom.models[model].has_key('beliefs'):
                nested = self.tom.models[model]['beliefs']
                self.assertEqual(len(nested),1)
                nested = nested.domain()[0]
                self.assertEqual(len(nested),1)
                self.assertAlmostEqual(nested[stateKey(self.jerry.name,'health')],10.,8)

    def testObservation(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.world.setOrder([self.tom.name])
        self.world.setModel(self.jerry.name,True)
        key = stateKey(self.jerry.name,'health')
        self.jerry.setBelief(key,Distribution({20: 0.5, 50: 0.5}))
        tree = makeTree({'if': thresholdRow(key,40),
                         True: {'distribution': [(KeyedVector({CONSTANT: 50}),.8),
                                                 (KeyedVector({CONSTANT: 20}),.2)]},
                         False: {'distribution': [(KeyedVector({CONSTANT: 50}),.2),
                                                  (KeyedVector({CONSTANT: 20}),.8)]}})
        self.jerry.defineObservation(key,tree)
        actions = {self.tom.name: self.hit}
        vector = self.world.state[None].domain()[0]
        omegaDist = self.jerry.observe(vector,actions)
        for omega in omegaDist.domain():
            new = KeyedVector(vector)
            model = self.jerry.index2model(self.jerry.stateEstimator(vector,new,omega))
            beliefs = self.jerry.models[model]['beliefs']
            if omega[key] > 30:
                # We observed a high value, so we should have a stronger belief in the higher value
                # which is now 40 after the hit
                for belief in beliefs.domain():
                    if beliefs[belief] > 0.5:
                        self.assertAlmostEqual(belief[key],40,8)
                    else:
                        self.assertAlmostEqual(belief[key],10,8)
            else:
                # We observed a low value, so we should have a stronger belief in the lower value
                # which is now 10 after the hit
                for belief in beliefs.domain():
                    if beliefs[belief] < 0.5:
                        self.assertAlmostEqual(belief[key],40,8)
                    else:
                        self.assertAlmostEqual(belief[key],10,8)

    def testUnobservedAction(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.addModels()
        self.world.setOrder([self.tom.name])
        self.world.setModel(self.jerry.name,True)
        self.jerry.setBelief(stateKey(self.jerry.name,'health'),50)
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        tree = makeTree(True)
        self.jerry.defineObservation(self.tom.name,tree,self.hit,domain=ActionSet)
        tree = makeTree({'distribution': [(True,0.25),(False,0.75)]})
        self.jerry.defineObservation(self.tom.name,tree,self.chase,domain=ActionSet)
        vector = self.world.state[None].domain()[0]
        self.saveload()
        self.world.step({self.tom.name: self.hit})
        vector = self.world.state[None].domain()[0]

    def testRewardModels(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.addModels()
        self.world.setOrder([self.tom.name])
        # Add Jerry's model to the world (so that it gets updated)
        self.world.setModel(self.jerry.name,True)
        # Give Jerry uncertainty about Tom
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        self.saveload()
        # Hitting should make Jerry think Tom is more of a foe
        actions = {self.tom.name: self.hit}
        self.world.step(actions)
        vector = self.world.state[None].domain()[0]
        belief01 = self.jerry.getAttribute('beliefs',self.world.getModel(self.jerry.name,vector))
        key = modelKey(self.tom.name)
        for belief in belief01.domain():
            if self.tom.index2model(belief[key]) == 'foe':
                prob01 = belief01[belief]
                break
        self.assertGreater(prob01,0.5)
        # If we think of Tom as even more of an optimizer, then our update should be stronger
        self.tom.setAttribute('rationality',10.,'foe')
        self.tom.setAttribute('rationality',10.,'friend')
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        self.world.step(actions)
        vector = self.world.state[None].domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief10 = self.jerry.getAttribute('beliefs',model)
        key = modelKey(self.tom.name)
        for belief in belief10.domain():
            if self.tom.index2model(belief[key]) == 'foe':
                prob10 = belief10[belief]
                break
        self.assertGreater(prob10,prob01)
        # If we keep the same models, but get another observation, we should update even more
        self.world.step(actions)
        vector = self.world.state[None].domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief1010 = self.jerry.getAttribute('beliefs',model)
        key = modelKey(self.tom.name)
        for belief in belief1010.domain():
            if self.tom.index2model(belief[key]) == 'foe':
                prob1010 = belief1010[belief]
                break
        self.assertGreater(prob1010,prob10)

    def testDynamics(self):
        self.world.setOrder([self.tom.name])
        self.addStates()
        self.addActions()
        self.addDynamics()
        key = stateKey(self.jerry.name,'health')
        self.assertEqual(len(self.world.state[None]),1)
        vector = self.world.state[None].domain()[0]
        self.assertTrue(vector.has_key(stateKey(self.tom.name,'health')))
        self.assertTrue(vector.has_key(turnKey(self.tom.name)))
        self.assertTrue(vector.has_key(key))
        self.assertTrue(vector.has_key(CONSTANT))
        self.assertEqual(len(vector),4)
        self.assertEqual(vector[stateKey(self.tom.name,'health')],50)
        self.assertEqual(vector[key],50)
        outcome = self.world.step({self.tom.name: self.chase})
        for i in range(7):
            self.assertEqual(len(self.world.state[None]),1)
            vector = self.world.state[None].domain()[0]
            self.assertTrue(vector.has_key(stateKey(self.tom.name,'health')))
            self.assertTrue(vector.has_key(turnKey(self.tom.name)))
            self.assertTrue(vector.has_key(key))
            self.assertTrue(vector.has_key(CONSTANT))
            self.assertEqual(len(vector),4)
            self.assertEqual(vector[stateKey(self.tom.name,'health')],50)
            self.assertEqual(vector[key],max(50-10*i,0))
            outcome = self.world.step({self.tom.name: self.hit})
            self.saveload()

    def testRewardOnOthers(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.world.setOrder([self.tom.name])
        vector = self.world.state[None].domain()[0]
        # Create Jerry's goals
        goal = maximizeFeature(stateKey(self.jerry.name,'health'))
        self.jerry.setReward(goal,1.)
        jVal = -self.jerry.reward(vector)
        # Create Tom's goals from scratch
        minGoal = minimizeFeature(stateKey(self.jerry.name,'health'))
        self.tom.setReward(minGoal,1.)
        self.saveload()
        tRawVal = self.tom.reward(vector)
        self.assertAlmostEqual(jVal,tRawVal,8)
        # Create Tom's goals as a function of Jerry's
        self.tom.models[True]['R'].clear()
        self.tom.setReward(self.jerry.name,-1.)
        self.saveload()
        tFuncVal = self.tom.reward(vector)
        self.assertAlmostEqual(tRawVal,tFuncVal,8)
        # Test effect of functional reward on value function
        self.tom.setHorizon(1)
        self.saveload()
        vHit = self.tom.value(vector,self.hit)['V']
        vChase = self.tom.value(vector,self.chase)['V']
        self.assertAlmostEqual(vHit,vChase+.1,8)

    def testReward(self):
        self.addStates()
        key = stateKey(self.jerry.name,'health')
        goal = makeTree({'if': thresholdRow(key,5),
                         True: KeyedVector({key: -2}),
                         False: KeyedVector({key: -1})})
        goal = goal.desymbolize(self.world.symbols)
        self.jerry.setReward(goal,1.)
        R = self.jerry.models[True]['R']
        self.assertEqual(len(R),1)
        newGoal = R.keys()[0]
        self.assertEqual(newGoal,goal)
        self.assertAlmostEqual(R[goal],1.,8)
        self.jerry.setReward(goal,2.)
        self.assertEqual(len(R),1)
        self.assertEqual(R.keys()[0],goal)
        self.assertAlmostEqual(R[goal],2.,8)

    def testTurnDynamics(self):
        self.addStates()
        self.addActions()
        self.world.setOrder([self.tom.name,self.jerry.name])
        self.assertEqual(self.world.maxTurn,1)
        self.saveload()
        vector = self.world.state[None].domain()[0]
        jTurn = turnKey(self.jerry.name)
        tTurn = turnKey(self.tom.name)
        self.assertEqual(self.world.next(),[self.tom.name])
        self.assertEqual(vector[tTurn],0)
        self.assertEqual(vector[jTurn],1)
        self.world.step()
        vector = self.world.state[None].domain()[0]
        self.assertEqual(self.world.next(),[self.jerry.name])
        self.assertEqual(vector[tTurn],1)
        self.assertEqual(vector[jTurn],0)
        self.world.step()
        vector = self.world.state[None].domain()[0]
        self.assertEqual(self.world.next(),[self.tom.name])
        self.assertEqual(vector[tTurn],0)
        self.assertEqual(vector[jTurn],1)
        # Try some custom dynamics
        self.world.setTurnDynamics(self.tom.name,self.hit,makeTree(noChangeMatrix(tTurn)))
        self.world.setTurnDynamics(self.jerry.name,self.hit,makeTree(noChangeMatrix(tTurn)))
        self.world.step()
        vector = self.world.state[None].domain()[0]
        self.assertEqual(self.world.next(),[self.tom.name])
        self.assertEqual(vector[tTurn],0)
        self.assertEqual(vector[jTurn],1)
        self.world.step({self.tom.name: self.chase})
        vector = self.world.state[None].domain()[0]
        self.assertEqual(self.world.next(),[self.jerry.name])
        self.assertEqual(vector[tTurn],1)
        self.assertEqual(vector[jTurn],0)

    def testStatic(self):
        self.addStates()
        self.addActions()
        self.addDynamics()
        self.addModels()
        self.world.setModel(self.jerry.name,True)
        self.world.setMentalModel(self.jerry.name,self.tom.name,{'friend': 0.5,'foe': 0.5})
        self.world.setOrder([self.tom.name])
        vector = self.world.state[None].domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief0 = self.jerry.models[model]['beliefs']
        result = self.world.step({self.tom.name: self.hit})
        vector = self.world.state[None].domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief1 = self.jerry.models[model]['beliefs']
        key = modelKey(self.tom.name)
        for vector in belief0.domain():
            if self.tom.index2model(vector[key]) == 'friend':
                self.assertGreater(belief0[vector],belief1[vector])
            else:
                self.assertGreater(belief1[vector],belief0[vector])
        # Now with the static beliefs
        self.jerry.setAttribute('static',True,model)
        self.saveload()
        self.world.step()
        vector = self.world.state[None].domain()[0]
        model = self.world.getModel(self.jerry.name,vector)
        belief2 = self.jerry.models[model]['beliefs']
        for vector in belief1.domain():
            self.assertAlmostEqual(belief1[vector],belief2[vector],8)
Beispiel #16
0
        for i in range(totals[base]):
            num = num + 1
            me = Agent(base + str(num))
            world.addAgent(me)
            # State
            # 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)
Beispiel #17
0
def createWorld(numPlayers,regionTable,starts,generation='additive',maxResources=32):
    """
    @param numPlayers: number of players in the game
    @type numPlayers: int
    @param regionTable: a table of regions, indexed by name
    @param starts: a list of starting regions, one for each player
    @param maxResources: the maximum number of resources a player may have
    @type maxResources: int
    """
    world = ResourceWorld(allocateVerb='allocate',allocationState='invaders',winnerState='invader')

    # Create regions
    regions = set()
    for name,table in regionTable.items():
        region = Agent(name)
        world.addAgent(region)
        regions.add(region)

        world.defineState(name,'occupants',int,lo=0,hi=maxResources,
                          description='Number of resources in %s' % (region))
        region.setState('occupants',table['occupants'] if table.has_key('occupants') else table['value'])

        world.defineState(name,'value',int,lo=0,hi=maxResources,
                          description='Number of resources generated by %s' % (region))
        region.setState('value',table['value'])

        world.defineState(name,'invaders',int,lo=0,hi=numPlayers*maxResources,
                         description='Number of resources invading %s' % (region))
        region.setState('invaders',0)
        world.dynamics[stateKey(region.name,'invaders')] = True

    # Create agents for human players
    players = []
    for player in range(numPlayers):
        players.append(ResourceAgent('Player%d' % (player+1),'resources','allocate',
                                     [region.name for region in regions]))
        world.addAgent(players[player])
        players[player].allocateAll = True

        world.defineState(players[player].name,'resources',int,lo=0,hi=maxResources,
                          description='Number of total resources owned by %s' % (players[player].name),
                          combinator='*')
        players[player].setState('resources',0)

    # Create agent for "enemy"
    enemy = Agent('Enemy')
    world.addAgent(enemy)

    owners = world.agents.keys()

    for region in regions:
        world.defineState(region.name,'owner',set,set(owners),
                          description='Name of owner of %s' % (region))
        region.setState('owner',enemy.name)

        world.defineState(region.name,'invader',set,set(owners)-{enemy.name}|{world.nullAgent},
                          description='Name of invader who will own %s if successful' % (region))
        try:
            index = starts.index(region.name)
            region.setState('invader','Player%d' % (index+1))
        except ValueError:
            region.setState('invader',world.nullAgent)
        world.dynamics[stateKey(region.name,'invader')] = True

    # Set players' initial territories
    for index in range(numPlayers):
        region = world.agents[starts[index]]
        region.setState('owner',players[index].name)
        # Players can invade only if enemy owns it and they (or teammate) own a neighboring country
        for region in regions:
            tree = False
            for neighbor in regionTable[region.name]['neighbors']:
                tree = {'if': equalRow(stateKey(neighbor,'owner'),enemy.name),
                        True: tree,
                        False: True}
            tree = makeTree({'if': equalRow(stateKey(region.name,'owner'),enemy.name),
                             True: tree,
                             False: False})
            players[index].objectLegality[region.name] = tree.desymbolize(world.symbols)

    # Create region "action"
    for region in regions:
        region.addAction({'verb': 'generate'})
    
    # Set order of play
    world.setOrder([set([region.name for region in regions]),set([player.name for player in players])])

    # Winner determination
    for region in regions:
        # Determine the owner after determining who's invading
        world.addDependency(stateKey(region.name,'owner'),stateKey(region.name,'invader'))
        # Determine the winner of the invasion
        owner = stateKey(region.name,'owner')
        world.dynamics[owner] = True
        invader = stateKey(region.name,'invader')
        defenders = stateKey(region.name,'occupants')
        invaders = stateKey(region.name,'invaders')
        value = stateKey(region.name,'value')
        for player in players:
            resources = stateKey(player.name,'resources')
            # Determine how many resources lost
            action = Action({'subject': player.name,'verb': 'allocate','object': region.name})
            world.addDependency(resources,invader)
            if generation == 'additive': # or generation == 'none':
                # Lose only those resources allocated
                tree = makeTree(incrementMatrix(resources,'-%s' % (actionKey('amount'))))
            else:
                # Lose all resources
                tree = makeTree(setToConstantMatrix(resources,0))
            world.setDynamics(resources,action,tree)
            # Regain resources from owned territories
            action = Action({'subject': region.name,'verb': 'generate'})
            if generation == 'additive' or generation == 'restorative':
                tree = makeTree({'if': equalRow(owner,player.name),
                                 True: addFeatureMatrix(resources,value),
                                 False: None})
            elif generation == 'minimal':
                if region is world.agents[starts[int(player.name[-1])-1]]:
                    # Get resources from home base (repeated)
                    tree = makeTree({'if': equalRow(owner,player.name),
                                     True: addFeatureMatrix(resources,value),
                                     False: None})
                else:
                    # And any new winnings (one-time)
                    tree = makeTree({'if': equalRow(owner,player.name),
                                     True: {'if': equalFeatureRow(owner,invader),
                                            True: addFeatureMatrix(resources,value),
                                            False: None},
                                     False: None})
            elif generation is 'none':
                if region is world.agents[starts[int(player.name[-1])-1]]:
                    # Get resources from home base if below threshold
                    tree = makeTree({'if': greaterThanRow(resources,value),
                                     True: None,
                                     False: setToFeatureMatrix(resources,value)})
                else:
                    tree = makeTree({'if': equalRow(owner,player.name),
                                     True: {'if': equalFeatureRow(owner,invader),
                                            True: addFeatureMatrix(resources,value),
                                            False: None},
                                     False: None})
            world.setDynamics(resources,action,tree)
    # The game has two phases: generating resources and allocating resources
    world.defineState(None,'phase',list,['generate','allocate'],combinator='*',
                      description='The current phase of the game')
    world.setState(None,'phase','generate')
    key = stateKey(None,'phase')
    # If we generate, then the phase becomes allocate
    action = Action({'subject': list(regions)[0].name,'verb': 'generate'})
    tree = makeTree(setToConstantMatrix(key,'allocate'))
    world.setDynamics(key,action,tree)
    # If we allocate, then the phase becomes generate
    tree = makeTree(setToConstantMatrix(key,'generate'))
    for region in regions:
        for player in players:
            action = Action({'subject': player.name,'verb': 'allocate','object': region.name})
            world.setDynamics(key,action,tree)

    # Game ends when territory is all won
    tree = {'if': equalRow(key,'allocate'),
            True: True,
            False: False}
    for region in regions:
        tree = {'if': equalRow(stateKey(region.name,'owner'),enemy.name),
                True: False,
                False: tree}
    world.addTermination(makeTree(tree))
    # Or if nobody has any resources
    vector = KeyedVector()
    for player in players:
        vector[stateKey(player.name,'resources')] = 1.
    tree = {'if': equalRow(stateKey(None,'phase'),'allocate'),
            True: {'if': KeyedPlane(vector,0.5),
                   True: False, False: True},
            False: False}
    world.addTermination(makeTree(tree))

    # Keep track of which round it is
    world.defineState(None,'round',int,description='The current round of the game')
    world.setState(None,'round',0)
    action = Action({'subject': list(regions)[0].name,
                     'verb': 'generate'})
    key = stateKey(None,'round')
    world.setDynamics(key,action,makeTree(incrementMatrix(key,1)))
    return world
LOW = 50
NUM_BINS = 11
NUM_SAMPLES = 100

if __name__ == '__main__':

    # create world and add agent
    world = World()
    agent = Agent('Agent')
    world.addAgent(agent)

    # add variable
    feat = world.defineState(agent.name, 'x', float, lo=LOW, hi=HIGH)

    # add single action that discretizes the feature
    action = agent.addAction({'verb': '', 'action': 'discretize'})
    tree = makeTree(discretization_tree(world, feat, NUM_BINS))
    world.setDynamics(feat, action, tree)

    world.setOrder([{agent.name}])

    print('====================================')
    print('High:\t{}'.format(HIGH))
    print('Low:\t{}'.format(LOW))
    print('Bins:\t{}'.format(NUM_BINS))

    print('\nSamples/steps:')
    values_original = []
    values_discrete = []
    for i in range(NUM_SAMPLES):
        num = np.random.uniform(LOW, HIGH)