Ejemplo n.º 1
0
def test_bidirectional_breadth_first_search_vs_depth_first_graph_search():
    eight_puzzle = EightPuzzle(initial=instance_three, goal=goal)
    eight_puzzle_reversed = EightPuzzle(initial=goal, goal=instance_three)
    ins_problem = InstrumentedProblem(eight_puzzle)
    res = instrumented_bidirectional_breadth_first_search(eight_puzzle, eight_puzzle_reversed, True)
    depth_first_graph_search(ins_problem)
    assert (res[1]) <= ins_problem.explored
Ejemplo n.º 2
0
def test_astar_gaschnig_vs_depth_first():
    eight_problem = InstrumentedProblem(
        EightPuzzle(initial=instance_one, goal=goal))
    eight_problem1 = InstrumentedProblem(
        EightPuzzle(initial=instance_one, goal=goal))
    res = astar_search(eight_problem,
                       EightPuzzleExtended(eight_problem).gaschnig_index)
    res1 = depth_first_graph_search(eight_problem1)
    assert res.state == goal
    assert res1.state == goal
    assert eight_problem.explored < eight_problem1.explored
Ejemplo n.º 3
0
def test_astar_gaschnig_vs_bidirectional_breadth_first():
    eight_problem = InstrumentedProblem(
        EightPuzzle(initial=instance_one, goal=goal))
    eight_problem1 = EightPuzzle(initial=instance_one, goal=goal)
    inversed_eight_problem = EightPuzzle(initial=goal, goal=instance_one)
    res = astar_search(eight_problem,
                       EightPuzzleExtended(eight_problem).gaschnig_index)
    res1 = instrumented_bidirectional_breadth_first_search(
        eight_problem1, inversed_eight_problem, True)
    assert res.state == goal
    n = res1[0]
    assert n.state == goal
    assert eight_problem.explored > res1[1]
Ejemplo n.º 4
0
def exchange(index):
	""" Interchanges the position of the selected tile with the zero tile under certain conditions """

	global state
	global solution
	global puzzle
	zero_ix = list(state).index(0)
	actions = puzzle.actions(state)
	current_action = ''
	i_diff = index//3 - zero_ix//3
	j_diff = index%3 - zero_ix%3
	if i_diff == 1:
		current_action += 'DOWN'
	elif i_diff == -1:
		current_action += 'UP'

	if j_diff == 1:
		current_action += 'RIGHT'
	elif j_diff == -1:
		current_action += 'LEFT'

	if abs(i_diff) + abs(j_diff) != 1:
		current_action = ''

	if current_action in actions:
		b[zero_ix].grid_forget()
		b[zero_ix] = Button(root, text=f'{state[index]}', width=6, font=('Helvetica', 40, 'bold'), command=partial(exchange, zero_ix))
		b[zero_ix].grid(row=zero_ix//3, column=zero_ix%3, ipady=40)
		b[index].grid_forget()
		b[index] = Button(root, text=None, width=6, font=('Helvetica', 40, 'bold'), command=partial(exchange, index))
		b[index].grid(row=index//3, column=index%3, ipady=40)
		state[zero_ix], state[index] = state[index], state[zero_ix]
		puzzle = EightPuzzle(tuple(state))
Ejemplo n.º 5
0
def scramble():
    global state
    global puzzle
    possible_actions = ['UP', 'DOWN', 'LEFT', 'RIGHT']
    scramble = []
    for _ in range(60):
        scramble.append(random.choice(possible_actions))

    for move in scramble:
        if move in puzzle.actions(state):
            state = list(puzzle.result(state, move))
            puzzle = EightPuzzle(tuple(state))
            create_buttons()
Ejemplo n.º 6
0
def scramble():
    """ Scrambles the puzzle starting from the goal state """

    global state
    global puzzle
    possible_actions = ['UP', 'DOWN', 'LEFT', 'RIGHT']
    scramble = []
    for _ in range(60):
        scramble.append(random.choice(possible_actions))

    for move in scramble:
        if move in puzzle.actions(state):
            state = list(puzzle.result(state, move))
            puzzle = EightPuzzle(tuple(state))
Ejemplo n.º 7
0
def test_astar_manhattan_3():
    eight_problem = InstrumentedProblem(
        EightPuzzle(initial=instance_three, goal=goal))
    res = astar_search(eight_problem,
                       EightPuzzleExtended(eight_problem).manhattan_distance)
    assert res.state == goal
Ejemplo n.º 8
0
def test_astar_misplaced_tiles_cols_rows_3():
    eight_problem = InstrumentedProblem(
        EightPuzzle(initial=instance_three, goal=goal))
    res = astar_search(eight_problem,
                       EightPuzzleExtended(eight_problem).misplaced_cols_rows)
    assert res.state == goal
Ejemplo n.º 9
0
def test_astar_misplaced_tiles_3():
    eight_problem = InstrumentedProblem(
        EightPuzzle(initial=instance_three, goal=goal))
    res = astar_search(eight_problem, eight_problem.h)
    assert res.state == goal
Ejemplo n.º 10
0
def test_astar_gaschnig_3():
    eight_problem = InstrumentedProblem(
        EightPuzzle(initial=instance_three, goal=goal))
    res = astar_search(eight_problem,
                       EightPuzzleExtended(eight_problem).gaschnig_index)
    assert res.state == goal
Ejemplo n.º 11
0
import time
import random
import numpy as np

import sys
import os.path
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))

from search import astar_search, EightPuzzle
import utils

root = Tk()

state = [1, 2, 3, 4, 5, 6, 7, 8, 0]
puzzle = EightPuzzle(tuple(state))
solution = None

b = [None]*9

# TODO: refactor into OOP, remove global variables

def scramble():
	""" Scrambles the puzzle starting from the goal state """

	global state
	global puzzle
	possible_actions = ['UP', 'DOWN', 'LEFT', 'RIGHT']
	scramble = []
	for _ in range(60):
		scramble.append(random.choice(possible_actions))
Ejemplo n.º 12
0
def test_bidirectional_breadth_first_search_instance_three():
    eight_puzzle = EightPuzzle(initial=instance_three, goal=goal)
    eight_puzzle_reversed = EightPuzzle(initial=goal, goal=instance_three)
    res = bidirectional_breadth_first_search(eight_puzzle, eight_puzzle_reversed)
    print(res.path())
    assert res.state == goal