Esempio n. 1
0
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
Esempio n. 2
0
def main():
    r = template_class.rubiks_cube()
    r.front()
    r.back()
    r.down_prime()
    r.front_prime()
    rec_solve_master(r, 4, 1)
Esempio n. 3
0
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)
Esempio n. 4
0
def main():
### Continuously Reads inputs  ###	

# Error Checking:
	# Argument Check
	if len(sys.argv) != 2:
		print(usage)
		exit()

	# Help print Statement
	if sys.argv[1].lower() == 'help':
		print(help_arg)
		exit()

	arg = str(sys.argv[1])

	# Supported Cubes	
	if arg.lower().isnumeric():
		i = int(arg)
		# If cube dim within bounds (only 2 right now)
		if i < 2 or i > 2:
			print(unsupp, '\n')
			print(usage)
			exit()
	else:
		print(usage)
		exit()

# Program Start
	# Declare Cube
	cube = rubiks_cube()

	# Print Intro Text
	print(intro)
	
	# Get initial Input
	usr_in = input(enter)
	print()

	# Handle inputs
	while(not usr_in.lower() == 'q' and not usr_in.lower() == 'quit' \
		and not usr_in.lower() == 'exit' and not usr_in.lower() == 'e'): 
	
		# Calls commands for cube
		in_handler(cube, usr_in)
		
		# Gets the input
		usr_in = input(enter)
		print()
	
	print("\n~Exiting interface~")
Esempio n. 5
0
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)
Esempio n. 6
0
def reverse_shuffle(rotations):	
	
	rc    = rubiks_cube()	
	mvs   = []
	names = sh.names_of_moves() 

	for i in range(0, rotations):
		check = random.randint(1, 12)
	
		if (check == 1):
			rc.front()
			mvs.append(0)

		elif (check == 2):
			rc.front_prime()
			mvs.append(1)

		elif (check == 3):	
			rc.back()	
			mvs.append(2)

		elif (check == 4):
			rc.back_prime()
			mvs.append(3)
					
		elif (check == 5):		
			rc.up()
			mvs.append(4)

		elif (check == 6):
			rc.up_prime()		
			mvs.append(5)

		elif (check == 7):
			rc.down()		
			mvs.append(6)

		elif (check == 8):
			rc.down_prime()		
			mvs.append(7)

		elif (check == 9):
			rc.left()		
			mvs.append(8)

		elif (check == 10):
			rc.left_prime()		
			mvs.append(9)

		elif (check == 11):		
			rc.right()
			mvs.append(10)

		elif (check == 12):		
			rc.right_prime()
			mvs.append(11)
	
	j = rotations - 1			
	solution = []

	while (j >= 0):
		# Check if same move has been performed 4 times before
		curr_ind  = mvs[j]
		curr_name = names[ curr_ind ]
		comp_name = sh.counter_move( curr_name ) 
		comp_ind  = names.index( comp_name )

		temp = j - 1
		same_count = 0
		while (temp >= 0 and temp > j - 4):
			if (curr_ind == mvs[temp] ):
				same_count += 1
				temp -= 1
			else:
				break	
	
		# Four consecutive same moves is redudant
		if (same_count == 3):
			j -= 4
			continue
		
		# Three consecutive same moves is compliment move
		elif (same_count == 2):
			solution.append(curr_name)
			j -= 3 

		# Check if next two are consec. compliments
		elif ((same_count == 1 and j > 2) and (mvs[ j - 2 ] == mvs[j - 3]) and (mvs[j - 2] == comp_ind)): 
			j -= 4 

		# If two consecutive moves are compliments
		elif (( j != 0 ) and mvs[j - 1] == comp_ind ):
			if (j == 1):
				break
			else:
				j -= 2
		else:
			solution.append(comp_name)
			j -= 1
	
	return [ sh.flatten_faces(rc), solution, rc ]
Esempio n. 7
0
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
Esempio n. 8
0
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