def test_action_and_result():
    errors = {}
    for state, example_result in action_and_result_examples.items():
        problem = NQueensProblem(state=state)
        result = [
            problem.result(problem.initial, action)
            for action in problem.actions(problem.initial)
        ]
        if set(result) != set(example_result):
            errors[state] = {
                "example_result": example_result,
                "result": result,
            }
    return errors
def time_function(problem, function):
    problem2 = NQueensProblem(state=problem.random_state())
    start = time.time()
    results = function(problem2)
    finish = time.time()
    results["time"] = finish - start
    return results
def generate_answers(hill_climbing_method, problems=None):
    """Return dictionary of results of running hill_climbing_method on problems."""
    if problems is None:
        examples = globals()[hill_climbing_method.__name__ + "_examples"]
    answers = {}
    for key in problems:
        state, *args = key
        problem = NQueensProblem(state=state)
        random.seed(SEED)
        result = hill_climbing_method(problem, *args)
        answers[key] = result
    return answers
    num_exanded_nodes = []
    solved = 0
    for i in range(number):
        results = time_function(problem, function)
        times.append(results["time"])
        num_exanded_nodes.append(results["expanded"])
        if results["solved"]:
            solved += 1
    time_standard_deviation = numpy.std(times)
    time_average = numpy.average(times)
    expanded_standard_deviation = numpy.std(num_exanded_nodes)
    expanded_average = numpy.average(num_exanded_nodes)
    solving_probability = solved / number
    print_and_write("expanded: {0:.5f} +- {1:.5f}".format(
        expanded_average, expanded_standard_deviation))
    print_and_write("time: {0:.5f} +- {1:.5f}".format(time_average,
                                                      time_standard_deviation))
    print_and_write("solving_probability: " + str(solving_probability))


NUM_TRIALS = 100
NUM_QUEENS = 50

for i in range(1, NUM_QUEENS + 5):
    print_and_write("N: " + str(i))
    problem = NQueensProblem(N=i)
    average_std(problem, hill_climbing_instrumented, NUM_TRIALS)
    average_std(problem, hill_climbing_sideways, NUM_TRIALS)
    average_std(problem, hill_climbing_random_restart, NUM_TRIALS)
    print_and_write("")