From 7e247e8980328500de518b57d86309299af92368 Mon Sep 17 00:00:00 2001 From: omri Date: Sat, 24 Aug 2024 10:24:27 +0300 Subject: [PATCH] add masking --- __main__.py | 8 ++++---- cube.py | 28 +++++++++++++++++----------- moves.yaml | 1 + solver.py | 20 +++++++++----------- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/__main__.py b/__main__.py index 1dbf15e..81a1f38 100644 --- a/__main__.py +++ b/__main__.py @@ -11,7 +11,7 @@ from eolr import ( ) import humanize from dataclasses import dataclass -from solver import EOLRBSolution, solve_eolrb, create_prune_table +from solver import EOLRBSolution, solve_eolrb, create_eolrb_prune_table from scorer import ( FingerTrickWithRegrip, load_config, @@ -21,7 +21,7 @@ from scorer import ( import heapq from typing import List, Any, Generator, Dict -SOLUTIONS_TO_EVAL = 40 +SOLUTIONS_TO_EVAL = 1000 SOLUTIONS_TO_SHOW = 3 PRUNE = 10 @@ -52,7 +52,7 @@ def load_or_generate_prune_table(file_path: str, prune_size: int) -> Dict: return pickle.load(file) else: print("generating prune table...") - prune_table = create_prune_table(prune_size) + prune_table = create_eolrb_prune_table(prune_size) print("saving prune table to", file_path) with open(file_path, "wb") as file: pickle.dump(prune_table, file) @@ -61,7 +61,6 @@ def load_or_generate_prune_table(file_path: str, prune_size: int) -> Dict: def main(): config = load_config("./moves.yaml") - prune_table = load_or_generate_prune_table("prune_table.pkl", PRUNE) print("prune table size:", humanize.naturalsize(sys.getsizeof(prune_table))) @@ -113,6 +112,7 @@ def main(): { "alg": " ".join(pretty_alg), "stm": len(solution.alg), + # "mc": solution.mc, "score": score, "raw_alg": " ".join(solution.alg), "pre_auf": solution.pre_auf, diff --git a/cube.py b/cube.py index 8d8e0cc..0cbd384 100644 --- a/cube.py +++ b/cube.py @@ -38,12 +38,12 @@ class LSECube: # "DF": Edge("DF", True), # } self.edges = { - "UB": Edge("", True), + "UB": Edge("UB", True), "UR": Edge("UR", True), - "UF": Edge("", True), + "UF": Edge("UF", True), "UL": Edge("UL", True), - "DB": Edge("", True), - "DF": Edge("", True), + "DB": Edge("DB", True), + "DF": Edge("DF", True), } self.centers = { "U": "U", @@ -58,16 +58,22 @@ class LSECube: "UBL": "UBL", } - def __hash__(self): + def eolrb_hash(self): # TODO: I fucking hate this language. why can't hashing be simpelr hash_dict = lambda d: hashlib.md5(str(sorted(d.items())).encode()).hexdigest() - edges_hash = hash_dict(self.edges) - centers_hash = hash_dict(self.centers) - corners_hash = hash_dict(self.corners) + eolrb_state = {} + # mask edges to only care about LR edges and orientation of everything else + for location, edge in self.edges.items(): + if edge.name in ["UR", "UL"]: + eolrb_state[location] = edge + else: + eolrb_state[location] = Edge("", edge.oriented) + + eolrb_state["center_oriented"] = self.centers["U"] in ["U", "D"] + eolrb_state["UFR"] = self.corners["UFR"] + return int( - hashlib.md5( - (edges_hash + centers_hash + corners_hash).encode() - ).hexdigest(), + hashlib.md5(hash_dict(eolrb_state).encode()).hexdigest(), 16, ) diff --git a/moves.yaml b/moves.yaml index a23eaae..9cc4191 100644 --- a/moves.yaml +++ b/moves.yaml @@ -1,3 +1,4 @@ +# TODO: do a lot of random MU algs and find out which moves are statistically faster finger_tricks: - name: "U push" move: "U" diff --git a/solver.py b/solver.py index c37a31c..776180f 100644 --- a/solver.py +++ b/solver.py @@ -50,19 +50,19 @@ def lse_brute_force_generator(max_length: int): # -def create_prune_table(prune_depth: int): +def create_eolrb_prune_table(prune_depth: int): prune_table = defaultdict(list) cube = LSECube() - prune_table[hash(cube)] = [[]] + prune_table[cube.eolrb_hash()] = [[]] generator = lse_brute_force_generator(prune_depth) + # TODO: for every solved state (pre auf), we can apply the generator's moves and save to prune table? for alg in generator: - # TODO: turn the prune table into a dict of list values to store multiple solutions cube.reset() cube.alg(" ".join(alg)) - setup = reverse_algorithm(alg) - prune_table[hash(cube)].append(setup) + solution = reverse_algorithm(alg) + prune_table[cube.eolrb_hash()].append(solution) return prune_table @@ -79,15 +79,13 @@ def solve_eolrb(cube: LSECube, prune_table: Dict, solve: int): for moves in lse_brute_force_generator(solve): c = copy.deepcopy(cube) c.alg(" ".join(moves)) - if hash(c) in prune_table: - for alg_start in prune_table[hash(c)]: - alg = condense_algorithm(alg_start + moves) + if c.eolrb_hash() in prune_table: + for prune_solution in prune_table[c.eolrb_hash()]: + alg = condense_algorithm(moves + prune_solution) # TODO: this is very weird for _ in range(10): alg = condense_algorithm(alg) - if len(alg) == 0: - yield EOLRBSolution(alg, "", "", False) - else: + if len(alg) > 2: # TODO: check if this is mc pre_auf = "" if alg[0].startswith("U"):