示例#1
0
def reset():
    players = [agents.Agent(constants.PLAYER1_CHAR,
                            qfile=constants.FILE1),
               agents.Agent(constants.ENEMY1_CHAR,
                            qfile=constants.FILE2)]
    world1 = world.World(players, True)
    world1.save_qtable()
示例#2
0
def reset():
    players = [
        agents.Agent(constants.PLAYER1_CHAR),
        agents.Agent(constants.PLAYER2_CHAR),
        agents.Agent(constants.ENEMY1_CHAR),
        agents.Agent(constants.ENEMY2_CHAR),
        agents.Agent(constants.DRAGON_CHAR)
    ]
    world1 = world.World(players, True)
    world1.save_qtable()
示例#3
0
def HW2Agent() -> object:
    def program(percept):
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus, = program.oldPercepts[-1]
            if bump == 'None':
                action = 'Right'
            else:
                action = 'Left'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#4
0
def HW2Agent() -> object:
    def program(percept):
        bump, status = percept
        # lastAction = program.oldActions[-1]
        # lastBump, lastStatus = program.oldPercepts[-1]

        if status == 'Dirty':
            action = 'Suck'
        else:
            if not program.top:
                # print('Going Up')
                if bump == 'Bump':
                    program.top = True
                    action = 'Left'
                else:
                    action = 'Up'
            elif program.direction == 'Left':
                # print('Going Left')
                if bump == 'Bump':
                    if program.oldDirection == 'Right':
                        program.direction = 'Right'
                        program.oldDirection = 'Left'
                        action = 'Down'
                    elif program.oldDirection == 'Left':
                        program.direction = 'Right'
                        action = 'Right'
                else:
                    action = 'Left'
            elif program.direction == 'Right':
                # print('Going Right')
                if bump == 'Bump':
                    if program.oldDirection == 'Left':
                        program.direction = 'Left'
                        program.oldDirection = 'Right'
                        action = 'Down'
                    elif program.oldDirection == 'Right':
                        program.direction = 'Left'
                        action = 'Left'
                else:
                    action = 'Right'
            else:
                action = 'Right'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.top = False
    program.direction = 'Left'
    program.oldDirection = 'Left'
    program.step = 0

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#5
0
def test():
    for env in ENVIRONMENTS:
        agents = {}
        for alg, params in ALGORITHMS:
            reward = -1
            steps = 0
            while reward <= 0 and steps < (1 if STOP_AFTER_ONE else MAX_TESTS):
                steps += 1
                agent = a.Agent()
                agent.learn(env,
                            alg=alg,
                            numOfTrials=NUM_OF_TRIALS[env.name],
                            **params[env.name])
                test_id = (alg, env)
                utilities, reward = agent.solve(env, agent.getPolicy())
                agents[alg.func_name] = agent
            print env.name, alg.func_name, reward, steps

        for field, funcs in graphs_funcs.iteritems():
            for f, fun in enumerate(funcs):
                filename = "%s-%s-%d.png" % (
                    env.name,
                    field,
                    f,
                )
                title = "Scalability (%s) " % (field, )
                graph.plot_agents(agents,
                                  field,
                                  show=False,
                                  fname=filename,
                                  title=title)
示例#6
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    oldPercepts = [('None', 'Clean')]
    oldActions = ['NoOp']

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        directions = ['Up', 'Left', 'Down', 'Right']
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus = oldPercepts[-1]
            olderBump, olderStatus = oldPercepts[-2]

            if lastBump == 'None' and bump == 'none':
                action = directions[0]
            else:
                action = directions[2]
            if lastBump == 'Bump' and bump == 'Bump':
                action = directions[1]
            if lastBump == 'Bump' and bump == 'Bump' and olderBump == 'Bump':
                action = directions[3]

        oldPercepts.append(percept)
        oldActions.append(action)
        return action

    return ag.Agent(program)
示例#7
0
def test():

    for alg in ALGORITHMS:
        for env in ENVIRONMENTS:
            for maxIter in MAX_ITERATIONS:
                rewards = []
                for tst in range(NUM_OF_TESTS):
                    agent = a.Agent()
                    agent.learn(env,
                                alg=alg,
                                numOfTrials=NUM_OF_TRIALS,
                                maxItr=maxIter)
                    test_id = (
                        alg.func_name,
                        env.name,
                        maxIter,
                        NUM_OF_TRIALS,
                        tst,
                    )

                    result = agent.solve(env, agent.getPolicy())
                    rewards.append(1 if result[1] > 0 else 0)

                success[test_id] = float(sum(rewards)) / NUM_OF_TESTS
                results[test_id] = result

    from pprint import pprint
    pprint(success)
示例#8
0
def HW2Agent() -> object:
    def program(percept):
        lastAction = program.oldActions[-1]
        lastBump, lastStatus, = program.oldPercepts[-1]
        bump, status = percept
        if lastAction == 'Up' and bump == 'None':
            program.rowCleaned = program.rowCleaned + 1
        if status == 'Dirty':
            action = 'Suck'
        elif program.rowCleaned > 0 and program.goDown == True:
            action = 'Down'
            program.rowCleaned = program.rowCleaned - 1
        elif lastAction == 'Right' and bump == 'Bump' and program.goLeft == True:
            action = 'Left'
            program.goRight = False
        elif lastAction == 'Left' and bump == 'None':
            action = 'Left'
        elif lastAction == 'Left' and bump == 'Bump' and program.goDown == False:
            action = 'Up'
            program.goRight = True
            program.goLeft = False
        elif lastAction == 'Right' and bump == 'Bump' and program.goDown == False:
            action = 'Up'
            program.goRight = False
            program.goLeft = True
        elif lastAction == 'Left' and bump == 'Bump':
            action = 'Down'
            program.goRight = True
            program.goLeft = False
        elif lastAction == 'Right' and bump == 'Bump':
            action = 'Down'
            program.goRight = False
            program.goLeft = True
        elif bump == 'Bump':
            action = 'Down'
            program.goDown = True
        else:
            lastBump, lastStatus, = program.oldPercepts[-1]
            if bump == 'None' and program.goRight == True:
                action = 'Right'
            else:
                action = 'Left'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.rowCleaned = 0
    program.goDown = False
    program.goLeft = True
    program.goRight = True

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#9
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    oldPercepts = [('None', 'Clean')]
    #the first action is a lie!!!
    oldActions = ['Left']

    #while (score < (90 and (date <= datetime.strptime('Sep 7 2016  6:00AM', '%b %d %Y %I:%M%p')))):

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        elif bump == 'None':
            if oldActions[(oldActions.__len__() - 1)] == 'Suck':
                if oldActions[(oldActions.__len__() - 2)] != 'Up':
                    action = oldActions[(oldActions.__len__() - 2)]
                else:
                    if oldActions[(oldActions.__len__() - 3)] == 'Right':
                        action = 'Left'
                    else:
                        action = 'Right'

            elif oldActions[(oldActions.__len__() -
                             1)] == 'Up' and oldActions[(oldActions.__len__() -
                                                         2)] == 'Right':
                action = 'Left'
            elif oldActions[(oldActions.__len__() -
                             1)] == 'Up' and oldActions[(oldActions.__len__() -
                                                         2)] == 'Left':
                action = 'Right'
            else:
                action = oldActions[(oldActions.__len__() - 1)]
        elif bump == 'Bump':
            if oldActions[(oldActions.__len__() - 1)] == 'Right':
                action = 'Up'
            if oldActions[(oldActions.__len__() - 1)] == 'Up' and oldActions[(
                    oldActions.__len__() - 2)] == 'Left':
                action = 'Right'
            elif oldActions[(oldActions.__len__() -
                             1)] == 'Up' and oldActions[(oldActions.__len__() -
                                                         2)] == 'Right':
                action = 'Left'
            elif oldActions[(oldActions.__len__() - 1)] == 'Up':
                action = 'Right'
            if oldActions[(oldActions.__len__() - 1)] == 'Down':
                action = 'Right'
            if oldActions[(oldActions.__len__() - 1)] == 'Left':
                action = 'Down'
                for x in range(0, oldActions.__len__()):
                    if oldActions[x] == 'Down':
                        action = 'Up'
                        break

        oldPercepts.append(percept)
        oldActions.append(action)
        return action

    return ag.Agent(program)
示例#10
0
def HW2Agent() -> object:
    def program(percept):
        bump, status = percept
        lastBump, lastStatus, = program.oldPercepts[-1]

        if program.oldActions[-1] == 'Right' and lastBump == 'Bump':
            program.rightBumped = True
        if program.oldActions[-1] == 'Left' and lastBump == 'Bump':
            program.leftBumped = True
        if program.oldActions[-1] == 'Down' and lastBump == 'Bump':
            program.downBumped = True
        if program.oldActions[-1] == 'Up' and lastBump == 'Bump':
            program.upBumped = True

        if status == 'Dirty':
            action = 'Suck'
        else:
            if (bump == 'None') and (program.leftBumped == False):
                action = 'Left'
            elif (bump == 'Bump') and (program.oldActions[-1:] == 'Left'):
                program.leftBumped = True
            elif (bump == 'Bump') and (program.oldActions[-1:] == 'Right'):
                program.rightBumped = True
            elif (bump == 'None') and (program.rightBumped == False):
                action = 'Right'
            elif program.rightBumped and program.leftBumped:
                action = 'Down'
            elif (bump == 'Bump') and (program.oldActions[-1:] == 'Down'):
                program.downBumped = True
            elif program.rightBumped and program.leftBumped and program.DownBumped:
                action = 'Up'
            else:
                action = 'Right'

            #(program.oldActions[-1] == 'Left') and (status == 'Bumped')

            #if bump == 'None':
            #    action = 'Left'
            #else:
            #    action = 'Right'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.leftBumped = False
    program.rightBumped = False
    program.upBumped = False
    program.downBumped = False

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#11
0
def HW2Agent() -> object:

    # This agent will snake through the room to clean it.
    # It starts by heading for the top of the room. Then it snakes down
    # Once it reaches the bottom corner, it will go to the top as fast as possible and snake again.
    def program(percept):
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus, = program.oldPercepts[-1]
            lastAction = program.oldActions[-1]
            upOrDown, leftOrRight = program.snakeDirection
            if lastAction == 'NoOp':
                action = upOrDown
            elif lastStatus == 'Dirty':
                action = leftOrRight
            elif bump == 'None':
                if lastAction == 'Up':
                    # Go all the way up to the top of the room
                    action = 'Up'
                    program.snakeDirection = ('Up', leftOrRight)
                elif lastAction == 'Down':
                    action = 'Left' if leftOrRight == 'Right' else 'Right'
                    program.snakeDirection = (upOrDown, action)
                else:
                    action = lastAction
                    program.snakeDirection = (upOrDown, leftOrRight)
            else:
                if lastBump:
                    # we've bumped multiple times, so we're in a corner
                    if lastAction == 'Down':
                        action = 'Up'
                        program.snakeDirection = (action, leftOrRight)
                    elif lastAction == 'Up':
                        action = 'Left' if leftOrRight == 'Right' else 'Right'
                        program.snakeDirection = ('Down', action)
                    else:
                        action = upOrDown
                        program.snakeDirection = (action, leftOrRight)
                else:
                    action = upOrDown
                    program.snakeDirection = (action, leftOrRight)
        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.snakeDirection = ('Up', 'Right')

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')
    return agt
示例#12
0
    def add_cluster(self, happiness=50, movement=movements.idle, game=None):

        for i in range(len(self.starting_positions)):
            self.members.append(
                agents.Agent(self.starting_positions[i][0],
                             self.starting_positions[i][1],
                             happiness,
                             cluster=self,
                             movement=movement,
                             attitude=self.attitude))
def generate_agents():
    result = []
    control_n = parameters.my_n.get_n()
    for index in range(len(parameters.my_n.get_cod())):
        for sub_n in range(parameters.my_n.get_pop_mun()[index]):
            result.append(
                agents.Agent(control_n,
                             parameters.my_n.get_cod()[index]))
            control_n -= 1
    return result
示例#14
0
def HW2Agent() -> object:

    def program(percept):
        bump, status = percept
        lastBump, lastStatus, lastAction = program.oldPercepts[-1], program.oldPercepts[-1], program.oldActions[-1]

        if status == 'Dirty':
            if lastAction == 'Left':
                program.d = 0
                action = 'Suck'
            elif lastAction == 'Right':
                program.d = 1
                action = 'Suck'
            elif lastAction == 'Up':
                program.d = 2
                action = 'Suck'
            elif lastAction == 'Down':
                program.d = 3
                action = 'Suck'
            else:
                action = 'Suck'
        else:

            if bump == 'None':
                if lastAction == 'Left':
                    action = 'Left'
                elif lastAction == 'Down':
                    action = 'Down'
                elif lastAction == 'Up':
                    action = 'Up'
                else:
                    action = program.dir[program.d]

            else:
                if lastAction == 'Down':
                    action = 'Up'
                elif lastAction == 'Right':
                    action = 'Left'
                elif lastAction == 'Up':
                    action = 'Down'
                else:
                    action = 'Down'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.dir = ['Left', 'Right', 'Up', 'Down']
    program.d = 1

    agt = ag.Agent(program)

    return agt
示例#15
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus = program.oldPercepts[-1]

            if bump == 'None':
                action = program.act
            else:
                if program.act == 'Up':
                    program.act = 'Right'
                    action = program.act
                    if program.counter >= 4:
                        action = "Down"
                    program.counter = program.counter + 1
                else:
                    if program.act == 'Right':
                        program.act = 'Down'
                        action = program.act
                        if program.counter >= 4:
                            action = "Left"
                        program.counter = program.counter + 1
                    else:
                        if program.act == 'Down':
                            program.act = 'Left'
                            action = program.act
                            if program.counter >= 4:
                                action = "Up"
                            program.counter = program.counter + 1
                        else:
                            if program.act == 'Left':
                                program.act = 'Up'
                                action = program.act
                                if program.counter >= 4:
                                    action = "Right"
                                program.counter = program.counter + 1
            # program.top = program.top + 1

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.counter = 0
    program.tall = 0
    program.wide = 0
    program.act = 'Up'

    return ag.Agent(program)
示例#16
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    oldPercepts = [('None', 'Clean', 'Right', 'Left', 'Up', 'Down')]
    oldActions = ['NoOp']

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        wall, status = percept
        lastLoc = oldPercepts[-1]
        lastAction = oldActions[-1]
        # checks to see if the starting point is dirty
        if status == 'Dirty':
            action = 'Suck'
        else:

            if lastLoc == 'Up':
                if wall == 'Bump':  # checks to see if it hits the top
                    action = 'Down'
                    action = 'Left'
# lastAction = 'Bump'
                else:
                    action = 'Up'
                    # moves for the vacuum at the latest location of right
            elif lastLoc == 'Right':

                lastAction = oldActions[-1]
                if lastAction == 'Left':
                    action = 'Right'
                elif wall == 'Bump':
                    lastLoc = 'Left'
                    action = 'Down'
                else:
                    action = 'Right'

                    # moves for the vacuum at the latest location of left
            elif lastLoc == 'Left':

                lastAction = oldActions[-1]
                if lastAction == 'Up' or lastAction == 'Down':
                    action = 'Left'
                elif wall == 'Bump':
                    lastLoc = 'Right'
                    action = 'Right'

                else:
                    action = 'Left'
            else:
                action = 'Right'

        oldPercepts.append([wall, status, lastLoc])
        oldActions.append(action)

        return oldActions

    return ag.Agent(program)
示例#17
0
def ReflexVacuumAgent():
    "A reflex agent for the two-state vacuum environment. [Figure 2.8]"
    def program(percept):
        location, status = percept
        if status == 'Dirty':
            return 'Suck'
        elif location == loc_A:
            return 'Right'
        elif location == loc_B:
            return 'Left'
    return ag.Agent(program)
示例#18
0
def HW2Agent() -> object:
    # oldPercepts = [('None', 'Clean','noBott')]
    # oldActions = ['NoOp']
    def program(percept):
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus, bottom = program.oldPercepts[-1]
            lastAction = program.oldActions[-1]

            if bprogram.ottom == 'down':
                if bump == 'None':
                    action = 'Down'
                else:
                    program.bottom = 'right'
                    action = 'Right'
            if bottom == 'right':
                if bump == 'Bump':
                    action = 'Up'
                    bottom = 'up'
                else:
                    action = 'Left'
            # elif bump == 'Bump':
            #     action = 'Up'
            # else:
            #     action = 'Right'

            #elif lastAction == 'Left' and bump == 'Bump':
            #     leftEdge = 'leftEdge'
            #     action = 'Up'
            # elif leftEdge == 'leftEdge' and lastAction == 'Left' and ceil == 'noCeil':
            #     action = 'Up'
            # elif lastAction == 'Up' and bump == 'Bump':
            #     action = 'Right'
            #     ceil = 'ceil'
            #     leftEdge = 'noLeftEdge'
            # elif lastAction == 'Right' and bump == 'Bump':
            #     rightEdge = 'rightEdge'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.bottom = 'noBott'

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#19
0
def HW2Agent() -> object:
    def program(percept):
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus, = program.oldPercepts[-1]
            lastAction = program.oldActions[-1]
            vertical, horiz = program.direction

            if lastAction == 'NoOp':
                action = vertical
            elif lastStatus == 'Dirty':
                action = horiz
            elif bump == 'None':
                if lastAction == 'Up':
                    action = 'Up'
                    program.direction = ('Up', horiz)
                elif lastAction == 'Down':
                    action = 'Left' if horiz == 'Right' else 'Right'
                    program.direction = (vertical, action)
                else:
                    action = lastAction
                    program.direction = (vertical, horiz)
            else:
                if lastBump:
                    #we hit a wall both vertically and horizontally
                    if lastAction == 'Down':
                        action = 'Up'
                        program.direction = (action, horiz)
                    elif lastAction == 'Up':
                        action = 'Left' if horiz == 'Right' else 'Right'
                        program.direction = ('Down', action)
                    else:
                        action = vertical
                        program.direction = (action, horiz)
                else:
                    action = vertical
                    program.direction = (action, horiz)

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.direction = ('Up', 'Right')

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#20
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    oldPercepts = [('None', 'Clean', 'topNotFound', 'moveLeft')]
    oldActions = ['NoOp']

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        # Step 1: Move to top
        # Step 2: Move to left
        # Step 3: Move to Right
        # Step 4: Move Down one
        # Step 5: Repeat Steps 2-4 until complete
        bump, status = percept
        lastBump, lastStatus, topFound, moveDirection = oldPercepts[-1]
        lastAction = oldActions[-1]
        if status == 'Dirty':
            action = 'Suck'
        else:
            print(oldPercepts[-1])
            if topFound == 'topNotFound':
                if bump == 'Bump':
                    action = 'Left'
                    topFound = 'topFound'
                else:
                    action = 'Up'
            elif moveDirection == 'moveRight':
                print('inside the move right')
                lastAction2 = oldActions[-2]
                if lastAction2 == 'Left':
                    action = 'Right'
                elif bump == 'Bump':
                    moveDirection = 'moveLeft'
                    action = 'Down'
                else:
                    action = 'Right'
            elif moveDirection == 'moveLeft':
                print('inside the move left')
                lastAction2 = oldActions[-2]
                if lastAction2 == 'Up' or lastAction == 'Down':
                    action = 'Left'
                elif bump == 'Bump':
                    moveDirection = 'moveRight'
                    action = 'Right'
                else:
                    action = 'Left'
            else:
                action = 'Right'
        oldPercepts.append([bump, status, topFound, moveDirection])
        oldActions.append(action)
        # print(lastBump, lastStatus, topFound, widthFound, moveDirection)
        return action

    return ag.Agent(program)
示例#21
0
def HW2Agent() -> object:
    def program(percept):
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus, = program.oldPercepts[-1]
            lastAction = program.oldActions[-1]
            if (len(program.oldActions) > 1):
                twoActionsAgo = program.oldActions[-2]
            if lastAction == 'Suck':
                lastAction = program.oldActions[-2]
                if (len(program.oldActions) > 2):
                    twoActionsAgo = program.oldActions[-3]
            if bump == 'None':
                if (lastAction == 'Left' or lastAction == 'Right'):
                    action = lastAction
                elif lastAction == 'Up':
                    if twoActionsAgo == 'Right':
                        action = 'Left'
                    else:
                        action = 'Right'
                elif lastAction == 'Down':
                    action = 'Down'
                else:
                    action = 'Right'
            elif lastAction == 'Up':
                if twoActionsAgo == 'Left':
                    program.directionAfterDownbump = 'Right'
                elif twoActionsAgo == 'Right':
                    program.directionAfterDownBump = 'Left'
                action = 'Down'
            elif lastAction == 'Down':
                action = program.directionAfterDownBump
            elif lastAction == 'Right' and twoActionsAgo == 'Down':
                action = 'Left'
            else:
                action = 'Up'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']
    program.directionAfterDownBump = 'Right'

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#22
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    oldPercepts = [('None', 'Clean')]
    oldActions = ['NoOp']

    actionScores = [{
        'Right': 0,
        'Left': 0,
        'Up': -1,
        'Down': -1,
        'NoOp': -100,
    }]
    level = 0

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        level = len(actionScores) - 1
        bump, status = percept
        lastBump, lastStatus = oldPercepts[-1]
        lastAction = oldActions[-1]

        if status == 'Dirty':
            action = 'Suck'
            actionScores[level][lastAction] += 2

        else:
            if bump == 'Bump':
                actionScores[level][lastAction] -= 10
            else:
                if lastAction == 'Up' or lastAction == 'Down':
                    actionScores.append({
                        'Right': 0,
                        'Left': 0,
                        'Up': -1,
                        'Down': -1,
                    })

            highest = -80
            for actionType, score in actionScores[level].items():
                if score > highest:
                    highest = score
                    action = actionType


        print(actionScores)

        oldPercepts.append(percept)
        oldActions.append(action)
        return action

    return ag.Agent(program)
示例#23
0
def HW2Agent() -> object:
    oldPercepts = [('None', 'Clean')]
    oldActions = ['NoOp']

    def program(percept):
        wall, status = percept
        myAction = oldActions[-1]
        if myAction == 'NoOp':
            program.do = 'Left'
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus = oldPercepts[-1]
            if wall != 'Bump':
                #Effectively, if I'm at the start, go to the Right if possible....
                if myAction == 'Suck':
                    if oldActions[-2] == 'NoOp':
                        action = 'Right'
                    else:
                        action = oldActions[-2]
                #...if not, go to the Left
                else:
                    if oldActions == 'NoOp':
                        action = 'Left'
                    else:
                        action = program.do

            elif wall == 'Bump':
                #If I hit the wall coming from the right, go to the Left
                if myAction == 'Right':
                    program.do = 'Left'
                    action = 'Left'
                #If I hit the wall coming from the left, go to the Right
                elif myAction == 'Left':
                    program.do = 'Right'
                    action = 'Right'
                #If I hit the wall coming Up, go Down
                elif myAction == 'Up':
                    program.do = 'Down'
                    action = 'Down'
                #If I hit the wall coming Down, go Up
                elif myAction == 'Down':
                    program.do = 'Up'
                    action = 'Up'

        oldPercepts.append(percept)
        oldActions.append(action)
        return action

    return ag.Agent(program)
示例#24
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    oldPercepts = [('None', 'Clean')]
    oldActions = ['NoOp']

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        bump, status = percept
        curAct = oldActions[-1]
        #program.do = curAct
        if curAct == 'NoOp':
            program.do = 'Left'
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus = oldPercepts[-1]
            if bump == 'None':
                if curAct == 'Suck':
                    if oldActions[-2] == 'NoOp':
                        action = 'Left'
                    else:
                        action = oldActions[-2]
                else:
                    if oldActions == 'NoOp':
                        action = 'Right'
                    else:
                        action = program.do
            else:
                if bump == 'Bump':
                    if curAct == 'Right':
                        program.do = 'Left'
                        action = 'Up'
                    else:
                        if curAct == 'Down':
                            program.do = 'Right'
                            action = 'Right'
                        else:
                            if curAct == 'Up':
                                program.do = 'Down'
                                action = 'Down'
                            else:
                                if curAct == 'Left':
                                    program.do = 'Right'
                                    action = 'Up'

        oldPercepts.append(percept)
        oldActions.append(action)
        return action

    return ag.Agent(program)
示例#25
0
def HW2Agent() -> object:
    def program(percept):
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus, = program.oldPercepts[-1]
            lastAction = program.oldActions[-1]
            if bump == 'None':
                action = 'Left'

            if bump != 'None':
                action = 'Right'

            if bump != 'None' and lastAction == 'Left':
                action = 'Right'

            if bump != 'None' and lastAction == 'Right':
                action = 'Down'

            if bump != 'None' and lastAction == 'Down':
                action = 'Up'

            if bump == 'None' and lastAction == 'Down':
                action = 'Down'

            if bump == 'None' and lastAction == 'Right':
                action = 'Right'

            if bump == 'None' and lastAction == 'Left':
                action = 'Right'

            if bump != 'None' and lastAction == 'Left':
                action = 'Up'
#it says local variable might be referenced before assingment?

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']

    agt = ag.Agent(program)
    # assign class attributes here:
    # agt.direction = ag.Direction('left')

    return agt
示例#26
0
def TableDrivenVacuumAgent():
    "[Figure 2.3]"
    table = {((loc_A, 'Clean'),): 'Right',
             ((loc_A, 'Dirty'),): 'Suck',
             ((loc_B, 'Clean'),): 'Left',
             ((loc_B, 'Dirty'),): 'Suck',
             ((loc_A, 'Clean'), (loc_A, 'Clean')): 'Right',
             ((loc_A, 'Clean'), (loc_A, 'Dirty')): 'Suck',
             # ...
             ((loc_A, 'Clean'), (loc_A, 'Clean'), (loc_A, 'Clean')): 'Right',
             ((loc_A, 'Clean'), (loc_A, 'Clean'), (loc_A, 'Dirty')): 'Suck',
             # ...
             }
    p = ag.TableDrivenAgentProgram(table)
    return ag.Agent()
示例#27
0
def ModelBasedVacuumAgent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    model = {loc_A: None, loc_B: None}

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        location, status = percept
        model[location] = status  # Update the model here
        if model[loc_A] == model[loc_B] == 'Clean':
            return 'NoOp'
        elif status == 'Dirty':
            return 'Suck'
        elif location == loc_A:
            return 'Right'
        elif location == loc_B:
            return 'Left'
    return ag.Agent(program)
示例#28
0
def HW2Agent() -> object:
    "An agent that keeps track of what locations are clean or dirty."
    oldPercepts = [('None', 'Clean')]
    oldActions = ['NoOp']

    # oldLocation = [location]

    def program(percept):
        "Same as ReflexVacuumAgent, except if everything is clean, do NoOp."
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus = oldPercepts[-1]
            lastAction = oldActions[-1]

            if lastAction != 'Suck':

                if lastAction == 'Left' and bump == 'Bump' and status == 'Clean':
                    action = 'Right'
                elif lastAction == 'Right' and bump == 'Bump' and status == 'Clean':
                    action = 'Up'
                elif lastAction == 'Up' and bump == 'Bump' and status == 'Clean':
                    action = 'Down'
                elif bump == 'None' and status == 'Clean' and lastStatus == 'Clean':
                    action = 'Right'
                else:
                    action = 'Left'

            else:
                if bump == 'Bump' and status == 'Clean':
                    action = 'Right'
                elif bump == 'Bump' and status == 'Clean':
                    action = 'Up'
                elif bump == 'Bump' and status == 'Clean':
                    action = 'Down'
                elif bump == 'None' and status == 'Clean' and lastStatus == 'Clean':
                    action = 'Right'
                else:
                    action = 'Left'

            oldPercepts.append(percept)
            oldActions.append(action)
        return action

    return ag.Agent(program)
示例#29
0
def HW2Agent() -> object:
    def program(percept):
        bump, status = percept
        if status == 'Dirty':
            action = 'Suck'
        else:
            lastBump, lastStatus, = program.oldPercepts[-1]
            if bump == 'None':
                if program.bumpcount == 0:
                    action = 'Right'
                elif program.bumpcount == 1:
                    action = 'Left'
                elif program.bumpcount == 2:
                    action = 'Down'
                else:
                    action = 'Up'
            else:
                if bump == 'Bump' and program.oldActions[-1] == 'Right':
                    program.bumpcount += 1
                    action = 'Up'
                elif bump == 'Bump' and program.oldActions[-1] == 'Up':
                    program.bumpcount = 2
                    action = 'Down'
                elif bump == 'Bump' and program.oldActions[-1] == 'Left':
                    program.bumpcount -= 1
                    action = 'Up'
                else:
                    program.bumpcount -= 1
                    action = 'Left'

        program.oldPercepts.append(percept)
        program.oldActions.append(action)
        return action

    # assign static variables here
    program.bumpcount = 0
    program.oldPercepts = [('None', 'Clean')]
    program.oldActions = ['NoOp']

    agt = ag.Agent(program)
    # assign class attributes here:

    return agt
示例#30
0
def main():
    env = gym.make('gym_mmab:mmab-v0', n_players=3, n_arms=5)
    # env = gym.make('gym_mmab:mmab-v0') # If you leave arguments empty, default: n_players=3, n_arms=10


    agent0_candidates = [agents.Agent(0, env.n_arms), agents.RandomAgent(0, env.n_arms), agents.QAgent(0, env.n_arms)]
    saved_obs_history = []
    for agent0 in agent0_candidates:
        print("====================")
        print(f"Testing with {type(agent0).__name__}")
        print("====================")
        obs_history = test_selectedagent(env, agent0)
        saved_obs_history.append(obs_history)
        print()

    print("====================")
    print("Comparing cumulative rewards in last 100 rounds")
    for n, agent0 in enumerate(agent0_candidates):
        tot_rewards = np.array(saved_obs_history[n][-100:]).sum(axis=0)[0]
        print(f"{type(agent0).__name__:>14}: {tot_rewards}")
    print()