# Authors: Liuqing Chen, Feng Shi, # and Isaac Engel (13th September 2017) # Last updated: 2nd December 2017 # #################################################### from main import Tetris import utils import timeit from copy import deepcopy # Example target shape # target = [[1, 0, 1, 1], [1, 1, 1, 1], [0, 1, 1, 1], [1, 1, 0, 0]] # NOTE: in your test, you may not use this example. # Uncomment the following line to generate a random target shape target = utils.generate_target( width=100, height=100, density=0.6) # NOTE: it is recommended to keep density below 0.8 solution = Tetris(deepcopy(target)) valid, missing, excess, error_pieces = utils.check_solution( target, solution) # checks if the solution is valid if not valid: print("The solution is not valid!") else: # if the solution is valid, test time performance and accuracy # TIME PERFORMANCE # There will be three different values of the parameter 'target' with increasing complexity in real test.
# #################################################### import improved #from UsingLimits import Tetris #from Anusha import Tetris from improved import Tetris import utils import timeit #from ComparisonTest import CompareTarget # Example target shape #target = utils.generate_target(20,20,0.7) # NOTE: in your test, you may not use this example. #print(target) # Uncomment the following line to generate a random target shape target = utils.generate_target(1000,1000,0.7) # NOTE: it is recommended to keep density below 0.8 #target = [[0, 0, 1, 1, 1, 1, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0], [0, 1, 1, 1, 1, 0, 1, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 1, 0], [0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 0, 1, 1], [1, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 0], [0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [1, 1, 1, 1, 1, 1, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1], [1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1], [0, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1, 0, 0, 1, 0], [1, 1, 1, 1, 1, 1, 1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solution) valid, missing, excess, error_pieces = utils.check_solution(target, solution) # checks if the solution is valid #CompareTarget(target) if not valid: print("The solution is not valid!")
[0, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 0] ] perfect_solution = [ [(0, 0), (0, 0), (8, 1), (0, 0), (0, 0), (0, 0)], [(0, 0), (0, 0), (8, 1), (0, 0), (13, 2), (0, 0)], [(0, 0), (8, 1), (8, 1), (13, 2), (13, 2), (13, 2)], [(0, 0), (13, 3), (18, 4), (18, 4), (0, 0), (0, 0)], [(13, 3), (13, 3), (13, 3), (18, 4), (18, 4), (0, 0)] ] """ # NOTE: This example is used for the mock solution from 'main.py' only. # Uncomment the following line to generate a random target shape target, perfect_solution = utils.generate_target(width=100, height=100, density=0.7, forbidden_pieces=the_forbidden_pieces) # NOTE: it is recommended to keep density below 0.8 solution = Tetris(deepcopy(target)) #solution = perfect_solution valid, missing, excess, error_pieces = utils.check_solution(target, solution, the_forbidden_pieces) # checks if the solution is valid if not valid or len(error_pieces)!=0: if len(error_pieces) != 0: print('WARNING: {} pieces have a wrong shapeID. They are labelled in image of the solution, and their PieceID are: {}.' .format(len(error_pieces), error_pieces)) print("Displaying solution...") utils.visual_perfect(perfect_solution, solution, the_forbidden_pieces) print("WARNING: The solution is not valid, no score will be given!") else: # if the solution is valid, test time performance and accuracy # TIME PERFORMANCE
#debugGrid(lambda x : x.stateid) ############################ #emptying the grid solution = [] for y, row in enumerate(ogrid): solution.append([]) for sq in row: if sq.state == 0: solution[y].append((0, 0)) elif sq.state == -1: solution[y].append((0, 0)) else: solution[y].append((sq.state, sq.stateid)) return solution #utils.visualisation(target,solution) width = 20 height = 25 dencity = 0.8 target, a, TheSolution = utils.generate_target(width, height, dencity) solution2 = Tetris(target, a) valid, missing, excess, error_pieces, use_diff = utils.check_solution( target, solution2, a) total_blocks = sum([sum(row) for row in target]) percent = (missing + excess) / total_blocks print(1 - percent) print(a)
16: 0, 17: 0, 18: 1, 19: 0 } perfect_solution = [[(0, 0), (0, 0), (8, 1), (0, 0), (0, 0)], [(0, 0), (0, 0), (8, 1), (1, 2), (1, 2)], [(0, 0), (8, 1), (8, 1), (1, 2), (1, 2)], [(0, 0), (13, 3), (18, 4), (18, 4), (0, 0)], [(13, 3), (13, 3), (13, 3), (18, 4), (18, 4)]] # NOTE: This example is used for the mock solution from 'main.py' only. # Uncomment the following line to generate a random target shape target, limit_tetris, perfect_solution = utils.generate_target( width=20, height=20, density=0.8) # NOTE: it is recommended to keep density below 0.8 solution = Tetris(deepcopy(target), deepcopy(limit_tetris)) valid, missing, excess, error_pieces, use_diff = utils.check_solution( target, solution, limit_tetris) # checks if the solution is valid if not valid or len(error_pieces) != 0: if len(error_pieces) != 0: print( 'WARNING: {} pieces have a wrong shapeID. They are labelled in image of the solution, and their PieceID are: {}.' .format(len(error_pieces), error_pieces)) print("Displaying solution...") utils.visual_perfect(perfect_solution, solution) print("WARNING: The solution is not valid, no score will be given!")
#generate_shape(shape_id) # Co ordingate are done backwards y,x # # # from copy import deepcopy # copy 'target' to avoid modifying it import utils # it might be helpful to use 'utils.py' from operator import add import timeit ######################################## #Varible Bin!!!! width = 1000 height = 1000 dencity = 0.7 target, testParts, TheSolution = utils.generate_target(width, height, dencity) orderlist = "?" # ok so now this may be usefull ###################### #Running varibles shapesPlaced = 1 ##################################### #test testgrid = [[0, 1, 1, 0, 1, 0, 1, 0, 0, 1], [0, 1, 1, 0, 1, 0, 1, 1, 1, 1], [0, 1, 0, 1, 1, 1, 1, 1, 1, 1], [0, 1, 1, 1, 1, 0, 1, 1, 1, 1], [0, 1, 1, 0, 1, 1, 1, 0, 1, 0], [0, 0, 1, 1, 0, 1, 1, 1, 0, 0], [1, 1, 1, 1, 1, 1, 0, 1, 1, 0], [1, 1, 0, 1, 1, 0, 1, 1, 0, 1], [1, 1, 1, 0, 1, 1, 1, 0, 1, 1], [1, 0, 0, 1, 1, 1, 1, 0, 1, 0]] #testParts = {1: 4, 2: 0, 3: 1, 4: 1, 5: 1, 6: 0, 7: 1, 8: 1, 9: 1, 10: 0, 11: 0, 12: 2, 13: 0, 14: 1, 15: 0, 16: 1, 17: 1, 18: 1, 19: 1}
actions_dict[a.split()[1]] = int(a.split()[0]) num_classes = len(actions_dict) # train trainer = Trainer(num_stages, num_layers, num_f_maps, features_dim, num_classes) no_change = 1 if args.action == "train": batch_gen = BatchGenerator(num_classes, actions_dict, segmentation_path, features_path, sample_rate) batch_gen.read_data(vid_list_file) weights = batch_gen.set_class_weights() trainer.ce(weight=weights) while (no_change): trainer.train(model_dir, batch_gen, num_epochs=num_epochs, batch_size=bz, learning_rate=lr, device=device) trainer.predict(model_dir, temp_results_dir, features_path, vid_list_file, num_epochs, actions_dict, device, sample_rate) utils.generate_target(segmentation_path, temp_results_dir, vid_list_file) if args.action == "predict": trainer.predict(model_dir, results_dir, features_path, vid_list_file_tst, num_epochs, actions_dict, device, sample_rate)
#limit_tetris = {1: 1, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 1, 9: 0, 10: 0, 11: 0, 12: 0, 13: 1, 14: 0, 15: 0, 16: 0, 17: 0, 18: 1, 19: 0} # #perfect_solution = [ # [(0, 0), (0, 0), (8, 1), (0, 0), (0, 0)], # [(0, 0), (0, 0), (8, 1), (1, 2), (1, 2)], # [(0, 0), (8, 1), (8, 1), (1, 2), (1, 2)], # [(0, 0), (13, 3), (18, 4), (18, 4), (0, 0)], # [(13, 3), (13, 3), (13, 3), (18, 4), (18, 4)] # ] # NOTE: This example is used for the mock solution from 'main.py' only. # Uncomment the following line to generate a random target shape #target, limit_tetris, perfect_solution = utils.generate_target(width=8, height=8, density=0.5) # NOTE: it is recommended to keep density below 0.8 target, limit_tetris, perfect_solution = utils.generate_target( cols, rows, density) solution = Tetris(deepcopy(target), deepcopy(limit_tetris)) valid, missing, excess, error_pieces, use_diff = utils.check_solution( target, solution, limit_tetris) # checks if the solution is valid if not valid or len(error_pieces) != 0: if len(error_pieces) != 0: print( 'WARNING: {} pieces have a wrong shapeID. They are labelled in image of the solution, and their PieceID are: {}.' .format(len(error_pieces), error_pieces)) print("Displaying solution...") utils.visual_perfect(perfect_solution, solution) print("WARNING: The solution is not valid, no score will be given!") else: # if the solution is valid, test time performance and accuracy
else: # if the solution is valid, test time performance and accuracy # TIME PERFORMANCE # There will be three different values of the parameter 'target' with increasing complexity in real test. # ACCURACY if len(error_pieces) != 0: raise TypeError('Wrong shape') total_blocks = sum([sum(row) for row in target]) total_error = (100 * missing / total_blocks) + (100 * excess / total_blocks) print('total error: {}'.format(total_error)) return total_error e_main = 0 for n in range(0, 100): #sample = Partial_samples(10, 10, 1, 'Three') #sample.fill_square() #T = sample.T.tolist() T = utils.generate_target(width=50, height=50, density=0.6) e_main += test(T, main.Tetris) print('----------- For solvable samples --------------') print('avg e_main: {}'.format(e_main / 100))