def phase_search(phase, puzzle_arr, depth):
    global path
    if depth == 0:
        if distance(puzzle_arr, phase) == 0:
            return True
    else:
        if distance(puzzle_arr, phase) <= depth:
            l_twist = path[-1] if len(path) >= 1 else -10
            ll_twist = path[-2] if len(path) >= 2 else -10
            for twist in successor[phase]:
                if face(twist) == face(l_twist) or axis(twist) == axis(
                        l_twist) == axis(ll_twist):
                    continue
                n_puzzle_arr = twist_arr(phase, puzzle_arr, twist)
                path.append(twist)
                if phase_search(phase, n_puzzle_arr, depth - 1):
                    return True
                path.pop()
示例#2
0
文件: solver.py 项目: Nyanyan/MLolver
def solver(puzzle):
    global path
    res = []
    que = []
    dis_first = distance(puzzle)
    heapify(que)
    heappush(que, [dis_first, [], puzzle])
    weight = 1
    offset = 2
    cnt = 0
    while que:
        cnt += 1
        if cnt % 10 == 0:
            pass  #print(cnt)
        dis, path, puz = heappop(que)
        dis_int = int(round(dis))
        l = len(path)
        for _ in range(dis_int):
            print('=', end='')
        for _ in range(20 - dis_int):
            print('.', end='')
        print(l, dis)
        if dis == 0:
            return path
        l0_twist = path[-1] if len(path) >= 1 else -10
        l1_twist = path[-2] if len(path) >= 2 else -10
        twist = 0
        while twist < 18:
            if face(twist) == face(l0_twist):
                twist += 1
                continue
            if axis(twist) == axis(l0_twist) == axis(l1_twist):
                twist = skip_axis[twist]
                continue
            n_puz = puz.move(twist)
            n_dis = distance(n_puz)
            if dis_first + offset < n_dis + l * weight:
                twist = skip_axis[twist]
                continue
            n_path = [i for i in path]
            n_path.append(twist)
            heappush(que, [n_dis, n_path, n_puz])
            twist += 1
    return -1
示例#3
0
文件: main.py 项目: Nyanyan/Solvour
def robotize(solution, rpm=200):
    regrip_rpm = 250
    robot_solution = []
    for twist in solution:
        amount = (twist % 3 + 1) * 90
        wide_flag = wide(twist)
        axis_arr = [1, 3, 0, 2, 0, 2]
        move_arm = axis_arr[twist // 6]
        if axis(twist) == 1:  # U or D
            robot_solution.append([0, 4000])
            robot_solution.append([2, 4000])
            robot_solution.append([3, 90, regrip_rpm])
            robot_solution.append([1, -90, regrip_rpm])
            robot_solution.append([0, 1000])
            robot_solution.append([1, 1000])
            robot_solution.append([2, 1000])
            robot_solution.append([3, 1000])
        grab_arms = [move_arm % 2, move_arm % 2 + 2]
        if wide_flag:
            for i in grab_arms:
                robot_solution.append([i, 2000])
        else:
            robot_solution.append([move_arm, 3000])
        release_arms = [(move_arm + 1) % 2, (move_arm + 1) % 2 + 2]
        for i in release_arms:
            robot_solution.append([i, 4000])
        robot_solution.append([move_arm, amount, rpm])
        robot_solution.append([move_arm, 1000])
        if wide_flag:
            robot_solution.append([(move_arm + 2) % 4, 1000])
        for i in release_arms:
            robot_solution.append([i, 1000])
        if axis(twist) == 1:  # U or D
            robot_solution.append([0, 4000])
            robot_solution.append([2, 4000])
            robot_solution.append([1, 90, regrip_rpm])
            robot_solution.append([3, -90, regrip_rpm])
            robot_solution.append([0, 1000])
            robot_solution.append([2, 1000])
    return robot_solution
示例#4
0
'''

print('phase1 CP')
prunning = [99 for _ in range(40320)]
solved_idx = solved.idx_cp()
que = deque([[solved_idx, 0, -10, -10]])
prunning[solved_idx] = 0
cnt = 0
while que:
    cnt += 1
    if cnt % 10000 == 0:
        print(cnt, len(que))
    puzzle, num, l_twist, ll_twist = que.popleft()
    for twist in successor[1]:
        if face(twist) == face(
                l_twist) or axis(twist) == axis(l_twist) == axis(ll_twist):
            continue
        n_puzzle = move_cp[puzzle][twist]
        if prunning[n_puzzle] > num + 1:
            prunning[n_puzzle] = num + 1
            que.append([n_puzzle, num + 1, twist, l_twist])
with open('prunning_phase1_0.csv', mode='w') as f:
    writer = csv.writer(f, lineterminator='\n')
    writer.writerow(prunning)
'''
print('phase1 EP')
prunning = [99 for _ in range(967680)]
solved_idx = solved.idx_phase1_ep_ud() * 24 + solved.idx_phase1_ep_fbrl()
que = deque([[solved_idx, 0, -10, -10]])
prunning[solved_idx] = 0
cnt = 0
示例#5
0
solved = Cube()
print('phase 0 1/1')
prunning = [99 for _ in range(735471)]
solved_idx = solved.idx_ce_phase0()
prunning[solved_idx] = 0
que = deque([[solved_idx, 0, -10, -10, -10]])
cnt = 0
while que:
    cnt += 1
    if cnt % 10000 == 0:
        #tmp = prunning.count(100)
        print(cnt, len(que))
        #print(prunning[0])
    puzzle, num, l1_twist, l2_twist, l3_twist = que.popleft()
    for twist in successor[0]:
        if face(twist) == face(l1_twist) or axis(twist) == axis(
                l1_twist) == axis(l2_twist) == axis(l3_twist) or (
                    axis(twist) == axis(l1_twist)
                    and wide(twist) == wide(l1_twist) == 1):
            continue
        n_puzzle = move_ce_phase0[puzzle][twist_to_idx[twist]]
        if prunning[n_puzzle] > num + 1:
            prunning[n_puzzle] = num + 1
            que.append([n_puzzle, num + 1, twist, l1_twist, l2_twist])
with open('prun/prunning0.csv', mode='w') as f:
    writer = csv.writer(f, lineterminator='\n')
    writer.writerow(prunning)

# phase1 center
ce_parity = [-1 for _ in range(70)]
solved = Cube()