def train():
    goal = [i + 1 for i in range(pow(size, 2) - 1)]
    goal.append(0)
    snake = get_snake(size)
    to_move = goal[snake.index(0)]
    to_remember = snake[goal.index(0)]
    goal = Node(goal, size)
    heu = Heuristics(['manhattan'], size, to_move, to_remember)
    for pattern in patterns:
        pdbs.append(train_pattern(goal, pattern, heu))
        print("DB CACHED")
Beispiel #2
0
def getPath():
	global localdata
	global path
	global mapper
	global PATH
	cmap = CustomMap(img.imread(PATH))
	heuristics = Heuristics()
	search = Astar(cmap, heuristics)
	start = [localdata[0], localdata[1]]
	start = search.LocaltoGlobal(start[0],start[1])
	destination = [coordinates[0][0], coordinates[0][1]]
	destination = search.LocaltoGlobal(destination[0],destination[1])
	start = [start[0], start[1]]
	destination = [destination[0] , destination[1]]
	path = search.searchPath(start, destination)
	mapper.printpath(path)
	path = pixeltoglobal(path)
	path = dropWaypoints(path)
	addToDestinations(path)
	pixelToWaypoints()
Beispiel #3
0
def main():
    print("Sliding Puzzle Solver!\n")
    # Get size of the board from user
    size = int()
    while True:
        try:
            size = int(input("Insert the size of the board:"))
            if size < 2:
                raise ValueError
            else:
                break
        except ValueError:
            print("Insert a positive natural number > 1 !")
            continue

    game = G.Game(size)
    heuristic = H.Heuristics(game)

    inserted_value = str()
    inserted_position = []
    initial_state = None

    # Get the initial state (doesn't check the user input properly :()
    while not (inserted_value.upper() == 'Y') or (inserted_value.upper()
                                                  == 'N'):
        inserted_value = input(
            "Would you like to start from a random position (Y/N)?").upper()
        if inserted_value == 'Y':
            # Start with a random initial position
            game.shuffle()
            # Save the initial state/table
            initial_state = copy.deepcopy(game.state)
            break
        if inserted_value.upper() == 'N':
            for i in range(size**2):
                value = input(f"Insert the value number {i + 1}: ")
                try:
                    inserted_position.append(int(value))
                except ValueError:
                    print("Insert a positive natural number! Exiting ...")
                    break

            s = from_list_to_state(inserted_position, size)
            game.set_state(s)
            initial_state = copy.deepcopy(game.state)
            break

    game.set_state(initial_state)
    print("Initial state:")
    game.print()
    print()

    input("Press Enter to continue ...")
    starting_date = time.asctime(time.localtime(time.time()))
    print(f"Start searching at {starting_date}", )
    time.sleep(1)
    searching = True
    attempt = 1

    # Search routine
    while searching:
        searching = not search(game, heuristic, attempt)
        # If search return False executes the following if clause
        if searching:
            print("I'll try again from:")
            # Clean the game and set the previous initial state
            attempt += 1
            game.set_state(initial_state)
            game.explored = []
            game.state.previous_actions = []
            game.print()
            print()
            print("Start again in 2 seconds ...")
            time.sleep(2)
            # input("Press Enter to continue ...")
            searching = True
            continue