sliding-tile problem using hill climbing algorithm

I am trying to solve the 8 puzzle or sliding tile problem using Hill-Climbing algorithm in python. I Wrote the code to print each intermediate step until goal node is reached but no output is shown by the interpreter. Please review my code and tell me about the errors in the code and solution to them.

Hi there, I am trying to solve the 8 puzzle or sliding tile problem using Hill-Climbing algorithm in python. I Wrote the code to print each intermediate step until goal node is reached but no output is shown by the interpreter. Please tell me about the errors in the code and solution to them.

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>from copy import deepcopy
class State:
def __init__(self, state, parent):
goal_state = [[1, 2, 3], [8, 0, 4], [7, 6, 5]]
self.parent = parent
self.state = state
self.cost = self.calculate_cost(goal_state)
def calculate_cost(self, goal_state):
cost = 0
for i in range(3):
for j in range(3):
if self.state[i][j] != goal_state[i][j]:
cost += 1
return cost
def generate_neighbors(self):
neighbors = []
for i in range(3):
for j in range(3):
if self.state[i][j] == 0:
if i >= 0 and i != 2: #move down
new_state = deepcopy(self.state)
new_state[i][j], new_state[i+1][j] = new_state[i+1][j], new_state[i][j]
neighbors.append(State(new_state, self))
if i <=2 and i != 0: #move up
new_state = deepcopy(self.state)
new_state[i][j], new_state[i-1][j] = new_state[i-1][j], new_state[i][j]
neighbors.append(State(new_state, self))
if j >= 0 and j != 2: # move right
new_state = deepcopy(self.state)
new_state[i][j], new_state[i][j+1] = new_state[i][j+1], new_state[i][j]
neighbors.append(State(new_state, self))
if j <= 2 and j != 0: # move left
new_state = deepcopy(self.state)
new_state[i][j], new_state[i][j-1] = new_state[i][j-1], new_state[i][j]
neighbors.append(State(new_state, self))
return neighbors
def hill_climbing(initial_state):
current_state = State(initial_state, None)
while current_state.cost > 0:
neighbors = current_state.generate_neighbors()
best_neighbor = min(neighbors, key = lambda state: state.cost)
if current_state.cost > best_neighbor.cost:
current_state = best_neighbor
path = []
while current_state is not None:
path.append(current_state.state)
current_state = current_state.parent
return path[::-1]
initial_state = [[2, 0, 3], [1, 8, 4], [7, 6, 5]]
path = hill_climbing(initial_state)
# Print the solution path
for state in path:
print(state)
</code>
<code>from copy import deepcopy class State: def __init__(self, state, parent): goal_state = [[1, 2, 3], [8, 0, 4], [7, 6, 5]] self.parent = parent self.state = state self.cost = self.calculate_cost(goal_state) def calculate_cost(self, goal_state): cost = 0 for i in range(3): for j in range(3): if self.state[i][j] != goal_state[i][j]: cost += 1 return cost def generate_neighbors(self): neighbors = [] for i in range(3): for j in range(3): if self.state[i][j] == 0: if i >= 0 and i != 2: #move down new_state = deepcopy(self.state) new_state[i][j], new_state[i+1][j] = new_state[i+1][j], new_state[i][j] neighbors.append(State(new_state, self)) if i <=2 and i != 0: #move up new_state = deepcopy(self.state) new_state[i][j], new_state[i-1][j] = new_state[i-1][j], new_state[i][j] neighbors.append(State(new_state, self)) if j >= 0 and j != 2: # move right new_state = deepcopy(self.state) new_state[i][j], new_state[i][j+1] = new_state[i][j+1], new_state[i][j] neighbors.append(State(new_state, self)) if j <= 2 and j != 0: # move left new_state = deepcopy(self.state) new_state[i][j], new_state[i][j-1] = new_state[i][j-1], new_state[i][j] neighbors.append(State(new_state, self)) return neighbors def hill_climbing(initial_state): current_state = State(initial_state, None) while current_state.cost > 0: neighbors = current_state.generate_neighbors() best_neighbor = min(neighbors, key = lambda state: state.cost) if current_state.cost > best_neighbor.cost: current_state = best_neighbor path = [] while current_state is not None: path.append(current_state.state) current_state = current_state.parent return path[::-1] initial_state = [[2, 0, 3], [1, 8, 4], [7, 6, 5]] path = hill_climbing(initial_state) # Print the solution path for state in path: print(state) </code>
from copy import deepcopy

class State:
    def __init__(self, state, parent):
        goal_state = [[1, 2, 3], [8, 0, 4], [7, 6, 5]]
        self.parent = parent
        self.state = state
        self.cost = self.calculate_cost(goal_state)

    def calculate_cost(self, goal_state):

        cost = 0

        for i in range(3):
            for j in range(3):
                if self.state[i][j] != goal_state[i][j]:
                    cost += 1

        return cost


    def generate_neighbors(self):
        neighbors = []

        for i in range(3):
            for j in range(3):
                if self.state[i][j] == 0:

                    if i >= 0 and i != 2:   #move down
                        new_state = deepcopy(self.state)
                        new_state[i][j], new_state[i+1][j] = new_state[i+1][j], new_state[i][j]
                        neighbors.append(State(new_state, self))

                    if i <=2 and i != 0:    #move up
                        new_state = deepcopy(self.state)
                        new_state[i][j], new_state[i-1][j] = new_state[i-1][j], new_state[i][j]
                        neighbors.append(State(new_state, self))

                    if j >= 0 and j != 2: # move right
                        new_state = deepcopy(self.state)
                        new_state[i][j], new_state[i][j+1] = new_state[i][j+1], new_state[i][j]
                        neighbors.append(State(new_state, self))

                    if j <= 2 and j != 0:   # move left
                        new_state = deepcopy(self.state)
                        new_state[i][j], new_state[i][j-1] = new_state[i][j-1], new_state[i][j]
                        neighbors.append(State(new_state, self))


        return neighbors

def hill_climbing(initial_state):

    current_state = State(initial_state, None)

    while current_state.cost > 0:

        neighbors = current_state.generate_neighbors()

        best_neighbor = min(neighbors, key = lambda state: state.cost)

        if current_state.cost > best_neighbor.cost:
            current_state = best_neighbor

    path = []
    while current_state is not None:
        path.append(current_state.state)
        current_state = current_state.parent

    return path[::-1]






initial_state = [[2, 0, 3], [1, 8, 4], [7, 6, 5]]

path = hill_climbing(initial_state)

# Print the solution path
for state in path:
    print(state)

New contributor

Shrey Kumar is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật