예제 #1
0
def find_median(array, left, right):
    middle = left + ((right - left)/2)
    elements = [(array[x], x) for x in set([left, middle, right])]
    if len(elements) == 3:
        # 'true' median is the middle element
        median, index = sorted(elements)[1]
    else:
        # no median found, use minimum as pivot
        median, index = min(elements)
    # avoid overhead, skip debug logging
    if log.level == logging.DEBUG:
        debug_message = []
        for x, y in elements:
            is_median = median == x
            msg = utils.array2str(array, y, mark=is_median, spacing=8)
            debug_message.append(msg)
        log.debug("\n" + "\n".join(debug_message))
    return (median, index)
예제 #2
0
def find_pivot(array, left, right):
    array_name = "pivot"
    pivot = None
    if __question__ == 1:
        # Question 2.1 (pivot = first element)
        pivot = left
    elif __question__ == 2:
        # Question 2.2 (pivot = final element)
        pivot = right
    elif __question__ == 3:
        # Question 2.3 (Median of 3; left/right)
        median, pivot = find_median(array, left, right)
    
    if pivot is None or pivot < 0:
        raise Exception("Failed to find pivot")
    log.debug(utils.array2str(array, pivot, array_name))
    swap(array, left, pivot)
    return array[left]
예제 #3
0
    for p in problems:
        start = time.clock()

        initial_state = shape(p)
        s = Sudoku(initial_state)
        s.random_fill()
        final_state, steps, score = heuristics_options[args.heuristic](args, s)
        x.append(steps)
        y.append(score)

        tclock = time.clock() - start
        if args.verbose:
            print("execution time:", tclock)

        if args.heuristic == "sa":
            data.append((p, args.heuristic, array2str(final_state.flatten()),
                         s.cost_function(final_state), len(steps), tclock))
        else:
            data.append((p, args.heuristic, array2str(final_state.flatten()),
                         score, steps, tclock))

    output_csv(args, data)

    plots_options = {
        "hc": plot_2D_grid_hc,
        "hcr": plot_2D_grid_hcr,
        "sa": plot_2D_grid_sa
    }

    plots_options[args.heuristic](x, y, args.img_output_dir)