def solveto(cubestring, goalstring, max_length=20, timeout=3):
    """Solve a cube defined by cubstring to a position defined by goalstring.
     :param cubestring: The format of the string is given in the Facelet class defined in the file enums.py
     :param goalstring: The format of the string is given in the Facelet class defined in the file enums.py
     :param max_length: The function will return if a maneuver of length <= max_length has been found
     :param timeout: If the function times out, the best solution found so far is returned. If there has not been found
     any solution yet the computation continues until a first solution appears.
    """
    fc0 = face.FaceCube()
    fcg = face.FaceCube()
    s = fc0.from_string(cubestring)
    if s != cubie.CUBE_OK:
        return 'first cube ' + s  # no valid cubestring, gives invalid facelet cube
    s = fcg.from_string(goalstring)
    if s != cubie.CUBE_OK:
        return 'second cube ' + s  # no valid goalstring, gives invalid facelet cube
    cc0 = fc0.to_cubie_cube()
    s = cc0.verify()
    if s != cubie.CUBE_OK:
        return 'first cube ' + s  # no valid facelet cube, gives invalid cubie cube
    ccg = fcg.to_cubie_cube()
    s = ccg.verify()
    if s != cubie.CUBE_OK:
        return 'second cube ' + s  # no valid facelet cube, gives invalid cubie cube
    # cc0 * S = ccg  <=> (ccg^-1 * cc0) * S = Id
    cc = cubie.CubieCube()
    ccg.inv_cubie_cube(cc)
    cc.multiply(cc0)

    my_threads = []
    s_time = time.monotonic()

    # these mutable variables are modidified by all six threads
    shortest_length = [999]
    solutions = []
    terminated = thr.Event()
    terminated.clear()
    syms = cc.symmetries()
    if len(list({16, 20, 24, 28} & set(syms))
           ) > 0:  # we have some rotational symmetry along a long diagonal
        tr = [0, 3]  # so we search only one direction and the inverse
    else:
        tr = range(6)  # This means search in 3 directions + inverse cube
    if len(list(set(range(48, 96)) & set(syms))
           ) > 0:  # we have some antisymmetry so we do not search the inverses
        tr = list(filter(lambda x: x < 3, tr))
    for i in tr:
        th = SolverThread(cc, i % 3, i // 3, max_length, timeout, s_time,
                          solutions, terminated, shortest_length)
        my_threads.append(th)
        th.start()
    for t in my_threads:
        t.join()  # wait until all threads have finished
    s = ''
    if len(solutions) > 0:
        for m in solutions[-1]:  # the last solution is the shortest
            s += m.name + ' '
    return s + '(' + str(len(s) // 3) + 'f)'
Example #2
0
def solve(cubestring):
    """Solves a 2x2x2 cube defined by its cube definition string.
     :param cubestring: The format of the string is given in the Facelet class defined in the file enums.py
     :return A list of all optimal solving maneuvers
    """
    global solutions
    fc = face.FaceCube()
    s = fc.from_string(
        cubestring
    )  #####################################################################################
    if s is not True:
        return s  # Error in facelet cube
    cc = fc.to_cubie_cube()
    s = cc.verify()
    if s != cubie.CUBE_OK:
        return s  # Error in cubie cube

    solutions = []
    co_cube = coord.CoordCube(cc)

    togo = pr.corner_depth[N_TWIST * co_cube.cornperm + co_cube.corntwist]
    search(co_cube.cornperm, co_cube.corntwist, [], togo)

    s = ''
    for i in range(
            len(solutions)
    ):  # use  range(min(len(solutions), 1)) if you want to return only a single solution
        ps = ''
        for m in solutions[i]:
            ps += m.name + ' '
        ps += '(' + str(len(ps) // 3) + 'f)\r\n'
        s += ps
    return s
 def to_facelet_cube(self):
     """Returns a facelet representation of the cube."""
     fc = face.FaceCube()
     for i in Co:
         j = self.cp[i]  # corner j is at corner position i
         ori = self.co[i]  # orientation of C j at position i
         for k in range(3):
             fc.f[cornerFacelet[i][(k + ori) % 3]] = cornerColor[j][k]
     return fc
Example #4
0
def solve(cubestring, max_length=20, timeout=3):
    """Solve a cube defined by its cube definition string.
     :param cubestring: The format of the string is given in the Facelet class defined in the file enums.py
     :param max_length: The function will return if a maneuver of length <= max_length has been found
     :param timeout: If the function times out, the best solution found so far is returned. If there has not been found
     any solution yet the computation continues until a first solution appears.
    """
    begin_time = time.time()

    fc = face.FaceCube()
    s = fc.from_string(cubestring)
    if s != cubie.CUBE_OK:
        return s  # Error in facelet cube
    cc = fc.to_cubie_cube()
    s = cc.verify()
    if s != cubie.CUBE_OK:
        return s  # Error in cubie cube

    print("Cube feature:\n", cc.to_one_hot(), "\n")

    my_threads = []
    s_time = time.monotonic()

    # these mutable variables are modidified by all six threads
    # s_length = [999]
    solutions = []
    terminated = thr.Event()
    terminated.clear()
    syms = cc.symmetries()
    # we have some rotational symmetry along a long diagonal
    if len(list({16, 20, 24, 28} & set(syms))) > 0:
        tr = [0, 3]  # so we search only one direction and the inverse
    else:
        tr = range(6)  # This means search in 3 directions + inverse cube
    # we have some antisymmetry so we do not search the inverses
    if len(list(set(range(48, 96)) & set(syms))) > 0:
        tr = list(filter(lambda x: x < 3, tr))
    for i in tr:
        th = SolverThread(cc, i % 3, i // 3, max_length, timeout, s_time,
                          solutions, terminated, [999])
        my_threads.append(th)
        th.start()
    for t in my_threads:
        t.join()  # wait until all threads have finished

    end_time = time.time()

    s = ''
    if len(solutions) > 0:
        for m in solutions[-1]:  # the last solution is the shortest
            s += m.name + ' '
    return s + '(' + str(
        len(s) // 3) + 'f:{:.5f}s)'.format(end_time - begin_time)
 def to_facelet_cube(self):
     """Return a facelet representation of the cube."""
     fc = face.FaceCube()
     for i in Co:
         j = self.cp[i]  # corner j is at corner position i
         ori = self.co[i]  # orientation of C j at position i
         for k in range(3):
             fc.f[cornerFacelet[i][(k + ori) % 3]] = cornerColor[j][k]
     for i in Ed:
         j = self.ep[i]  # similar for Es
         ori = self.eo[i]
         for k in range(2):
             fc.f[edgeFacelet[i][(k + ori) % 2]] = edgeColor[j][k]
     return fc
Example #6
0
    def parse_file(self, file_path):
        with open(file_path, 'r') as f:
            lines = f.readlines()
        assert (len(lines) % 3 == 0)
        for i in range(0, len(lines), 3):
            fc = face.FaceCube()
            fc.from_string(lines[i][:-1])
            cc = fc.to_cubie_cube()
            inp = cc.to_one_hot().flatten()
            po = np.zeros(18)
            po[int(lines[i + 1])] = 1
            # po = int(lines[i + 1])
            val = 1 / float(lines[i + 2])

            self.data.append([inp, po, val])