Beispiel #1
0
def main():

    nd_list=[]
    dir_List = []
    maze = mz.Maze("test_dis.csv")
    next_nd = maze.getStartPoint()
    car_dir = Direction.SOUTH
    point = score.Scoreboard("UID_405.csv")
    #interface = student.interface()         the part of calling student.py was commented out.

    
    ############################connect#########################################################

    
    
   ############################Game1###############################################
    if(sys.argv[1] == '0'):
        nd_list = maze.BFS(1.0)
        print(nd_list)
        breakcheck_ = 0
        check_read = 1
        while(1):
            for i in range(len(maze.dd_List)):
                if(maze.dd_List[i]!=0):
                    nd_list_tmp = maze.BFS(nd_list[(len(nd_list)-1)])
                    for j in range(len(nd_list_tmp)):
                        nd_list.append(nd_list_tmp[j])
                    break 
                if(i==len(maze.dd_List)-1):
                    breakcheck_=1
            if(breakcheck_): 
                break

        dir_List.append('U')
        for i in range(len(nd_list)-1):
            if(i==0):
                dir_last = 2
                dir_next = maze.nodes[(int)(nd_list[0])-1].getDirection(maze.nodes[(int)(nd_list[1])-1])
            else:
                dir_sum = maze.getdir(nd_list[i-1],nd_list[i],nd_list[i+1])
                dir_last = (int)(((int)(dir_sum)) / 10)
                dir_next = ((int)(dir_sum)) % 10
            direction = maze.dircheck(dir_last,dir_next)
            #if((direction == 'R') or (direction == 'L')):
                #dir_List.append('S')
            if(direction!='S'):
                dir_List.append(direction)
        dir_List.append('D')
        print(dir_List)
    

        bt = BT.bluetooth()
        bt.do_connect('COM3')
        _quit = False
        count = 0
        bt.SerialWrite('U')
        #for i in range (100):
            #bt.SerialWrite('U')
            #sleep(0.05)
        #print('U')
        bt.SerialReadString()

        while _quit is False:
            #readstring = bt.SerialReadString()
            readstring = bt.SerialReadString()
            print(readstring)
            if ('N\n' in readstring):
                print("i read it")
                count = count+1
                if(count<len(dir_List)):
                    cmd = dir_List[count]
                    bt.SerialWrite(cmd)
                    print(cmd)
                    if cmd != 'D':
                        sleep(1.2)
                    else:
                        sleep(0.2)
                else:
                    bt.SerialWrite('S')
                bt.SerialReadString()
            elif(readstring == ''):
                continue
            elif('R\n' in readstring):
                sleep(0.2)
                word = bt.SerialReadString()
                UID = ''
                for i in range(8):
                    UID+=word[i]
                #UID = str(UID)
                print(UID)
                point.add_UID(UID)
             
            if count == len(dir_List):
                _quit = True
        bt.SerialWrite('S')
        print(bt.ser.isOpen())
        bt.disconnect()

            #TODO: Impliment your algorithm here and return the UID for evaluation function
            # ================================================
            # Basically, you will get a list of nodes and corresponding UID strings after the end of algorithm.
			# The function add_UID() would convert the UID string score and add it to the total score.
			# In the sample code, we call this function after getting the returned list. 
            # You may place it to other places, just make sure that all the UID strings you get would be converted.
            # ================================================

        #for i in range(1, len(ndList)):
         #   node = 0
          #  get_UID = "just a test"
           # point.add_UID(get_UID)

############Game2###################################################
    
    elif(sys.argv[1] == '1'):
        next_nd =  1
        now_nd = 1
        count = 0

        while (1):
            #TODO: Implement your algorithm here and return the UID for evaluation function
            now_nd = next_nd
            next_nd = int(input("destination: "))
            count = count+1
            nd_list_tmp = maze.BFS_2(now_nd,next_nd)
            for i in range(len(nd_list_tmp)):
                nd_list.append(nd_list_tmp[i])
            if(count == 5):
                break       
        dir_List.append('U')
        for i in range(len(nd_list)-1):
            if(i==0):
                dir_last = 2
                dir_next = maze.nodes[(int)(nd_list[0])-1].getDirection(maze.nodes[(int)(nd_list[1])-1])
            else:
                dir_sum = maze.getdir(nd_list[i-1],nd_list[i],nd_list[i+1])
                dir_last = (int)(((int)(dir_sum)) / 10)
                dir_next = ((int)(dir_sum)) % 10
            direction = maze.dircheck(dir_last,dir_next)
            #if((direction == 'R') or (direction == 'L')):
                #dir_List.append('S')
            dir_List.append(direction)
        dir_List.append('S')
        print(dir_List)

        bt = BT.bluetooth()
        bt.do_connect('COM1')
        _quit = False
        count = 0
        while _quit is False:
            readstring = bt.SerialReadString()
            if readstring == 'Node encountered.':
                print("i read it")
                count = count+1
                cmd = dir_List[count]
                bt.SerialWrite(cmd)
                if count == len(dir_List):
                    _quit = True
            else:
                print("ya")
                point.add_UID(readstring)

        print(bt.ser.isOpen())
        bt.disconnect()
             



            #for i in range(1, len(ndList)):
             #   get_UID = "just a test"
             #   point.add_UID(get_UID)

    """
    node = 0
    while(not node):
        node = interface.wait_for_node()

    interface.end_process()
    """
    print("complete")
    print("")
    a = point.getCurrentScore()
    print("The total score: ", a)
Beispiel #2
0
            list: A list of tuples

        """
        passage = set(self.maze.free_pos)
        end_positions = set(self.maze.endl[0])
        freeway = passage - end_positions
        chosen_positions = random.sample(freeway, len(self.items))
        return chosen_positions

    def place_items(self):
        """Place objects randomly on the maze

        Get the list of tuples in find_random_positions()
        According to the number of objects in items
        Get a position in list of tuples
        Place the object accordind to this position

        """
        position = self.find_random_positions()
        for n, item in enumerate(self.items):
            i, j = position[n]
            self.maze.set(i, j, item)


if __name__ == "__main__":
    maze = maze.Maze()
    syringe = Syringe(maze)
    maze.load()
    syringe.place_items()
    maze.show()
Beispiel #3
0
 def __init__(self):
     self.my_maze = maze.Maze(None)
     self.my_maze.blank_board()
     self.my_maze.setup_default_maze()
Beispiel #4
0
def main():
    current_maze = maze.Maze('create')
    create_dfs(current_maze)
    while 1:
        maze.check_for_exit()
    return
Beispiel #5
0
signal.signal(signal.SIGINT, ctrlC)

if len(sys.argv) != 4:
    sys.exit(
        'Error: navigateMaze.py requires 3 arguments: x position, y position, and heading'
    )
try:
    pos = [int(sys.argv[1]), int(sys.argv[2])]
except ValueError:
    sys.exit('Error: arguments must be integers: x position, y position')
heading = str(sys.argv[3].lower())
if heading != 'n' and heading != 's' and heading != 'e' and heading != 'w':
    sys.exit('Error: heading must be N, E, S, or W')
if pos[0] > 3 or pos[0] < 0 or pos[1] > 3 or pos[1] < 0:
    sys.exit('Error: positions must be 0, 1, 2, or 3')
mz = maze.Maze(pos, heading)

# mz.goForward()
# mz.goForward()
# mz.turn('right')
# mz.goForward()
# mz.turn('right')
# mz.goForward()
# mz.turn('left')
# mz.goForward()
# mz.goForward()
# mz.turn('right')
# mz.goForward()
# mz.turn('right')
# mz.goForward()
# mz.goForward()
Beispiel #6
0
import pygame
import character
import maze
import resource

pygame.init()

game_maze = maze.Maze(25, 25, 15, 1)
display_width = game_maze.get_display_width()
display_height = game_maze.get_display_length()
tile_size = game_maze.get_tile_size()
map_grid = game_maze.gridit()
map_obstacles = game_maze.map_obstacles()

GRASS = pygame.image.load(r'./assets/grass.png')
GRASS = pygame.transform.scale(GRASS, (tile_size, tile_size))
GLASS = pygame.image.load(r'./assets/glass.png')
GLASS = pygame.transform.scale(GLASS, (tile_size, tile_size))
FIRE = pygame.image.load(r'./assets/fire.png')
FIRE = pygame.transform.scale(FIRE, (tile_size, tile_size))
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

resource_maze = resource.Resource("Glass", GLASS, 10, True)
resource_location = resource_maze.random_resource_spread(game_maze)

agent = character.Character()
agent.transform_avatar(tile_size)
car = agent.get_avatar()
car_length, car_width = agent.get_avatar_dim()
crashed = agent.get_state()
Beispiel #7
0
def main():
    # Initialization
    maze = mz.Maze("data/maze_small.csv")
    next_nd = maze.getStartPoint()
    node_dict = maze.getNodeDict()
    car_dir = Direction.SOUTH
    point = score.Scoreboard("data/UID.csv")
    interf = interface.interface(
    )  # the part of calling interface.py was commented out.
    team_name = "Team_1"
    # Mode 0: for treasure-hunting with rule 1
    if (sys.argv[1] == '0'):

        # When you start the game, call the following judge code.
        ######### Do not change #########

        ### Add Here !!!
        ########## Juge code ############
        point.start_Judging()
        #################################

        print("Mode 0: Game Start!")
        while (1):
            ndList = []
            deadend_node_num = 0
            for node in maze.nodes:
                if len(node.getSuccessors()) == 1:
                    deadend_node_num += 1
            start_nd = next_nd
            for i in range(1, deadend_node_num + 1):
                BFS_list = maze.strategy(start_nd)
                start_nd = BFS_list[-1]
                if i == deadend_node_num:
                    ndList = ndList + BFS_list
                else:
                    ndList = ndList + BFS_list[:-1]
                print("The route to deadend {}: {}".format(
                    i, [j.getIndex() for j in BFS_list]))
            print("The whole BFS route:", [node.getIndex() for node in ndList])

            count = 0
            for i in range(1, len(ndList)):
                current_node = ndList[i - 1]
                next_node = ndList[i]
                print("The coming node: Node", current_node.getIndex())
                print("The next going node: Node", next_node.getIndex())

                # current car position + current node + next node => action + new car direction
                print("Current car direction:", car_dir)
                action, car_dir = maze.getAction(car_dir, current_node,
                                                 next_node)
                print("Updated car direction:", car_dir)
                # Node Detecting
                while (1):
                    python_get_information = interf.ser.SerialReadString()
                    print(python_get_information is 'N')
                    if python_get_information is 'N':
                        count = count + 1
                        print("The car see a node!\n")
                        break
                # Tell BT to send the action back to Arduino
                print("Get action:", action)
                interf.send_action(action)
                # RFID receiving
                (read_UID, waiting) = interf.ser.SerialReadByte()
                while read_UID == "Not receive" and waiting == 0:
                    (read_UID, waiting) = interf.ser.SerialReadByte()
                while waiting < 4:
                    (read_UID_tmp, waiting_tmp) = interf.ser.SerialReadByte()
                    if waiting_tmp != 0:
                        waiting = waiting + waiting_tmp
                        read_UID = read_UID + read_UID_tmp
                print("read_UID: ", read_UID, "  waiting: ", waiting)
                print("***** waiting: ", waiting)
                if read_UID != "Not receive" and waiting == 4:
                    print("RFID ID: ", read_UID)
                    point.add_UID(read_UID)
                current_score = point.getCurrentScore()

                # When you get an UID, please call the following judge code.
                ######### Do not change #########

                ########## Juge code ############
                # current_score = point.getCurrentScore()

                #################################

            print("Get action: ", mz.Action.HALT)
            interf.send_action(mz.Action.HALT)
            break

    # Mode 1: for treasure-hunting with rule 2
    elif (sys.argv[1] == '1'):
        print("Mode 1: Game Start!")
        while (1):
            nd = int(input("destination: "))
            if (nd == 0):
                print("end process")
                print('')
                break
            try:
                nd = node_dict[nd]
            except:
                print("Your input is not a valid node!")
                raise IndexError("No node!")
            ndList = maze.strategy_2(next_nd, nd)

    # Mode 2: Self-testing mode.
    elif (sys.argv[1] == '2'):
        print("Mode 2: Self-testing mode.")
        # TODO: You can write your code to test specific function.
        while (1):
            state_cmd = input("Please enter a mode command: ")
            interf.ser.SerialWrite(state_cmd)
Beispiel #8
0
from sys import setrecursionlimit
import pygame
import maze

setrecursionlimit(2000)

pygame.display.init()
pygame.font.init()
window = pygame.display.set_mode((200, 200), flags=pygame.RESIZABLE)
font = pygame.font.Font(None, 30)
gameClock = pygame.time.Clock()
running = True
successPrinted = False
fCount = 0

maze = maze.Maze(2, 2, window)
solveRate = 1
solveSteps = 1  # Account of steps executed each `solveRate` frames
print("Stepping {} times each {} frames".format(solveSteps, solveRate))

print(maze)

while running:
    gameClock.tick(60)
    fCount += 1
    e = pygame.event.poll()
    if e.type == pygame.QUIT:
        running = False
    if e.type == pygame.VIDEORESIZE:
        #maze.setCsize(e=e)
        maze.cw = 30
Beispiel #9
0
def main():
    maze = mz.Maze("data/small_maze.csv")
    interf = interface.interface()
    if (sys.argv[1] == '1'):
        sequence = input("Enter your sequence.(by index, split by spacebars): ")
    point = score.Scoreboard("data/UID.csv", "Mingto's English Corner", sys.argv[1])
    
    # TODO : Initialize necessary variables

    if (sys.argv[1] == '0'):
        print("Mode 0: for treasure-hunting with rule 1")
        # TODO : for treasure-hunting with rule 1, which encourages you to hunt as many scores as possible
        maze.getStartPoint()
        message_L = []
        direct_L = []
        while len(maze.deadend) != 0:
            path = maze.strategy(maze.now)
            while len(path) != 0:
                direct_L.append(maze.getAction(maze.now_d, maze.now, path.pop(0)))
            
        while len(direct_L) != 0:
            message_L.append(interf.save_action(direct_L.pop(0)))
        
        message_L.pop(0)
        #send all paths    
        message = ''.join(message_L)
        interf.send_action(message)
        
        #score
        while True:
            UID = interf.get_UID()
            if UID != 0:
                point.add_UID(UID)
                now_score = point.getCurrentScore()
                print("score:", now_score)

    elif (sys.argv[1] == '1'):
        print("Mode 1: for treasure-hunting with rule 2")
        # TODO : for treasure-hunting with rule 2, which requires you to hunt as many specified treasures as possible
        maze.getStartPoint()
        message_L = []
        direct_L = []
        while len(sequence) != 0:
            path = maze.strategy_2(maze.now, sequence.pop(0))
            while len(path) != 0:
                direct_L.append(maze.getAction(maze.now_d, maze.now, path.pop(0)))
        while len(direct_L) != 0:
            message_L.append(interf.save_action(direct_L.pop(0)))
        
        message_L.pop(0)
        #send all paths
        message = ''.join(message_L)
        interf.send_action(message)   

        #score
        while True:
            UID = interf.get_UID()
            if UID != 0:
                point.add_UID(UID)
                now_score = point.getCurrentScore()
                print("score:", now_score)

#TEST!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    elif (sys.argv[1] == '2'):
        while True:

            print(point.getCurrentScore())                                              #
Beispiel #10
0
def main():
    system('cls')
    init()
    sys.setrecursionlimit(10000)
    size = int(input("Enter maze size between 5-100: "))
    system('cls')
    if size < 5 or size > 100:
        print("Too big or too low maze :(")
        time.sleep(2.5)
        main()
    width = math.ceil((size-1)/2)
    height = math.ceil((size-1)/2)
    
    mazeObj = maze.Maze(width,height)
    mazeObj.randomize()
    strMatrixBFS = mazeObj._to_str_matrix()
    
    printMaze(strMatrixBFS)
    
    
    
    while True:
        print("Do you want to change maze ?\n1- Add wall\n2- Delete wall\n3- Nothing\n")
        optionWall = input()
        if optionWall == '1':    
                print("Where do you want to add wall ? Enter coordinates (e.g >15 27)")
                wallCoord = input()
                wallCoordX = wallCoord[:wallCoord.index(' ')]
                wallCoordY = wallCoord[wallCoord.index(' '):]
                strMatrixBFS[int(wallCoordX)][int(wallCoordY)] = 'O'
                system('cls')
                printMaze(strMatrixBFS)
                
    
        if optionWall == '2':    
                print("Where would you like to remove a wall ? Enter coordinates (e.g >15 27)")
                wallCoord = input()
                wallCoordX = wallCoord[:wallCoord.index(' ')]
                wallCoordY = wallCoord[wallCoord.index(' '):]
                strMatrixBFS[int(wallCoordX)][int(wallCoordY)] = ' '
                system('cls')
                printMaze(strMatrixBFS)
                
        
        if optionWall == '3':
            break


    system('cls')
    printMaze(strMatrixBFS)
    playerCoordinates = []
    targetCoordinates = []
    print("Select an option :\n")
    print("1- Select Start and End Points Automatically")
    print("2- Select Start and End Points Manually\n")
    
    option = input()
    if option == '1':
        print("How many target points do you want ?")
        targetPointCount = input()
        for i in range(int(targetPointCount)):
            generateStartAndEndPositions(playerCoordinates, targetCoordinates, width, height, strMatrixBFS, i)
        system('cls')
        printMaze(strMatrixBFS)
    elif option == '2':
        print("Enter x and y coordinates of starting position (e.g > 15 20 )\n")
        startingPoint = input()
        xCoordOfStarting = startingPoint[:startingPoint.index(' ')]
        yCoordOfStarting = startingPoint[startingPoint.index(' '):]
        strMatrixBFS[int(xCoordOfStarting)][int(yCoordOfStarting)] = 'X'
        playerCoordinates.append(int(xCoordOfStarting))
        playerCoordinates.append(int(yCoordOfStarting))
        system('cls')
        printMaze(strMatrixBFS)
        
        if (playerCoordinates[0] == 0 and playerCoordinates[1] == 0) or (playerCoordinates[0] == 0 and playerCoordinates[1] == len(strMatrixBFS)-1) or (playerCoordinates[0] == len(strMatrixBFS)-1 and playerCoordinates[1] == 0) or (playerCoordinates[0] == len(strMatrixBFS)-1 and playerCoordinates[1] == len(strMatrixBFS)-1):
            print("Player Coordinates can not be corners")
            return
        
        print("How many target points do you want ?")
        targetPointCount = input()
        
        for i in range(int(targetPointCount)):
            print("Enter x and y coordinates of target position (e.g > 10 34 )\n")
            targetPoint = input()
            xCoordOfTarget = targetPoint[:targetPoint.index(' ')]
            yCoordOfTarget = targetPoint[targetPoint.index(' '):]
            
            
            
            strMatrixBFS[int(xCoordOfTarget)][int(yCoordOfTarget)] = 'Y'
            targetCoordinates.append(int(xCoordOfTarget))
            targetCoordinates.append(int(yCoordOfTarget))
            
            system('cls')
            printMaze(strMatrixBFS)
            
            if (targetCoordinates[0] == 0 and targetCoordinates[1] == 0) or (targetCoordinates[0] == 0 and targetCoordinates[1] == len(strMatrixBFS)-1) or (targetCoordinates[0] == len(strMatrixBFS)-1 and targetCoordinates[1] == 0) or (targetCoordinates[0] == len(strMatrixBFS)-1 and targetCoordinates[1] == len(strMatrixBFS)-1):
                print("Target Coordinates can not be corners.")
                return
    
    
    
        
        
        
        
        
    strMatrixDFS = copy.deepcopy(strMatrixBFS)
    strMatrixUCS = copy.deepcopy(strMatrixBFS)
    

    
    print("\nWhich algorithm do you want to use to solve the maze with?")
    print("1- Depth First Search")
    print("2- Breadth First Search")
    print("3- Uniform Cost Search")
    print("4- All of them")
    
    selection = input()
    print()
   
    if selection == "1":
        start_time = time.time()
        queueDFS = []
        visitedDFS = []
        pathDFS = DFS(strMatrixDFS, playerCoordinates, queueDFS, visitedDFS)
        copyPath = copy.deepcopy(pathDFS)
        combinePathAndMatrix(pathDFS, strMatrixDFS)
        system('cls')
        printMaze(strMatrixDFS)
        if len(copyPath) <= 1:
            print(Back.RED + "\nPath not found")
            print(Back.RESET + "" , end= "")
        print("\nRunning time of DFS is %s " % (time.time() - start_time))
        
    elif selection == "2":
        start_time = time.time()
        stackBFS = []
        visitedBFS = []
        pathBFS = BFS(strMatrixBFS, playerCoordinates, stackBFS, visitedBFS)
        copyPath = copy.deepcopy(pathBFS)
        combinePathAndMatrix(pathBFS, strMatrixBFS)
        system('cls')
        printMaze(strMatrixBFS)
        if len(copyPath) == 0:
            print(Back.RED + "\nPath not found")
            print(Back.RESET + "" , end= "")
        print("\nRunning time of BFS is %s " % (time.time() - start_time))
    elif selection == "3":
        start_time = time.time()
        queueUCS = []
        visitedUCS = []
        pathUCS = UCS(strMatrixUCS, playerCoordinates, queueUCS, visitedUCS)
        copyPath = copy.deepcopy(pathUCS)
        combinePathAndMatrix(pathUCS, strMatrixUCS)
        system('cls')
        printMaze(strMatrixUCS)
        if len(copyPath) == 0:
            print(Back.RED + "\nPath not found")
            print(Back.RESET + "" , end= "")
        print("\nRunning time of UCS is %s " % (time.time() - start_time))
    elif selection == "4":
        start_timeDFS = time.time()
        queueDFS = []
        visitedDFS = []
        pathDFS = DFS(strMatrixDFS, playerCoordinates, queueDFS, visitedDFS)
        copyPathDFS = copy.deepcopy(pathDFS)
        combinePathAndMatrix(pathDFS, strMatrixDFS)
        end_timeDFS = time.time()
        
        
        start_timeBFS = time.time()
        stackBFS = []
        visitedBFS = []
        pathBFS = BFS(strMatrixBFS, playerCoordinates, stackBFS, visitedBFS)
        copyPathBFS = copy.deepcopy(pathBFS)
        combinePathAndMatrix(pathBFS, strMatrixBFS)
        end_timeBFS = time.time()
        
        
        
        start_timeUCS = time.time()
        queueUCS = []
        visitedUCS = []
        pathUCS = UCS(strMatrixUCS, playerCoordinates, queueUCS, visitedUCS)
        copyPathUCS = copy.deepcopy(pathUCS)
        combinePathAndMatrix(pathUCS, strMatrixUCS)
        end_timeUCS = time.time()
        
        system('cls')
        print("DFS Solution:\n")
        printMaze(strMatrixDFS)
        print("\nBFS Solution:\n")
        printMaze(strMatrixBFS)
        print("\nUCS Solution:\n")
        printMaze(strMatrixUCS)
        if len(copyPathDFS) <= 1 or len(copyPathBFS) == 0 or len(copyPathUCS) == 0:
            print(Back.RED + "\nPath not found")
            print(Back.RESET + "" , end= "")
        
        print("\nRunning time of DFS is %s " % (end_timeDFS - start_timeDFS))
        print("Running time of BFS is %s " % (end_timeBFS - start_timeBFS))
        print("Running time of UCS is %s " % (end_timeUCS - start_timeUCS))
        
    print("\nDo you want to try again ?")
    print("1-Yes \n2-No")
    selection2 = input()
    if selection2 == "1":
        main()
    elif selection2 == "2":
        return
Beispiel #11
0
def main():
    maze = mz.Maze("data/final_map.csv")
    # interf = interface.interface()
    # point = score.Scoreboard("data/medium_maze_UID.csv", "Madame_killer 師奶殺手阿水", sys.argv[1])

    # TODO : Initialize necessary variables
    nownode = 1
    path = []
    sequence = []

    preUID = ""

    if (sys.argv[1] == '0'):
        print("Mode 0: for treasure-hunting with rule 1")
        # TODO : for treasure-hunting with rule 1, which encourages you to hunt as many scores as possible
        maze.setStartDirection(1)

        path = maze.strategy(nownode)
        if len(path) == 1:
            # interf.end_process()
            print('end')
        else:
            print(path)
            while (len(path) >= 2):
                nd_to = path[1]
                nd_from = path.pop(0)
                nowdirection = maze.getNowDirection()
                action = maze.getAction(nowdirection, nd_from, nd_to)
                # interf.send_action(str(int(action)))
                print(action)
                nownode = nd_to

        while (True):
            #     if preUID != "":
            #         point.add_UID(preUID)
            #         print(preUID)
            #         preUID = ""

            path = maze.strategy(nownode)
            if len(path) == 1:
                break

            # interf.send_action('5')
            print('Action.HALT')

            print(path)
            while (len(path) >= 2):
                nd_to = path[1]
                nd_from = path.pop(0)
                nowdirection = maze.getNowDirection()
                action = maze.getAction(nowdirection, nd_from, nd_to)
                # interf.send_action(str(int(action)))
                print(action)
                nownode = nd_to

            # RFID
            # while(True):
            #     UID = interf.get_string()
            #     if UID != "":
            #         print(UID)
            #         preUID = UID
            #         break

        #RFID
        # interf.send_action('5')
        print('Action.HALT')
        # while(True):
        #     UID = interf.get_string()
        #     if UID != "":
        #         print(UID)
        #         point.add_UID(UID)
        #         break
        # interf.end_process()
        print('end')

    elif (sys.argv[1] == '1'):
        print("Mode 1: for treasure-hunting with rule 2")
        # TODO : for treasure-hunting with rule 2, which requires you to hunt as many specified treasures as possible
        sequence = [1, 8, 24, 44, 41, 36]
        print(sequence)
        maze.setStartDirection(sequence[0])
        print(maze.getNowDirection())

        if len(sequence) == 1:
            # interf.end_process()
            print("end")
        else:
            ND_TO = sequence[1]
            ND_FROM = sequence.pop(0)
            path = maze.strategy_2(ND_FROM, ND_TO)
            print(path)
            while (len(path) >= 2):
                nd_to = path[1]
                nd_from = path.pop(0)
                nowdirection = maze.getNowDirection()
                action = maze.getAction(nowdirection, nd_from, nd_to)
                # interf.send_action(str(int(action)))
                print(action)
                nownode = nd_to

        while (True):

            # if preUID != "":
            #     point.add_UID(preUID)
            #     print(preUID)
            #     preUID = ""

            if len(sequence) == 1:
                break

            # interf.send_action('5')
            print('Action.HALT')

            ND_TO = sequence[1]
            ND_FROM = sequence.pop(0)
            path = maze.strategy_2(ND_FROM, ND_TO)
            print(path)
            while (len(path) >= 2):
                nd_to = path[1]
                nd_from = path.pop(0)
                nowdirection = maze.getNowDirection()
                action = maze.getAction(nowdirection, nd_from, nd_to)
                # interf.send_action(str(int(action)))
                print(action)
                nownode = nd_to

            #RFID
            # while(True):
            #     UID = interf.get_string()
            #     if UID != "":
            #         print(UID)
            #         preUID = UID
            #         break
        #RFID
        # interf.send_action('5')
        print('Action.HALT')
        # while(True):
        #     UID = interf.get_string()
        #     if UID != "":
        #         print(UID)
        #         point.add_UID(UID)
        #         break

    elif (sys.argv[1] == '2'):
        # print("Mode 2: Self-testing mode.")
        # TODO: You can write your code to test specific function.

        # interf.send_action('2')
        # interf.send_action(input())
        for i in range(0, 4):
            for j in range(2):
                interf.send_action('3')
                interf.send_action('3')
                interf.send_action('2')
            for j in range(2):
                interf.send_action('4')
                interf.send_action('4')
                interf.send_action('2')
Beispiel #12
0
def test_maze(file, meta):
    m = maze.Maze(file)
    m.solve()
    assert m.solution[1][-1] == meta
Beispiel #13
0
                    action="store",
                    default=0,
                    type=int,
                    help='How many mazes to create.')
args = parser.parse_args()

number_of_mazes = args.number
height = args.complexity

while True:
    if height < 2 or height > 1000:
        height = raw_input("Complexity of maze (2-1000): ")
        height = int(height) if len(height) > 0 else 0
    else:
        break

while True:
    if number_of_mazes < 1:
        number_of_mazes = raw_input("Number of mazes to generate: ")
        number_of_mazes = int(
            number_of_mazes) if len(number_of_mazes) > 0 else 0
    else:
        break

width = int(height * 1.414)
painter = maze_painter.MazePainter()

for i in range(0, number_of_mazes):
    new_maze = maze.Maze(width, height)
    painter.paint_maze(new_maze, i)
Beispiel #14
0
                # Dictionary comprehension to get value associated with each action
                action_values = {
                    a: grid[getattr(grid[i][j], a)[0]][getattr(grid[i][j],
                                                               a)[1]].value
                    for a in actions
                }
                policy[i][j] = max(action_values, key=action_values.get)
                # Compare to previous policy
            else:
                policy[i][j] = '#'

    return (policy)


def prettify_policy(policy):
    policy_str = '\n'.join([''.join(row) for row in policy])
    policy_str = re.sub('up', '↑', policy_str)
    policy_str = re.sub('down', '↓', policy_str)
    policy_str = re.sub('right', '→', policy_str)
    policy_str = re.sub('left', '←', policy_str)
    return (policy_str)


if __name__ == '__main__':
    test_maze = maze.Maze(w=20, h=20)
    test_policy = value_iteration(test_maze.grid, .9)
    test_policy_str = prettify_policy(test_policy)

    print(test_maze)
    print(test_policy_str)
Beispiel #15
0
    process_maze(maze, maze_start_column, wall_start_row, bottom_sub_maze_columns, bottom_sub_maze_rows)



#####################
#
# Main Program
#
#####################


# create an empty maze (with just perimeter walls):
# ------------------------------------
columns = 11   # should be an odd number!
rows = 9      # should be an odd number!
maze = maze.Maze(columns, rows)     # size of maze (columns, rows)
block_maze_perimeter(maze)         # draw the surrounding walls around the maze

# Create the goal:
# ----------------
# The default position in the lower right corner of the maze, inside the maze (hence -1):
goal = goal.Goal(maze, columns - 1, rows - 1, "gold")    # (maze, column, row, color)

# Create a maze walker:
# ---------------------
# The position should be top-right, inside the maze (hence 2,2) 
walker = walker.Walker(maze, 2, 2, 0, "blue")     # (maze, column, row, heading, color)
maze.update_display()     # don't change this line. for display acceleration.


Beispiel #16
0
def main():
    maze = mz.Maze("data/small_maze.csv")
    point = score.Scoreboard("data/UID.csv", "team_NTUEE")
    interf = interface.interface()
    # TODO : Initializ1e necessary variables

    if (sys.argv[1] == '0'):
        print("Mode 0: for treasure-hunting")
        direction = "2"  # input("Enter the initial direction:(1,2,3,4)")
        route = maze.strategy("6")
        print(route)
        start = 0
        action = maze.getAction(direction, route[start], route[start + 1])
        interf.send_action(action)
        direction = str(
            int(maze.nd_dict[route[start]].getDirection(route[start + 1])))
        start += 1

        while (start < (len(route) - 1)):
            command = interf.get_command()
            print(command)
            if command == "n":
                action = maze.getAction(direction, route[start],
                                        route[start + 1])
                interf.send_action(action)
                if action == mz.Action(2):
                    uidcode = interf.get_UID()
                    print(uidcode)
                    point.add_UID(str(uidcode))
                    print(point.getCurrentScore())
                direction = str(
                    int(maze.nd_dict[route[start]].getDirection(route[start +
                                                                      1])))
                start += 1


#            if uidcode != 0:
# point.add_UID(str(uidcode))
# print(point.getCurrentScore())
#               print(uidcode)
#              point.add_UID(str(uidcode))
#             print(point.getCurrentScore())

        command = interf.get_command()
        interf.send_action(mz.Action(5))

        print(route)
        # TODO : for treasure-hunting, which encourages you to hunt as many scores as possible

    elif (sys.argv[1] == '1'):
        # TODO: You can write your code to test specific function.
        print("Mode 1: for checkpoint")
        direction = "2"  # input("Enter the initial direction:(1,2,3,4)")
        in_node = 1
        interf.send_action(mz.Action(1))
        while (in_node < 13):
            command = interf.get_command()
            if command == "n":
                if in_node == 1 or in_node == 2 or in_node == 3:
                    interf.send_action(mz.Action(1))
                    in_node += 1
                elif in_node == 4:
                    interf.send_action(mz.Action(3))
                    in_node += 1
                elif in_node == 5:
                    interf.send_action(mz.Action(2))
                    in_node += 1
                    uidcode = interf.get_UID()
                    print(uidcode)
                    point.add_UID(str(uidcode))
                    print(point.getCurrentScore())
                elif in_node == 6:
                    interf.send_action(mz.Action(1))
                    in_node += 1
                elif in_node == 7:
                    interf.send_action(mz.Action(3))
                    in_node += 1
                elif in_node == 8:
                    interf.send_action(mz.Action(4))
                    in_node += 1
                elif in_node == 9:
                    interf.send_action(mz.Action(3))
                    in_node += 1
                elif in_node == 10:
                    interf.send_action(mz.Action(2))
                    in_node += 1
                    uidcode = interf.get_UID()
                    print(uidcode)
                    point.add_UID(str(uidcode))
                    print(point.getCurrentScore())
                elif in_node == 11:
                    interf.send_action(mz.Action(1))
                    in_node += 1
                elif in_node == 12:
                    interf.send_action(mz.Action(5))
                    in_node += 1
                print(in_node)
        command = interf.get_command()
        interf.send_action(mz.Action(5))

    elif (sys.argv[1] == '2'):
        # TODO: You can write your code to test specific function.
        print("Mode 2: for checkpoint with backward")
        direction = "2"  # input("Enter the initial direction:(1,2,3,4)")
        in_node = 1
        interf.send_action(mz.Action(1))
        while (in_node < 13):
            command = interf.get_command()
            if command == "n":
                if in_node == 1 or in_node == 2 or in_node == 3:
                    interf.send_action(mz.Action(1))
                    in_node += 1
                elif in_node == 4:
                    interf.send_action(mz.Action(6))
                    in_node += 2
                    uidcode = interf.get_UID(2)
                    print(uidcode)
                    point.add_UID(str(uidcode))
                    print(point.getCurrentScore())
                elif in_node == 6:
                    interf.send_action(mz.Action(1))
                    in_node += 1
                elif in_node == 7:
                    interf.send_action(mz.Action(3))
                    in_node += 1
                elif in_node == 8:
                    interf.send_action(mz.Action(4))
                    in_node += 1
                elif in_node == 9:
                    interf.send_action(mz.Action(3))
                    in_node += 1
                elif in_node == 10:
                    interf.send_action(mz.Action(2))
                    in_node += 1
                    uidcode = interf.get_UID()
                    print(uidcode)
                    point.add_UID(str(uidcode))
                    print(point.getCurrentScore())
                elif in_node == 11:
                    interf.send_action(mz.Action(1))
                    in_node += 1
                elif in_node == 12:
                    interf.send_action(mz.Action(5))
                    in_node += 1
                print(in_node)

        command = interf.get_command()
        interf.send_action(mz.Action(5))

    interf.end_process()
Beispiel #17
0
def test_solvs(n, number_of_runs, seed=1):

    random.seed(seed)
    print("test average runtime, number of runs =", number_of_runs,
          ", maze size is equal", "{}*{}".format(n, n), '\n')

    available_algorithms = ["A*", "bfs", "dfs", "dfs iterative"]

    #test recursive_backtracker
    recursive_backtracker_time = {item: 0 for item in available_algorithms}

    different_solutions_count = 0

    for i in range(number_of_runs):
        maze_object = maze.Maze(n, n)
        maze_object.clear()
        maze_object.fill()
        maze_generator.recursive_backtracker(None, maze_object)
        solutions = {}

        for item in available_algorithms:
            start = time.time()
            solutions[item] = maze_solver.general_solver(
                item, None, maze_object, 1, 1, n, n)
            end = time.time()

            recursive_backtracker_time[item] += (end - start) / number_of_runs

        for i in range(len(available_algorithms) - 1):
            if solutions[available_algorithms[i]] != solutions[
                    available_algorithms[i + 1]]:
                different_solutions_count += 1

    print("Recursive backtracker average time is:")

    for item in available_algorithms:
        print(item + ':', recursive_backtracker_time[item], "sec.")
    print("different solutions found during", different_solutions_count,
          "iterations")
    print("\n")

    #test hunt_and_kill
    hunt_and_kill_time = {item: 0 for item in available_algorithms}

    different_solutions_count = 0

    for i in range(number_of_runs):
        maze_object = maze.Maze(n, n)
        maze_object.clear()
        maze_object.fill()
        maze_generator.hunt_and_kill_optimized(None, maze_object)
        solutions = {}

        for item in available_algorithms:
            start = time.time()
            solutions[item] = maze_solver.general_solver(
                item, None, maze_object, 1, 1, n, n)
            end = time.time()

            hunt_and_kill_time[item] += (end - start) / number_of_runs

        for i in range(len(available_algorithms) - 1):
            if solutions[available_algorithms[i]] != solutions[
                    available_algorithms[i + 1]]:
                different_solutions_count += 1

    print("Hunt and Kill average time is:")

    for item in available_algorithms:
        print(item + ':', hunt_and_kill_time[item], "sec.")
    print("different solutions found during", different_solutions_count,
          "iterations")
    print("\n")

    #test eller
    eller_time = {item: 0 for item in available_algorithms}

    different_solutions_count = 0

    for i in range(number_of_runs):
        maze_object = maze.Maze(n, n)
        maze_object.clear()
        maze_object.fill()
        maze_generator.eller_optimized(None, maze_object)
        solutions = {}

        for item in available_algorithms:
            start = time.time()
            solutions[item] = maze_solver.general_solver(
                item, None, maze_object, 1, 1, n, n)
            end = time.time()

            eller_time[item] += (end - start) / number_of_runs

        for i in range(len(available_algorithms) - 1):
            if solutions[available_algorithms[i]] != solutions[
                    available_algorithms[i + 1]]:
                different_solutions_count += 1

    print("Eller average time is:")

    for item in available_algorithms:
        print(item + ':', eller_time[item], "sec.")
    print("different solutions found during", different_solutions_count,
          "iterations")
    print("\n")

    return None
Beispiel #18
0
def test_prueba(file_name, goal):
    m = maze.Maze(file_name)
    m.solve
    assert m.goal == goal
Beispiel #19
0
# Sound and graphics init for PyGame
pygame.init()

# Set up the screen.
screen_size = (55 * 8, 61 * 8)
screen = pygame.display.set_mode([screen_size[0], screen_size[1]])
pygame.display.set_caption("Gobbler")

# System setup
g = graphics.Graphics()
f = functions.GameFunctions(g, None)

# Tilesets Setup
tmxdata = tmxdata.TmxData()
maze = maze.Maze(tmxdata)
pellets_template = pellets.Pellets(tmxdata)
pellets = pellets.Pellets(tmxdata)
pellets.reset(pellets_template)
gobbler_paths = gobblerpaths.GobblerPaths(tmxdata)

# Sprites Setup
gobbler = gobblersprite.GobblerSprite(g, VoidNavigator())

# Game Loop setup
fps = 60

# Set gobbler's starting position in the maze
gobbler.reset()

# This is the main game loop.  Everything below here repeats forever.
Beispiel #20
0
Known issues:
 + It can happen that the maze ends up being very sparse.
 + Some of the border segments are not plotted correctly.

Date: 13.12.2018
"""

# Set grid size
user_input = False
if user_input:
    n = int(input("Give number >1 of grid boxes for each row"))
    print("Grid size is set to " + str(n) + "x" + str(n))
else:
    n = 60

maze = mz.Maze(n, verbose=False)

plt.figure()
plt.axis('off')

# Basic grid visualized:
for i in range(n + 1):
    plt.plot(maze.grid_rows[0, 0:(n + 1), i], maze.grid_rows[1, 0:(n + 1), i],
             'k')
    plt.plot(maze.grid_rows[0, i, 0:(n + 1)], maze.grid_rows[1, i, 0:(n + 1)],
             'k')
plt.title("Maze before deleting segments")

# Build the maze
maze.build_maze()
Beispiel #21
0
def main():
    test_case = 0  ###  Test_Case for Maze
    sample_individual = individual(
        'UDLR')  ### Sample Instance for Class Individual

    number_of_population = 1000  ### Number of Population
    generations_of_population = 5  ### Generation of Population

    population = [
        individual(
            sample_individual.generate_random(
                maze_samples.string_length[test_case]))
        for x in range(number_of_population)
    ]  ### Create an array of population of first generation
    gmap = ga(maze_samples.maze[test_case][::-1])  ### Initializing Grid Map
    M = maze.Maze(maze_samples.maze[test_case])
    M.Visualize()

    while generations_of_population > 1:
        generations_of_population -= 1

        for p in population:
            p.fitness = gmap.fitness_function(
                p.move
            )[0]  ### Calculating Fitness Score for each individual of Population

        new_generation = []  ### Blank List for New Generation

        L = [y.fitness
             for y in population]  ### List of Fitness Scores of Population

        S = sample_individual.SetWeightsForMonteCarloSelection(
            L)  ### Setting Weights for Monte Carlo Selection

        for p in range(len(population) // 2):
            p1 = sample_individual.MonteCarloSelection(
                S)  ### 1st Parent from Monte Carlo Selection
            p2 = sample_individual.MonteCarloSelection(
                S)  ### 2nd Parent from Monte Carlo Selection
            p3 = sample_individual.cross_breed(
                population[p1],
                population[p2])  ### Cross Breeding Parent1 and Parent2
            new_generation.extend(
                [sample_individual.mutate(x)
                 for x in p3])  ### Adding New Population to New_Generation

        random.shuffle(new_generation)  ### Shuffling New_Generation Array

        if max(
            [x.fitness for x in population]
        ) > 100:  ### If Fitness Score of any element is Greater than 100, Break
            for x in range(len(population)):
                if population[x].fitness > 200:
                    p1 = gmap.fitness_function(population[x].move)[1]
                    sample_individual = population[x]
                    break
            break

        else:
            for x in range(
                    len(population)
            ):  #### Calculating Maximum Fitness Score in Population
                if population[x].fitness > sample_individual.fitness:
                    sample_individual = population[x]

        population = new_generation  ### New_Generaton is now Population
    M.RunMaze(
        gmap.fitness_function(sample_individual.move)[1]
    )  ### Simulates Run Maze with Maximum Fitness Score in all population and generations
Beispiel #22
0
import maze
import draw
import robot
from math import *

import threading

speed = 1
target_frames = 30

maze = maze.Maze(5, 5)
maze.generate()
lines = []
# lines = [((0,0), (0,40)), ((0,0), (40,0)), ((40,0), (40,40)), ((0,40), (40,40))]
for l in maze.getWalls(10, 10):
    lines.append(l)

robot = robot.setup(100, 100, lines)


def redraw():
    draw.clear()

    for i in range(speed):
        robot.update()

    for l in lines:
        draw.line(*l)

    robot.draw()
    draw.camera = robot.getPos()
Beispiel #23
0
def test_prueba(file, goalF):
    m = maze.Maze(file)
    m.solve
    assert m.goal == goalF
Beispiel #24
0
import cv2
import maze

if __name__ == '__main__':

    cap = cv2.VideoCapture(0)
    maze.Maze(cap, "test")
Beispiel #25
0
import maze
import pygame as pg
import sys

frameRate = 0
width = 1000
height = 1000
ct = int(input("Input Size \n"))
anim = False
if input("Would You Like The Generation To Be Animated? (y/n) \n") == "y":
    anim = True
    while frameRate <= 0:
        frameRate = int(
            input(
                "Input Framerate \n (Limited By Computer Specs, Positive Nonzero Number) \n"
            ))
scl = int(width / ct)
screen = pg.display.set_mode((width, height))
pg.display.set_caption("Python Maze Generator | Generating...")
maze = maze.Maze(ct, ct, scl, screen, frameRate)
maze.Generate(anim)

while True:
    for e in pg.event.get():
        if e.type == pg.QUIT:
            sys.exit()
    maze.currentCell = None
    maze.Show()
    pg.display.flip()
    pg.image.save(screen, f'maze__{ct}x{ct}_maze.jpg')
Beispiel #26
0
                                   interval=100,
                                   blit=True,
                                   repeat=False)

    if (save_filename is not None):
        mpeg_writer = animation.FFMpegWriter(
            fps=24,
            bitrate=1000,
            codec="libx264",
            extra_args=["-pix_fmt", "yuv420p"])
        anim.save("{}{}x{}.mp4".format(save_filename, maze.num_rows,
                                       maze.num_cols),
                  writer=mpeg_writer)

    return anim


if (__name__ == "__main__"):
    maze_generator = mz.Maze(10, 10, 1)
    grid, path_gen = maze_generator.generate_maze((0, 0))

    plot_maze(maze_generator, grid)
    anim = animate_maze_generate(maze_generator, path_gen)

    path_solve = maze_generator.solve_maze(grid, method="fancy")

    plot_maze_solution(maze_generator, grid, path_solve)
    anim_solve = animate_maze_solve(maze_generator, grid, path_solve)

    plt.show()
Beispiel #27
0
tries = 100
interval = .05

search_functions = {
    "strategy 1" : strategy_1.run,
    "strategy 2" : strategy_2.run,
    "strategy 3" : strategy_3.run,
}

success_rates = {}
for (strategy, _) in search_functions.items():
    success_rates[strategy] = []

flammabilities = []

copy = maze.Maze(width, height, .5)
m = maze.Maze(width, height, .5)

flammability = 0
while flammability <= 1:
    successes = {} # the number of successful paths for this flammability
    counts = {} # the number of attempts at paths for this flammability

    # initialize the two dictionaries
    for (strategy, _) in search_functions.items():
        successes[strategy] = 0
        counts[strategy] = 0

    for i in range(0, tries):
        m.generate_maze(density)
        for (strategy, search_function) in search_functions.items():
Beispiel #28
0
import matplotlib.pyplot as plt
import maze

#Parameters
gamma = 0.8  # Discount factor - Tells the network how much it should prioritize future rewards
alpha = 0.9  # Learning rate - Tells the network how fast it should learn
epochs = 5000  # Number of epochs
layerSize = 75  # size of the hidden layers
dims = 4  # Size of the maze
changingMaze = False  # The maze layout changes each time
changingGoal = True  # The osition of the goal changes each time. If changing maze is on, it has no effect
lr = 0.012  # Optimizer learning rate. If it's too high or low it will not learn well
decay = 0.93  # Decay of random probability. Higher decay reduces the probabilites slower
maxTurns = 200  #Number of turns before the run gets aborted

board = maze.Maze(dims)
print(board.mazeState().reshape(3, dims, dims))


def choose(board, eps):
    #Uses the network to choose an action and has a chance of doing a random action
    a = np.random.rand()
    if a < eps:
        opt = np.random.randint(0, 4)
    else:
        opt = np.argmax(sess.run(layerFinal, feed_dict={inputState: [board]}))
    return opt


#Neural Network
inputState = tf.placeholder(tf.float32, shape=[None, 3 * dims**2])
Beispiel #29
0
import numpy as np
import maze as mz
import pandas as pd
import matplotlib.pyplot as plt

# Description of the maze as a numpy array
maze = np.array([[0, 0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 1, 0, 0],
                 [0, 0, 1, 0, 0, 1, 1, 1], [0, 0, 1, 0, 0, 1, 0, 0],
                 [0, 0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 1, 1, 1, 0],
                 [0, 0, 0, 0, 1, 2, 0, 0]])

mz.draw_maze(maze)
env = mz.Maze(maze)
gamma = 1 - (1 / 30.0)
epsilon = 0.01
method = 'ValIter'
win_count = 0.0
start = (0, 0)
runs = 100
for _ in range(runs):
    V, policy = mz.value_iteration(env, gamma, epsilon)
    # Simulate the shortest path starting from position A
    path, pathm, win = env.simulate(start, policy, method)
    if win:
        win_count += 1.0

prob = win_count / runs
print("Final probability: ", prob)
Beispiel #30
0
def main(output_file_name):
    # housekeeping
    m = maze.Maze(mc.U_maze)
    sess = tf.Session()
    learning_rate = 0.0001
    num_memory_units = 4
    graphical = True
    file_output = True

    if file_output is True:
        # output to file (this is set to overwrite!)
        file = open(output_file_name + ".txt", "w")
        file.write("Iter\tWon?\tSteps\tAll steps\n")

    # neural network structure
    x = tf.placeholder(tf.float32, [None, 2 + num_memory_units])

    W1 = tf.Variable(tf.truncated_normal([2 + num_memory_units, 6]))
    b1 = tf.Variable(tf.truncated_normal([1, 6]))

    h1 = tf.sigmoid(tf.matmul(x, W1) + b1)

    W2 = tf.Variable(tf.truncated_normal([6, 6]))
    b2 = tf.Variable(tf.truncated_normal([1, 6]))

    h2 = tf.sigmoid(tf.matmul(h1, W2) + b2)

    W3 = tf.Variable(tf.truncated_normal([6, 4 + num_memory_units]))
    b3 = tf.Variable(tf.truncated_normal([1, 4 + num_memory_units]))

    output_final_layer_before_activation_function = tf.matmul(h2, W3) + b3
    left_output = output_final_layer_before_activation_function[:, 0:4]
    right_output = output_final_layer_before_activation_function[:, 4:]
    y = tf.nn.softmax(left_output)
    memory_units = tf.sigmoid(right_output)

    sess.run(tf.global_variables_initializer())

    weights_list = [W1, b1, W2, b2, W3, b3]

    # gradients (i.e. dp/dw) for backpropagation
    dprobability0_dweights = tf.gradients(y[:, 0], weights_list)
    dprobability1_dweights = tf.gradients(y[:, 1], weights_list)
    dprobability2_dweights = tf.gradients(y[:, 2], weights_list)
    dprobability3_dweights = tf.gradients(y[:, 3], weights_list)

    # weight update operation
    ph_delta_weights_list = [
        tf.placeholder(tf.float32, w.get_shape()) for w in weights_list
    ]
    update_weights = [
        tf.assign(weights_list[i], weights_list[i] + ph_delta_weights_list[i])
        for i in range(len(weights_list))
    ]

    # training setup
    maxSteps = 30
    iteration = 0
    maxIterations = 10000

    steps_taken = np.zeros(maxIterations)

    # Plot display -----------------------------------------------------------------------------------------------------
    if graphical is True:
        spread = 50

        plt.ion()
        fig = plt.figure("Maze solver")
        ax = fig.add_subplot(111)
        ax.axis([0, maxIterations / spread + 1, 0, maxSteps + 1])
        plt.ylabel("Steps taken")
        plt.xlabel("Iterations ({})".format(spread))
        ax.plot([0], [0])
        ax.grid()

        iterations = []
        duration_history = []

    # Looping through iterations
    while iteration < maxIterations:
        # Current step
        step = 0

        # All outputs and dp_dthetas for this iteration
        probabilities = np.zeros(maxSteps)
        dp_dthetas = list()

        memory = np.zeros(num_memory_units)

        movements = ""

        while m.won is False and step < maxSteps:
            # Defining neural network input
            input_values = np.array([m.normal_x(), m.normal_y()])
            input_values = np.append(input_values, memory)

            # Running input through the neural network
            [output, dp0dtheta, dp1dtheta, dp2dtheta, dp3dtheta, output_memory] =\
                sess.run([y, dprobability0_dweights, dprobability1_dweights, dprobability2_dweights,
                          dprobability3_dweights, memory_units],
                         feed_dict={x: [input_values]})

            # Random value between 0 and 1, inclusive on both sides
            result = r.uniform(0, 1)

            if result <= output[0][0]:
                # Up
                m.move_up()
                probabilities[step] = output[0][0]
                dp_dthetas.append(dp0dtheta)
                movements += "U"
            elif result <= output[0][0] + output[0][1]:
                # Right
                m.move_right()
                probabilities[step] = output[0][1]
                dp_dthetas.append(dp1dtheta)
                movements += "R"
            elif result <= output[0][0] + output[0][1] + output[0][2]:
                # Down
                m.move_down()
                probabilities[step] = output[0][2]
                dp_dthetas.append(dp2dtheta)
                movements += "D"
            elif result <= output[0][0] + output[0][1] + output[0][2] + output[
                    0][3]:
                # Left
                m.move_left()
                probabilities[step] = output[0][3]
                dp_dthetas.append(dp3dtheta)
                movements += "L"

            memory = output_memory[0]
            step += 1

        print("Iteration #{:05d}\tWon: {}\tSteps taken: {:04d}\tSteps: {}".
              format(iteration, m.won, step, movements))
        if file_output is True:
            file.write("{:05d}\t{}\t{:04d}\t{}\n".format(
                iteration, m.won, step, movements))

        # Assigning a reward
        reward = maxSteps - (2 * step)  # linear reward function
        # reward = maxSteps - pow(step, 2)  # power reward function

        # Applying weight change for every step taken, based on the reward given at the end
        for i in range(step):
            deltaTheta = [(learning_rate * (1 / probabilities[i]) * reward) *
                          dp_dthetas[i][j] for j in range(len(weights_list))]

            sess.run(update_weights,
                     feed_dict=dict(zip(ph_delta_weights_list, deltaTheta)))

        steps_taken[iteration] = step
        if graphical is True and iteration % spread == 0:
            steps_mean = np.mean(steps_taken[iteration - spread:iteration + 1])
            iterations = iterations + [iteration / spread]
            duration_history = duration_history + [steps_mean]
            del ax.lines[0]
            ax.plot(iterations, duration_history, 'b-', label='Traj1')
            plt.draw()
            plt.pause(0.001)

        m.reset()
        # Try to guide the neural network towards learning the correct steps
        """
        if iteration < maxIterations / 10:
            m.goalX = 0
            m.goalY = 1
        elif iteration < 2 * maxIterations / 10:
            m.goalX = 0
            m.goalY = 2
        elif iteration < 3 * maxIterations / 10:
            m.goalX = 1
            m.goalY = 2
        elif iteration < 4 * maxIterations / 10:
            m.goalX = 2
            m.goalY = 2
        elif iteration < 5 * maxIterations / 10:
            m.goalX = 2
            m.goalY = 1
        else:
            m.goalX = 2
            m.goalY = 0
        """

        iteration += 1
    if file_output is True:
        file.close()
    if graphical is True:
        if file_output is True:
            plt.savefig(output_file_name + ".png")
        else:
            plt.show()
        #input("Press [enter] to continue.")
        plt.close()
    sess.close()