def main(f_name, rot, snum): sol = [] r = rubiks_cube() with open("cnn_solver/" + f_name, 'w', newline = '') as file: writer = csv.writer(file) writer.writerow( ['state', 'move'] ) for i in range(0, snum): state, sol, r = reverse_shuffle(rot) while ( len(sol) != 0 ) : s_ind = sol.pop(0) s_ind_str = sh.convert_sol(s_ind, into_num = 1 ) state = sh.flatten_faces(r) into = [ state, s_ind_str ] writer.writerow( into ) sh.get_move( r, s_ind )() state = sh.flatten_faces(r) # Gets the solved state into = [ state, 12 ] # Inputs '0' for move on solved state writer.writerow( into ) r.reset() # Reset cube for good measure
def test_obo(mod_name, num_shuf): model = keras.models.load_model("cnn_solver/models/" + mod_name) rc = rubiks_cube() rc.shuffle(num_shuf) i = 0 while (rc.if_solved() == 0): rc_flat = sh.flatten_faces(rc) # into string rc_np = sh.two_dim_data(rc_flat) # np array rc_np = np.array(rc_np).reshape((1, 2, 12, 1)) # np reshape out = model.predict_classes(rc_np) out_p = sh.mv_num_to_char(int(out[0])) print(out_p) # Print out prediction mv = sh.get_move(rc, out[0]) # Get the callable fn move mv() # Make the move i += 1 if (i > 20): print("No solve") break rc_flat = sh.flatten_faces(rc) # into string print(rc_flat)
def solve_back(sol, rc): for i in range(0, len(sol)): temp = sh.get_move(rc, sol[i]) temp() print(sh.flatten_faces(rc)) return rc
def main(): #create rubiks_cube object cube = rubiks_cube() mod_name = input("What model do you want to use? ") shuf_num = input("How many shuffles? ") cube.shuffle(int(shuf_num)) model = keras.models.load_model("cnn_solver/models/" + mod_name) last_move = -1 #keeps track of last move done rand_check = 0 #makes sure we don't keep calling random continuously repeat_same_move = 0 #keeps track of repeating the same move count = 0 #keeps track of all moves done mvs = names_of_moves() #used for conversion to letter for comparison while(cube.if_solved() == 0) : #model reads in data and makes prediction flat = flatten_faces(cube) data = two_dim_data(flat) out = model.predict_classes(data.reshape(1,2,12,1)) #if this move is the counter of the last move (model is repeating itself) or if model has repeated same move 4 times if((mvs[last_move] == counter_move(mvs[out[0]]) and rand_check != 1 and last_move != -1) or (repeat_same_move == 4)): #perform a random move on cube ranm = random_move(cube) ranm_out = mv_num_to_char(ranm) p_out = mv_num_to_char(int(out[0])) print(p_out), print("Called random move:", ranm_out) #rand_check makes sure we don't call random check multiple times in a row rand_check = 1 #reset counter for repeated moves repeat_same_move = 0 else: #perform model's predicted move p_out = mv_num_to_char(int(out[0])) print(p_out) mv = get_move(cube, out[0]) mv() #reset counter for repeated moves to 0 if the last move was different than current move if(out[0] != last_move) : repeat_same_move = 0 else: repeat_same_move+=1 #keep track of last move last_move = out[0] rand_check = 0 #while loop ends if we have done 50 moves count+=1 if(count == 50) : print("No solve (in less than 50 moves)") break final_state = flatten_faces(cube) print(final_state)
def ml_test_random(model_name, f_name, ub=20, each=100, max_allow=20): model = keras.models.load_model("cnn_solver/models/" + model_name) rc = rubiks_cube() mvs = sh.names_of_moves() # used for conversion to letter for comparison with open(f_name, 'w', newline='') as file: writer = csv.writer(file) writer.writerow(['rotations', 'solve_prop', 'solve_rotations']) for i in range(1, ub + 1): # 1 - ub rotations solve_sum = 0 # Reset sum variables before another iteration rot_sum = 0 for k in range(0, each): # Counts the number of times rc.shuffle(i) solve = 0 # Checks if we have a solve or not j = 0 # Keeps track of all moves done last_move = -1 # Keeps track of last move done rand_check = 0 # Makes sure we don't keep calling random repeat_same_move = 0 # Keeps track of repeating the same move while (rc.if_solved() == 0): rc_flat = sh.flatten_faces(rc) # into string rc_np = sh.two_dim_data(rc_flat) # np array rc_np = np.array(rc_np).reshape( (1, 2, 12, 1)) # np reshape out = model.predict_classes(rc_np) # prediction from model if (out[0] == 12): if (rc.if_solved() == 1): solve = 1 break else: solve = 0 break if ((mvs[last_move] == sh.counter_move(mvs[out[0]]) and rand_check != 1 and last_move != -1) or (repeat_same_move == 4)): ranm = random_move(cube) # Calls random move rand_check = 1 repeat_same_move = 0 else: if (out[0] != 12): #Get the callable function move mv = sh.get_move(rc, out[0]) mv() # Make the move if (out[0] != last_move): # Reset counter if different move repeat_same_move = 0 else: repeat_same_move += 1 j += 1 # If we solve the cube if (rc.if_solved() == 1): solve = 1 break # If we reach the maximum number of rotations allowed for the model if (j > max_allow): solve = 0 break if (solve == 1): # If the model solved the cube solve_sum += 1 rot_sum += j rc.reset() # Reset the cube before next trial progress = 100 * ((k + (each * (i - 1))) / (each * ub)) print("Progres:", progress, "%") solve_prop = (solve_sum) / ( each) # Proportion of times the model solved the cube if (solve_sum > 0): rot_prop = (rot_sum) / ( solve_sum) # Number of rotations per each solve trial else: rot_prop = 0 r = [i, solve_prop, rot_prop] # Input into the csv file writer.writerow(r) # Write to csv file
def ml_test(model_name, f_name, ub=20, each=100, max_allow=20): model = keras.models.load_model("cnn_solver/models/" + model_name) rc = rubiks_cube() with open(f_name, 'w', newline='') as file: writer = csv.writer(file) writer.writerow(['rotations', 'solve_prop', 'solve_rotations']) for i in range(1, ub + 1): # 1 - ub rotations solve_sum = 0 # Reset sum variables before another iteration rot_sum = 0 for k in range(0, each): # Counts the number of times rc.shuffle(i) solve = 0 j = 0 while (rc.if_solved() == 0): # If we reach the maximum number of rotations allowed for the model if (j > max_allow): solve = 0 break rc_flat = sh.flatten_faces(rc) # into string rc_np = sh.two_dim_data(rc_flat) # np array rc_np = np.array(rc_np).reshape( (1, 2, 12, 1)) # np reshape out = model.predict_classes(rc_np) # prediction from model if (out[0] != 12): mv = sh.get_move(rc, out[0]) # Get the callable fn move mv() # Make the move j += 1 # If we solve the cube if (rc.if_solved() == 1): solve = 1 break if (solve == 1): # If the model solved the cube solve_sum += 1 rot_sum += j rc.reset() # Reset the cube before next trial progress = 100 * ((k + (each * (i - 1))) / (each * ub)) print("Progres:", progress, "%") solve_prop = (solve_sum) / ( each) # Proportion of times the model solved the cube if (solve_sum > 0): rot_prop = (rot_sum) / ( solve_sum) # Number of rotations per each solve trial else: rot_prop = 0 r = [i, solve_prop, rot_prop] # Input into the csv file writer.writerow(r) # Write to csv file