def idfs(start): cost_limit = 6 nodes = 0 frontier = list() branching_factors = list() while True: frontier.append(start) while len(frontier) != 0: curr = frontier.pop() if goal_reached(curr.cube): print('Goal Height:', curr.cost) print("Nodes Generated:", nodes) return if curr.cost + 1 <= cost_limit: child_cost = curr.cost + 1 b = 0 for i in range(12): nodes = nodes + 1 new = State() new.cube = np.array(curr.cube) new.cost = child_cost new.parent = curr new.move = make_move(new.cube, i + 1, 0) # if curr.parent is not None and np.array_equal(curr.parent.cube, new.cube): if curr.parent is not None and (contains1(new.cube, curr) or contains2(new.cube, frontier)): continue frontier.append(new) b = b + 1 branching_factors.append(b) branching_factors.clear()
def ida(start): start.h = corner_edge_sum_max(start.cube) cost_limit = start.h nodes = 0 frontier = list() branching_factors = list() while True: minimum = None frontier.append(start) while len(frontier) != 0: curr = frontier.pop(\ [i.g + i.h for i in frontier].index(\ min([i.g+i.h for i in frontier])\ )) if goal_reached(curr): print('Goal Height:', curr.g) #print('Branching Factor:', sum(branching_factors)/len(branching_factors)) # while curr is not None: # if curr.move is not None: # print(curr.move) # curr = curr.parent #print("mem ",nodes.__sizeof__) print("Nodes Generated:", nodes) return curr.g, nodes b = 0 nodes = nodes + 12 for i in range(12): new = State() new.cube = np.array(curr.cube) new.g = curr.g + 1 new.parent = curr new.move = make_move(new.cube, i + 1, 0) new.h = corner_edge_sum_max(new.cube) if new.g + new.h > cost_limit: if minimum is None or new.g + new.h < minimum: minimum = new.g + new.h continue if curr.parent is not None and (contains1(new.cube, curr) or contains2(new.cube, frontier)): continue frontier.append(new) b = b + 1 if b != 0: branching_factors.append(b) cost_limit = minimum
def ida(start): start.h = corner_edge_sum_max(start.cube) cost_limit = start.h nodes = 0 frontier = list() branching_factors = list() while True: minimum = None frontier.append(start) while len(frontier) != 0: curr = frontier.pop(\ [i.h for i in frontier].index(\ min([i.h for i in frontier])\ )) if goal_reached(curr): print('Goal Height:', curr.depth) print("Nodes Generated:", nodes) return b = 0 nodes = nodes + 12 for i in range(12): new = State() new.cube = np.array(curr.cube) new.depth = curr.depth + 1 new.parent = curr new.move = make_move(new.cube, i + 1, 0) new.h = corner_edge_sum_max(new.cube) print("Depth: " + str(new.depth)) print("current cost_limit: " + str(cost_limit)) print("minimum: " + str(minimum)) print("houristic: " + str(new.h)) if new.h > cost_limit: if minimum is None or new.h < minimum: minimum = new.h continue if curr.parent is not None and (contains1(new.cube, curr) or contains2(new.cube, frontier)): continue frontier.append(new) b = b + 1 if b != 0: branching_factors.append(b) cost_limit = minimum
db = dict() front = list() goal = St() goal.cube = np.array(xInitial) front.append(goal) db[get_corner_string(goal.cube)] = 0 # cost = 0 while len(front) != 0: curr = front.pop(0) if curr.cost < 5: child_cost = curr.cost + 1 for i in range(12): new = St() new.cube = np.array(curr.cube) new.cost = child_cost make_move(new.cube, i + 1, 0) # if cost < child_cost: # cost = child_cost # print(cost) string = get_corner_string(new.cube) if string not in db.keys(): db[string] = new.cost front.append(new) try: cube = goal.cube conn = sqlite3.connect('corners.db') cursor = conn.cursor() cursor.execute('CREATE TABLE CornerValue(Corners VARCHAR, Value INT)') print(sqlite3.version)