Skip to content

Commit

Permalink
Cube Solver Implemented
Browse files Browse the repository at this point in the history
  • Loading branch information
Erik_L committed Mar 24, 2024
1 parent b948e59 commit 77cf77a
Show file tree
Hide file tree
Showing 20 changed files with 4,114 additions and 888 deletions.
25 changes: 12 additions & 13 deletions astar.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,19 @@ def astar_search_pq(scrambled_cube : Cube, N, scale_factor = 0.6) -> dict:

open_list = PriorityQueue()
closed_list = set()
cube_to_node = {}
cube_to_fitness = {}

initial_g = 0
initial_f = compute_fitness([scrambled_cube.state])[0] + (scale_factor * initial_g)
start_node = Astar_Node(scrambled_cube.to_string(), [], initial_g, initial_f)

open_list.put((start_node.f, start_node))

cube_to_node[scrambled_cube.__hash__()] = start_node.f
cube_to_fitness[scrambled_cube.__hash__()] = initial_f

iteration = 0
while not open_list.empty():

best_nodes = []
batch_info = []
batch_states = []
Expand All @@ -55,8 +57,8 @@ def astar_search_pq(scrambled_cube : Cube, N, scale_factor = 0.6) -> dict:
closed_list.add(current_node.cube)
best_nodes.append(current_node)

# print(f"Best Fitness: {best_nodes[0].f}, Best Moves: {best_nodes[0].moves}")

# print(f"Iteration: {iteration}, Node Explored: {node_explored}, Best F: {best_nodes[0].f}")
# Generate new states for the batch
for node in best_nodes:
allowed_moves = get_allowed_moves(node.moves)
Expand All @@ -74,30 +76,27 @@ def astar_search_pq(scrambled_cube : Cube, N, scale_factor = 0.6) -> dict:
batch_info.append((tempcube.to_string(), new_moves, node.g , tempcube.__hash__()))

del tempcube

# Convert batch_states to numpy array and compute fitness
fitness_scores = compute_fitness(batch_states)

for ((cube_str, new_moves, g, cube_hash), fitness) in zip(batch_info, fitness_scores):
updated_g = g + 1
updated_f = (scale_factor * updated_g) + fitness
new_node : Astar_Node = Astar_Node(cube_str, new_moves, updated_g, updated_f)

score = cube_to_node.get(cube_hash)
score = cube_to_fitness.get(cube_hash)
if score and score <= new_node.f:
continue

cube_to_node[cube_hash] = new_node.f
cube_to_fitness[cube_hash] = new_node.f
open_list.put((new_node.f, new_node))

node_explored += 1

# print(f"Nodes Explored: {node_explored}")

iteration += 1

return {"success" : False, "solutions": None, "num_nodes": node_explored, "time_taken": time.time() - time_start}



if __name__ == "__main__":
from scramble100 import selected_scrambles

Expand Down
43 changes: 14 additions & 29 deletions beam_search.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import time
import torch
from cube import Cube, load_model, get_allowed_moves, device
from cube import Cube, load_model, get_allowed_moves, device, Move
import numpy as np
from queue import PriorityQueue
from collections import namedtuple
# import tracemalloc

Expand Down Expand Up @@ -44,11 +43,10 @@ def generate_new_generation(generation: list[Beam_Node], seen_state):

batch_states.append(tempcube.state)
batch_info.append((tempcube.to_string(), new_moves))
nodes_searched += 1

del tempcube

nodes_searched += 1

fitness_scores = compute_fitness(batch_states)
for (cube_str, new_moves), fitness in zip(batch_info, fitness_scores):
new_generation.append(Beam_Node(cube_str, new_moves, fitness, False))
Expand All @@ -57,7 +55,7 @@ def generate_new_generation(generation: list[Beam_Node], seen_state):
new_generation.sort(key=lambda x: x.fitness)
return new_generation, nodes_searched, False

def beam_search(scrambled_cube : Cube, beam_width = 1024, max_depth = 100) -> dict:
def beam_search(scrambled_cube : Cube, beam_width = 3000, max_depth = 100, adaptive = False) -> dict:
root_fitness = compute_fitness([scrambled_cube.state])[0]
root = Beam_Node(scrambled_cube.to_string(), [], root_fitness, scrambled_cube.is_solved())

Expand All @@ -77,16 +75,22 @@ def beam_search(scrambled_cube : Cube, beam_width = 1024, max_depth = 100) -> di
return {"success" : False, "solutions": None, "num_nodes": node_searched, "time_taken": time.time() - start_time}

new_generation, searched_nodes, success = generate_new_generation(generation, seen_state)

if success:
return {"success" : True, "solutions": new_generation[0].moves, "num_nodes": node_searched + searched_nodes, "time_taken": time.time() - start_time}

node_searched += searched_nodes
generation = new_generation[: beam_width]

adaptive_beam_width = beam_width

if adaptive:
adaptive_beam_width = int(beam_width * (1 + (depth/26)))

generation = new_generation[: adaptive_beam_width]

for node in generation:
seen_state.add(node.cube)

return {"success" : False, "solutions": None, "num_nodes": node_searched, "time_taken": time.time() - start_time}

if __name__ == "__main__":
Expand All @@ -96,33 +100,14 @@ def beam_search(scrambled_cube : Cube, beam_width = 1024, max_depth = 100) -> di
total_sol_length = 0
total_nodes = 0
total_time = 0

# limit = 39000
"""
1024 =>
Success Rate: 0.68, Avg Sol Length: 25.235294117647058, Avg Num Nodes: 298359.92647058825, Avg Time Taken: 61.83388700204737
1911 =>
Success Rate: 0.85, Avg Sol Length: 25.0, Avg Num Nodes: 541414.0352941176, Avg Time Taken: 116.34260092623093
2048 =>
Success Rate: 0.88, Avg Sol Length: 24.59090909090909, Avg Num Nodes: 566032.8863636364, Avg Time Taken: 110.20973552898927
4096 =>
Success Rate: 0.98, Avg Sol Length: 24.183673469387756, Avg Num Nodes: 1087275.0204081633, Avg Time Taken: 214.24532394506494
Memory Usage:
3000 => Peak memory usage: 400 ~ 500MB
"""


for i, scramble in enumerate(selected_scrambles):
print(f"Test {i + 1}")
cube = Cube()
cube.move_list(cube.convert_move(scramble))

# tracemalloc.start()
result = beam_search(cube, 3000, 100)
result = beam_search(cube, 1000, 100, adaptive = False)

# _, max_mem = tracemalloc.get_traced_memory()
# print(f"Peak memory usage: {max_mem / 10**6}MB")
Expand Down
5 changes: 4 additions & 1 deletion cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ def get_allowed_moves(move_sequence):
return allowed_moves

last_move = get_last_move(move_sequence)

allowed_moves.remove(inverse_moves[last_move])

allowed_moves = prevent_moves_pre(move_sequence, last_move, allowed_moves)
return allowed_moves

Expand Down Expand Up @@ -420,7 +423,7 @@ def randomize_n(self, n):
scramble_string = ""
for move in scramble_move:
scramble_string += move_dict[move] + " "
return scramble_string
return scramble_string, scramble_move

def from_state(self, state):
self.state = state
Expand Down
44 changes: 44 additions & 0 deletions methodology/AStar_Search/AStarList.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Iteration Open List Size Batch Time Prepare Time Compute Time Check Time Total Time
1 1 1.2874603271484375e-05 0.0007798671722412109 0.003116130828857422 0.00012636184692382812 0.004035234451293945
2 12 3.6716461181640625e-05 0.006631135940551758 0.007340192794799805 0.00040793418884277344 0.014415979385375977
4 1068 0.004849672317504883 0.671586275100708 0.17975521087646484 0.028857707977294922 0.8850488662719727
6 36920 0.23144936561584473 1.690781831741333 0.3628866672515869 0.1231529712677002 2.408270835876465
8 93355 0.35564255714416504 1.6938610076904297 0.3495781421661377 0.16141366958618164 2.560495376586914
10 149951 0.4654395580291748 1.679875135421753 0.4209473133087158 0.19761109352111816 2.7638731002807617
12 206186 0.5880885124206543 1.810049295425415 0.34201478958129883 0.08222842216491699 2.822381019592285
14 262281 0.7002136707305908 1.704401969909668 0.3449845314025879 0.08631253242492676 2.8359127044677734
16 318469 0.8099842071533203 1.7322185039520264 0.34914731979370117 0.08891177177429199 2.98026180267334
18 374667 0.874018669128418 1.7077486515045166 0.33286142349243164 0.08222293853759766 2.996851682662964
20 430699 1.591015338897705 1.7348651885986328 0.34294843673706055 0.0897054672241211 3.7585344314575195
22 486887 1.267815351486206 1.7408347129821777 0.3266618251800537 0.08802628517150879 3.4233381748199463
24 543007 1.8767430782318115 2.129680633544922 0.3870420455932617 0.08708047866821289 4.480546236038208
26 598970 1.8500094413757324 1.704484462738037 0.34801268577575684 0.10614800453186035 4.008654594421387
28 654998 2.1778852939605713 1.7195298671722412 0.4274563789367676 0.09143400192260742 4.4163055419921875
30 710977 2.6661698818206787 1.7319281101226807 0.36532139778137207 0.08448529243469238 4.847904682159424
32 767090 2.7307910919189453 1.7268695831298828 0.33415865898132324 0.08444046974182129 4.876259803771973
34 823105 2.755009889602661 1.719940423965454 0.32895874977111816 0.08394598960876465 4.887855052947998
36 879097 3.0496931076049805 1.6962742805480957 0.3492298126220703 0.08265113830566406 5.1778483390808105
38 935259 3.4862442016601562 1.7083566188812256 0.34024882316589355 0.08401823043823242 5.618867874145508
40 991280 3.7823922634124756 1.7168302536010742 0.3424086570739746 0.6499359607696533 6.491567134857178
42 1047269 4.222656726837158 1.9512357711791992 0.42587947845458984 0.09067320823669434 6.690445184707642
44 1103382 4.441939830780029 1.7261991500854492 0.33483099937438965 0.0838630199432373 6.5868330001831055
46 1159344 4.22368597984314 2.501943349838257 0.35712575912475586 0.09401965141296387 7.176774740219116
48 1215400 5.344472885131836 1.7433505058288574 0.33666539192199707 0.08915400505065918 7.51364278793335
50 1271555 4.831951856613159 1.801839828491211 0.33130669593811035 0.08500838279724121 7.050106763839722
52 1327598 5.550878524780273 1.9555716514587402 0.42690515518188477 0.09193658828735352 8.025291919708252
54 1383648 5.289432048797607 1.7284605503082275 0.34327077865600586 0.08337736129760742 7.444540739059448
56 1439596 5.425415515899658 1.7883963584899902 0.35346078872680664 0.0860595703125 7.653332233428955
58 1495587 5.465919733047485 1.744382619857788 0.3475301265716553 0.08861994743347168 7.6464524269104
60 1551562 6.238528251647949 2.68929386138916 0.35859203338623047 0.0888361930847168 9.375250339508057
62 1607606 5.721263647079468 1.7094635963439941 0.35004687309265137 0.08289790153503418 7.8636720180511475
64 1663522 6.201148986816406 1.748889684677124 0.39062952995300293 0.08910441398620605 8.42977261543274
66 1719528 6.311014413833618 1.7154338359832764 0.34247565269470215 0.09274458885192871 8.461668491363525
68 1775441 6.404118776321411 1.6973848342895508 0.34423828125 1.1519861221313477 9.59772801399231
70 1831480 6.254588842391968 1.702868938446045 0.326946496963501 0.08402824401855469 8.368432521820068
72 1887477 7.120541095733643 1.7344017028808594 0.35193943977355957 0.09241795539855957 9.299300193786621
74 1943450 7.556537866592407 1.729332685470581 0.32794189453125 0.08573508262634277 9.699547529220581
76 1999451 6.9561333656311035 1.701998233795166 0.3881556987762451 0.08626055717468262 9.132547855377197
78 2055427 7.8917248249053955 1.7277023792266846 0.3438091278076172 1.4330663681030273 11.396302700042725
80 2111411 8.134589433670044 1.7268273830413818 0.41082048416137695 0.0859825611114502 10.358219861984253
82 2167337 8.918860912322998 1.7884159088134766 0.3848295211791992 0.08690881729125977 11.179015159606934
84 2223355 8.215734004974365 1.7715024948120117 0.48146963119506836 0.08950567245483398 10.55821180343628
44 changes: 44 additions & 0 deletions methodology/AStar_Search/AStarPQ.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
Iteration Open List Size Batch Time Prepare Time Compute Time Check Time Total Time
1 1 1.4543533325195312e-05 0.0006577968597412109 0.0029854774475097656 0.0001728534698486328 0.0038306713104248047
2 12 6.270408630371094e-05 0.006512880325317383 0.008498430252075195 0.0007536411285400391 0.015827655792236328
4 1068 0.01098775863647461 0.5886592864990234 0.17676091194152832 0.05528545379638672 0.8316934108734131
6 36920 0.06346869468688965 1.6688730716705322 0.3532249927520752 0.282928466796875 2.368495225906372
8 93355 0.07506108283996582 1.6437509059906006 0.33255577087402344 0.2939894199371338 2.3453571796417236
10 149951 0.07393789291381836 1.6431255340576172 0.35981154441833496 0.3134441375732422 2.3903191089630127
12 206186 0.09162473678588867 1.652564287185669 0.3479773998260498 0.19261956214904785 2.2847859859466553
14 262281 0.08398222923278809 1.7684094905853271 0.38457274436950684 0.20315885543823242 2.4401233196258545
16 318469 0.08505535125732422 1.6894094944000244 0.33210158348083496 0.1952831745147705 2.301849603652954
18 374667 0.09103560447692871 1.6891100406646729 0.43180394172668457 0.19951701164245605 2.411466598510742
20 430699 0.10103607177734375 1.729729175567627 0.3332352638244629 0.19923162460327148 2.363232135772705
22 486887 0.08878374099731445 2.0097827911376953 0.351548433303833 0.20556330680847168 2.6556782722473145
24 543016 0.09002208709716797 1.658433437347412 0.3463871479034424 0.22341394424438477 2.3182566165924072
26 598979 0.08973050117492676 1.6630170345306396 0.33231329917907715 0.1962888240814209 2.2813496589660645
28 655006 0.09130644798278809 1.6684367656707764 0.32857275009155273 0.6771993637084961 2.7655153274536133
30 711004 0.09205460548400879 1.678187370300293 0.3336818218231201 0.20012259483337402 2.304046392440796
32 767120 0.09268641471862793 1.6579351425170898 0.3325307369232178 0.19735097885131836 2.280503273010254
34 823135 0.0906982421875 1.6746482849121094 0.3481414318084717 0.19676733016967773 2.310255289077759
36 879124 0.09564042091369629 1.6633961200714111 0.3402431011199951 0.19683480262756348 2.296114444732666
38 935295 0.09408926963806152 2.3405728340148926 0.33390307426452637 0.2084958553314209 2.9770610332489014
40 991319 0.09325885772705078 1.6671440601348877 0.3332197666168213 0.19899463653564453 2.2926173210144043
42 1047308 0.09372830390930176 1.6509134769439697 0.35041213035583496 0.19898056983947754 2.294034481048584
44 1103417 0.09229469299316406 1.6651053428649902 0.3487377166748047 0.19862747192382812 2.304765224456787
46 1159383 0.09582829475402832 1.8120007514953613 0.4192647933959961 0.20562529563903809 2.532719135284424
48 1215440 0.09512853622436523 1.689972162246704 0.3664722442626953 0.2034749984741211 2.3550479412078857
50 1271594 0.09981989860534668 1.6943016052246094 0.42040038108825684 1.1418194770812988 3.3563413619995117
52 1327638 0.1006019115447998 1.6611344814300537 0.3500030040740967 0.1996016502380371 2.3113410472869873
54 1383684 0.09488344192504883 1.6533443927764893 0.359677791595459 0.19826817512512207 2.306173801422119
56 1439635 0.10381960868835449 1.6861624717712402 0.3618605136871338 0.22175097465515137 2.37359356880188
58 1495624 0.0975644588470459 1.6814050674438477 0.3454618453979492 1.3409852981567383 3.465416669845581
60 1551600 0.09600567817687988 1.6592497825622559 0.34392714500427246 0.20180487632751465 2.300987482070923
62 1607654 0.09742522239685059 1.690631628036499 0.357022762298584 0.20169377326965332 2.346773386001587
64 1663569 0.09876847267150879 1.6715137958526611 0.34674525260925293 0.20252323150634766 2.3195507526397705
66 1719572 0.09837746620178223 1.6722192764282227 0.3562045097351074 0.20132112503051758 2.32812237739563
68 1775485 0.09799981117248535 1.6804049015045166 0.3349905014038086 0.20202898979187012 2.3154242038726807
70 1831529 0.09859490394592285 1.7130811214447021 0.423480749130249 0.20534300804138184 2.440499782562256
72 1887536 0.10100364685058594 1.6683061122894287 0.3361082077026367 0.20173382759094238 2.3071517944335938
74 1943508 0.09908914566040039 1.667468786239624 0.3513200283050537 0.1995697021484375 2.3174476623535156
76 1999506 0.10095000267028809 1.679269790649414 0.3563113212585449 0.20444917678833008 2.340980291366577
78 2055485 0.10066890716552734 3.359379291534424 0.3349740505218506 0.1998903751373291 3.994912624359131
80 2111470 0.10191869735717773 1.671790599822998 0.43301939964294434 0.21356487274169922 2.4202935695648193
82 2167394 0.10184025764465332 1.6666595935821533 0.3399839401245117 0.20217442512512207 2.3106582164764404
84 2223412 0.10227036476135254 1.6812617778778076 0.334200382232666 0.2029426097869873 2.3206751346588135
1 change: 0 additions & 1 deletion results/AStar/list.txt

This file was deleted.

4 changes: 0 additions & 4 deletions results/AStar/pq.txt

This file was deleted.

Loading

0 comments on commit 77cf77a

Please sign in to comment.