Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I'm stuck on this question. Can you help me to solve this question with code using Python? Thank you for helping me. This is

Hello, I'm stuck on this question. Can you help me to solve this question with code using Python? Thank you for helping me. This is the full instruction of this question.

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

2-D Rubik's Cube 'The Rubik's Cube is a 3-D combination puzzle invented in 1974 by Hungarian sculptor and professor of architecture Erno Rubik. Rubik's Cube won the 1980 German Game of the Year special award for Best Puzzle. As of January 2009, 350 million cubes had been sold worldwide, making it the world's bestselling puzzle game and bestselling toy." - Wikipedia. In this task, we explore a simplified version, 2-D Rubik's "Cube". To help you understand A" search, you will design and implement an A search algorithm to find the solution of any cube. We take a cube of shape 3 rows 3 columns as an example to explain the rule of the game. Given an initial configuration of the cube, we are interested in finding a sequence of moves that leads the cube to be in a predefined goal configuration in the least number of steps. In the following example, an initial configuration of the cube is [[R,G,B],[R,G,B],[R,G,B]] and we are interested in taking the least possible number of actions to reach the predefined goal configuration [[R,R,R],[G,G,G],[B,B,B]]. initial:RRRGGGBBBgoal:RGBRGBRGB On each move, we can pick a [number] and a [direction] to manipulate the cube. i.e. select a row number and a horizontal move direction (leftright), or select a column number and a vertical move direction (up/down). Each move will only change the elements in the selected row/column, leaving the rest of the cube unchanged For example if row 1 and move direction left are picked, all elements in row 1 will be shifted to the left with the leftmost element re-emerging on the rightmost column of the same row and the rest of the rows unchanged: 012RRRGGGBBBRGRGBGBRB Note that the effect of a move is circular and therefore consecutively moving the cube on the same row/column and direction twice is the same as moving the cube on the same row/column in the opposite direction once in a 3-by-3 cube. We encourage you to play with this cube to discover more insights and useful rules Here we provide a simple solution for the above example. You can walk through this solution step by step to get a better understanding of this problem. 012RRRGGGBBBRGRGBGBRBRGBGBRBRGRGBRGBBRGRGBRGBRGB(1,left)(2,right)(1,down)(2,up) Please run the following cell before proceeding. You may use any of the imported libraries/classes here. Please run the following cell before proceeding. You may use any of the imported libraries/classes here. [2] import copy import heapq import math import os import random import sys import time import utils import cube from typing import List from typing import Tuple * For foltowing test cases def wrap test(func): def inner("args, **kargs): try: return func (*args, *kwargs) return inner To allow you to focus on implementing search instead of having to set up states, the class Cube provided in cube.py supports the following methods: - goal_test(state) : tests whether the provided state is the goal state. - actions(state) retums a list of actions at the provided state - resuIt(state, action) : returns the new state after taking action from the provided state . It is deterministic. - path_cost (c, state1, action, state2) : returns the accumulated cost of reaching state1 from the initial state and then reaching state2 from state1 by action. In the cube problem, the state of the cube is an instance of State class, It is a hashable type. Action in Cube is a tuple of an integer representing label and a string representing direction. Your search function should take and only take legal actions to transition from one state to another. For your convenience, we have provided a Node class for constructing a search tree and priorityQueue class for your search algorithm in the utils.py . You may also choose to implement your own Node and PriorityQueue class instead. Our autograder will follow the same import structure as that of the ps2.py . Please run the following code block to use the helper classes. If you do not wish to use them, you may skip the execution of the following code block. If you choose to override the provided helpers, please include all your code implementations in the template file ps 2 .py as well as Coursemology If you choose to override the provided helpers, please include all your code implementations in the template file psz.py as well as Coursemology We provide implementations for the Node and PriorityQueue classes in utils.py, but you can implement your own if you wish from utils import Node from utils import PriorityQueue Task 1.1: Design a heuristic for A Search Implement the A" Search in two parts. First, design your heuristic function heuristic func (problem, state) , which takes in an instance of the cube class and the state class (see below). It returns the estimated cost of reaching the goal state from the state given. Note: 1 The heunstic function estimates the "distance" to the goal state 2. The heuristic should be admissibie never overestimates the cost to reach a goal. With an admissible heuristic, A* is cost-optimal 3. The template heunistic returns 0 for all the cases. It does not provide any intormation. Thus, you will see the connection betueen the A* search and the BFS graph search (PS1) in terms of performance 4 Please try your best to find the best heuristic for this problem. CS Scanned with CamScanner [3]:# Test case for Task 1.1 . \[ \begin{array}{l} \text { ewrap_test } \\ \text { def test_heuristic(case): } \\ \text { input_dict }=\text { case[ 'input_dict'] } \\ \text { answer }=\text { case['answer'] } \\ \text { problem }=\text { cube.cube(input_dict }=\text { input_dict) } \end{array} \] assert heuristic_func(problem, problem.goal) =0, "Heuristic is not at the goal state" assert heuristic_func(problem, problem.initial) = answer['cost'], "Heuristic is not admissible" return "PASSED" cube2 ={ 'input dict': \{"initial': { 'shape': [3,3], 'layout': [S,0, c,5 ', 0,0,C,5,0,C]},goa1: ' shape': [3,3], 'layout': ['s', 's', 's', 0 ', 'c, c,c,cc,c,cC]}}, 'answer': ['solution': [[2, 'right' ],[1, left ],[1, down' ], [2, 'up']], 'cost': 4}}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions