def main(count_type=count_type): assert count_type in ['decisions', 'actions'] effects = [] lengths = [] for length in tqdm(range(1, 41)): n_trials = 100 effect = 0 for trial in range(n_trials): d = cube.Cube() f = random_action_skill(length) d.apply(f) effect += len(d.summarize_effects()) lengths.append(length) effects.append(effect / n_trials) lengths = [l - 0.1 for l in lengths] plt.scatter(lengths, effects, marker='^', label='Primitive Actions') effects = [] lengths = [] for length in tqdm(range(1, 41)): n_trials = 50 effect = 0 n_actions = 0 for trial in range(n_trials): d = cube.Cube() o_seq, m_seq = random_option_skill(length) for o, m in zip(o_seq, m_seq): n_actions += len(o) d.apply(swap_list=m) effect += len(d.summarize_effects()) if count_type == 'actions': lengths.append(n_actions / n_trials) # count actions else: lengths.append(length) # count options effects.append(effect / n_trials) lengths = [l for l in lengths] plt.scatter(lengths, effects, marker='o', label='Options') x_max = 40 if count_type == 'decisions' else 500 plt.hlines(48, 0, x_max, linestyles='dotted') plt.legend(loc='lower right') plt.title('Average number of squares modified by sequence') plt.xlabel('Number of {} per sequence'.format(count_type)) plt.ylim([0, 50]) if count_type == 'actions': plt.xlim([0, 500]) plt.show()
def import_cube(data: list) -> cube.Cube: """ Returns a cube, given a length 54 list. """ c = [cube.list_mat(data[:9])] + [[data[i:i + 3] for i in range(j, 43, 12)] for j in range(9, 21, 3)] + [cube.list_mat(data[-9:])] obj = cube.Cube() obj.cube = cube.str_cubies(c) return obj
class expert: alg_formulas = [ skills.swap_3_edges_face, skills.swap_3_edges_mid, skills.swap_3_corners, skills.orient_2_edges, skills.orient_2_corners, ] options = [ variation for f in alg_formulas for variation in formula.variations(f) ] models = [cube.Cube().apply(o).summarize_effects() for o in options]
import copy import random from tqdm import tqdm from cube import cube from cube import formula from cube import skills from cube import options from cube import pattern c = cube.Cube() c.apply(pattern.scramble1) c.render() #%% mods = c.summarize_effects() steps = [] experiences = 0 tqdm.write('experiences:{}--steps:{}--errors:{}'.format(experiences, len(steps),len(mods))) option_set = options.expert max_depth = 2 def option_sequences(depth, prefix=None): assert depth > 0, 'Depth must be > 0' options = [o for o in random.sample(option_set.options, len(option_set.options))] if depth==1: return [[o] if prefix==None else prefix+[o] for o in options] else: new_prefixes = [[o] if prefix==None else prefix+[o] for o in options] result = []
import copy import matplotlib.pyplot as plt import random from tqdm import tqdm from collections import namedtuple from cube import cube from cube import formula from cube import skills from cube import options from cube import pattern c = cube.Cube() c.render() Skill = namedtuple('Skill', ['seq', 'mdl']) def combine_skills(skills, depth, prefix=None): assert depth > 0, 'Depth must be > 0' # skills = [s for s in random.sample(skills, len(skills))] if depth == 1: seqs = [ s.seq if prefix == None else formula.simplify(prefix.seq + s.seq) for s in skills ] mdls = [ s.mdl if prefix == None else cube.combine_swaps(prefix.mdl, s.mdl) for s in skills ] return [
for f in cube_files: seed = int(f.split('/')[-1].split('.')[-2].split('-')[-1]) with open(f, 'rb') as f: cubefail = pickle.load(f) if seed in [8]:#[1,8,14,35,53,76,100]: print(seed) cubefail.render() break pass #%% cube_mod = copy.deepcopy(cubefail) rot = formula.rotate cube_mod.apply("R R".split()) cube_mod.apply(rot(rot("F F R' F' U' F' U F R F' U U F U U F' U'".split(),cube.Face.L,2),cube.Face.D,2)) cube_mod.apply("R R".split()) # cube_mod.apply("D' F".split()) # cube_mod.apply(rot("F F R' F' U' F' U F R F' U U F U U F' U'".split(),cube.Face.U)) # cube_mod.apply(formula.inverse(rot(skills.orient_2_corners,cube.Face.U))) # cube_mod.apply("F' D".split()) cube_mod.render() #%% newcube = cube.Cube() newcube.apply(rot(rot("F F R' F' U' F' U F R F' U U F U U F' U'".split(),cube.Face.L,0),cube.Face.D,0)) newcube.render() # len(newcube.summarize_effects())
def mult(l1: list, l2: list) -> list: """ Applies permutation l2 to l1. """ return [l1[n] for n in l2] if __name__ == "__main__": ### U move written as other moves # c = cube.Cube() # c.turn("U") # noU = cube.Metric(cube.mat_list(((move, move + "'", move + "2") for move in "DFBRL"))) # print(cube.IDsolve(c, metric=noU)[:-1]) # exit() ### 3x3 with double move algs c = cube.Cube() ## four edge cycles # c.turn("M2 U2 M2 U2 z M2 U2 M2 U2 z'") # c.turn("M' U2 M2 U2 M' y M' U2 M2 U2 M' y'") # c.turn("M2 U2 M2 U2 z M2 U2 M2 U2 z' x M2 U' M2 U2 M2 U' M2 x'") # c.turn("M' U2 M2 U2 M' y M' U2 M2 U2 M' y' M2 U' M2 U2 M2 U' M2") # c.turn("M' U2 M2 U2 M' y M' U2 M2 U2 M' y' x2 M2 U' M2 U2 M2 U' M2 x2") print(c) print(cube.solve(c, metric=cube.HALF)[:-1]) exit() print(mult(["a", "b", "c", "d", "e"], [2, 4, 0, 1, 3])) ### TESTING GENERATION AND EQUAL TO MOVE
class random: alg_formulas = [skills.random_skill(len(a)) for a in expert.alg_formulas] options = [ variation for f in alg_formulas for variation in formula.variations(f) ] models = [cube.Cube().apply(o).summarize_effects() for o in options]
import matplotlib.pyplot as plt from tqdm import tqdm from cube import cube from cube import skills as random_skills #%% effects = [] lengths = [] short_skills = [] for length in tqdm(range(1, 25)): n_trials = 1000 effect = 0 for trial in range(n_trials): d = cube.Cube() f = random_skills.random_skill(length) d.apply(f) effect = len(d.summarize_effects()) if effect < 20: short_skills.append(f) if f: tqdm.write('moves={}; effect={}; seq={}'.format( len(f), effect, ' '.join(f))) lengths.append(len(f)) effects.append(effect) lengths = [l - 0.1 for l in lengths] plt.scatter(lengths, effects, marker='o', label='Random') #%% effects = []
import copy from cube import cube from notebooks import astar start = cube.Cube() newcube = copy.deepcopy(start) newcube.transform('R') newcube.render() start.render() is_goal = lambda cube: cube == cube.Cube() heuristic = lambda cube: len(cube.summarize_effects()) def get_successors(cube): [copy.deepcopy(cube).transform(a) for a in cube.Action.keys] astar.search(start, is_goal, 1, heuristic, get_successors)
import cube.cube as cube # example of a reconstruction # 41 HTM/5.44 = 7.54 TPS done while talking about railgun c = cube.Cube() c.turn("L2 U B U B' L F B' L' R2 D2 L2 F2 B2 U R2 F2 U2 B2 U L") # scramble c.turn("x2 y") # inspection c.turn("L F' D' R' D") # cross c.turn("y U R U R' L U' L'") # 1st pair c.turn("U2 R' U' R U' R' U R") # 2nd pair c.turn("y' U2 R' U' R") # 3rd pair c.turn("U2 R U' R' U R U' R'") # 4th pair c.turn("U' R U R' U R U2 R'") # OLL + PLL skip c.turn("U'") # AUF print(c) # last 2 pairs + LL is 2gen # what if I did it optimally? c = cube.Cube() c.turn("L2 U B U B' L F B' L' R2 D2 L2 F2 B2 U R2 F2 U2 B2 U L") # scramble c.turn("x2 y") # inspection c.turn("L F' D' R' D") # cross c.turn("y U R U R' L U' L'") # 1st pair c.turn("U2 R' U' R U' R' U R") # 2nd pair c.turn("y'") print(c) goal = cube.Cube() goal.turn("x2 y") sol = cube.solve(c, target=(goal, cube.solved), metric=cube.TGEN)[1] print(sol, len(cube.tokenize(sol)))