def main(): # test_shuffle() # test_shuffle_elimination(directions=[F, L, L, L, L, F]) # test_shuffle_elimination(directions=[F, L, L, L, L]) # test_shuffle_elimination(directions=[L, L, L, L]) # test_shuffle_elimination(directions=[L, L, L, L, F]) # test_shuffle_elimination(directions=[F, B, L, L, L, L, L, R]) # test_shuffle_elimination(directions=[F, B, L, L_, B_]) # for i in range(0, 100000): # print i # validate_shuffle_elimination() cube = Cube() game = Game(cube) game.shuffle(min_step=3, max_step=3) game.export_to_file(file_path='./sample_file.dat') print 'Full step' game.print_shuffle_full_steps() print 'Step' game.print_shuffle_steps() with open('./sample_file.dat', 'r') as fp: is_eof = False while True: step_sample = [] for i in range(0, CUBE_DATA_LEN): data = fp.read(4) if '' == data: is_eof = True else: step_sample.append(data) if is_eof: break print create_Sample_from_buffer(step_sample)
def generate_shuffle_bayesian(): for i in range(0, 10000): c = Cube() game = Game(c) game.shuffle(min_step=10, max_step=1024) for step in game.shuffle_steps: bayesian.add_feature_and_decision(step.feature_string, DIRECTIONS[-step.direction + DIRECTION_OFFSET]) if i % 100 == 0: print i bayesian.export('bayesiantest.yaml')
def test_shuffle_elimination(directions): cube = Cube() game = Game(cube) game.shuffle(directions=directions) print '============= FULL STEPS ==============' game.print_shuffle_full_steps() print '============= ELIMINATED STEPS ==============' game.print_shuffle_steps()
def test_shuffle(): cube = Cube() game = Game(cube) game.shuffle() step = len(game.shuffle_full_steps) print '=========== initialized with %s-step shuffle ============' % step cube.show() print '' print 'Feature string is ', cube.calculate_feature_string() print 'Is standard? ', cube.check() command = raw_input("Command: ").upper() while command != 'Q': if command not in command_map: print("Invalid input. Input again.") continue cube.transform(command_map[command]) print '=========== %s ============' % command print '' cube.show() print '' command = raw_input("Command: ").upper()
def generate_file(file_path, data_num, min_step, max_step): with open(file_path, 'w') as fp: for i in range(0, data_num): c = Cube() game = Game(c) game.shuffle(min_step=min_step, max_step=max_step) game.export_to_file_point(fp) print '%s generate %i' % (file_path, i)
def validate_shuffle_elimination(): cube = Cube() game = Game(cube) game.shuffle() # print '============= FULL STEPS ==============' # game.print_shuffle_full_steps() # print '============= ELIMINATED STEPS ==============' # game.print_shuffle_steps() print 'Full steps: ', len(game.shuffle_full_steps) print 'Steps: ', len(game.shuffle_steps) directions = [-step.direction for step in game.shuffle_steps] directions.reverse() # print [DIRECTIONS[direction + DIRECTION_OFFSET] for direction in directions] game.play(directions) assert game.cube.check() print 'Validation PASS!' return True
def test_train_and_solve(): cube = Cube() game = Game(cube) game.shuffle() step = len(game.shuffle_full_steps) print '=========== initialized with %s-step shuffle ============' % step cube.show() print 'Solving . . . . . . ' game.solve() cube.show() if game.is_solved: print 'Congratulations!!!! Solved after %s steps!' % len( game.solve_steps) print( 'Steps: %s' % ''.join([ DIRECTIONS[step.direction + DIRECTION_OFFSET] for step in game.solve_steps ])) else: print 'Stupid!!!! Cannot solve a cube in %s steps!' % len( game.solve_steps)
def test_shuffle_bayesian(): cube = Cube() game = Game(cube) game.shuffle(min_step=3, max_step=100) for step in game.shuffle_steps: bayesian.add_feature_and_decision(step.feature_string, DIRECTIONS[-step.direction + DIRECTION_OFFSET]) cube = Cube() game = Game(cube) game.shuffle(min_step=3, max_step=100) for step in game.shuffle_steps: bayesian.add_feature_and_decision(step.feature_string, DIRECTIONS[-step.direction + DIRECTION_OFFSET]) cube = Cube() game = Game(cube) game.shuffle(min_step=3, max_step=100) for step in game.shuffle_steps: bayesian.add_feature_and_decision(step.feature_string, DIRECTIONS[-step.direction + DIRECTION_OFFSET]) print_possibility() bayesian.export('bayesiantest.yaml') bayesian.load('bayesiantest.yaml') print_possibility()