from rubikscubennnsolver.RubiksCube777 import RubiksCube777 cube = RubiksCube777(args.state, args.order, args.colormap, args.debug) elif size % 2 == 0: from rubikscubennnsolver.RubiksCubeNNNEven import RubiksCubeNNNEven cube = RubiksCubeNNNEven(args.state, args.order, args.colormap, args.debug) else: from rubikscubennnsolver.RubiksCubeNNNOdd import RubiksCubeNNNOdd cube = RubiksCubeNNNOdd(args.state, args.order, args.colormap, args.debug) cube.min_memory = args.min_memory cube.sanity_check() cube.print_cube() cube.www_header() cube.www_write_cube("Initial Cube") try: cube.solve() except NotSolving: if cube.heuristic_stats: log.info("%s: heuristic_stats raw\n%s\n\n" % (cube, pformat(cube.heuristic_stats))) for (key, value) in cube.heuristic_stats.items(): cube.heuristic_stats[key] = int(median(value)) log.info("%s: heuristic_stats median\n%s\n\n" % (cube, pformat(cube.heuristic_stats))) sys.exit(0) else:
def getResults(state): if sys.version_info < (3, 4): raise SystemError("Must be using Python 3.4 or higher") logging.basicConfig( level=logging.INFO, format="%(asctime)s %(filename)22s %(levelname)8s: %(message)s") log = logging.getLogger(__name__) log.info("rubiks-cube-solver.py begin") start_time = dt.datetime.now() parser = argparse.ArgumentParser() parser.add_argument("--print-steps", default=False, action="store_true", help="Display animated step-by-step solution") parser.add_argument("--debug", default=False, action="store_true", help="set loglevel to DEBUG") parser.add_argument("--no-comments", default=False, action="store_true", help="No comments in alg.cubing.net url") # CPU mode parser.add_argument( "--min-memory", default=False, action="store_true", help="Load smaller tables to use less memory...takes longer to run", ) parser.add_argument("--fast", default=True, action="store_true", help="Find a solution quickly") parser.add_argument("--normal", default=False, action="store_true", help="Find a shorter solution but takes longer") parser.add_argument("--slow", default=False, action="store_true", help="Find shortest solution we can, takes a while") action = parser.add_mutually_exclusive_group(required=False) parser.add_argument("--openwith", default=None, type=str, help="Colors for sides U, L, etc") parser.add_argument("--colormap", default=None, type=str, help="Colors for sides U, L, etc") parser.add_argument( "--order", type=str, default="URFDLB", help="order of sides in --state, default kociemba URFDLB") parser.add_argument("--solution333", type=str, default=None, help="cube explorer optimal steps for solving 3x3x3") parser.add_argument( "--state", type=str, help="Cube state", # no longer used # parser.add_argument('--test', default=False, action='store_true') # 2x2x2 # default='DLRRFULLDUBFDURDBFBRBLFU') # default='UUUURRRRFFFFDDDDLLLLBBBB') # 3x3x3 # default='RRBBUFBFBRLRRRFRDDURUBFBBRFLUDUDFLLFFLLLLDFBDDDUUBDLUU') # default='UUUUUUUUURRRRRRRRRFFFFFFFFFDDDDDDDDDLLLLLLLLLBBBBBBBBB') # solved # 4x4x4 # default='DRFDFRUFDURDDLLUFLDLLBLULFBUUFRBLBFLLUDDUFRBURBBRBDLLDURFFBBRUFUFDRFURBUDLDBDUFFBUDRRLDRBLFBRRLB') # xyzzy test cube # default='FLDFDLBDFBLFFRRBDRFRRURBRDUBBDLURUDRRBFFBDLUBLUULUFRRFBLDDUULBDBDFLDBLUBFRFUFBDDUBFLLRFLURDULLRU') # TPR cube # default="BRBLLLBRDLBBDDRRFUDFUDUDFUDDDRURBBBUUDRLFRDLLFBRFLRFLFFFBRULDRUBUBBLDBFRDLLUBUDDULFLRRFLFUBFUFUR", ) # default='UUUUUUUUUUUUUUUURRRRRRRRRRRRRRRRFFFFFFFFFFFFFFFFDDDDDDDDDDDDDDDDLLLLLLLLLLLLLLLLBBBBBBBBBBBBBBBB') # solved # 5x5x5 # default='RFFFUDUDURBFULULFDBLRLDUFDBLUBBBDDURLRDRFRUDDBFUFLFURRLDFRRRUBFUUDUFLLBLBBULDDRRUFUUUBUDFFDRFLRBBLRFDLLUUBBRFRFRLLBFRLBRRFRBDLLDDFBLRDLFBBBLBLBDUUFDDD') # https://www.speedsolving.com/forum/threads/arnauds-5x5x5-edge-pairing-method-examples.1447/ # default='LDFRDDUUUUFUUUBLUUUFLDFDRFDDFBBRRRULRRRBFRRRURFRFDUBDRUBFFFUBFFFUUFFFRLDLRFDLBDDLDDDRDDDDUDDDDUULDLFBFLFFULLLRFLLLRLLLLRRBLBBRBULULBBBRUBBBRBBBBULBRFB') # default='UDLFDLDDLUFDUBRLBDLFLRBFRBLBBFUDURDULRRBRLFUURBUFLUBDUDRURRRBUFUFFFRUFFLDUURURFFULFFRLFDBRRFRDDBRFBBLBRDFBBBBUDDLLLDBUULUDULDLDDLBRRLRLUBBFFBDLFBDDLFR') # default='UUUUUUUUUUUUUUUUUUUUUUUUURRRRRRRRRRRRRRRRRRRRRRRRRFFFFFFFFFFFFFFFFFFFFFFFFFDDDDDDDDDDDDDDDDDDDDDDDDDLLLLLLLLLLLLLLLLLLLLLLLLLBBBBBBBBBBBBBBBBBBBBBBBBB') # solved # default='DFFURRULDLDLURLBDDRRBFRURFBFBFRBDLBBFRBLRFBRBBFLULDLBLULLFRUBUFLDFFLDULDDLUURRDRFBRLULUDRBDUUUBBRFFDBDFURDBBDDRULBUDRDLLLBDRFDLRDLLFDBBUFBRURFFUFFUUFU') # step10 takes 2s # default='URUBFUUFRDFFUUFLRDBLLBDDDLUULRDLDUBDLRBBLFLBRBFUUBBRBFFUDLFLLBFUFUDRLBFUBBURRLLRUFRDUFFDFRFUBRBBDRFRFLLFURLLFBRBLUDRDDRRDRRFDUDLFLDLUUDUDBRBBBRBDDLDFL') # step10 takes 9s # default='RFUBLFUBRULLUDDRLRLLFFFLUBDBLBFFUFLFURBFFLDDLFFBBRLUUDRRDLLLRDFFLBBLFURUBULBRLBDRUURDRRDFURDBUUBBFBUDRUBURBRBDLFLBDFBDULLDBBDDDRRFURLDUDUBRDFRFFDFDRLU') # step10 takes 6s, centers take 37 steps :( # 6x6x6 # default='FBDDDFFUDRFBBLFLLURLDLLUFBLRFDUFLBLLFBFLRRBBFDRRDUBUFRBUBRDLUBFDRLBBRLRUFLBRBDUDFFFDBLUDBBLRDFUUDLBBBRRDRUDLBLDFRUDLLFFUUBFBUUFDLRUDUDBRRBBUFFDRRRDBULRRURULFDBRRULDDRUUULBLLFDFRRFDURFFLDUUBRUFDRFUBLDFULFBFDDUDLBLLRBL') # default='UUUUUUUUUUUUUUUU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UDDDBURDRBUBRUBDUBLDLLDLURLDFDBRUBDLDFRRRBRLULFRFLDRLBUBRUBLFBFDFFLFRFDFLBRULLRBLDRBBFURRRDUUULLULLDLBLBBDFBUUUBRRUFFBRUDBFRDFDLFLFFRFFFFRULDFFDFRUBBBRURBUFLBDFBBBBBRRRLFLFBDRRUFLURDDLRRBRLLFURRURBRFLLLFFURBFULFRFFBLDUUUUBDDUFFDRBRLDDFRBULDDDFFRURUFLDRFLDFBLRUFFUBBDFFDBLLDBDUBDLDLUDFBFLRULRRBDBLRBLDLUURRLLRULDBLBLLRRFDDRBBRBUBDDULDRFBFBBFLUFBLUULDDFDBRLLUBUBBDFBBLBBUBLULDRUDBLRULDUDLUFRRDLLUDDBUFLFLBUFUURFDRDLBURLLRRRULRBFFRRBRFBUBRBUUFRLRDRDLBBRFLLLDDBRFUFRBULFLFDRDDRRDBF') args = parser.parse_args() args.state = state if "G" in args.state: args.state = args.state.replace("G", "F") args.state = args.state.replace("Y", "D") args.state = args.state.replace("O", "L") args.state = args.state.replace("W", "U") if args.debug: log.setLevel(logging.DEBUG) try: size = int(sqrt((len(args.state) / 6))) if args.slow: cpu_mode = "slow" elif args.normal: cpu_mode = "normal" elif args.fast: cpu_mode = "fast" else: raise Exception("What CPU mode to use?") if size == 2: # rubiks cube libraries from rubikscubennnsolver.RubiksCube222 import RubiksCube222 cube = RubiksCube222(args.state, args.order, args.colormap, args.debug) elif size == 3: # rubiks cube libraries from rubikscubennnsolver.RubiksCube333 import RubiksCube333 cube = RubiksCube333(args.state, args.order, args.colormap, args.debug) elif size == 4: # rubiks cube libraries from rubikscubennnsolver.RubiksCube444 import RubiksCube444 cube = RubiksCube444(args.state, args.order, args.colormap, avoid_pll=True, debug=args.debug) elif size == 5: # rubiks cube libraries from rubikscubennnsolver.RubiksCube555 import RubiksCube555 cube = RubiksCube555(args.state, args.order, args.colormap, args.debug) elif size == 6: # rubiks cube libraries from rubikscubennnsolver.RubiksCube666 import RubiksCube666 cube = RubiksCube666(args.state, args.order, args.colormap, args.debug) elif size == 7: # rubiks cube libraries from rubikscubennnsolver.RubiksCube777 import RubiksCube777 cube = RubiksCube777(args.state, args.order, args.colormap, args.debug) elif size % 2 == 0: # rubiks cube libraries from rubikscubennnsolver.RubiksCubeNNNEven import RubiksCubeNNNEven cube = RubiksCubeNNNEven(args.state, args.order, args.colormap, args.debug) else: # rubiks cube libraries from rubikscubennnsolver.RubiksCubeNNNOdd import RubiksCubeNNNOdd cube = RubiksCubeNNNOdd(args.state, args.order, args.colormap, args.debug) if args.openwith: cube.print_cube() for step in args.openwith.split(): cube.rotate(step) cube.cpu_mode = cpu_mode log.info("CPU mode %s" % cube.cpu_mode) cube.sanity_check() cube.print_cube() cube.www_header() cube.www_write_cube("Initial Cube") try: if args.solution333: solution333 = reverse_steps(args.solution333.split()) else: solution333 = [] cube.solve(solution333) except NotSolving: if cube.heuristic_stats: log.info("%s: heuristic_stats raw\n%s\n\n" % (cube, pformat(cube.heuristic_stats))) for (key, value) in cube.heuristic_stats.items(): cube.heuristic_stats[key] = int(median(value)) log.info("%s: heuristic_stats median\n%s\n\n" % (cube, pformat(cube.heuristic_stats))) sys.exit(0) else: raise end_time = dt.datetime.now() log.info("Final Cube") cube.print_cube() res = cube.print_solution(not args.no_comments) log.info( "*********************************************************************************" ) log.info( "See /tmp/rubiks-cube-NxNxN-solver/index.html for more detailed solve instructions" ) log.info( "*********************************************************************************\n" ) # Now put the cube back in its initial state and verify the solution solves it solution = cube.solution cube.re_init() len_steps = len(solution) for (i, step) in enumerate(solution): if args.print_steps: print(("Phase : %s" % cube.phase())) print(("Move %d/%d: %s" % (i + 1, len_steps, step))) cube.rotate(step) www_desc = "Phase: %s<br>\nCube After Move %d/%d: %s<br>\n" % ( cube.phase(), i + 1, len_steps, step) cube.www_write_cube(www_desc) if args.print_steps: cube.print_cube() print("\n\n\n\n") cube.www_footer() if args.print_steps: cube.print_cube() if args.min_memory: print("\n\n****************************************") print("--min-memory has been replaced by --fast") print("****************************************\n\n") log.info("rubiks-cube-solver.py end") log.info("Memory : {:,} bytes".format( resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)) log.info("Time : %s" % (end_time - start_time)) log.info("") success = True if not cube.solved(): kociemba_string = cube.get_kociemba_string(False) # edge_swap_count = cube.get_edge_swap_count(edges_paired=True, orbit=None, debug=True) # corner_swap_count = cube.get_corner_swap_count(debug=True) # raise SolveError("cube should be solved but is not, edge parity %d, corner parity %d, kociemba %s" % # (edge_swap_count, corner_swap_count, kociemba_string)) raise SolveError("cube should be solved but is not") except (ImplementThis, SolveError, StuckInALoop, NoSteps, KeyError, NoPruneTableState, InvalidCubeReduction): cube.enable_print_cube = True cube.print_cube_layout() cube.print_cube() res = cube.print_solution(False) success = True print((cube.get_kociemba_string(True))) log.info("rubiks-cube-solver.py end") raise return res, success