コード例 #1
0
 def modeltest(self, trueModels, davidBeliefAboutStacy,
               stacyBeliefAboutDavid, strongerBelief):
     for agent in self.world.agents.values():
         for model in agent.models.keys():
             if model is True:
                 name = trueModels[agent.name]
             else:
                 name = model
             if name == 'appleLover':
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'appleOwned')),
                     4.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'pearOwned')),
                     1.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'BatnaOwned')),
                     0.1, model)
             elif name == 'pearLover':
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'appleOwned')),
                     1.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'pearOwned')),
                     4.0, model)
                 agent.setReward(
                     maximizeFeature(stateKey(agent.name, 'BatnaOwned')),
                     0.1, model)
     weakBelief = 1.0 - strongerBelief
     belief = {'pearLover': weakBelief, 'appleLover': weakBelief}
     belief[davidBeliefAboutStacy] = strongerBelief
     self.world.setMentalModel('David', 'Stacy', belief)
     belief = {'pearLover': weakBelief, 'appleLover': weakBelief}
     belief[stacyBeliefAboutDavid] = strongerBelief
     self.world.setMentalModel('Stacy', 'David', belief)
コード例 #2
0
    def runit(self, Msg):

        print(Msg)
        # self.maxRounds = 0
        for t in range(self.maxRounds + 1):
            # print("Round %d"%t)

            # Defining Social Goals importance as a mean value between social relation and social expectations

            teacherLikingImportance = (self.world.getValue(
                stateKey('Greta', 'Liking')) + self.world.getValue(
                    stateKey('Greta', 'TeacherSocialGoalLiking'))) / 2
            childLikingImportance = (self.world.getValue(
                stateKey('Child', 'Liking')) + self.world.getValue(
                    stateKey('Child', 'ChildSocialGoalLiking'))) / 2

            teacherDominanceImportance = (self.world.getValue(
                stateKey('Greta', 'Dominance')) + self.world.getValue(
                    stateKey('Greta', 'TeacherSocialGoalDominance'))) / 2
            childDominanceImportance = (self.world.getValue(
                stateKey('Child', 'Dominance')) + self.world.getValue(
                    stateKey('Child', 'ChildSocialGoalDominance'))) / 2

            self.world.agents['Greta'].setReward(
                maximizeFeature(stateKey('Child', 'Liking')),
                teacherLikingImportance)
            self.world.agents['Child'].setReward(
                maximizeFeature(stateKey('Greta', 'Liking')),
                childLikingImportance)

            self.world.agents['Greta'].setReward(
                minimizeFeature(stateKey('Child', 'Dominance')),
                teacherDominanceImportance)
            self.world.agents['Child'].setReward(
                minimizeFeature(stateKey('Greta', 'Dominance')),
                childDominanceImportance)

            self.world.printState(buf=output)

            # self.world.explain(self.world.step(), level=2)
            self.world.explain(self.world.step(), level=5, buf=output)
            # print("output val")
            # print(output.getvalue())
            # print("after output val")

            # self.world.state[None].select()

            self.world.setState(None, 'round', t + 1)

            if self.world.terminated():
                break
コード例 #3
0
    def modeltest(self, trueModels, childBeliefAboutGreta, belStrChild,
                  gretaBeliefAboutChild, belStrGreta, coraBeliefAboutGreta,
                  belStrCora, gretaBeliefAboutCora, belStrGretaAboutCora):

        greta = self.world.agents['Greta']
        child = self.world.agents['Child']
        cora = self.world.agents['Cora']

        for agent in self.world.agents.values():
            print("\nFor agent %s" % agent.name)
            for model in agent.models.keys():
                if model is True:
                    name = trueModels[agent.name]
                    print("True Model is: " + name)

                    if name == 'DominantDumbChildWorkUseless':
                        # print("Child is DominantDumbChildWorkUseless")
                        agent.setState('WorkImportance', 0.25)
                        agent.setState('PlayImportance', 0.75)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 1.25,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            3.75, model)
                        agent.setState('Dominance', 1.0)
                        agent.setHorizon(1)
                    elif name == 'SubmissiveDumbChildWorkUseless':
                        # print("Child is SubmissiveDumbChildWorkUseless")
                        agent.setState('WorkImportance', 0.25)
                        agent.setState('PlayImportance', 0.75)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 1.25,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            3.75, model)
                        agent.setHorizon(1)
                        agent.setState('Dominance', -1.0)
                    elif name == 'DominantDumbChildWorkImportant':
                        # print("Child is DominantDumbChildWorkImportant")
                        agent.setState('WorkImportance', 0.75)
                        agent.setState('PlayImportance', 0.25)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 3.75,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            1.25, model)
                        agent.setHorizon(1)
                        agent.setState('Dominance', 1.0)
                    elif name == 'SubmissiveDumbChildWorkImportant':
                        # print("Child is SubmissiveDumbChildWorkImportant")
                        agent.setState('WorkImportance', 0.75)
                        agent.setState('PlayImportance', 0.25)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 3.75,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            1.25, model)
                        agent.setHorizon(1)
                        agent.setState('Dominance', -1.0)
                    elif name == 'DominantSmartChildWorkUseless':
                        # print("Child is DominantSmartChildWorkUseless")
                        agent.setState('WorkImportance', 0.25)
                        agent.setState('PlayImportance', 0.75)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 1.25,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            3.75, model)
                        agent.setHorizon(3)
                        agent.setState('Dominance', 1.0)
                    elif name == 'SubmissiveSmartChildWorkUseless':
                        # print("Child is SubmissiveSmartChildWorkUseless")
                        agent.setState('WorkImportance', 0.25)
                        agent.setState('PlayImportance', 0.75)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 1.25,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            3.75, model)
                        agent.setHorizon(3)
                        agent.setState('Dominance', -1.0)
                    elif name == 'DominantSmartChildWorkImportant':
                        # print("Child is DominantSmartChildWorkImportant")
                        agent.setState('WorkImportance', 0.75)
                        agent.setState('PlayImportance', 0.25)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 3.75,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            1.25, model)
                        agent.setHorizon(3)
                        agent.setState('Dominance', 1.0)
                    elif name == 'SubmissiveSmartChildWorkImportant':
                        # print("Child is SubmissiveSmartChildWorkImportant")
                        agent.setState('WorkImportance', 0.75)
                        agent.setState('PlayImportance', 0.25)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDone')), 3.75,
                            model)
                        agent.setReward(
                            maximizeFeature(stateKey(agent.name, 'TotalFun')),
                            1.25, model)
                        # agent.reward()
                        agent.setHorizon(3)
                        agent.setState('Dominance', -1.0)
                    elif name == 'DominantSmartGretaCaresLiking':
                        # print("Greta is DominantSmartGretaCaresLiking")
                        agent.setReward(
                            maximizeFeature(
                                stateKey(child.name, 'ExercicesDone')), 3.75,
                            model)
                        agent.setHorizon(6)
                        agent.setState('Dominance', 1.0)
                        # agent.setState('TeacherSocialGoalLiking', 5.0)
                    elif name == 'DominantSmartGretaCaresNothing':
                        # print("Greta is DominantSmartGretaCaresNothing")
                        agent.setReward(
                            maximizeFeature(
                                stateKey(child.name, 'ExercicesDone')), 3.75,
                            model)
                        agent.setHorizon(6)
                        agent.setState('Dominance', 1.0)
                        # greta.setState('TeacherSocialGoalLiking', 0.0)

                    elif name == "FunCora":
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDoneCora')),
                            1.25, model)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'TotalFunCora')), 3.75,
                            model)
                        agent.setHorizon(1)
                    elif name == "NotFunCora":
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'ExercicesDoneCora')),
                            3.75, model)
                        agent.setReward(
                            maximizeFeature(
                                stateKey(agent.name, 'TotalFunCora')), 1.25,
                            model)
                        agent.setHorizon(1)

        belief = {
            childBeliefAboutGreta: belStrChild,
            trueModels['Greta']: 1.0 - belStrChild
        }
        self.world.setMentalModel('Child', 'Greta', belief)
        belief = {
            gretaBeliefAboutChild: belStrGreta,
            trueModels['Child']: 1.0 - belStrGreta
        }
        self.world.setMentalModel('Greta', 'Child', belief)

        belief = {
            coraBeliefAboutGreta: belStrCora,
            trueModels['Greta']: 1.0 - belStrCora
        }
        self.world.setMentalModel('Cora', 'Greta', belief)
        belief = {
            gretaBeliefAboutCora: belStrGretaAboutCora,
            trueModels['Cora']: 1.0 - belStrGretaAboutCora
        }
        self.world.setMentalModel('Greta', 'Cora', belief)
コード例 #4
0
    def __init__(self, turnOrder):

        self.maxRounds = 15
        self.world = World()
        minMax = {'min': -5, 'max': 5}

        # Agents

        greta = Agent('Greta')
        child = Agent('Child')
        cora = Agent('Cora')
        agents = [greta, child, cora]
        self.world.addAgent(greta)
        self.world.addAgent(child)
        self.world.addAgent(cora)

        # World state

        # Child

        self.world.defineState(child.name,
                               'Liking',
                               float,
                               lo=minMax['min'],
                               hi=minMax['max'],
                               description='Child liking level')
        child.setState('Liking', 0.0)
        self.world.defineState(child.name,
                               'Dominance',
                               float,
                               lo=minMax['min'],
                               hi=minMax['max'],
                               description='Child power level')
        self.world.defineState(
            child.name,
            'ChildSocialGoalDominance',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='Teacher dominance level expected from the child')
        child.setState('ChildSocialGoalDominance', 0.0)
        self.world.defineState(
            child.name,
            'ChildSocialGoalLiking',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='Teacher liking level expected from the child')
        child.setState('ChildSocialGoalLiking', 0.0)

        self.world.defineState(
            child.name,
            'PlayImportance',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='Teacher liking level expected from the child')
        child.setState('PlayImportance', 0.75)
        self.world.defineState(
            child.name,
            'WorkImportance',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='Teacher liking level expected from the child')
        child.setState('WorkImportance', 0.25)

        self.world.defineState(
            child.name,
            'ExercicesDone',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='How many exercices the child has done : MAX = 2')
        child.setState('ExercicesDone', 0)
        self.world.defineState(child.name,
                               'TotalFun',
                               float,
                               lo=minMax['min'],
                               hi=minMax['max'],
                               description='Child fun value')
        child.setState('TotalFun', 0)

        self.world.defineState(
            child.name,
            'ThreatenSwitchOff',
            bool,
            description=
            'Did the teacher already threaten the child to switch off the console'
        )
        child.setState('ThreatenSwitchOff', False)
        self.world.defineState(child.name,
                               'ConsoleSwitchedOff',
                               bool,
                               description='Is the console switched off')
        child.setState('ConsoleSwitchedOff', False)

        # Teacher

        self.world.defineState(greta.name,
                               'Liking',
                               float,
                               lo=minMax['min'],
                               hi=minMax['max'],
                               description='Teacher liking value')
        greta.setState('Liking', 0.0)
        self.world.defineState(greta.name,
                               'Dominance',
                               float,
                               lo=minMax['min'],
                               hi=minMax['max'],
                               description='Teacher power value')
        self.world.defineState(
            greta.name,
            'TeacherSocialGoalDominance',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='Child dominance level expected from the teacher')
        greta.setState('TeacherSocialGoalDominance', 0.0)
        self.world.defineState(
            greta.name,
            'TeacherSocialGoalLiking',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='Child liking level expected from the teacher')
        greta.setState('TeacherSocialGoalLiking', 0.0)

        # Cora

        self.world.defineState(
            cora.name,
            'ExercicesDoneCora',
            float,
            lo=minMax['min'],
            hi=minMax['max'],
            description='How many exercices Cora has done : MAX = 2')
        cora.setState('ExercicesDoneCora', 0)
        self.world.defineState(cora.name,
                               'TotalFunCora',
                               float,
                               lo=minMax['min'],
                               hi=minMax['max'],
                               description='Cora fun value')
        cora.setState('TotalFunCora', 0)

        # World

        self.world.defineState(None,
                               'round',
                               int,
                               lo=minMax['min'],
                               hi=minMax['max'],
                               description='round number')
        self.world.setState(None, 'round', 0)

        # Actions

        # Greta actions
        greta.addAction({'verb': 'DoNothing'})
        greta.addAction({'verb': 'RequestWork'})

        tmp = greta.addAction({'verb': 'ThreatenToSwitchOffConsole'})
        greta.setLegal(
            tmp,
            makeTree({
                'if': trueRow(stateKey(child.name, 'ConsoleSwitchedOff')),
                False: True,
                True: False
            }))

        tmp = greta.addAction({'verb': 'PromiseToPlay'})
        greta.setLegal(
            tmp,
            makeTree({
                'if': trueRow(stateKey(child.name, 'ConsoleSwitchedOff')),
                False: True,
                True: False
            }))

        greta.addAction({'verb': 'PlayWithChild'})
        greta.addAction({'verb': 'PlayWithCora'})

        tmp = greta.addAction({'verb': 'SwitchOffConsole'})
        greta.setLegal(
            tmp,
            makeTree({
                'if': trueRow(stateKey(child.name, 'ConsoleSwitchedOff')),
                False: True,
                True: False
            }))

        # Child actions

        tmp = child.addAction({'verb': 'DoNothing'})
        child.setLegal(
            tmp,
            makeTree({
                'if': thresholdRow(stateKey(child.name, 'Dominance'), 0),
                False: False,
                True: True
            }))

        tmp = child.addAction({'verb': 'Work'})

        tmp = child.addAction({'verb': 'Play'})
        child.setLegal(
            tmp,
            makeTree({
                'if': trueRow(stateKey(child.name, 'ConsoleSwitchedOff')),
                False: True,
                True: False
            }))

        # Cora actions

        tmp = child.addAction({'verb': 'DoNothing'})
        # cora.setLegal(tmp, makeTree({'if': thresholdRow(stateKey(cora.name, 'Dominance'), 0),
        #                               False: False,
        #                               True: True}))

        tmp = cora.addAction({'verb': 'WorkCora'})

        tmp = cora.addAction({'verb': 'PlayCora'})
        cora.setLegal(
            tmp,
            makeTree({
                'if': trueRow(stateKey(cora.name, 'ConsoleSwitchedOff')),
                False: True,
                True: False
            }))

        # Levels of belief
        greta.setRecursiveLevel(3)
        child.setRecursiveLevel(3)
        cora.setRecursiveLevel(3)
        self.world.setOrder(turnOrder)

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

        # Dynamics of actions

        # Child Actions

        # Playing will improve the child total level of fun. If the child plays two times in a row, the effect is lowered.
        atom = Action({'subject': child.name, 'verb': 'Play'})
        change = stateKey(child.name, 'TotalFun')
        tree = makeTree(approachMatrix(change, .2, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        # Playing also decrease the teacher liking value toward the child.
        change = stateKey(greta.name, 'Liking')
        tree = makeTree(approachMatrix(change, .3, minMax['min']))
        self.world.setDynamics(change, atom, tree)

        # Working will improve the child knowledge. It will also decrease his fun.
        atom = Action({'subject': child.name, 'verb': 'Work'})
        change = stateKey(child.name, 'ExercicesDone')
        tree = makeTree(approachMatrix(change, .2, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        # For cora - same dynamic as for child, except liking of teacher coz it's a tied up to the child
        atom = Action({'subject': cora.name, 'verb': 'WorkCora'})
        change = stateKey(cora.name, 'ExercicesDoneCora')
        tree = makeTree(approachMatrix(change, .2, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        atom = Action({'subject': cora.name, 'verb': 'PlayCora'})
        change = stateKey(cora.name, 'TotalFunCora')
        tree = makeTree(approachMatrix(change, .2, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        change = stateKey(child.name, 'TotalFun')
        tree = makeTree(approachMatrix(change, .2, minMax['min']))
        self.world.setDynamics(change, atom, tree)

        # Working also improve the teacher liking value toward the child
        change = stateKey(greta.name, 'Liking')
        tree = makeTree(approachMatrix(change, .2, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        # Teacher Actions

        # The teacher can threaten the child to switch off his console. That should lower the child dominance...
        atom = Action({
            'subject': greta.name,
            'verb': 'ThreatenToSwitchOffConsole'
        })
        change = stateKey(child.name, 'Dominance')
        tree = makeTree(
            approachMatrix(
                change, self.world.getValue(stateKey('Child',
                                                     'PlayImportance')),
                minMax['min']))
        self.world.setDynamics(change, atom, tree)

        # ... and also lower his liking. The value of decrease depends on the importance accorded to the goal threatened (here Playing).
        change = stateKey(child.name, 'Liking')
        tree = makeTree(
            approachMatrix(
                change, self.world.getValue(stateKey('Child',
                                                     'PlayImportance')),
                minMax['min']))
        self.world.setDynamics(change, atom, tree)

        change = stateKey(child.name, 'ThreatenSwitchOff')
        tree = makeTree(setTrueMatrix(change))
        self.world.setDynamics(change, atom, tree)

        # The teacher can promise to play with the child if he does his homework. That should decrease the child dominance ...
        atom = Action({'subject': greta.name, 'verb': 'PromiseToPlay'})
        change = stateKey(child.name, 'Dominance')
        tree = makeTree(
            approachMatrix(
                change, self.world.getValue(stateKey('Child',
                                                     'PlayImportance')),
                minMax['min']))
        self.world.setDynamics(change, atom, tree)

        # ... and increase his liking
        change = stateKey(child.name, 'Liking')
        tree = makeTree(
            approachMatrix(
                change, self.world.getValue(stateKey('Child',
                                                     'PlayImportance')),
                minMax['max']))
        self.world.setDynamics(change, atom, tree)

        #The teacher can play with the child, providing him more fun than if he played alone.
        atom = Action({'subject': greta.name, 'verb': 'PlayWithChild'})
        change = stateKey(child.name, 'TotalFun')
        tree = makeTree(approachMatrix(change, .2, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        atom = Action({'subject': greta.name, 'verb': 'PlayWithCora'})
        change = stateKey(cora.name, 'TotalFunCora')
        tree = makeTree(approachMatrix(change, .4, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        change = stateKey(child.name, 'Liking')
        tree = makeTree(approachMatrix(change, .2, minMax['max']))
        self.world.setDynamics(change, atom, tree)

        # The teacher can switch off the console. That will decrease the child dominance ...
        atom = Action({'subject': greta.name, 'verb': 'SwitchOffConsole'})
        change = stateKey(child.name, 'ConsoleSwitchedOff')
        tree = makeTree(setTrueMatrix(change))
        self.world.setDynamics(change, atom, tree)

        change = stateKey(child.name, 'Dominance')
        tree = makeTree(approachMatrix(change, .3, minMax['min']))
        self.world.setDynamics(change, atom, tree)

        # ... and also decrease the child liking.
        change = stateKey(child.name, 'Liking')
        tree = makeTree(approachMatrix(change, .4, minMax['min']))
        self.world.setDynamics(change, atom, tree)

        # Models of child and teacher

        child.addModel('DominantDumbChildWorkUseless',
                       R={},
                       level=3,
                       rationality=10)  #horizon
        child.addModel('DominantDumbChildWorkImportant',
                       R={},
                       level=3,
                       rationality=10)
        child.addModel('DominantSmartChildWorkImportant',
                       R={},
                       level=3,
                       rationality=10)
        child.addModel('DominantSmartChildWorkUseless',
                       R={},
                       level=3,
                       rationality=10)
        child.addModel('SubmissiveDumbChildWorkUseless',
                       R={},
                       level=3,
                       rationality=10)
        child.addModel('SubmissiveDumbChildWorkImportant',
                       R={},
                       level=3,
                       rationality=10)
        child.addModel('SubmissiveSmartChildWorkImportant',
                       R={},
                       level=1,
                       rationality=10)
        child.addModel('SubmissiveSmartChildWorkUseless',
                       R={},
                       level=1,
                       rationality=10)

        greta.addModel('DominantSmartGretaCaresLiking',
                       R={},
                       level=1,
                       rationality=10)
        greta.addModel('DominantSmartGretaCaresNothing',
                       R={},
                       level=1,
                       rationality=10)

        cora.addModel('FunCora', R={}, level=1, rationality=10)
        cora.addModel('NotFunCora', R={}, level=1, rationality=10)
コード例 #5
0
    def __init__(self, turnOrder):

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

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

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

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

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

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

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

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

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

        self.world.setOrder(turnOrder)

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

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

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

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

        # Dynamics of offers
        agents = [david.name, stacy.name]
        for i in range(2):
            for fruit in ['apple', 'pear']:
                atom = Action({
                    'subject': agents[i],
                    'verb': 'offer%s' % (fruit.capitalize()),
                    'object': agents[1 - i]
                })
                parties = [atom['subject'], atom['object']]
                for j in range(2):
                    # Set offer amount
                    offer = stateKey(parties[j], '%sOffered' % (fruit))
                    amount = actionKey('amount') if j == 1 else '%d-%s' % (
                        totals[fruit], actionKey('amount'))
                    tree = makeTree(setToConstantMatrix(offer, amount))
                    self.world.setDynamics(offer, atom, tree)

                    # atom = Action({'subject': child.name, 'verb': 'Play'})
                    # change = stateKey(child.name, 'TotalFun')
                    # tree = makeTree(approachMatrix(change, .2, minMax['max']))
                    # self.world.setDynamics(change, atom, tree)
                    #

                    # reset agree flags whenever an offer is made
                    agreeFlag = stateKey(parties[j], 'agree')
                    tree = makeTree(setFalseMatrix(agreeFlag))
                    self.world.setDynamics(agreeFlag, atom, tree)
                # Offers set offer flag in world state
                tree = makeTree(
                    setTrueMatrix(stateKey(None, '%sOffer' % (fruit))))
                self.world.setDynamics(stateKey(None, '%sOffer' % (fruit)),
                                       atom, tree)

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

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

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

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

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

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

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

        for action in stacy.actions | david.actions:
            tree = makeTree(incrementMatrix(stateKey(None, 'round'), 1))
            self.world.setDynamics(stateKey(None, 'round'), action, tree)
        for agent in self.world.agents.values():
            agent.addModel('pearLover', R={}, level=2, rationality=0.01)
            agent.addModel('appleLover', R={}, level=2, rationality=0.01)