Exemplo n.º 1
0
 def search(path, g, bound, evaluated):
     evaluated += 1
     node = path[0]
     f = g + HEURISTIC(node, solved, size_rows, size_cols)
     if f > bound:
         return f, evaluated
     if node == solved:
         return True, evaluated
     if evaluated % 500000 == 0:
         print(
             color('yellow',
                   "bound: {} evaluated: {}".format(bound, evaluated)))
     ret = inf
     moves = possible_moves(node, size_rows, size_cols)
     for m in moves:
         if m not in path:
             path.appendleft(m)
             t, evaluated = search(path, g + TRANSITION_COST, bound,
                                   evaluated)
             if t is True:
                 return True, evaluated
             if t < ret:
                 ret = t
             path.popleft()
     return ret, evaluated
Exemplo n.º 2
0
def verbose_info(args, puzzle, solved, size):
    opts1 = {
        'greedy search:': args.g,
        'uniform cost search:': args.u,
        'visualizer:': args.v,
        'solvable:': is_solvable(puzzle, solved, size)
    }
    opt_color = 'cyan2'
    for k, v in opts1.items():
        print(color(opt_color, k), color_yes_no(v))

    opts2 = {
        'heuristic function:': color('green2', args.f),
        'puzzle size:': str(size),
        'solution type:': color('green2', args.s),
        'initial state:': str(puzzle),
        'final state:': str(solved)
    }
    for k, v in opts2.items():
        print(color(opt_color, k), v)

    print(color('blue2', 'heuristic scores for initial state'))
    for k, v in heuristics.KV.items():
        print(color('blue2', '  - ' + k + '\t:'), v(puzzle, solved, size))

    print(color('red2', 'search algorithm:'), 'IDA*' if args.ida else 'A*')
Exemplo n.º 3
0
def verbose_info(args, puzzle, solved, size):
    opts1 = {
        "greedy search:": args.g,
        "uniform cost search:": args.u,
        "visualizer:": args.v,
        "solvable:": is_solvable(puzzle, solved, size),
    }
    opt_color = "cyan2"
    for k, v in opts1.items():
        print(color(opt_color, k), color_yes_no(v))

    opts2 = {
        "heuristic function:": color("green2", args.f),
        "puzzle size:": str(size),
        "solution type:": color("green2", args.s),
        "initial state:": str(puzzle),
        "final state:": str(solved),
    }
    for k, v in opts2.items():
        print(color(opt_color, k), v)

    print(color("blue2", "heuristic scores for initial state"))
    for k, v in heuristics.KV.items():
        print(color("blue2", f"  - {k}\t:"), v(puzzle, solved, size))

    print(color("red2", "search algorithm:"), "IDA*" if args.ida else "A*")
Exemplo n.º 4
0
def pretty_print_steps(steps, size_rows, size_cols):
	width = len(str(size_rows * size_cols))
	decor = '-'
	for n in range(len(steps)):
		if n == 0:
			print('-[initial state]%s' % (4 * decor,))
		else:
			print('-[step %2d]%s' % (n, 10 * decor,))
		print()
		for i in range(size_rows):
			for j in range(size_cols):
				tile = str(steps[n][i * size_cols + j])
				if tile == '0':
					tile = color('red2', '-' * width)
				sys.stdout.write(' %*s' % (width, tile))
			print()
		print()
	print('%s' % (20 * decor,))
Exemplo n.º 5
0
def pretty_print_steps(steps, size):
    width = len(str(size * size))
    decor = "-"
    for n in range(len(steps)):
        if n == 0:
            print(f"-[initial state]{4*decor}")
        else:
            print(f"-[step {n:2d}]{10*decor}")
        print()
        for i in range(size):
            for j in range(size):
                tile = str(steps[n][i * size + j])
                if tile == "0":
                    tile = color("red2", "-" * width)
                print(f" {tile:>{width}}", end="")
            print()
        print()
    print(f"{20*decor}")
Exemplo n.º 6
0
def color_yes_no(v):
    return color('green', 'YES') if v else color('red', 'NO')
Exemplo n.º 7
0
    if args.ida:
        args.g = False

    TRANSITION_COST = 1
    if args.g:
        TRANSITION_COST = 0

    HEURISTIC = heuristics.KV[args.f]
    if args.u:
        HEURISTIC = heuristics.uniform_cost

    solved = solved_states.KV[args.s](size)
    verbose_info(args, puzzle, solved, size)
    if not is_solvable(puzzle, solved, size):
        print(color('red', 'this puzzle is not solvable'))
        sys.exit(0)

    maxrss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    print(color('red', 'max rss before search:'), maxrss)

    t_start = perf_counter()
    if args.ida:
        res = ida_star_search(puzzle, solved, size, HEURISTIC, TRANSITION_COST)
    else:
        res = a_star_search(puzzle, solved, size, HEURISTIC, TRANSITION_COST)
    t_delta = perf_counter() - t_start

    maxrss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    print(color('red', 'max rss after search: '), maxrss)
Exemplo n.º 8
0
def color_yes_no(v):
    return color("green", "YES") if v else color("red", "NO")
Exemplo n.º 9
0
    if args.ida:
        args.g = False

    TRANSITION_COST = 1
    if args.g:
        TRANSITION_COST = 0

    HEURISTIC = heuristics.KV[args.f]
    if args.u:
        HEURISTIC = heuristics.uniform_cost

    solved = solved_states.KV[args.s](size)
    verbose_info(args, puzzle, solved, size)
    if not is_solvable(puzzle, solved, size):
        print(color("red", "this puzzle is not solvable"))
        sys.exit(0)

    maxrss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    print(color("red", "max rss before search:"), maxrss)

    t_start = perf_counter()
    if args.ida:
        res = ida_star_search(puzzle, solved, size, HEURISTIC, TRANSITION_COST)
    else:
        res = a_star_search(puzzle, solved, size, HEURISTIC, TRANSITION_COST)
    t_delta = perf_counter() - t_start

    maxrss = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss
    print(color("red", "max rss after search: "), maxrss)