Artificial Intelligence and Machine Learning (Assignment 2) Name: SAMEER KSHIRSAGAR Roll no: 202010025 1. Explain why problem formulation must follow goal formulation. Solution: In goal formulation, we decide which aspects we are interested in and which aspects can be ignored. In the goal formulation process, the goal is to be set and we should assess those states in which the goal is satisfied. In problem formulation, we decide how to manipulate the important aspects, and ignore the others. So, without doing goal formulation, if we do the problem formulation, we would not know what to include in our problem and what to leave, and what should be achieved. So problem formulation must follow goal formulation. That means problem formulation must be done only after the goal formation is done. 2. Consider the accessible, two-location vacuum world under Murphy's Law. Show that for each initial state, there is a sequence of actions that is guaranteed to reach a goal state. Solution: Murphy's law And, as it turns out, of automation and AI. Murphy's law, attributed to Edward Murphy (though not coined by him) states that: “Anything that can go wrong will go wrong.” ... This means that care must be taken when training artificial intelligence. Since the world is accessible you can just follow the steps: If there is dirt then suck, else move. 3. Give the initial state, goal test, operators, and path cost function for each of the following. There are several possible formulations for each problem, with varying levels of detail. The main thing is that your formulations should be precise and "hang together" so that they could be implemented. a. You want to find the telephone number of Mr. Jimwill Zollicoffer, who lives in Alameda, given a stack of directories alphabetically ordered by city. Solution: ● Initial State - x phone books containing n names ● Goal Test - Does the name match ● Operators - Select phone book: set of pages; select page: set of names; select name. ● Past Cost - Time taken to find name ● Algorithm - Select directory by town name, select page by start of last name, select name by first name b. As for part (a), but you have forgotten Jimwill's last name. Solution: ● Initial State - x phone books containing n names ● Goal Test - Does the name match ● Operators - Select phone book: set of pages; select page: set of names; select name. ● Past Cost - Time taken to find name ● Algorithm - Select directory by town name, go through each page searching for first-name c. You are lost in the Amazon jungle, and have to reach the sea. There is a stream nearby. Solution: ● ● ● ● ● Initial State - Position lat/long/alt, location of sea Goal Test - Are you at the sea Operators - Walk Past Cost - Time taken to reach sea Algorithm - Follow stream downhill as sea is at 0 alt d. You have to color a complex planar map using only four colors, with no two adjacent regions to have the same color. Solution: ● Initial State - Position/Edges of planes ● Goal Test - Are all plane colored with no adjacent regions having matching colours ● Operators - Fill plane with 1 of four colours ● Past Cost - Number of actions required ● Algorithm - CSP, can use algorithms like Most Constrained Variable e. A monkey is in a room with a crate, with bananas suspended just out of reach on the ceiling. He would like to get the bananas. Solution: ● ● ● ● ● Initial State - Location of monkey, box and bananas Goal Test - Does the monkey have the bananas Operators - Monkey can move itself and the box Past Cost - Time taken Algorithm - Move box under bananas. Means-Ends Analysis f. You are lost in a small country town, and must find a drug store before your hay fever becomes intolerable. There are no maps, and the natives are all locked indoors. Solution: ● ● ● ● ● Initial State - Town layout, your location Goal Test - Are you at the drugstore Operators - Walk, look, make map Past Cost - Time taken Algorithm - Walk towards most built-up area and work out from there 4. Implement the missionaries and cannibals problem and use breadth-first search to find the shortest solution. Is it a good idea to check for repeated states? Draw a diagram of the complete state space to help you decide. Solution: General purpose BFS implementation: (bfs root-node at-goal? get-children) where: (at-goal? node) returns #t if node is the goal (get-children node) returns a list of child nodes The algorithm: ● put root node on a queue Q ● Repeat: - if Q is empty, return failure - remove first node N from Q - if N is the goal, return success - add children of N to end of Q The given data are 1. There are three missionaries and three cannibals on the left bank of a river. 2. They wish to cross over to the right bank using a boat that can only carry two at a time. 3. The number of cannibals on either bank must never exceed the number of missionaries on the same bank, otherwise the missionaries will become the cannibals' dinner. 4. Plan a sequence of crossings that will take everyone safely across. For solving this, we use a 3-tuple (m, c, b) to represent the state of one side of the river, since the other side can be easily inferred. Here, m stands for the number of missionaries, c, the number of cannibals, and b, whether the boat is at this side of the river. Initially, we have the state (3, 3, 1) and the goal state should be (0, 0, 0). . . . . State space diagram: Here, Actions are taken as 1m --- one missionary crosses the river 1c --- one cannibal crosses the river 2m ---- two missionaries cross the river 2c --- two cannibals cross the river 1m, 1c --- one missionary and one cannibal cross the river b) Yes, it is the good idea for the repeated states to solve the problem optimally using an appropriate search algorithm. Here, all the links in the state space are bidirectional, indicating the graph is full of cycles. However, all the cycles correspond to the case where we undo an action immediately after it is performed. c) Even though the state space is simple, people have a hard time solving this puzzle because most of the moves are either illegal or revert to the previous state. Also there is a feeling of a large branching factor and no clear way to proceed. . 5. The GENERAL-SEARCH algorithm consists of three steps: goal test, generate, and ordering function, in that order. It seems a shame to generate a node that is in fact a solution, but to fail to recognize it because the ordering function fails to place it first. a. Write a version of GENERAL-SEARCH that tests each node as soon as it is generated and stops immediately if it has found a goal. Solution: function General-Search-New(problem, Queuing-FN) returns a solution, or failure nodes ß Make-Queue(MAKE-NODE(INITIAL STATE[problem]) ) loop do if nodes is empty then return failure node ß REMOVE-FRONT(nodes) new-node ß EXPAND(node, OPERATORS[problem]) if GOAL-TEST[problem] applied to the STATE of any of new-nodes succeeds then return that node nodes ß QUEUING-FN-FRONT(nodes, new-nodes) end b. Show how the GENERAL-SEARCH algorithm can be used unchanged to do this by giving it the proper ordering functions. Solution: Setting h (n) = - g(n) means the best nodes will be those with the longest path. Preferring the longest path means DFS. If we set h (n) = g(n) then it would be BFS. . 6. Describe a search space in which iterative deepening search performs much worse than depth-first search. Solution: In Depth First Search (DFS), traverse the node until the leaf is reached. In Iterative Deepening Search (IDS), during the first iteration, the child of the root is visited first and then in the second iteration, the child of the root is visited which is at the depth 2, now at the third iteration, the child of the root is visited which is at depth 3. Thus, the iterative deepening will perform worse if there is one goal or solution at the depth n and the branching factor is 1. 7. Write down the algorithm for bidirectional search, in pseudo-code or in a programming language. Assume that each search will be a breadth-first search, and that the forward and backward searches take turns expanding a node at a time. Be careful to avoid checking each node in the forward search against each node in the backward search! Solution: startq = Queue for BFS from start node endq = Queue for BFS from end node parent= Array where startparent[i] is parent of node i visited= Array where visited[i]=True if node i has been encountered while startq is not empty and endq is not empty perform next iteration of BFS for startq (also save the parent of children in parent array) perform next iteration of BFS for endq if we have encountered the intersection node save the intersection node break using intersection node, find the path using parent array 8. Give the time complexity of bidirectional search when the test for connecting the two searches is done by comparing a newly generated state in the forward direction against all the states generated in the backward direction, one at a time Solution: If you are parsing all n nodes or states in a given set, on each iteration of n states, that would be n*n, or n^2. However, if you are only parsing every node up to the current node, then it is the sum of all nodes up to n. The sum of all nodes up to n would be linear, specifically 1+2+3+...(n-1)+n = n(n+1)/2 Consider the current forward node to be n for this iteration, (n-1) is the first node backwards, (n-2) is the second node backwards, and so on until 1, the very last node backwards: N + (n-1) + (n-2) + ... + 3 + 2 + 1 = n(n+1)/2 So: [a, b, c, d, e, f] 1,a: a,b,c,d,e,f 2,b: a,b,c,d,e,f ... this would be n^2 And: 1,a: [] 2,b: [a] 3,c: [a,b] 4,d: [a,b,c] ..... this would be linear as described above. 9. We said that at least one direction of a bidirectional search must be a breadth-first search. What would be a good choice for the other direction? Why? Solution: The main idea behind bidirectional searches is to reduce the time taken for search drastically. A depth-first search in both directions is not likely to work well because its small search frontiers are likely to pass each other by. Breadth-first search in both directions would be guaranteed to meet. A combination of depth-first search in one direction and breadth-first search in the other would guarantee the required intersection of the search frontiers, but the choice of which to apply in which direction may be difficult. The decision depends on the cost of saving the breadth-first frontier and searching it to check when the depth-first method will intersect one of its elements. For optimality with constant step cost BFS or UCS will work, depth first could run into loops. From the goal-side searches point of view there is just an additional goal node added every step. 10. The search agents we have discussed make use of a complete model of the world to construct a solution that they then execute. Modify the depth-first search algorithm with repeated state checking so that an agent can use it to explore an arbitrary vacuum world even without a model of the locations of walls and dirt. It should not get stuck even with loops or dead ends. You may also wish to have your agent construct an environment description of the type used by the standard search algorithms. Solution: Approach - Depth-first search is an algorithm for traversing or searching tree or graph data structures. The algorithm starts at the root node (selecting some arbitrary node as the root node in the case of a graph) and explores as far as possible along each branch before backtracking. So the basic idea is to start from the root or any arbitrary node and mark the node and move to the adjacent unmarked node and continue this loop until there is no unmarked adjacent node. Then backtrack and check for other unmarked nodes and traverse them. Finally, print the nodes in the path. Algorithm 1. Create a recursive function that takes the index of the node and a visited array. 2. Mark the current node as visited and print the node. 3. Traverse all the adjacent and unmarked nodes and call the recursive function with the index of the adjacent node. Modification All search classes implement state checking via state/operator hashing.