def solve_problem(file_name): rectangles = read_file(file_name) clone = rectangles[:] # clone so that original info retained # get student solution and measure tie start = time.time() upper_left_coordinates = bin_packing.find_solution(clone) time_elapsed = time.time() - start # convert student solution to show upper left and lower right coordinates rectangle_coordinates = corner_coordinates(rectangles, upper_left_coordinates) # get a solution using the naive method naive_left_coordinates = find_naive_solution(rectangles) naive_rectangle_coordinates = corner_coordinates(rectangles, naive_left_coordinates) naive_perimeter = evaluate_solution(naive_rectangle_coordinates) if is_solution_valid (rectangle_coordinates): # is student solution valid? perimeter = evaluate_solution(rectangle_coordinates) if time_elapsed > 5.0: # is student solution fast enough? print("Error. Time Limit Exceeded.") perimeter = 0 # answer is penalized else: print("Error. Overlapping Rectangles in Solution.") perimeter = 0 # answer is penalized improvement = naive_perimeter / perimeter return improvement
def solve_problem(file_name): rectangles = read_file(file_name) clone = rectangles[:] # clone so that original info retained # get student solution and measure tie start = time.time() upper_left_coordinates = bin_packing.find_solution(clone) time_elapsed = time.time() - start print("Time elapsed in seconds =", time_elapsed) # convert student solution to show upper left and lower right coordinates rectangle_coordinates = corner_coordinates(rectangles, upper_left_coordinates) # get a solution using the naive method naive_left_coordinates = find_naive_solution(rectangles) naive_rectangle_coordinates = corner_coordinates(rectangles, naive_left_coordinates) naive_perimeter = evaluate_solution(naive_rectangle_coordinates) print("Bounding Rectangle Perimeter of Naive Solution =", naive_perimeter) if is_solution_valid (rectangle_coordinates): # is student solution valid? perimeter = evaluate_solution(rectangle_coordinates) print("Bounding Rectangle Perimeter of Your Solution =", perimeter) if time_elapsed > 5.0: # is student solution fast enough? print("Error. Time Limit Exceeded.") perimeter = 2 * naive_perimeter # answer is penalized else: print("Error. Overlapping Rectangles in Solution.") perimeter = 2 * naive_perimeter # answer is penalized print("Percentage Improvement Over Naive Solution =", 100 - (perimeter / naive_perimeter) * 100)
def solve_problem(file_name): rectangles = read_file(file_name) clone = rectangles[:] # clone so that original info retained # get student solution and measure tie start = time.time() upper_left_coordinates = bin_packing.find_solution(clone) time_elapsed = time.time() - start # convert student solution to show upper left and lower right coordinates rectangle_coordinates = corner_coordinates(rectangles, upper_left_coordinates) # get a solution using the naive method naive_left_coordinates = find_naive_solution(rectangles) naive_rectangle_coordinates = corner_coordinates(rectangles, naive_left_coordinates) naive_perimeter = evaluate_solution(naive_rectangle_coordinates) if is_solution_valid(rectangle_coordinates): # is student solution valid? perimeter = evaluate_solution(rectangle_coordinates) if time_elapsed > 5.0: # is student solution fast enough? print("Error. Time Limit Exceeded.") perimeter = 0 # answer is penalized else: print("Error. Overlapping Rectangles in Solution.") perimeter = 0 # answer is penalized improvement = naive_perimeter / perimeter return improvement