def test_check_imperfect_solution(self): n = 4 cols = 10 rows = 5 dg = DataGenerator() some_instance_np_array = np.array( [[1, 10, 1], [2, 10, 2], [1, 10, 3], [5, 10, 4], [1, 10, 1]]) solution_checker = SolutionChecker(n, cols, rows) self.assertEqual( solution_checker.get_reward(some_instance_np_array), (10 * 5) / (cols * rows) )
def test_check_imperfect_solution_count_files_2(self): n = 4 cols = 10 rows = 5 dg = DataGenerator() some_instance_np_array = np.array( [[1, 10, 1], [6, 10, 2], [2, 10, 3], [1, 10, 4], [1, 10, 1]]) solution_checker = SolutionChecker(n, cols, rows) self.assertEqual( solution_checker.get_reward(some_instance_np_array, count_tiles=True), 1 / n )
def test_check_perfect_solution(self): n = 20 w = 40 h = 40 dg = DataGenerator() some_instance_visual = dg.gen_instance_visual(n, w, h) perfect_bin_configuration = sorted(some_instance_visual, key=lambda x: (x[2][0], x[2][1])) some_instance_np_array = dg._transform_instance_visual_to_np_array(some_instance_visual) solution_checker = SolutionChecker(n, h, w) self.assertEqual( solution_checker.get_reward(np.array(perfect_bin_configuration)), 0 )
def test_check_imperfect_solution_count_tiles(self): n = 4 cols = 10 rows = 5 dg = DataGenerator() some_instance_visual = dg.gen_instance_visual(n, cols, rows) # NOTE: first bin always repeated some_instance_np_array = np.array( [[1, 10, 1], [2, 10, 2], [1, 10, 3], [5, 10, 4], [1, 10, 1]]) solution_checker = SolutionChecker(n, cols, rows) self.assertEqual( solution_checker.get_reward(some_instance_np_array, count_tiles=True), 1 / n )
freeze_first_batch=config.freeze_first_batch ) feed = {actor.input_: input_batch} # Get feed dict tour, reward = sess.run([actor.tour, actor.reward], feed_dict=feed) # sample tours j = np.argmin(reward) # find best solution best_permutation = tour[j][:-1] predictions_length.append(reward[j]) solution_checker = SolutionChecker(actor.n, actor.w, actor.h) # TODO: find how this is called in numpy (sort by index) bins = [] for el in best_permutation: bins.append(input_batch[0][el]) solution_checker.get_reward(bins) grid = solution_checker.grid print('reward',reward[j]) solution_checker.visualize_grid() #dataset.visualize_2D_trip(input_batch[0][best_permutation]) #dataset.visualize_sampling(tour) # dataset.visualize_2D_trip(opt_tour) predictions_length = np.asarray(predictions_length) # average tour length predictions_length_w2opt = np.asarray(predictions_length_w2opt) print("Testing COMPLETED ! Mean length1:",np.mean(predictions_length), "Mean length2:",np.mean(predictions_length_w2opt)) n1, bins1, patches1 = plt.hist(predictions_length, 50, facecolor='b', alpha=0.75) # Histogram n2, bins2, patches2 = plt.hist(predictions_length_w2opt, 50, facecolor='g', alpha=0.75) # Histogram