def login(accountListFile, transactionSummary):
    try:
        choice = input("Welcome to the front end: \n")
        if choice == 'login':                                                               # use correctly input login
            print("Successfully login.")     
            status = False                                                                  # check the validation of input
            while status == False:                                                          # loop if user input invalid command 
                mode = input("Select mode to enter: \n")                                      # select mode
                if mode == "atm":                                                           # user correctly input atm
                    print("Successfully entered ATM mode.")
                    newAtm = ATM(accountListFile, transactionSummary)                                           # create new atm object
                    status = True                                              
                elif mode == "agent":                                                       # user correctly input agent
                    print("Successfully entered agent mode.")
                    newAgent = AGENT(accountListFile, transactionSummary)                                       # create new agent object
                    status = True
                elif mode == "logout":
                    status = True
                    f = open(transactionSummary, "w")
                    f.writelines("EOS")
                    f.close()
                else:                                                                       # error for anything else
                    print("Error:Invalid mode choice, please input a valid mode choice!")
            print("Successfully logout.")
            login(accountListFile, transactionSummary)
        else:                                                                               # invalid input
            print("Error:Please login first!")
            login(accountListFile, transactionSummary)
    except:
        quit                                                                            # exit program
def runAgent():
    exp = AGENT(envSize,
                nRobot,
                nHidden,
                lR,
                maxIter=maxIter,
                rewardType=rewType)
    exp.reinforce(nEpisode, gamma, returnDF=False)
    return exp
Beispiel #3
0
    def __init__(self, args, Ite):

        self.args = args
        self.Ite = Ite

        #Dim of state and action
        self.num_states = 3
        self.num_actions = 1

        #Initialize Agent
        self.agent = AGENT(self.num_states, self.num_actions, self.args)
Beispiel #4
0
    def __init__(self, args, Ite):

        self.args = args
        self.Ite = Ite

        #Dim of state and action
        self.num_states = 3  #x=[sin\theta cos\theta \omega]
        self.num_actions = 1  #a=[a]

        #Initialize Agent
        self.agent = AGENT(self.num_states, self.num_actions, self.args)
Beispiel #5
0
def runGame():
    # setup variables for the start of the game
    board = getBlankBoard()
    lastMoveDownTime = time.time()
    lastMoveSidewaysTime = time.time()
    lastFallTime = time.time()
    movingDown = False  # note: there is no movingUp variable
    movingLeft = False
    movingRight = False
    score = 0
    reward = 0
    level, fallFreq = calculateLevelAndFallFreq(score)

    # make agent for RL
    agent = AGENT()

    fallingPiece = getNewPiece()
    nextPiece = getNewPiece()

    while True:  # game loop
        if fallingPiece == None:
            # No falling piece in play, so start a new piece at the top
            fallingPiece = nextPiece
            nextPiece = getNewPiece()
            lastFallTime = time.time()  # reset lastFallTime

            if not isValidPosition(board, fallingPiece):
                return  # can't fit a new piece on the board, so game over

        checkForQuit()

        # copy the board for agent
        board_copy = deepcopy(board)
        board_copy = addToBoard(board_copy, fallingPiece)
        action = agent.getAction(board_copy)

        # event handling loop
        if action == "KEYUP":
            if (action == K_p):
                # Pausing the game
                DISPLAYSURF.fill(BGCOLOR)
                pygame.mixer.music.stop()
                showTextScreen('Paused')  # pause until a key press
                pygame.mixer.music.play(-1, 0.0)
                lastFallTime = time.time()
                lastMoveDownTime = time.time()
                lastMoveSidewaysTime = time.time()
            elif (action == "K_LEFT"):
                movingLeft = False
            elif (action == "K_RIGHT"):
                movingRight = False
            elif (action == "K_DOWN"):
                movingDown = False

        else:
            # moving the piece sideways
            if (action == "K_LEFT") and isValidPosition(
                    board, fallingPiece, adjX=-1):
                fallingPiece['x'] -= 1
                movingLeft = True
                movingRight = False
                lastMoveSidewaysTime = time.time()

            elif (action == "K_RIGHT") and isValidPosition(
                    board, fallingPiece, adjX=1):
                fallingPiece['x'] += 1
                movingRight = True
                movingLeft = False
                lastMoveSidewaysTime = time.time()

                # rotating the piece (if there is room to rotate)
            elif (action == "K_UP"):
                fallingPiece['rotation'] = (fallingPiece['rotation'] +
                                            1) % len(
                                                PIECES[fallingPiece['shape']])
                if not isValidPosition(board, fallingPiece):
                    fallingPiece['rotation'] = (
                        fallingPiece['rotation'] - 1) % len(
                            PIECES[fallingPiece['shape']])
            elif (action == K_q):  # rotate the other direction
                fallingPiece['rotation'] = (fallingPiece['rotation'] -
                                            1) % len(
                                                PIECES[fallingPiece['shape']])
                if not isValidPosition(board, fallingPiece):
                    fallingPiece['rotation'] = (
                        fallingPiece['rotation'] + 1) % len(
                            PIECES[fallingPiece['shape']])

                # making the piece fall faster with the down key
            elif (action == "K_DOWN"):
                movingDown = True
                if isValidPosition(board, fallingPiece, adjY=1):
                    fallingPiece['y'] += 1
                lastMoveDownTime = time.time()

                # move the current piece all the way down
            elif action == "K_SPACE":
                movingDown = False
                movingLeft = False
                movingRight = False
                for i in range(1, BOARDHEIGHT):
                    if not isValidPosition(board, fallingPiece, adjY=i):
                        break
                fallingPiece['y'] += i - 1

        # handle moving the piece because of user input
        if (movingLeft or movingRight
            ) and time.time() - lastMoveSidewaysTime > MOVESIDEWAYSFREQ:
            if movingLeft and isValidPosition(board, fallingPiece, adjX=-1):
                fallingPiece['x'] -= 1
            elif movingRight and isValidPosition(board, fallingPiece, adjX=1):
                fallingPiece['x'] += 1
            lastMoveSidewaysTime = time.time()

        if movingDown and time.time(
        ) - lastMoveDownTime > MOVEDOWNFREQ and isValidPosition(
                board, fallingPiece, adjY=1):
            fallingPiece['y'] += 1
            lastMoveDownTime = time.time()

        # let the piece fall if it is time to fall
        if time.time() - lastFallTime > fallFreq:
            # see if the piece has landed
            if not isValidPosition(board, fallingPiece, adjY=1):
                # falling piece has landed, set it on the board
                addToBoard(board, fallingPiece)
                reward = removeCompleteLines(board)
                score += reward
                level, fallFreq = calculateLevelAndFallFreq(score)
                fallingPiece = None
            else:
                # piece did not land, just move the piece down
                fallingPiece['y'] += 1
                lastFallTime = time.time()

        board_copy = deepcopy(board)
        if fallingPiece != None:
            board_copy = addToBoard(board_copy, fallingPiece)

        agent.giveData(board_copy, reward)

        # drawing everything on the screen
        DISPLAYSURF.fill(BGCOLOR)
        drawBoard(board)
        drawStatus(score, level)
        drawNextPiece(nextPiece)
        if fallingPiece != None:
            drawPiece(fallingPiece)

        pygame.display.update()
        FPSCLOCK.tick(FPS)
from visualize_test import GraphicDisplay
from environment import grid_world
from agent import AGENT


WORLD_HEIGHT = 5
WORLD_WIDTH = 10

env = grid_world(WORLD_HEIGHT,WORLD_WIDTH,
                 GOAL = [[WORLD_HEIGHT-1, WORLD_WIDTH-1]],
                 OBSTACLES=[[0,2], [1,2], [2,2], [2,4], [3,4], [2, 6],[3, 6],[4, 6]])

agent = AGENT(env,is_upload=True)
grid_world_vis = GraphicDisplay(env, agent)
grid_world_vis.print_value_table()
grid_world_vis.mainloop()


from environment import grid_world
from agent import AGENT

WORLD_HEIGHT = 5
WORLD_WIDTH = 10

env = grid_world(WORLD_HEIGHT,
                 WORLD_WIDTH,
                 GOAL=[[WORLD_HEIGHT - 1, WORLD_WIDTH - 1]],
                 OBSTACLES=[[0, 2], [1, 2], [2, 2], [0, 4], [2, 4], [4, 4],
                            [2, 6], [3, 6], [4, 6], [2, 7], [2, 8]])

agent = AGENT(env, is_upload=False)
agent.Q_learning(epsilon=0.4, decay_period=10000, decay_rate=0.8)